blob: 4bd70f316f6d09965154dadae56749032c584146 [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
4#ifndef MAXPATHLEN
5#define MAXPATHLEN 1024
6#endif
7
Lev Walkinebfc44d2014-10-12 19:30:12 -07008/* Normally file permissions are (DEFFILEMODE & ~umask(2)) */
Lev Walkin4b102252004-08-19 13:29:18 +00009#ifndef DEFFILEMODE /* Normally in <sys/stat.h> */
Lev Walkinebfc44d2014-10-12 19:30:12 -070010
Lev Walkin93659562010-11-20 09:47:13 -080011#ifdef _WIN32
Lev Walkine751cd82007-11-13 22:30:09 +000012#define DEFFILEMODE (S_IREAD|S_IWRITE)
Lev Walkinebfc44d2014-10-12 19:30:12 -070013#define REASONABLE_FILE_MODE DEFFILEMODE
Lev Walkin4efbfb72005-02-25 14:20:30 +000014#else
Lev Walkin4b102252004-08-19 13:29:18 +000015#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
Lev Walkinebfc44d2014-10-12 19:30:12 -070016#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 Walkin4b102252004-08-19 13:29:18 +000023#endif
Lev Walkin4efbfb72005-02-25 14:20:30 +000024#endif
Lev Walkin4b102252004-08-19 13:29:18 +000025
Lev Walkin50d52cf2013-03-16 08:00:57 -070026#ifdef _WIN32
27int 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 Walkin27fd0b62007-08-27 23:57:45 +000037#ifdef HAVE_MKSTEMPS
38#undef mkstemp
39#define mkstemp(foo) mkstemps(foo, 0)
40#endif
41
Lev Walkin4b102252004-08-19 13:29:18 +000042FILE *
Lev Walkin4604d032005-03-04 08:52:50 +000043asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
Lev Walkin4b102252004-08-19 13:29:18 +000044 int created = 1;
Lev Walkin93659562010-11-20 09:47:13 -080045#ifndef _WIN32
Lev Walkin4b102252004-08-19 13:29:18 +000046 struct stat sb;
Lev Walkin4efbfb72005-02-25 14:20:30 +000047#endif
Lev Walkin4b102252004-08-19 13:29:18 +000048 char *fname;
Lev Walkin4efbfb72005-02-25 14:20:30 +000049 size_t len;
Lev Walkin4b102252004-08-19 13:29:18 +000050 FILE *fp;
Lev Walkin4604d032005-03-04 08:52:50 +000051 int ret;
Lev Walkin4b102252004-08-19 13:29:18 +000052 int fd;
53
54 /*
55 * Compute filenames.
56 */
Lev Walkin4604d032005-03-04 08:52:50 +000057 len = strlen(name) + strlen(ext) + sizeof(".XXXXXX");
Lev Walkin4b102252004-08-19 13:29:18 +000058 fname = alloca(len);
Lev Walkin4604d032005-03-04 08:52:50 +000059 ret = snprintf(fname, len, "%s%s%s", name, ext,
60 opt_tmpname ? ".XXXXXX" : "");
Lev Walkinc46b7cb2006-08-18 02:27:55 +000061 assert(ret > 0 && ret < (ssize_t)len);
Lev Walkin4b102252004-08-19 13:29:18 +000062
Lev Walkin4604d032005-03-04 08:52:50 +000063 if(opt_tmpname) {
64 /*
65 * Create temporary file.
66 */
67 fd = mkstemp(fname);
Lev Walkin93659562010-11-20 09:47:13 -080068#ifndef _WIN32
Lev Walkinebfc44d2014-10-12 19:30:12 -070069 /* fchmod() does not respect umask */
70 (void)fchmod(fd, REASONABLE_FILE_MODE);
Lev Walkin4604d032005-03-04 08:52:50 +000071#endif
72 } else {
73 /*
74 * Create specified file, or open the old one.
75 */
76 fd = open(fname, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE);
77 if(fd == -1 && errno == EEXIST) {
78 fd = open(fname, O_WRONLY, DEFFILEMODE);
79 created = 0;
80 }
Lev Walkin4b102252004-08-19 13:29:18 +000081 }
82 if(fd == -1) {
83 perror(fname);
84 return NULL;
85 }
86
Lev Walkin93659562010-11-20 09:47:13 -080087#ifndef _WIN32
Lev Walkin4b102252004-08-19 13:29:18 +000088 /*
89 * Check sanity.
90 */
91 if(fstat(fd, &sb) || !S_ISREG(sb.st_mode)) {
92 fprintf(stderr, "%s: Not a regular file\n", fname);
93 if(created) unlink(fname);
94 close(fd);
95 return NULL;
96 }
97
Lev Walkind62d7d52016-01-23 08:04:46 -080098 if(ftruncate(fd, 0) == -1) {
99 fprintf(stderr, "%s: ftruncate failed: %s\n",
100 fname, strerror(errno));
101 if(created) unlink(fname);
102 return NULL;
103 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000104#else
105 _chsize(fd, 0);
Lev Walkin93659562010-11-20 09:47:13 -0800106#endif /* _WIN32 */
Lev Walkin4b102252004-08-19 13:29:18 +0000107
108 /*
109 * Convert file descriptor into file pointer.
110 */
111 fp = fdopen(fd, "w");
112 if(fp == NULL) {
113 if(created) unlink(fname);
114 close(fd);
Lev Walkina895afb2005-10-06 10:09:34 +0000115 return NULL;
Lev Walkin4b102252004-08-19 13:29:18 +0000116 }
Lev Walkin4604d032005-03-04 08:52:50 +0000117
118 /* Return the temporary file name */
119 if(opt_tmpname) {
120 *opt_tmpname = strdup(fname);
Lev Walkina895afb2005-10-06 10:09:34 +0000121 if(*opt_tmpname) {
122 /* Successfull */
123 } else {
124 if(created) unlink(fname);
125 fclose(fp);
126 return NULL;
127 }
Lev Walkin4604d032005-03-04 08:52:50 +0000128 }
129
Lev Walkin4b102252004-08-19 13:29:18 +0000130 return fp;
131}
132
133
Lev Walkin79f54952004-08-13 16:58:19 +0000134char *
135a1c_basename(const char *path) {
136 static char strbuf[MAXPATHLEN];
137 const char *pend;
138 const char *name;
139
140 pend = path + strlen(path);
141 if(pend == path) {
142 strcpy(strbuf, ".");
143 return strbuf;
144 }
145
146 /* Skip tailing slashes */
147 for(pend--; pend > path && *pend == '/'; pend--);
148
149 if(pend == path && *path == '/') {
150 strcpy(strbuf, "/");
151 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
161 memcpy(strbuf, name, pend - name + 1);
162 strbuf[pend - name + 1] = '\0';
163
164 return strbuf;
165}
166
167
168char *
169a1c_dirname(const char *path) {
170 static char strbuf[MAXPATHLEN];
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 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