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