strcat/access question
Conor P. Cahill
cpcahil at virtech.UUCP
Tue Oct 3 21:12:35 AEST 1989
In article <5409 at umd5.umd.edu>, jjk at astro.UMD.EDU (Jim Klavetter) writes:
> Here is a section of code:
>
> if((home=getenv("HOME"))==(char *) 0)
> perror("GETENV\n");
Perror is useless here since getenv is not a system call. In addition,
since you use home in later code, you should exit here.
> printf("%s:\n", home);
> printf("%s:\n", strcpy(string,strcat(home, "/astro/data/obs.list")));
^^^^^
This modifies home. You should never do this to an environment pointer
since this will mess up your environment (or may cause the program to
core dump if HOME is at the end of your environment and the end of your
address space).
Home now points to "/a/jjk/astro/data/obs.list"
> printf("%s:\n", string);
> printf("%d:\n", access(string, 4));
> printf("%d:\n", access(strcat(home, "/astro/data/obs.list"), 4));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Home now points to "/a/jjk/astro/data/obs.list/astro/data/obs.list" hence
the failure code from access(2).
> The man page says that strcat returns a null-terminated string so that
> the two calls to access (I think) should both give "0" but the second
> is saying the file doesn't exist. I've included the appropriate
> files. Any response to the above address would be appreciated (and
> I will summarize the response if there is some good answer(s)).
You must read the man page for any routine you want to use. If you had
read the man page for strcat (string(3) or maybe strings(3)) you would have
seen that strcat appends the second string to the first string and returns
a pointer to the first string (in this case home).
Make sure you read the entire man page!!!!!!!
--
+-----------------------------------------------------------------------+
| Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 !
| Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 |
+-----------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list