Changeset 11544f4 in mainline for uspace/lib/posix/stdio.c


Ignore:
Timestamp:
2011-07-29T17:46:54Z (14 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2a53f71
Parents:
ffff746
Message:

Implement a few functions for dealing with temporary files.

File:
1 edited

Legend:

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

    rffff746 r11544f4  
    4444#include "assert.h"
    4545#include "errno.h"
     46#include "stdlib.h"
    4647#include "string.h"
    4748#include "sys/types.h"
     49#include "unistd.h"
    4850
    4951#include "libc/io/printf_core.h"
     
    734736
    735737/**
    736  *
    737  * @param s
    738  * @return
     738 * Get a unique temporary file name (obsolete).
     739 *
     740 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
     741 * @return The value of s on success, NULL on failure.
    739742 */
    740743char *posix_tmpnam(char *s)
    741744{
    742         // TODO: low priority, just a compile-time dependency of binutils
    743         not_implemented();
     745        assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
     746       
     747        static char buffer[L_tmpnam + 1];
     748        if (s == NULL) {
     749                s = buffer;
     750        }
     751       
     752        posix_strcpy(s, "/tmp/tnXXXXXX");
     753        posix_mktemp(s);
     754       
     755        if (*s == '\0') {
     756                /* Errno set by mktemp(). */
     757                return NULL;
     758        }
     759       
     760        return s;
     761}
     762
     763/**
     764 * Get an unique temporary file name with additional constraints (obsolete).
     765 *
     766 * @param dir Path to directory, where the file should be created.
     767 * @param pfx Optional prefix up to 5 characters long.
     768 * @return Newly allocated unique path for temporary file. NULL on failure.
     769 */
     770char *posix_tempnam(const char *dir, const char *pfx)
     771{
     772        /* Sequence number of the filename. */
     773        static int seq = 0;
     774       
     775        size_t dir_len = posix_strlen(dir);
     776        if (dir[dir_len - 1] == '/') {
     777                dir_len--;
     778        }
     779       
     780        size_t pfx_len = posix_strlen(pfx);
     781        if (pfx_len > 5) {
     782                pfx_len = 5;
     783        }
     784       
     785        char *result = malloc(dir_len + /* slash*/ 1 +
     786            pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
     787       
     788        if (result == NULL) {
     789                errno = ENOMEM;
     790                return NULL;
     791        }
     792       
     793        char *res_ptr = result;
     794        posix_strncpy(res_ptr, dir, dir_len);
     795        res_ptr += dir_len;
     796        posix_strncpy(res_ptr, pfx, pfx_len);
     797        res_ptr += pfx_len;
     798       
     799        for (; seq < 1000; ++seq) {
     800                snprintf(res_ptr, 8, "%03d.tmp", seq);
     801               
     802                int orig_errno = errno;
     803                errno = 0;
     804                /* Check if the file exists. */
     805                if (posix_access(result, F_OK) == -1) {
     806                        if (errno == ENOENT) {
     807                                errno = orig_errno;
     808                                break;
     809                        } else {
     810                                /* errno set by access() */
     811                                return NULL;
     812                        }
     813                }
     814        }
     815       
     816        if (seq == 1000) {
     817                free(result);
     818                errno = EINVAL;
     819                return NULL;
     820        }
     821       
     822        return result;
     823}
     824
     825/**
     826 * Create and open an unique temporary file.
     827 * The file is automatically removed when the stream is closed.
     828 *
     829 * @param dir Path to directory, where the file should be created.
     830 * @param pfx Optional prefix up to 5 characters long.
     831 * @return Newly allocated unique path for temporary file. NULL on failure.
     832 */
     833FILE *posix_tmpfile(void)
     834{
     835        char filename[] = "/tmp/tfXXXXXX";
     836        int fd = posix_mkstemp(filename);
     837        if (fd == -1) {
     838                /* errno set by mkstemp(). */
     839                return NULL;
     840        }
     841       
     842        /* Unlink the created file, so that it's removed on close(). */
     843        posix_unlink(filename);
     844        return fdopen(fd, "w+");
    744845}
    745846
Note: See TracChangeset for help on using the changeset viewer.