Changes in / [1814ee4d:d85a01c] in mainline


Ignore:
Location:
uspace/lib/posix
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/assert.h

    r1814ee4d rd85a01c  
    4040
    4141#ifndef NDEBUG
    42         #define assert(expr) ((expr) ? (void) 0 : assert_abort(#expr, __FILE__, __LINE__))
     42        #define assert(expr) \
     43                do { \
     44                        if (!(expr)) { \
     45                                assert_abort(#expr, __FILE__, __LINE__); \
     46                        } \
     47                } while (0)
    4348#else
    4449        #define assert(expr) ((void) 0)
  • uspace/lib/posix/ctype.c

    r1814ee4d rd85a01c  
    9494int posix_isprint(int c)
    9595{
    96         return posix_isascii(c) && !posix_iscntrl(c);
     96        return !posix_iscntrl(c);
    9797}
    9898
  • uspace/lib/posix/fnmatch.c

    r1814ee4d rd85a01c  
    525525static char *_casefold(const char *s)
    526526{
    527         assert(s != NULL);
    528527        char *result = strdup(s);
    529528        for (char *i = result; *i != '\0'; ++i) {
     
    543542int posix_fnmatch(const char *pattern, const char *string, int flags)
    544543{
    545         assert(pattern != NULL);
    546         assert(string != NULL);
    547 
    548544        // TODO: don't fold everything in advance, but only when needed
    549545
  • uspace/lib/posix/internal/common.h

    r1814ee4d rd85a01c  
    4343    __func__, __FILE__, __LINE__), abort())
    4444
    45 /* A little helper macro to avoid typing this over and over. */
    46 #define errnify(func, ...) ({ \
    47         int rc = func(__VA_ARGS__); \
    48         if (rc < 0) { \
    49                 errno = -rc; \
    50                 rc = -1; \
    51         } \
    52         rc; \
    53 })
    54 
    5545#endif /* LIBPOSIX_COMMON_H_ */
    5646
  • uspace/lib/posix/pwd.c

    r1814ee4d rd85a01c  
    3939#include "errno.h"
    4040
     41// TODO: documentation
     42
    4143static bool entry_read = false;
    4244
     
    4446static const struct posix_passwd dummy_pwd = {
    4547        .pw_name = (char *) "user",
    46         .pw_uid = 0,
    47         .pw_gid = 0,
     48        .pw_uid = 1,
     49        .pw_gid = 1,
    4850        .pw_dir = (char *) "/",
    4951        .pw_shell = (char *) "/app/bdsh"
     
    113115{
    114116        assert(name != NULL);
    115         assert(pwd != NULL);
    116         assert(buffer != NULL);
    117         assert(result != NULL);
    118117       
    119118        if (posix_strcmp(name, "user") != 0) {
     
    122121        }
    123122       
    124         return posix_getpwuid_r(0, pwd, buffer, bufsize, result);
     123        return posix_getpwuid_r(1, pwd, buffer, bufsize, result);
    125124}
    126125
     
    133132struct posix_passwd *posix_getpwuid(posix_uid_t uid)
    134133{
    135         if (uid != 0) {
     134        if (uid != 1) {
    136135                return NULL;
    137136        }
     
    160159            '/', '\0', 'b', 'd', 's', 'h', '\0' };
    161160       
    162         if (uid != 0) {
     161        if (uid != 1) {
    163162                *result = NULL;
    164163                return 0;
     
    172171
    173172        pwd->pw_name = (char *) bf;
    174         pwd->pw_uid = 0;
    175         pwd->pw_gid = 0;
     173        pwd->pw_uid = 1;
     174        pwd->pw_gid = 1;
    176175        pwd->pw_dir = (char *) bf + 5;
    177176        pwd->pw_shell = (char *) bf + 7;
  • uspace/lib/posix/pwd.h

    r1814ee4d rd85a01c  
    3535#ifndef POSIX_PWD_H_
    3636#define POSIX_PWD_H_
     37
     38// TODO: documentation
    3739
    3840#include "sys/types.h"
  • uspace/lib/posix/signal.c

    r1814ee4d rd85a01c  
    6666
    6767/**
    68  * Default signal handler. Executes the default action for each signal,
    69  * as reasonable within HelenOS.
    70  *
    71  * @param signo Signal number.
     68 *
     69 * @param signo
    7270 */
    7371void __posix_default_signal_handler(int signo)
     
    7775                abort();
    7876        case SIGQUIT:
    79                 fprintf(stderr, "Quit signal raised. Exiting.\n");
     77                fprintf(stderr, "Quit signal raised. Exiting.");
    8078                exit(EXIT_FAILURE);
    8179        case SIGINT:
    82                 fprintf(stderr, "Interrupt signal caught. Exiting.\n");
     80                fprintf(stderr, "Interrupt signal caught. Exiting.");
    8381                exit(EXIT_FAILURE);
    8482        case SIGTERM:
    85                 fprintf(stderr, "Termination signal caught. Exiting.\n");
     83                fprintf(stderr, "Termination signal caught. Exiting.");
    8684                exit(EXIT_FAILURE);
    8785        case SIGSTOP:
    88                 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n");
     86                fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.");
    8987                break;
    9088        case SIGKILL:
  • uspace/lib/posix/stdio.c

    r1814ee4d rd85a01c  
    5050#include "libc/str.h"
    5151#include "libc/malloc.h"
    52 #include "libc/adt/list.h"
    53 #include "libc/sys/stat.h"
    5452
    5553
     
    254252        assert(mode != NULL);
    255253        assert(stream != NULL);
     254
     255        if (filename == NULL) {
     256                // TODO
     257               
     258                /* print error to stderr as well, to avoid hard to find problems
     259                 * with buggy apps that expect this to work
     260                 */
     261                fprintf(stderr,
     262                    "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
     263                    "       libposix does not support that yet, the application may function improperly.\n");
     264                errno = ENOTSUP;
     265                return NULL;
     266        }
     267
     268        FILE* copy = malloc(sizeof(FILE));
     269        if (copy == NULL) {
     270                errno = ENOMEM;
     271                return NULL;
     272        }
     273        memcpy(copy, stream, sizeof(FILE));
     274        fclose(copy); /* copy is now freed */
    256275       
    257         /* Retieve the node. */
    258         struct stat st;
    259         int rc;
     276        copy = fopen(filename, mode); /* open new stream */
     277        if (copy == NULL) {
     278                /* fopen() sets errno */
     279                return NULL;
     280        }
    260281       
    261         if (filename == NULL) {
    262                 rc = fstat(stream->fd, &st);
    263         } else {
    264                 rc = stat(filename, &st);
    265         }
     282        /* move the new stream to the original location */
     283        memcpy(stream, copy, sizeof (FILE));
     284        free(copy);
    266285       
    267         if (rc != EOK) {
    268                 fclose(stream);
    269                 errno = -rc;
    270                 return NULL;
    271         }
    272        
    273         fdi_node_t node = {
    274                 .fs_handle = st.fs_handle,
    275                 .devmap_handle = st.devmap_handle,
    276                 .index = st.index
    277         };
    278        
    279         /* Open a new stream. */
    280         FILE* new = fopen_node(&node, mode);
    281         if (new == NULL) {
    282                 fclose(stream);
    283                 /* fopen_node() sets errno. */
    284                 return NULL;
    285         }
    286        
    287         /* Close the original stream without freeing it (ignoring errors). */
    288         if (stream->buf != NULL) {
    289                 fflush(stream);
    290         }
    291         if (stream->sess != NULL) {
    292                 async_hangup(stream->sess);
    293         }
    294         if (stream->fd >= 0) {
    295                 close(stream->fd);
    296         }
    297         list_remove(&stream->link);
    298        
    299         /* Move the new stream to the original location. */
    300         memcpy(stream, new, sizeof (FILE));
    301         free(new);
    302        
    303         /* Update references in the file list. */
     286        /* update references in the file list */
    304287        stream->link.next->prev = &stream->link;
    305288        stream->link.prev->next = &stream->link;
     
    693676
    694677/**
    695  * Remove a file or directory.
     678 * Remove a file.
    696679 *
    697680 * @param path Pathname of the file that shall be removed.
    698  * @return Zero on success, -1 (with errno set) otherwise.
     681 * @return Zero on success, -1 otherwise.
    699682 */
    700683int posix_remove(const char *path)
    701684{
    702         struct stat st;
    703         int rc = stat(path, &st);
    704        
    705         if (rc != EOK) {
    706                 errno = -rc;
    707                 return -1;
    708         }
    709        
    710         if (st.is_directory) {
    711                 rc = rmdir(path);
    712         } else {
    713                 rc = unlink(path);
    714         }
    715        
    716         if (rc != EOK) {
    717                 errno = -rc;
    718                 return -1;
    719         }
    720         return 0;
    721 }
    722 
    723 /**
    724  * Rename a file or directory.
    725  *
    726  * @param old
    727  * @param new
    728  * @return Zero on success, -1 (with errno set) otherwise.
    729  */
    730 int posix_rename(const char *old, const char *new)
    731 {
    732         return errnify(rename, old, new);
     685        // FIXME: unlink() and rmdir() seem to be equivalent at the moment,
     686        //        but that does not have to be true forever
     687        return unlink(path);
    733688}
    734689
  • uspace/lib/posix/stdio.h

    r1814ee4d rd85a01c  
    116116extern int posix_remove(const char *path);
    117117
    118 /* Renaming Files */
    119 extern int posix_rename(const char *old, const char *new);
    120 
    121118/* Temporary Files */
    122119#undef L_tmpnam
     
    173170        #define remove posix_remove
    174171
    175         #define rename posix_rename
    176 
    177172        #define tmpnam posix_tmpnam
    178173#endif
  • uspace/lib/posix/sys/wait.c

    r1814ee4d rd85a01c  
    3939#include "wait.h"
    4040
    41 #include "../libc/task.h"
    42 #include "../assert.h"
    43 #include "../errno.h"
    44 #include "../limits.h"
    45 #include "../signal.h"
    46 
    47 int __posix_wifexited(int status) {
    48         return status != INT_MIN;
    49 }
    50 
    51 int __posix_wexitstatus(int status) {
    52         assert(__posix_wifexited(status));
    53         return status;
    54 }
    55 
    56 int __posix_wifsignaled(int status) {
    57         return status == INT_MIN;
    58 }
    59 
    60 int __posix_wtermsig(int status) {
    61         assert(__posix_wifsignaled(status));
    62         /* There is no way to distinguish reason
    63          * for unexpected termination at the moment.
    64          */
    65         return SIGABRT;
    66 }
    67 
    6841/**
    6942 *
     
    7346posix_pid_t posix_wait(int *stat_ptr)
    7447{
    75         /* HelenOS does not support this. */
    76         errno = ENOSYS;
    77         return (posix_pid_t) -1;
     48        // TODO: low priority, just a compile-time dependency of binutils
     49        not_implemented();
    7850}
    7951
     
    8759posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options)
    8860{
    89         assert(stat_ptr != NULL);
    90         assert(options == 0 /* None of the options are supported. */);
    91        
    92         task_exit_t texit;
    93         int retval;
    94        
    95         int rc = task_wait((task_id_t) pid, &texit, &retval);
    96        
    97         if (rc < 0) {
    98                 /* Unable to retrieve status. */
    99                 errno = -rc;
    100                 return (posix_pid_t) -1;
    101         }
    102        
    103         if (texit == TASK_EXIT_NORMAL) {
    104                 // FIXME: relies on application not returning this value
    105                 assert(retval != INT_MIN);
    106                 *stat_ptr = retval;
    107         } else {
    108                 /* Reserve the lowest value for unexpected termination. */
    109                 *stat_ptr = INT_MIN;
    110         }
    111        
    112         return pid;
     61        // TODO: low priority, just a compile-time dependency of binutils
     62        not_implemented();
    11363}
    11464
  • uspace/lib/posix/sys/wait.h

    r1814ee4d rd85a01c  
    3838#include "types.h"
    3939
    40 #undef WIFEXITED
    41 #undef WEXITSTATUS
    42 #undef WIFSIGNALED
    43 #undef WTERMSIG
    44 #define WIFEXITED(status) __posix_wifexited(status)
    45 #define WEXITSTATUS(status) __posix_wexitstatus(status)
    46 #define WIFSIGNALED(status) __posix_wifsignaled(status)
    47 #define WTERMSIG(status) __posix_wtermsig(status)
    48 
    49 extern int __posix_wifexited(int status);
    50 extern int __posix_wexitstatus(int status);
    51 extern int __posix_wifsignaled(int status);
    52 extern int __posix_wtermsig(int status);
    53 
    5440extern posix_pid_t posix_wait(int *stat_ptr);
    5541extern posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options);
     
    5743#ifndef LIBPOSIX_INTERNAL
    5844        #define wait posix_wait
    59         #define waitpid posix_waitpid
     45        #define waitpid posix_waitpid
    6046#endif
    6147
  • uspace/lib/posix/unistd.c

    r1814ee4d rd85a01c  
    110110                return NULL;
    111111        }
    112        
    113         /* Save the original value to comply with the "no modification on
    114          * success" semantics.
    115          */
    116         int orig_errno = errno;
    117         errno = EOK;
    118        
    119112        char *ret = getcwd(buf, size);
    120         if (ret == NULL) {
    121                 /* Check errno to avoid shadowing other possible errors. */
    122                 if (errno == EOK) {
    123                         errno = ERANGE;
    124                 }
    125         } else {
    126                 /* Success, restore previous errno value. */
    127                 errno = orig_errno;
    128         }
    129        
     113        if (ret == NULL && errno == EOK) {
     114                errno = ERANGE;
     115        }
    130116        return ret;
    131 }
    132 
    133 /**
    134  * Change the current working directory.
    135  *
    136  * @param path New working directory.
    137  */
    138 int posix_chdir(const char *path)
    139 {
    140         return errnify(chdir, path);
    141117}
    142118
     
    181157        /* There is currently no support for user accounts in HelenOS. */
    182158        return 0;
    183 }
    184 
    185 /**
    186  * Close a file.
    187  *
    188  * @param fildes
    189  * @return 0 on success, -1 on error.
    190  */
    191 int posix_close(int fildes)
    192 {
    193         return errnify(close, fildes);
    194159}
    195160
     
    204169ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    205170{
    206         return errnify(read, fildes, buf, nbyte);
    207 }
    208 
    209 /**
    210  * Write to a file.
    211  *
    212  * @param fildes File descriptor of the opened file.
    213  * @param buf Buffer to write.
    214  * @param nbyte Size of the buffer.
    215  * @return Number of written bytes on success, -1 otherwise.
    216  */
    217 ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
    218 {
    219         return errnify(write, fildes, buf, nbyte);
    220 }
    221 
    222 /**
    223  * Requests outstanding data to be written to the underlying storage device.
    224  *
    225  * @param fildes
    226  */
    227 int posix_fsync(int fildes)
    228 {
    229         return errnify(fsync, fildes);
    230 }
    231 
    232 int posix_ftruncate(int fildes, posix_off_t length)
    233 {
    234         return errnify(ftruncate, fildes, (aoff64_t) length);
    235 }
    236 
    237 /**
    238  * Remove a directory.
    239  *
    240  * @param path Directory pathname.
    241  * @return Zero on success, -1 otherwise.
    242  */
    243 int posix_rmdir(const char *path)
    244 {
    245         return errnify(rmdir, path);
     171        int rc = read(fildes, buf, nbyte);
     172        if (rc < 0) {
     173                errno = -rc;
     174                return -1;
     175        } else {
     176                return rc;
     177        }
    246178}
    247179
     
    254186int posix_unlink(const char *path)
    255187{
    256         return errnify(unlink, path);
    257 }
    258 
    259 int posix_dup(int fildes)
    260 {
    261         return posix_fcntl(fildes, F_DUPFD, 0);
    262 }
    263 
    264 int posix_dup2(int fildes, int fildes2)
    265 {
    266         return errnify(dup2, fildes, fildes2);
     188        int rc = unlink(path);
     189        if (rc < 0) {
     190                errno = -rc;
     191                return -1;
     192        } else {
     193                return rc;
     194        }
    267195}
    268196
     
    276204int posix_access(const char *path, int amode)
    277205{
    278         if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
    279                 /* HelenOS doesn't support permissions, permission checks
    280                  * are equal to existence check.
    281                  *
    282                  * Check file existence by attempting to open it.
    283                  */
     206        if (amode == F_OK) {
     207                /* Check file existence by attempt to open it. */
    284208                int fd = open(path, O_RDONLY);
    285                 if (fd < 0) {
    286                         errno = -fd;
    287                         return -1;
     209                if (fd != -1) {
     210                        close(fd);
    288211                }
    289                 close(fd);
     212                return fd;
     213        } else if (amode & (X_OK | W_OK | R_OK)) {
     214                /* HelenOS doesn't support permissions, return success. */
    290215                return 0;
    291216        } else {
  • uspace/lib/posix/unistd.h

    r1814ee4d rd85a01c  
    6060/* Working Directory */
    6161extern char *posix_getcwd(char *buf, size_t size);
    62 extern int posix_chdir(const char *path);
    6362
    6463/* Query Memory Parameters */
     
    7069extern posix_gid_t posix_getgid(void);
    7170
    72 /* File Manipulation */
    73 extern int posix_close(int fildes);
     71/* File Input/Output */
     72extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
    7473
    75 extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
    76 extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte);
    77 
    78 extern int posix_fsync(int fildes);
    79 extern int posix_ftruncate(int fildes, posix_off_t length);
    80 
    81 extern int posix_rmdir(const char *path);
     74/* Deleting Files */
    8275extern int posix_unlink(const char *path);
    83 
    84 extern int posix_dup(int fildes);
    85 extern int posix_dup2(int fildes, int fildes2);
    8676
    8777/* Standard Streams */
     
    154144
    155145        #define getcwd posix_getcwd
    156         #define chdir posix_chdir
    157146
    158147        #define isatty posix_isatty
     
    165154        #define getgid posix_getgid
    166155
    167         #define close posix_close
    168156        #define read posix_read
    169         #define write posix_write
    170         #define fsync posix_fsync
    171         #define ftruncate posix_ftruncate
    172         #define rmdir posix_rmdir
     157
    173158        #define unlink posix_unlink
    174         #define dup posix_dup
    175         #define dup2 posix_dup2
    176159
    177160        #define access posix_access
Note: See TracChangeset for help on using the changeset viewer.