Changeset b7ee0369 in mainline for uspace/lib/posix/stdlib.c


Ignore:
Timestamp:
2011-07-31T05:08:44Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d3e241a
Parents:
051e6ac (diff), 27a8d1d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

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

    r051e6ac rb7ee0369  
    4040
    4141#include "errno.h"
     42#include "fcntl.h"
    4243#include "limits.h"
     44#include "string.h"
     45#include "sys/stat.h"
     46#include "unistd.h"
    4347
    4448#include "libc/sort.h"
     
    385389
    386390/**
    387  *
    388  * @param tmpl
    389  * @return
     391 * Creates and opens an unique temporary file from template.
     392 *
     393 * @param tmpl Template. Last six characters must be XXXXXX.
     394 * @return The opened file descriptor or -1 on error.
     395 */
     396int posix_mkstemp(char *tmpl)
     397{
     398        int fd = -1;
     399       
     400        char *tptr = tmpl + posix_strlen(tmpl) - 6;
     401       
     402        while (fd < 0) {
     403                if (*posix_mktemp(tmpl) == '\0') {
     404                        /* Errno set by mktemp(). */
     405                        return -1;
     406                }
     407               
     408                fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
     409               
     410                if (fd == -1) {
     411                        /* Restore template to it's original state. */
     412                        snprintf(tptr, 7, "XXXXXX");
     413                }
     414        }
     415       
     416        return fd;
     417}
     418
     419/**
     420 * Creates an unique temporary file name from template.
     421 *
     422 * @param tmpl Template. Last six characters must be XXXXXX.
     423 * @return The value of tmpl. The template is modified in place.
     424 *    If no temporary file name can be created, template is
     425 *    reduced to an empty string.
    390426 */
    391427char *posix_mktemp(char *tmpl)
    392428{
    393         // TODO: low priority, just a compile-time dependency of binutils
    394         not_implemented();
     429        int tmpl_len = posix_strlen(tmpl);
     430        if (tmpl_len < 6) {
     431                errno = EINVAL;
     432                *tmpl = '\0';
     433                return tmpl;
     434        }
     435       
     436        char *tptr = tmpl + tmpl_len - 6;
     437        if (posix_strcmp(tptr, "XXXXXX") != 0) {
     438                errno = EINVAL;
     439                *tmpl = '\0';
     440                return tmpl;
     441        }
     442       
     443        static int seq = 0;
     444       
     445        for (; seq < 1000000; ++seq) {
     446                snprintf(tptr, 7, "%06d", seq);
     447               
     448                int orig_errno = errno;
     449                errno = 0;
     450                /* Check if the file exists. */
     451                if (posix_access(tmpl, F_OK) == -1) {
     452                        if (errno == ENOENT) {
     453                                errno = orig_errno;
     454                                break;
     455                        } else {
     456                                /* errno set by access() */
     457                                *tmpl = '\0';
     458                                return tmpl;
     459                        }
     460                }
     461        }
     462       
     463        if (seq == 10000000) {
     464                errno = EEXIST;
     465                *tmpl = '\0';
     466                return tmpl;
     467        }
     468       
     469        return tmpl;
    395470}
    396471
Note: See TracChangeset for help on using the changeset viewer.