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