strcat/access question

mccaugh at s.cs.uiuc.edu mccaugh at s.cs.uiuc.edu
Wed Oct 4 17:13:00 AEST 1989


> Here is a section of code:
> 
> if((home=getenv("HOME"))==(char *) 0)
>     perror("GETENV\n");
> printf("%s:\n", home);
> printf("%s:\n", strcpy(string,strcat(home, "/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));
> 
> And the associated output:
> 
> /a/jjk:
> /a/jjk/astro/data/obs.list:
> /a/jjk/astro/data/obs.list:
> 0:
> -1:
> 
> 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.  

 Actually, I recall the 'man' page stating that strcat returns a pointer
 (char-type) to the null-terminated string, but in any case I suspect
 that the last printf contains a call to strcat which assigns to your
 string-variable 'home' the name of a truly non-existent file, namely:
 "/a/jjk/astro/data/obs.list/astro/data/obs.list", so a redundant 'strcat'
 appears to have occurred in variable 'home'.



More information about the Comp.lang.c mailing list