Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 2 | #include "asn1c_compat.h" |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 3 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 4 | #ifndef PATH_MAX |
| 5 | #define PATH_MAX 1024 |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 6 | #endif |
| 7 | |
Lev Walkin | ebfc44d | 2014-10-12 19:30:12 -0700 | [diff] [blame] | 8 | /* Normally file permissions are (DEFFILEMODE & ~umask(2)) */ |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 9 | #ifndef DEFFILEMODE /* Normally in <sys/stat.h> */ |
Lev Walkin | ebfc44d | 2014-10-12 19:30:12 -0700 | [diff] [blame] | 10 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 11 | #ifdef _WIN32 |
Lev Walkin | e751cd8 | 2007-11-13 22:30:09 +0000 | [diff] [blame] | 12 | #define DEFFILEMODE (S_IREAD|S_IWRITE) |
Lev Walkin | ebfc44d | 2014-10-12 19:30:12 -0700 | [diff] [blame] | 13 | #define REASONABLE_FILE_MODE DEFFILEMODE |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 14 | #else |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 15 | #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) |
Lev Walkin | ebfc44d | 2014-10-12 19:30:12 -0700 | [diff] [blame] | 16 | #define REASONABLE_FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
| 17 | #endif |
| 18 | #else /* !DEFFILEMODE */ |
| 19 | #ifdef _WIN32 |
| 20 | #define REASONABLE_FILE_MODE DEFFILEMODE |
| 21 | #else |
| 22 | #define REASONABLE_FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 23 | #endif |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 24 | #endif |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 25 | |
Lev Walkin | 50d52cf | 2013-03-16 08:00:57 -0700 | [diff] [blame] | 26 | #ifdef _WIN32 |
| 27 | int mkstemp(char *template) { |
| 28 | char *tmpFN = _mktemp(template); |
| 29 | if(tmpFN) |
| 30 | return open(tmpFN, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE); |
| 31 | else |
| 32 | return -1; |
| 33 | } |
| 34 | #undef HAVE_MKSTEMPS |
| 35 | #endif |
| 36 | |
Lev Walkin | 27fd0b6 | 2007-08-27 23:57:45 +0000 | [diff] [blame] | 37 | #ifdef HAVE_MKSTEMPS |
| 38 | #undef mkstemp |
| 39 | #define mkstemp(foo) mkstemps(foo, 0) |
| 40 | #endif |
| 41 | |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 42 | FILE * |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 43 | asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) { |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 44 | char fname[PATH_MAX]; |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 45 | int created = 1; |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 46 | #ifndef _WIN32 |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 47 | struct stat sb; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 48 | #endif |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 49 | FILE *fp; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 50 | int ret; |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 51 | int fd; |
| 52 | |
| 53 | /* |
| 54 | * Compute filenames. |
| 55 | */ |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 56 | ret = snprintf(fname, sizeof(fname), "%s%s%s", name, ext, |
| 57 | opt_tmpname ? ".XXXXXX" : ""); |
| 58 | assert(ret > 0 && ret < (ssize_t)sizeof(fname)); |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 59 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 60 | if(opt_tmpname) { |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Create temporary file. |
| 63 | */ |
| 64 | fd = mkstemp(fname); |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 65 | #ifndef _WIN32 |
Lev Walkin | ebfc44d | 2014-10-12 19:30:12 -0700 | [diff] [blame] | 66 | /* fchmod() does not respect umask */ |
| 67 | (void)fchmod(fd, REASONABLE_FILE_MODE); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 68 | #endif |
| 69 | } else { |
| 70 | /* |
| 71 | * Create specified file, or open the old one. |
| 72 | */ |
| 73 | fd = open(fname, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE); |
| 74 | if(fd == -1 && errno == EEXIST) { |
| 75 | fd = open(fname, O_WRONLY, DEFFILEMODE); |
| 76 | created = 0; |
| 77 | } |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 78 | } |
| 79 | if(fd == -1) { |
| 80 | perror(fname); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 84 | #ifndef _WIN32 |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Check sanity. |
| 87 | */ |
| 88 | if(fstat(fd, &sb) || !S_ISREG(sb.st_mode)) { |
| 89 | fprintf(stderr, "%s: Not a regular file\n", fname); |
| 90 | if(created) unlink(fname); |
| 91 | close(fd); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Lev Walkin | d62d7d5 | 2016-01-23 08:04:46 -0800 | [diff] [blame] | 95 | if(ftruncate(fd, 0) == -1) { |
| 96 | fprintf(stderr, "%s: ftruncate failed: %s\n", |
| 97 | fname, strerror(errno)); |
| 98 | if(created) unlink(fname); |
| 99 | return NULL; |
| 100 | } |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 101 | #else |
| 102 | _chsize(fd, 0); |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 103 | #endif /* _WIN32 */ |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Convert file descriptor into file pointer. |
| 107 | */ |
| 108 | fp = fdopen(fd, "w"); |
| 109 | if(fp == NULL) { |
| 110 | if(created) unlink(fname); |
| 111 | close(fd); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 112 | return NULL; |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 113 | } |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 114 | |
| 115 | /* Return the temporary file name */ |
| 116 | if(opt_tmpname) { |
| 117 | *opt_tmpname = strdup(fname); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 118 | if(*opt_tmpname) { |
| 119 | /* Successfull */ |
| 120 | } else { |
| 121 | if(created) unlink(fname); |
| 122 | fclose(fp); |
| 123 | return NULL; |
| 124 | } |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 127 | return fp; |
| 128 | } |
| 129 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 130 | const char * |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 131 | a1c_basename(const char *path) { |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 132 | static char strbuf[PATH_MAX]; |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 133 | const char *pend; |
| 134 | const char *name; |
| 135 | |
| 136 | pend = path + strlen(path); |
| 137 | if(pend == path) { |
| 138 | strcpy(strbuf, "."); |
| 139 | return strbuf; |
| 140 | } |
| 141 | |
| 142 | /* Skip tailing slashes */ |
| 143 | for(pend--; pend > path && *pend == '/'; pend--); |
| 144 | |
| 145 | if(pend == path && *path == '/') { |
| 146 | strcpy(strbuf, "/"); |
| 147 | return strbuf; |
| 148 | } |
| 149 | |
| 150 | for(name = pend; name > path && name[-1] != '/'; name--); |
| 151 | |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 152 | if((pend - name) >= (int)sizeof(strbuf) - 1) { |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 153 | errno = ENAMETOOLONG; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | memcpy(strbuf, name, pend - name + 1); |
| 158 | strbuf[pend - name + 1] = '\0'; |
| 159 | |
| 160 | return strbuf; |
| 161 | } |
| 162 | |
| 163 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 164 | const char * |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 165 | a1c_dirname(const char *path) { |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 166 | static char strbuf[PATH_MAX]; |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 167 | const char *pend; |
| 168 | const char *last = 0; |
| 169 | int in_slash = 0; |
| 170 | |
| 171 | /* One-pass determination of the last char of the pathname */ |
| 172 | for(pend = path; ; pend++) { |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 173 | switch(*pend) { |
| 174 | case '\0': break; |
| 175 | case '/': |
| 176 | if(!in_slash) { |
| 177 | last = pend; |
| 178 | in_slash = 1; |
| 179 | } |
| 180 | continue; |
| 181 | default: |
| 182 | if(in_slash) in_slash = 0; |
| 183 | continue; |
| 184 | } |
| 185 | break; |
| 186 | } |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 187 | |
| 188 | if(last <= path) { |
| 189 | strcpy(strbuf, *path == '/' ? "/" : "."); |
| 190 | return strbuf; |
| 191 | } |
| 192 | |
| 193 | if(!last) { |
| 194 | strcpy(strbuf, "/"); |
| 195 | return strbuf; |
| 196 | } |
| 197 | |
Lev Walkin | 4b10225 | 2004-08-19 13:29:18 +0000 | [diff] [blame] | 198 | if((last - path) >= (int)sizeof(strbuf)) { |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 199 | errno = ENAMETOOLONG; |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | memcpy(strbuf, path, last - path); |
| 204 | strbuf[last - path] = '\0'; |
| 205 | |
| 206 | return strbuf; |
| 207 | } |
| 208 | |