blob: 4a150d52df7f5943606d7d5c21cdc70d93266556 [file] [log] [blame]
Lev Walkin4b102252004-08-19 13:29:18 +00001#include "asn1c_internal.h"
Lev Walkin4efbfb72005-02-25 14:20:30 +00002#include "asn1c_compat.h"
Lev Walkin79f54952004-08-13 16:58:19 +00003
Lev Walkinebfc44d2014-10-12 19:30:12 -07004/* Normally file permissions are (DEFFILEMODE & ~umask(2)) */
Lev Walkin4b102252004-08-19 13:29:18 +00005#ifndef DEFFILEMODE /* Normally in <sys/stat.h> */
Lev Walkinebfc44d2014-10-12 19:30:12 -07006
Lev Walkin93659562010-11-20 09:47:13 -08007#ifdef _WIN32
Lev Walkine751cd82007-11-13 22:30:09 +00008#define DEFFILEMODE (S_IREAD|S_IWRITE)
Lev Walkinebfc44d2014-10-12 19:30:12 -07009#define REASONABLE_FILE_MODE DEFFILEMODE
Lev Walkin4efbfb72005-02-25 14:20:30 +000010#else
Lev Walkin4b102252004-08-19 13:29:18 +000011#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
Lev Walkinebfc44d2014-10-12 19:30:12 -070012#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 Walkin4b102252004-08-19 13:29:18 +000019#endif
Lev Walkin4efbfb72005-02-25 14:20:30 +000020#endif
Lev Walkin4b102252004-08-19 13:29:18 +000021
Lev Walkin50d52cf2013-03-16 08:00:57 -070022#ifdef _WIN32
23int 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 Walkin27fd0b62007-08-27 23:57:45 +000033#ifdef HAVE_MKSTEMPS
34#undef mkstemp
35#define mkstemp(foo) mkstemps(foo, 0)
36#endif
37
Lev Walkin4b102252004-08-19 13:29:18 +000038FILE *
Jon Ringle6431b1c2017-11-15 01:22:02 -050039asn1c_open_file(const char* destdir, const char *name, const char *ext, char **opt_tmpname) {
Lev Walkina4f8e942017-10-08 19:28:20 -070040 char fname[PATH_MAX];
Lev Walkin4b102252004-08-19 13:29:18 +000041 int created = 1;
Lev Walkin93659562010-11-20 09:47:13 -080042#ifndef _WIN32
Lev Walkin4b102252004-08-19 13:29:18 +000043 struct stat sb;
Lev Walkin4efbfb72005-02-25 14:20:30 +000044#endif
Lev Walkin4b102252004-08-19 13:29:18 +000045 FILE *fp;
Lev Walkin4604d032005-03-04 08:52:50 +000046 int ret;
Lev Walkin4b102252004-08-19 13:29:18 +000047 int fd;
48
49 /*
50 * Compute filenames.
51 */
Jon Ringle6431b1c2017-11-15 01:22:02 -050052 ret = snprintf(fname, sizeof(fname), "%s%s%s%s",
53 opt_tmpname ? "" : destdir,
54 name, ext,
Lev Walkina4f8e942017-10-08 19:28:20 -070055 opt_tmpname ? ".XXXXXX" : "");
56 assert(ret > 0 && ret < (ssize_t)sizeof(fname));
Lev Walkin4b102252004-08-19 13:29:18 +000057
Lev Walkina4f8e942017-10-08 19:28:20 -070058 if(opt_tmpname) {
Lev Walkin4604d032005-03-04 08:52:50 +000059 /*
60 * Create temporary file.
61 */
62 fd = mkstemp(fname);
Lev Walkin93659562010-11-20 09:47:13 -080063#ifndef _WIN32
Lev Walkinebfc44d2014-10-12 19:30:12 -070064 /* fchmod() does not respect umask */
65 (void)fchmod(fd, REASONABLE_FILE_MODE);
Lev Walkin4604d032005-03-04 08:52:50 +000066#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 Walkin4b102252004-08-19 13:29:18 +000076 }
77 if(fd == -1) {
78 perror(fname);
79 return NULL;
80 }
81
Lev Walkin93659562010-11-20 09:47:13 -080082#ifndef _WIN32
Lev Walkin4b102252004-08-19 13:29:18 +000083 /*
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 Walkind62d7d52016-01-23 08:04:46 -080093 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 Walkin4efbfb72005-02-25 14:20:30 +000099#else
100 _chsize(fd, 0);
Lev Walkin93659562010-11-20 09:47:13 -0800101#endif /* _WIN32 */
Lev Walkin4b102252004-08-19 13:29:18 +0000102
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 Walkina895afb2005-10-06 10:09:34 +0000110 return NULL;
Lev Walkin4b102252004-08-19 13:29:18 +0000111 }
Lev Walkin4604d032005-03-04 08:52:50 +0000112
113 /* Return the temporary file name */
114 if(opt_tmpname) {
115 *opt_tmpname = strdup(fname);
Lev Walkina895afb2005-10-06 10:09:34 +0000116 if(*opt_tmpname) {
117 /* Successfull */
118 } else {
119 if(created) unlink(fname);
120 fclose(fp);
121 return NULL;
122 }
Lev Walkin4604d032005-03-04 08:52:50 +0000123 }
124
Lev Walkin4b102252004-08-19 13:29:18 +0000125 return fp;
126}
127
Lev Walkina4f8e942017-10-08 19:28:20 -0700128const char *
Jon Ringle6431b1c2017-11-15 01:22:02 -0500129a1c_basename(const char *path, const char *destdir) {
Lev Walkina4f8e942017-10-08 19:28:20 -0700130 static char strbuf[PATH_MAX];
Lev Walkin79f54952004-08-13 16:58:19 +0000131 const char *pend;
132 const char *name;
Jon Ringle6431b1c2017-11-15 01:22:02 -0500133 char *sbuf = strbuf;
Lev Walkin79f54952004-08-13 16:58:19 +0000134
Jon Ringle6431b1c2017-11-15 01:22:02 -0500135 if(destdir) {
136 strncpy(strbuf, destdir, PATH_MAX - 1);
137 strbuf[PATH_MAX - 1] = '\0';
138 sbuf = strbuf + strlen(strbuf);
139 }
Lev Walkin79f54952004-08-13 16:58:19 +0000140 pend = path + strlen(path);
141 if(pend == path) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500142 strcpy(sbuf, ".");
Lev Walkin79f54952004-08-13 16:58:19 +0000143 return strbuf;
144 }
145
146 /* Skip tailing slashes */
147 for(pend--; pend > path && *pend == '/'; pend--);
148
149 if(pend == path && *path == '/') {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500150 strcpy(sbuf, "/");
Lev Walkin79f54952004-08-13 16:58:19 +0000151 return strbuf;
152 }
153
154 for(name = pend; name > path && name[-1] != '/'; name--);
155
Lev Walkin4b102252004-08-19 13:29:18 +0000156 if((pend - name) >= (int)sizeof(strbuf) - 1) {
Lev Walkin79f54952004-08-13 16:58:19 +0000157 errno = ENAMETOOLONG;
158 return 0;
159 }
160
Jon Ringle6431b1c2017-11-15 01:22:02 -0500161 memcpy(sbuf, name, pend - name + 1);
162 sbuf[pend - name + 1] = '\0';
Lev Walkin79f54952004-08-13 16:58:19 +0000163
164 return strbuf;
165}
166
167
Lev Walkina4f8e942017-10-08 19:28:20 -0700168const char *
Lev Walkin79f54952004-08-13 16:58:19 +0000169a1c_dirname(const char *path) {
Lev Walkina4f8e942017-10-08 19:28:20 -0700170 static char strbuf[PATH_MAX];
Lev Walkin79f54952004-08-13 16:58:19 +0000171 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 Walkin79f54952004-08-13 16:58:19 +0000177 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 Walkin79f54952004-08-13 16:58:19 +0000191
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 Walkin4b102252004-08-19 13:29:18 +0000202 if((last - path) >= (int)sizeof(strbuf)) {
Lev Walkin79f54952004-08-13 16:58:19 +0000203 errno = ENAMETOOLONG;
204 return 0;
205 }
206
207 memcpy(strbuf, path, last - path);
208 strbuf[last - path] = '\0';
209
210 return strbuf;
211}
212