mirror of
https://https.git.savannah.gnu.org/git/patch.git
synced 2026-01-26 16:09:26 +00:00
* cfg.mk: New file, to configure maint.mk. * Makefile.am (EXTRA_DIST): Add, so the new file is distributed. (config_h_header): Define, to make the sc_require_config_h_first syntax-check test pass. * pc/chdirsaf.c: Include <config.h>.
36 lines
580 B
C
36 lines
580 B
C
/* A safer version of chdir, which returns back to the
|
|
initial working directory when the program exits. */
|
|
|
|
#include <config.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
static char *initial_wd;
|
|
|
|
static void
|
|
restore_wd (void)
|
|
{
|
|
chdir (initial_wd);
|
|
}
|
|
|
|
int
|
|
chdir_safer (char const *dir)
|
|
{
|
|
if (! initial_wd)
|
|
{
|
|
size_t s;
|
|
for (s = 256; ! (initial_wd = getcwd (0, s)); s *= 2)
|
|
if (errno != ERANGE)
|
|
return -1;
|
|
if (atexit (restore_wd) != 0)
|
|
{
|
|
free (initial_wd);
|
|
initial_wd = 0;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
return chdir (dir);
|
|
}
|