Changeset 4e6a610 in mainline for uspace/lib/posix/src/stdlib.c
- Timestamp:
- 2018-06-25T09:54:28Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bfe90b6
- Parents:
- fb0ec570
- git-author:
- Jiri Svoboda <jiri@…> (2018-06-24 17:51:54)
- git-committer:
- Jiri Svoboda <jiri@…> (2018-06-25 09:54:28)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/stdlib.c
rfb0ec570 r4e6a610 38 38 39 39 #include <errno.h> 40 #include <tmpfile.h> 40 41 41 42 #include "posix/fcntl.h" … … 163 164 int mkstemp(char *tmpl) 164 165 { 165 int fd = -1; 166 167 char *tptr = tmpl + strlen(tmpl) - 6; 168 169 while (fd < 0) { 170 if (*mktemp(tmpl) == '\0') { 171 /* Errno set by mktemp(). */ 172 return -1; 173 } 174 175 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 176 177 if (fd == -1) { 178 /* Restore template to it's original state. */ 179 snprintf(tptr, 7, "XXXXXX"); 180 } 181 } 182 183 return fd; 166 int tmpl_len; 167 int file; 168 169 tmpl_len = strlen(tmpl); 170 if (tmpl_len < 6) { 171 errno = EINVAL; 172 return -1; 173 } 174 175 char *tptr = tmpl + tmpl_len - 6; 176 if (strcmp(tptr, "XXXXXX") != 0) { 177 errno = EINVAL; 178 return -1; 179 } 180 181 file = __tmpfile_templ(tmpl, true); 182 if (file < 0) { 183 errno = EIO; // XXX could be more specific 184 return -1; 185 } 186 187 return file; 184 188 } 185 189 … … 194 198 char *mktemp(char *tmpl) 195 199 { 196 int tmpl_len = strlen(tmpl); 200 int tmpl_len; 201 int rc; 202 203 tmpl_len = strlen(tmpl); 197 204 if (tmpl_len < 6) { 198 205 errno = EINVAL; … … 208 215 } 209 216 210 static int seq = 0; 211 212 for (; seq < 1000000; ++seq) { 213 snprintf(tptr, 7, "%06d", seq); 214 215 int orig_errno = errno; 216 errno = 0; 217 /* Check if the file exists. */ 218 if (access(tmpl, F_OK) == -1) { 219 if (errno == ENOENT) { 220 errno = orig_errno; 221 break; 222 } else { 223 /* errno set by access() */ 224 *tmpl = '\0'; 225 return tmpl; 226 } 227 } 228 } 229 230 if (seq == 10000000) { 231 errno = EEXIST; 217 rc = __tmpfile_templ(tmpl, false); 218 if (rc != 0) { 219 errno = EIO; // XXX could be more specific 232 220 *tmpl = '\0'; 233 221 return tmpl;
Note:
See TracChangeset
for help on using the changeset viewer.