Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!ginosko!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: strcat/access question Summary: strcat(st1,st2) modifies st1 Message-ID: <1222@virtech.UUCP> Date: 3 Oct 89 11:12:35 GMT References: <5409@umd5.umd.edu> Organization: Virtual Technologies Inc Lines: 44 In article <5409@umd5.umd.edu>, jjk@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 | +-----------------------------------------------------------------------+