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