[8] | 1 | /* |
---|
| 2 | * Revision Control Information |
---|
| 3 | * |
---|
| 4 | * $Id: tmpfile.c,v 1.11 2005/04/30 22:38:11 fabio Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
| 9 | * util_tmpfile -- open an unnamed temporary file |
---|
| 10 | * |
---|
| 11 | * Many compilers/systems do not have this, or have buggy versions. |
---|
| 12 | * |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /* LINTLIBRARY */ |
---|
| 16 | |
---|
| 17 | /* util_tempnam and check_directory are from |
---|
| 18 | Jonathan I. Kamens <jik@pit-manager.mit.edu> */ |
---|
| 19 | |
---|
| 20 | /* modified slightly by Ellen Sentovich ellen@ic.berkeley.edu */ |
---|
| 21 | |
---|
| 22 | #include <sys/types.h> |
---|
| 23 | #if HAVE_SYS_STAT_H |
---|
| 24 | # include <sys/stat.h> |
---|
| 25 | #endif |
---|
| 26 | #if HAVE_SYS_FILE_H |
---|
| 27 | # include <sys/file.h> |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | #include "util.h" |
---|
| 31 | |
---|
| 32 | static char check_directory(char *dir) |
---|
| 33 | { |
---|
| 34 | struct stat statbuf; |
---|
| 35 | |
---|
| 36 | if (! dir) |
---|
| 37 | return 0; |
---|
| 38 | else if (stat(dir, &statbuf) < 0) |
---|
| 39 | return 0; |
---|
| 40 | else if (S_ISDIR(statbuf.st_mode)) |
---|
| 41 | return 0; |
---|
| 42 | else if (access(dir, W_OK | X_OK) < 0) |
---|
| 43 | return 0; |
---|
| 44 | else |
---|
| 45 | return 1; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /* function for creating temporary filenames */ |
---|
| 49 | char *util_tempnam(char *dir, char *pfx) |
---|
| 50 | { |
---|
| 51 | extern char *getenv(const char *); |
---|
| 52 | char *tmpdir = NULL, *env, *filename; |
---|
| 53 | static char unique_letters[4] = "AAA"; |
---|
| 54 | char addslash = 0; |
---|
| 55 | |
---|
| 56 | /* |
---|
| 57 | * If a directory is passed in, verify that it exists and is a |
---|
| 58 | * directory and is writeable by this process. If no directory |
---|
| 59 | * is passed in, or if the directory that is passed in does not |
---|
| 60 | * exist, check the environment variable TMPDIR. If it isn't |
---|
| 61 | * set, check the predefined constant P_tmpdir. If that isn't |
---|
| 62 | * set, use "/tmp/". |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | if ((env = getenv ("TMPDIR")) && check_directory(env)) |
---|
| 66 | tmpdir = env; |
---|
| 67 | else if (dir && check_directory(dir)) |
---|
| 68 | tmpdir = dir; |
---|
| 69 | #ifdef P_tmpdir |
---|
| 70 | else if (check_directory(P_tmpdir)) |
---|
| 71 | tmpdir = P_tmpdir; |
---|
| 72 | #endif |
---|
| 73 | else |
---|
| 74 | tmpdir = "/tmp/"; |
---|
| 75 | |
---|
| 76 | /* |
---|
| 77 | * OK, now that we've got a directory, figure out whether or not |
---|
| 78 | * there's a slash at the end of it. |
---|
| 79 | */ |
---|
| 80 | if (tmpdir[strlen(tmpdir) - 1] != '/') |
---|
| 81 | addslash = 1; |
---|
| 82 | |
---|
| 83 | /* |
---|
| 84 | * Now figure out the set of unique letters. |
---|
| 85 | */ |
---|
| 86 | unique_letters[0]++; |
---|
| 87 | if (unique_letters[0] > 'Z') { |
---|
| 88 | unique_letters[0] = 'A'; |
---|
| 89 | unique_letters[1]++; |
---|
| 90 | if (unique_letters[1] > 'Z') { |
---|
| 91 | unique_letters[1] = 'A'; |
---|
| 92 | unique_letters[2]++; |
---|
| 93 | if (unique_letters[2] > 'Z') { |
---|
| 94 | unique_letters[2]++; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | * Allocate a string of sufficient length. |
---|
| 101 | */ |
---|
| 102 | if (pfx) { |
---|
| 103 | filename = (char *) malloc(strlen(tmpdir) + addslash + strlen(pfx) + 10 |
---|
| 104 | ); |
---|
| 105 | } else { |
---|
| 106 | filename = (char *) malloc(strlen(tmpdir) + addslash + 10); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* |
---|
| 110 | * And create the string. |
---|
| 111 | */ |
---|
| 112 | (void) sprintf(filename, "%s%s%s%sa%05d", tmpdir, addslash ? "/" : "", |
---|
| 113 | pfx ? pfx : "", unique_letters, (int)getpid()); |
---|
| 114 | |
---|
| 115 | return filename; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | #ifdef UNIX |
---|
| 120 | |
---|
| 121 | FILE * |
---|
| 122 | util_tmpfile(void) |
---|
| 123 | { |
---|
| 124 | FILE *fp; |
---|
| 125 | char *filename; |
---|
| 126 | |
---|
| 127 | filename = util_tempnam(NIL(char), "VIS"); |
---|
| 128 | if ((fp = fopen(filename, "w+")) == NULL) { |
---|
| 129 | FREE(filename); |
---|
| 130 | return NULL; |
---|
| 131 | } |
---|
| 132 | (void) unlink(filename); |
---|
| 133 | FREE(filename); |
---|
| 134 | return fp; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #else |
---|
| 138 | |
---|
| 139 | FILE * |
---|
| 140 | util_tmpfile(void) |
---|
| 141 | { |
---|
| 142 | FILE *fp; |
---|
| 143 | |
---|
| 144 | if ((fp = fopen("utiltmp", "w+")) == NULL) { |
---|
| 145 | return NULL; |
---|
| 146 | } |
---|
| 147 | (void) unlink("utiltmp"); |
---|
| 148 | return fp; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | #endif |
---|