Changeset 3e01316f in mainline for uspace/lib/posix/stdio.c


Ignore:
Timestamp:
2011-08-17T18:04:50Z (13 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:
0cf27ee
Parents:
e898296d (diff), e6165be (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.

File:
1 edited

Legend:

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

    re898296d r3e01316f  
    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"
     
    255257        assert(stream != NULL);
    256258       
    257         /* Retieve the node. */
     259        /* Retrieve the node. */
    258260        struct stat st;
    259261        int rc;
     
    263265        } else {
    264266                rc = stat(filename, &st);
     267                if (-rc == ENOENT) {
     268                        /* file does not exist, create new file */
     269                        FILE* tmp = fopen(filename, mode);
     270                        if (tmp != NULL) {
     271                                fclose(tmp);
     272                                /* try again */
     273                                rc = stat(filename, &st);
     274                        }
     275                }
    265276        }
    266277       
     
    273284        fdi_node_t node = {
    274285                .fs_handle = st.fs_handle,
    275                 .devmap_handle = st.devmap_handle,
     286                .service_id = st.service_id,
    276287                .index = st.index
    277288        };
     
    306317       
    307318        return stream;
    308 }
    309 
    310 /**
    311  *
    312  * @param buf
    313  * @param size
    314  * @param mode
    315  * @return
    316  */
    317 FILE *posix_fmemopen(void *restrict buf, size_t size,
    318     const char *restrict mode)
    319 {
    320         // TODO
    321         not_implemented();
    322 }
    323 
    324 /**
    325  *
    326  * @param bufp
    327  * @param sizep
    328  * @return
    329  */
    330 FILE *posix_open_memstream(char **bufp, size_t *sizep)
    331 {
    332         // TODO
    333         not_implemented();
    334319}
    335320
     
    724709 * Rename a file or directory.
    725710 *
    726  * @param old
    727  * @param new
     711 * @param old Old pathname.
     712 * @param new New pathname.
    728713 * @return Zero on success, -1 (with errno set) otherwise.
    729714 */
     
    734719
    735720/**
    736  *
    737  * @param s
    738  * @return
     721 * Get a unique temporary file name (obsolete).
     722 *
     723 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
     724 * @return The value of s on success, NULL on failure.
    739725 */
    740726char *posix_tmpnam(char *s)
    741727{
    742         // TODO: low priority, just a compile-time dependency of binutils
    743         not_implemented();
     728        assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
     729       
     730        static char buffer[L_tmpnam + 1];
     731        if (s == NULL) {
     732                s = buffer;
     733        }
     734       
     735        posix_strcpy(s, "/tmp/tnXXXXXX");
     736        posix_mktemp(s);
     737       
     738        if (*s == '\0') {
     739                /* Errno set by mktemp(). */
     740                return NULL;
     741        }
     742       
     743        return s;
     744}
     745
     746/**
     747 * Get an unique temporary file name with additional constraints (obsolete).
     748 *
     749 * @param dir Path to directory, where the file should be created.
     750 * @param pfx Optional prefix up to 5 characters long.
     751 * @return Newly allocated unique path for temporary file. NULL on failure.
     752 */
     753char *posix_tempnam(const char *dir, const char *pfx)
     754{
     755        /* Sequence number of the filename. */
     756        static int seq = 0;
     757       
     758        size_t dir_len = posix_strlen(dir);
     759        if (dir[dir_len - 1] == '/') {
     760                dir_len--;
     761        }
     762       
     763        size_t pfx_len = posix_strlen(pfx);
     764        if (pfx_len > 5) {
     765                pfx_len = 5;
     766        }
     767       
     768        char *result = malloc(dir_len + /* slash*/ 1 +
     769            pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
     770       
     771        if (result == NULL) {
     772                errno = ENOMEM;
     773                return NULL;
     774        }
     775       
     776        char *res_ptr = result;
     777        posix_strncpy(res_ptr, dir, dir_len);
     778        res_ptr += dir_len;
     779        posix_strncpy(res_ptr, pfx, pfx_len);
     780        res_ptr += pfx_len;
     781       
     782        for (; seq < 1000; ++seq) {
     783                snprintf(res_ptr, 8, "%03d.tmp", seq);
     784               
     785                int orig_errno = errno;
     786                errno = 0;
     787                /* Check if the file exists. */
     788                if (posix_access(result, F_OK) == -1) {
     789                        if (errno == ENOENT) {
     790                                errno = orig_errno;
     791                                break;
     792                        } else {
     793                                /* errno set by access() */
     794                                return NULL;
     795                        }
     796                }
     797        }
     798       
     799        if (seq == 1000) {
     800                free(result);
     801                errno = EINVAL;
     802                return NULL;
     803        }
     804       
     805        return result;
     806}
     807
     808/**
     809 * Create and open an unique temporary file.
     810 * The file is automatically removed when the stream is closed.
     811 *
     812 * @param dir Path to directory, where the file should be created.
     813 * @param pfx Optional prefix up to 5 characters long.
     814 * @return Newly allocated unique path for temporary file. NULL on failure.
     815 */
     816FILE *posix_tmpfile(void)
     817{
     818        char filename[] = "/tmp/tfXXXXXX";
     819        int fd = posix_mkstemp(filename);
     820        if (fd == -1) {
     821                /* errno set by mkstemp(). */
     822                return NULL;
     823        }
     824       
     825        /* Unlink the created file, so that it's removed on close(). */
     826        posix_unlink(filename);
     827        return fdopen(fd, "w+");
    744828}
    745829
Note: See TracChangeset for help on using the changeset viewer.