src/global.h | 2 +- src/main.c | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/global.h b/src/global.h index a61f59e..03e738e 100644 --- a/src/global.h +++ b/src/global.h @@ -260,12 +260,12 @@ extern char *namefile; /* file of file names */ extern BOOL ogs; /* display OGS book and subsystem names */ extern char *prependpath; /* prepend path to file names */ extern FILE *refsfound; /* references found file */ +extern char tmpdir[]; /* temporary directory */ extern char temp1[]; /* temporary file name */ extern char temp2[]; /* temporary file name */ extern long totalterms; /* total inverted index terms */ extern BOOL trun_syms; /* truncate symbols to 8 characters */ extern char tempstring[TEMPSTRING_LEN + 1]; /* global dummy string buffer */ -extern char *tmpdir; /* temporary directory */ /* command.c global data */ extern BOOL caseless; /* ignore letter case when searching */ diff --git a/src/main.c b/src/main.c index 70b1eb9..8a041f5 100644 --- a/src/main.c +++ b/src/main.c @@ -52,6 +52,8 @@ #endif #include /* needed by stat.h */ #include /* stat */ +#include +#include #include /* defaults for unset environment variables */ @@ -99,14 +101,14 @@ char *namefile; /* file of file names */ BOOL ogs; /* display OGS book and subsystem names */ char *prependpath; /* prepend path to file names */ FILE *refsfound; /* references found file */ +char tmpdir[PATHLEN + 1]; /* temporary directory */ char temp1[PATHLEN + 1]; /* temporary file name */ char temp2[PATHLEN + 1]; /* temporary file name */ -char tempdirpv[PATHLEN + 1]; /* private temp directory */ long totalterms; /* total inverted index terms */ BOOL trun_syms; /* truncate symbols to 8 characters */ char tempstring[TEMPSTRING_LEN + 1]; /* use this as a buffer, instead of 'yytext', * which had better be left alone */ -char *tmpdir; /* temporary directory */ +char *_tmpdir; /* temporary directory */ static BOOL onesearch; /* one search only in line mode */ static char *reflines; /* symbol reference lines file */ @@ -145,10 +147,10 @@ main(int argc, char **argv) unsigned int i; pid_t pid; struct stat stat_buf; + struct passwd *pwd; #if defined(KEY_RESIZE) && !defined(__DJGPP__) struct sigaction winch_action; #endif - mode_t orig_umask; yyin = stdin; yyout = stdout; @@ -333,7 +335,7 @@ cscope: reffile too long, cannot be > %d characters\n", sizeof(path) - 1); shell = mygetenv("SHELL", SHELL); lineflag = mygetenv("CSCOPE_LINEFLAG", LINEFLAG); lineflagafterfile = getenv("CSCOPE_LINEFLAG_AFTER_FILE") ? 1 : 0; - tmpdir = mygetenv("TMPDIR", TMPDIR); + _tmpdir = mygetenv("TMPDIR", TMPDIR); /* XXX remove if/when clearerr() in dir.c does the right thing. */ if (namefile && strcmp(namefile, "-") == 0 && !buildonly) { @@ -342,30 +344,33 @@ cscope: reffile too long, cannot be > %d characters\n", sizeof(path) - 1); } /* make sure that tmpdir exists */ - if (lstat (tmpdir, &stat_buf)) { + if (lstat (_tmpdir, &stat_buf)) { fprintf (stderr, "\ cscope: Temporary directory %s does not exist or cannot be accessed\n", - tmpdir); + _tmpdir); fprintf (stderr, "\ cscope: Please create the directory or set the environment variable\n\ cscope: TMPDIR to a valid directory\n"); myexit(1); } - /* create the temporary file names */ - orig_umask = umask(S_IRWXG|S_IRWXO); - pid = getpid(); - snprintf(tempdirpv, sizeof(tempdirpv), "%s/cscope.%d", tmpdir, pid); - if(mkdir(tempdirpv,S_IRWXU)) { - fprintf(stderr, "\ -cscope: Could not create private temp dir %s\n", - tempdirpv); + /* create the temporary directory */ + if ((pwd = getpwuid(getuid())) == NULL) { + fprintf (stderr, "cscope: Can't get your username: %s\n", strerror(errno)); + myexit(1); + } + (void) snprintf(tmpdir, sizeof(tmpdir), "%s/cscope-%s-XXXXXX", _tmpdir, pwd->pw_name); + if (mkdtemp(tmpdir) == NULL) { + fprintf (stderr, "cscope: Can't create temporary directory: %s\n", strerror(errno)); + /* FIXME: don't delete that ain't created */ + tmpdir[0] = 0; myexit(1); } - umask(orig_umask); - snprintf(temp1, sizeof(temp1), "%s/cscope.1", tempdirpv); - snprintf(temp2, sizeof(temp2), "%s/cscope.2", tempdirpv); + /* create the temporary file names */ + pid = getpid(); + snprintf(temp1, sizeof(temp1), "%s/cscope%d.1", tmpdir, pid); + snprintf(temp2, sizeof(temp2), "%s/cscope%d.2", tmpdir, pid); /* if running in the foreground */ if (signal(SIGINT, SIG_IGN) != SIG_IGN) { @@ -881,7 +886,10 @@ myexit(int sig) if (temp1[0] != '\0') { unlink(temp1); unlink(temp2); - rmdir(tempdirpv); + } + /* ...and directories */ + if (tmpdir[0] != '\0') { + (void) rmdir(tmpdir); } /* restore the terminal to its original mode */ if (incurses == YES) {