Steve Graegert wrote:
> > I have to remember the working directory and save it in my program so that
> > I can return to it later..
> >
> > Which one is better/safer and why ? The environment var's or the
> > getcwd() function fam... ?
>
> I usually take the following approach:
>
> int fd_dir;
> char buf[PATH_MAX];
>
> if (getcwd(buf, sizeof(buf)) == NULL) {
> /* error handling */
> }
>
> /* open a directory for reading */
> if ((fd = open(buf, O_RDONLY)) < 0) {
> /* error handling*/
> }
With this approach, you may as well skip the getcwd() and just use
open(".", ...) to get a descriptor for the cwd.
Also the behaviour of using a descriptor and fchdir() versus using
getcwd() and chdir() differs if the directory is moved or renamed
while the program is running.
--
Glynn Clements <[EMAIL PROTECTED]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
in
the body of a message to [EMAIL PROTECTED]
More majordomo info at http://vger.kernel.org/majordomo-info.html