Changeset 4e6a610 in mainline for uspace/lib/posix/src/stdlib.c


Ignore:
Timestamp:
2018-06-25T09:54:28Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
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)
Message:

Temporary file functions rework. Fix libposix access() not working on directories.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/src/stdlib.c

    rfb0ec570 r4e6a610  
    3838
    3939#include <errno.h>
     40#include <tmpfile.h>
    4041
    4142#include "posix/fcntl.h"
     
    163164int mkstemp(char *tmpl)
    164165{
    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;
    184188}
    185189
     
    194198char *mktemp(char *tmpl)
    195199{
    196         int tmpl_len = strlen(tmpl);
     200        int tmpl_len;
     201        int rc;
     202
     203        tmpl_len = strlen(tmpl);
    197204        if (tmpl_len < 6) {
    198205                errno = EINVAL;
     
    208215        }
    209216
    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
    232220                *tmpl = '\0';
    233221                return tmpl;
Note: See TracChangeset for help on using the changeset viewer.