Changeset 1cc4a09 in mainline for uspace/lib/posix


Ignore:
Timestamp:
2011-08-14T00:19:31Z (15 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b705ecc
Parents:
6d100fd (diff), e0e922d (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.

Location:
uspace/lib/posix
Files:
8 edited

Legend:

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

    r6d100fd r1cc4a09  
    252252                    " or fully unsupported signal. This handler may only be"
    253253                    " invoked by the raise() function, which may not be what"
    254                     " the application developer intended.\nSignal name");
     254                    " the application developer intended");
    255255        }
    256256
     
    360360        }
    361361
     362        if (signo > _TOP_SIGNAL) {
     363                errno = EINVAL;
     364                return -1;
     365        }
     366
    362367        if (pid == (posix_pid_t) task_get_id()) {
    363368                return posix_raise(signo);
    364         }
    365 
    366         if (pid > _TOP_SIGNAL) {
    367                 errno = EINVAL;
    368                 return -1;
    369369        }
    370370
  • uspace/lib/posix/stdio.c

    r6d100fd r1cc4a09  
    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       
     
    724735 * Rename a file or directory.
    725736 *
    726  * @param old
    727  * @param new
     737 * @param old Old pathname.
     738 * @param new New pathname.
    728739 * @return Zero on success, -1 (with errno set) otherwise.
    729740 */
     
    734745
    735746/**
    736  *
    737  * @param s
    738  * @return
     747 * Get a unique temporary file name (obsolete).
     748 *
     749 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
     750 * @return The value of s on success, NULL on failure.
    739751 */
    740752char *posix_tmpnam(char *s)
    741753{
    742         // TODO: low priority, just a compile-time dependency of binutils
    743         not_implemented();
     754        assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
     755       
     756        static char buffer[L_tmpnam + 1];
     757        if (s == NULL) {
     758                s = buffer;
     759        }
     760       
     761        posix_strcpy(s, "/tmp/tnXXXXXX");
     762        posix_mktemp(s);
     763       
     764        if (*s == '\0') {
     765                /* Errno set by mktemp(). */
     766                return NULL;
     767        }
     768       
     769        return s;
     770}
     771
     772/**
     773 * Get an unique temporary file name with additional constraints (obsolete).
     774 *
     775 * @param dir Path to directory, where the file should be created.
     776 * @param pfx Optional prefix up to 5 characters long.
     777 * @return Newly allocated unique path for temporary file. NULL on failure.
     778 */
     779char *posix_tempnam(const char *dir, const char *pfx)
     780{
     781        /* Sequence number of the filename. */
     782        static int seq = 0;
     783       
     784        size_t dir_len = posix_strlen(dir);
     785        if (dir[dir_len - 1] == '/') {
     786                dir_len--;
     787        }
     788       
     789        size_t pfx_len = posix_strlen(pfx);
     790        if (pfx_len > 5) {
     791                pfx_len = 5;
     792        }
     793       
     794        char *result = malloc(dir_len + /* slash*/ 1 +
     795            pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
     796       
     797        if (result == NULL) {
     798                errno = ENOMEM;
     799                return NULL;
     800        }
     801       
     802        char *res_ptr = result;
     803        posix_strncpy(res_ptr, dir, dir_len);
     804        res_ptr += dir_len;
     805        posix_strncpy(res_ptr, pfx, pfx_len);
     806        res_ptr += pfx_len;
     807       
     808        for (; seq < 1000; ++seq) {
     809                snprintf(res_ptr, 8, "%03d.tmp", seq);
     810               
     811                int orig_errno = errno;
     812                errno = 0;
     813                /* Check if the file exists. */
     814                if (posix_access(result, F_OK) == -1) {
     815                        if (errno == ENOENT) {
     816                                errno = orig_errno;
     817                                break;
     818                        } else {
     819                                /* errno set by access() */
     820                                return NULL;
     821                        }
     822                }
     823        }
     824       
     825        if (seq == 1000) {
     826                free(result);
     827                errno = EINVAL;
     828                return NULL;
     829        }
     830       
     831        return result;
     832}
     833
     834/**
     835 * Create and open an unique temporary file.
     836 * The file is automatically removed when the stream is closed.
     837 *
     838 * @param dir Path to directory, where the file should be created.
     839 * @param pfx Optional prefix up to 5 characters long.
     840 * @return Newly allocated unique path for temporary file. NULL on failure.
     841 */
     842FILE *posix_tmpfile(void)
     843{
     844        char filename[] = "/tmp/tfXXXXXX";
     845        int fd = posix_mkstemp(filename);
     846        if (fd == -1) {
     847                /* errno set by mkstemp(). */
     848                return NULL;
     849        }
     850       
     851        /* Unlink the created file, so that it's removed on close(). */
     852        posix_unlink(filename);
     853        return fdopen(fd, "w+");
    744854}
    745855
  • uspace/lib/posix/stdio.h

    r6d100fd r1cc4a09  
    123123#define L_tmpnam PATH_MAX
    124124extern char *posix_tmpnam(char *s);
     125extern char *posix_tempnam(const char *dir, const char *pfx);
     126extern FILE *posix_tmpfile(void);
    125127
    126128#ifndef LIBPOSIX_INTERNAL
     129        /* DEBUG macro does not belong to POSIX stdio.h. Its unconditional
     130         * definition in the native stdio.h causes unexpected behaviour of
     131         * applications which uses their own DEBUG macro (e.g. debugging
     132         * output is printed even if not desirable). */
     133        #undef DEBUG
     134
    127135        #define ctermid posix_ctermid
    128136
     
    176184
    177185        #define tmpnam posix_tmpnam
     186        #define tempnam posix_tempnam
     187        #define tmpfile posix_tmpfile
    178188#endif
    179189
  • uspace/lib/posix/stdlib.c

    r6d100fd r1cc4a09  
    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
  • uspace/lib/posix/stdlib.h

    r6d100fd r1cc4a09  
    113113extern void posix_free(void *ptr);
    114114
     115/* Temporary Files */
     116extern int posix_mkstemp(char *tmpl);
     117
    115118/* Legacy Declarations */
    116119extern char *posix_mktemp(char *tmpl);
     
    158161        #define free posix_free
    159162
     163        #define mkstemp posix_mkstemp
     164
    160165        #define mktemp posix_mktemp
    161166        #define getloadavg bsd_getloadavg
  • uspace/lib/posix/sys/wait.c

    r6d100fd r1cc4a09  
    6767
    6868/**
     69 * Wait for any child process to stop or terminate.
    6970 *
    70  * @param stat_ptr
    71  * @return
     71 * @param stat_ptr Location of the final status code of the child process.
     72 * @return ID of the child process for which status is reported,
     73 *     -1 on signal interrupt, (pid_t)-1 otherwise.
    7274 */
    7375posix_pid_t posix_wait(int *stat_ptr)
     
    7981
    8082/**
     83 * Wait for a child process to stop or terminate.
    8184 *
    82  * @param pid
    83  * @param stat_ptr
    84  * @param options
    85  * @return
     85 * @param pid What child process shall the caller wait for. See POSIX manual
     86 *     for details.
     87 * @param stat_ptr Location of the final status code of the child process.
     88 * @param options Constraints of the waiting. See POSIX manual for details.
     89 * @return ID of the child process for which status is reported,
     90 *     -1 on signal interrupt, 0 if non-blocking wait is requested but there is
     91 *     no child process whose status can be reported, (pid_t)-1 otherwise.
    8692 */
    8793posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options)
  • uspace/lib/posix/unistd.c

    r6d100fd r1cc4a09  
    186186 * Close a file.
    187187 *
    188  * @param fildes
     188 * @param fildes File descriptor of the opened file.
    189189 * @return 0 on success, -1 on error.
    190190 */
     
    223223 * Requests outstanding data to be written to the underlying storage device.
    224224 *
    225  * @param fildes
     225 * @param fildes File descriptor of the opened file.
     226 * @return Zero on success, -1 otherwise.
    226227 */
    227228int posix_fsync(int fildes)
     
    230231}
    231232
     233/**
     234 * Truncate a file to a specified length.
     235 *
     236 * @param fildes File descriptor of the opened file.
     237 * @param length New length of the file.
     238 * @return Zero on success, -1 otherwise.
     239 */
    232240int posix_ftruncate(int fildes, posix_off_t length)
    233241{
     
    257265}
    258266
     267/**
     268 * Duplicate an open file descriptor.
     269 *
     270 * @param fildes File descriptor to be duplicated.
     271 * @return On success, new file descriptor for the same file, otherwise -1.
     272 */
    259273int posix_dup(int fildes)
    260274{
     
    262276}
    263277
     278/**
     279 * Duplicate an open file descriptor.
     280 *
     281 * @param fildes File descriptor to be duplicated.
     282 * @param fildes2 File descriptor to be paired with the same file description
     283 *     as is paired fildes.
     284 * @return fildes2 on success, -1 otherwise.
     285 */
    264286int posix_dup2(int fildes, int fildes2)
    265287{
  • uspace/lib/posix/unistd.h

    r6d100fd r1cc4a09  
    7272/* File Manipulation */
    7373extern int posix_close(int fildes);
    74 
    7574extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
    7675extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte);
    77 
    7876extern int posix_fsync(int fildes);
    7977extern int posix_ftruncate(int fildes, posix_off_t length);
    80 
    8178extern int posix_rmdir(const char *path);
    8279extern int posix_unlink(const char *path);
    83 
    8480extern int posix_dup(int fildes);
    8581extern int posix_dup2(int fildes, int fildes2);
Note: See TracChangeset for help on using the changeset viewer.