Changeset 6afc9d7 in mainline for uspace/lib/posix


Ignore:
Timestamp:
2015-10-06T19:01:36Z (10 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0328987
Parents:
f1f7584
Message:

UNIX-like I/O functions should use errno to return error code for many reasons.

Location:
uspace/lib/posix/source
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/source/fcntl.c

    rf1f7584 r6afc9d7  
    112112        }
    113113
    114         int rc = open(pathname, flags, mode);
    115         if (rc < 0) {
    116                 errno = -rc;
    117                 rc = -1;
    118         }
    119 
    120         return rc;
     114        return negerrno(open, pathname, flags, mode);
    121115}
    122116
  • uspace/lib/posix/source/internal/common.h

    rf1f7584 r6afc9d7  
    4848        } while (0)
    4949
    50 /* A little helper macro to avoid typing this over and over. */
     50/* Convert negative error return value to positive errno */
    5151#define errnify(func, ...) ({ \
    5252        int rc = func(__VA_ARGS__); \
     
    5858})
    5959
     60/* Convert negative errno to positive errno */
     61#define negerrno(func, ...) ({ \
     62        int rc = func(__VA_ARGS__); \
     63        if (rc < 0) { \
     64                errno = -errno; \
     65        } \
     66        rc; \
     67})
     68
    6069#endif /* LIBPOSIX_COMMON_H_ */
    6170
  • uspace/lib/posix/source/stdio.c

    rf1f7584 r6afc9d7  
    315315int posix_fflush(FILE *stream)
    316316{
    317         int rc = fflush(stream);
    318         if (rc < 0) {
    319                 errno = -rc;
    320                 return EOF;
    321         } else {
    322                 return 0;
    323         }
     317        return negerrno(fflush, stream);
    324318}
    325319
     
    351345{
    352346        ssize_t wr = write(*(int *) fd, str, size);
     347        if (wr < 0)
     348                return errno;
    353349        return str_nlength(str, wr);
    354350}
     
    579575int posix_remove(const char *path)
    580576{
    581         struct stat st;
    582         int rc = stat(path, &st);
    583        
    584         if (rc != EOK) {
    585                 errno = -rc;
    586                 return -1;
    587         }
    588        
    589         if (st.is_directory) {
    590                 rc = rmdir(path);
    591         } else {
    592                 rc = unlink(path);
    593         }
    594        
    595         if (rc != EOK) {
    596                 errno = -rc;
    597                 return -1;
    598         }
    599         return 0;
     577        return negerrno(remove, path);
    600578}
    601579
     
    609587int posix_rename(const char *old, const char *new)
    610588{
    611         return errnify(rename, old, new);
     589        return negerrno(rename, old, new);
    612590}
    613591
  • uspace/lib/posix/source/stdlib.c

    rf1f7584 r6afc9d7  
    278278         * to be updated when that support is implemented.
    279279         */
    280         char* absolute = absolutize(name, NULL);
     280        char* absolute = vfs_absolutize(name, NULL);
    281281       
    282282        if (absolute == NULL) {
  • uspace/lib/posix/source/sys/stat.c

    rf1f7584 r6afc9d7  
    8080{
    8181        struct stat hst;
    82         int rc = fstat(fd, &hst);
    83         if (rc < 0) {
    84                 /* fstat() returns negative error code instead of using errno. */
    85                 errno = -rc;
    86                 return -1;
    87         }
     82        int rc = negerrno(fstat, fd, &hst);
     83        if (rc < 0)
     84                return rc;
    8885        stat_to_posix(st, &hst);
    8986        return 0;
     
    113110{
    114111        struct stat hst;
    115         int rc = stat(path, &hst);
    116         if (rc < 0) {
    117                 /* stat() returns negative error code instead of using errno. */
    118                 errno = -rc;
    119                 return -1;
    120         }
     112        int rc = negerrno(stat, path, &hst);
     113        if (rc < 0)
     114                return rc;
    121115        stat_to_posix(st, &hst);
    122116        return 0;
  • uspace/lib/posix/source/unistd.c

    rf1f7584 r6afc9d7  
    105105char *posix_getcwd(char *buf, size_t size)
    106106{
    107         /* Native getcwd() does not set any errno despite the fact that general
    108          * usage pattern of this function depends on it (caller is repeatedly
    109          * guessing the required size of the buffer by checking for ERANGE on
    110          * failure). */
    111         if (size == 0) {
    112                 errno = EINVAL;
     107        char *p = getcwd(buf, size);
     108
     109        if (p == NULL) {
     110                errno = -errno;
    113111                return NULL;
    114112        }
    115        
    116         /* Save the original value to comply with the "no modification on
    117          * success" semantics.
    118          */
    119         int orig_errno = errno;
    120         errno = EOK;
    121        
    122         char *ret = getcwd(buf, size);
    123         if (ret == NULL) {
    124                 /* Check errno to avoid shadowing other possible errors. */
    125                 if (errno == EOK) {
    126                         errno = ERANGE;
    127                 }
    128         } else {
    129                 /* Success, restore previous errno value. */
    130                 errno = orig_errno;
    131         }
    132        
    133         return ret;
     113
     114        return p;
    134115}
    135116
     
    141122int posix_chdir(const char *path)
    142123{
    143         return errnify(chdir, path);
     124        return negerrno(chdir, path);
    144125}
    145126
     
    194175int posix_close(int fildes)
    195176{
    196         return errnify(close, fildes);
     177        return negerrno(close, fildes);
    197178}
    198179
     
    207188ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    208189{
    209         return errnify(read, fildes, buf, nbyte);
     190        return negerrno(read, fildes, buf, nbyte);
    210191}
    211192
     
    220201ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
    221202{
    222         return errnify(write, fildes, buf, nbyte);
     203        return negerrno(write, fildes, buf, nbyte);
    223204}
    224205
     
    234215posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
    235216{
    236         return errnify(lseek, fildes, offset, whence);
     217        return negerrno(lseek, fildes, offset, whence);
    237218}
    238219
     
    245226int posix_fsync(int fildes)
    246227{
    247         return errnify(fsync, fildes);
     228        return negerrno(fsync, fildes);
    248229}
    249230
     
    257238int posix_ftruncate(int fildes, posix_off_t length)
    258239{
    259         return errnify(ftruncate, fildes, (aoff64_t) length);
     240        return negerrno(ftruncate, fildes, (aoff64_t) length);
    260241}
    261242
     
    268249int posix_rmdir(const char *path)
    269250{
    270         return errnify(rmdir, path);
     251        return negerrno(rmdir, path);
    271252}
    272253
     
    279260int posix_unlink(const char *path)
    280261{
    281         return errnify(unlink, path);
     262        return negerrno(unlink, path);
    282263}
    283264
     
    303284int posix_dup2(int fildes, int fildes2)
    304285{
    305         return errnify(dup2, fildes, fildes2);
     286        return negerrno(dup2, fildes, fildes2);
    306287}
    307288
     
    321302                 * Check file existence by attempting to open it.
    322303                 */
    323                 int fd = open(path, O_RDONLY);
     304                int fd = negerrno(open, path, O_RDONLY);
    324305                if (fd < 0) {
    325                         errno = -fd;
     306                        /* errno was set by open() */
    326307                        return -1;
    327308                }
Note: See TracChangeset for help on using the changeset viewer.