Changes in uspace/lib/posix/src/stdlib.c [4e6a610:75c430e3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/stdlib.c
r4e6a610 r75c430e3 38 38 39 39 #include <errno.h> 40 #include <tmpfile.h>41 40 42 41 #include "posix/fcntl.h" … … 50 49 #include "libc/vfs/vfs.h" 51 50 #include "libc/stats.h" 51 52 /** 53 * Integer absolute value. 54 * 55 * @param i Input value. 56 * @return Absolute value of the parameter. 57 */ 58 int abs(int i) 59 { 60 return i < 0 ? -i : i; 61 } 62 63 /** 64 * Long integer absolute value. 65 * 66 * @param i Input value. 67 * @return Absolute value of the parameter. 68 */ 69 long labs(long i) 70 { 71 return i < 0 ? -i : i; 72 } 73 74 /** 75 * Long long integer absolute value. 76 * 77 * @param i Input value. 78 * @return Absolute value of the parameter. 79 */ 80 long long llabs(long long i) 81 { 82 return i < 0 ? -i : i; 83 } 52 84 53 85 /** … … 164 196 int mkstemp(char *tmpl) 165 197 { 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; 198 int fd = -1; 199 200 char *tptr = tmpl + strlen(tmpl) - 6; 201 202 while (fd < 0) { 203 if (*mktemp(tmpl) == '\0') { 204 /* Errno set by mktemp(). */ 205 return -1; 206 } 207 208 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 209 210 if (fd == -1) { 211 /* Restore template to it's original state. */ 212 snprintf(tptr, 7, "XXXXXX"); 213 } 214 } 215 216 return fd; 188 217 } 189 218 … … 198 227 char *mktemp(char *tmpl) 199 228 { 200 int tmpl_len; 201 int rc; 202 203 tmpl_len = strlen(tmpl); 229 int tmpl_len = strlen(tmpl); 204 230 if (tmpl_len < 6) { 205 231 errno = EINVAL; … … 215 241 } 216 242 217 rc = __tmpfile_templ(tmpl, false); 218 if (rc != 0) { 219 errno = EIO; // XXX could be more specific 243 static int seq = 0; 244 245 for (; seq < 1000000; ++seq) { 246 snprintf(tptr, 7, "%06d", seq); 247 248 int orig_errno = errno; 249 errno = 0; 250 /* Check if the file exists. */ 251 if (access(tmpl, F_OK) == -1) { 252 if (errno == ENOENT) { 253 errno = orig_errno; 254 break; 255 } else { 256 /* errno set by access() */ 257 *tmpl = '\0'; 258 return tmpl; 259 } 260 } 261 } 262 263 if (seq == 10000000) { 264 errno = EEXIST; 220 265 *tmpl = '\0'; 221 266 return tmpl;
Note:
See TracChangeset
for help on using the changeset viewer.