Changes in / [d85a01c:1814ee4d] in mainline


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

Legend:

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

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

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

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

    rd85a01c r1814ee4d  
    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
    4555#endif /* LIBPOSIX_COMMON_H_ */
    4656
  • uspace/lib/posix/pwd.c

    rd85a01c r1814ee4d  
    3939#include "errno.h"
    4040
    41 // TODO: documentation
    42 
    4341static bool entry_read = false;
    4442
     
    4644static const struct posix_passwd dummy_pwd = {
    4745        .pw_name = (char *) "user",
    48         .pw_uid = 1,
    49         .pw_gid = 1,
     46        .pw_uid = 0,
     47        .pw_gid = 0,
    5048        .pw_dir = (char *) "/",
    5149        .pw_shell = (char *) "/app/bdsh"
     
    115113{
    116114        assert(name != NULL);
     115        assert(pwd != NULL);
     116        assert(buffer != NULL);
     117        assert(result != NULL);
    117118       
    118119        if (posix_strcmp(name, "user") != 0) {
     
    121122        }
    122123       
    123         return posix_getpwuid_r(1, pwd, buffer, bufsize, result);
     124        return posix_getpwuid_r(0, pwd, buffer, bufsize, result);
    124125}
    125126
     
    132133struct posix_passwd *posix_getpwuid(posix_uid_t uid)
    133134{
    134         if (uid != 1) {
     135        if (uid != 0) {
    135136                return NULL;
    136137        }
     
    159160            '/', '\0', 'b', 'd', 's', 'h', '\0' };
    160161       
    161         if (uid != 1) {
     162        if (uid != 0) {
    162163                *result = NULL;
    163164                return 0;
     
    171172
    172173        pwd->pw_name = (char *) bf;
    173         pwd->pw_uid = 1;
    174         pwd->pw_gid = 1;
     174        pwd->pw_uid = 0;
     175        pwd->pw_gid = 0;
    175176        pwd->pw_dir = (char *) bf + 5;
    176177        pwd->pw_shell = (char *) bf + 7;
  • uspace/lib/posix/pwd.h

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

    rd85a01c r1814ee4d  
    6666
    6767/**
    68  *
    69  * @param signo
     68 * Default signal handler. Executes the default action for each signal,
     69 * as reasonable within HelenOS.
     70 *
     71 * @param signo Signal number.
    7072 */
    7173void __posix_default_signal_handler(int signo)
     
    7577                abort();
    7678        case SIGQUIT:
    77                 fprintf(stderr, "Quit signal raised. Exiting.");
     79                fprintf(stderr, "Quit signal raised. Exiting.\n");
    7880                exit(EXIT_FAILURE);
    7981        case SIGINT:
    80                 fprintf(stderr, "Interrupt signal caught. Exiting.");
     82                fprintf(stderr, "Interrupt signal caught. Exiting.\n");
    8183                exit(EXIT_FAILURE);
    8284        case SIGTERM:
    83                 fprintf(stderr, "Termination signal caught. Exiting.");
     85                fprintf(stderr, "Termination signal caught. Exiting.\n");
    8486                exit(EXIT_FAILURE);
    8587        case SIGSTOP:
    86                 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.");
     88                fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n");
    8789                break;
    8890        case SIGKILL:
  • uspace/lib/posix/stdio.c

    rd85a01c r1814ee4d  
    5050#include "libc/str.h"
    5151#include "libc/malloc.h"
     52#include "libc/adt/list.h"
     53#include "libc/sys/stat.h"
    5254
    5355
     
    252254        assert(mode != NULL);
    253255        assert(stream != NULL);
    254 
     256       
     257        /* Retieve the node. */
     258        struct stat st;
     259        int rc;
     260       
    255261        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;
     262                rc = fstat(stream->fd, &st);
     263        } else {
     264                rc = stat(filename, &st);
     265        }
     266       
     267        if (rc != EOK) {
     268                fclose(stream);
     269                errno = -rc;
    265270                return NULL;
    266271        }
    267 
    268         FILE* copy = malloc(sizeof(FILE));
    269         if (copy == NULL) {
    270                 errno = ENOMEM;
     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. */
    271284                return NULL;
    272285        }
    273         memcpy(copy, stream, sizeof(FILE));
    274         fclose(copy); /* copy is now freed */
    275        
    276         copy = fopen(filename, mode); /* open new stream */
    277         if (copy == NULL) {
    278                 /* fopen() sets errno */
    279                 return NULL;
    280         }
    281        
    282         /* move the new stream to the original location */
    283         memcpy(stream, copy, sizeof (FILE));
    284         free(copy);
    285        
    286         /* update references in the file list */
     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. */
    287304        stream->link.next->prev = &stream->link;
    288305        stream->link.prev->next = &stream->link;
     
    676693
    677694/**
    678  * Remove a file.
     695 * Remove a file or directory.
    679696 *
    680697 * @param path Pathname of the file that shall be removed.
    681  * @return Zero on success, -1 otherwise.
     698 * @return Zero on success, -1 (with errno set) otherwise.
    682699 */
    683700int posix_remove(const char *path)
    684701{
    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);
     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 */
     730int posix_rename(const char *old, const char *new)
     731{
     732        return errnify(rename, old, new);
    688733}
    689734
  • uspace/lib/posix/stdio.h

    rd85a01c r1814ee4d  
    116116extern int posix_remove(const char *path);
    117117
     118/* Renaming Files */
     119extern int posix_rename(const char *old, const char *new);
     120
    118121/* Temporary Files */
    119122#undef L_tmpnam
     
    170173        #define remove posix_remove
    171174
     175        #define rename posix_rename
     176
    172177        #define tmpnam posix_tmpnam
    173178#endif
  • uspace/lib/posix/sys/wait.c

    rd85a01c r1814ee4d  
    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
     47int __posix_wifexited(int status) {
     48        return status != INT_MIN;
     49}
     50
     51int __posix_wexitstatus(int status) {
     52        assert(__posix_wifexited(status));
     53        return status;
     54}
     55
     56int __posix_wifsignaled(int status) {
     57        return status == INT_MIN;
     58}
     59
     60int __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
    4168/**
    4269 *
     
    4673posix_pid_t posix_wait(int *stat_ptr)
    4774{
    48         // TODO: low priority, just a compile-time dependency of binutils
    49         not_implemented();
     75        /* HelenOS does not support this. */
     76        errno = ENOSYS;
     77        return (posix_pid_t) -1;
    5078}
    5179
     
    5987posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options)
    6088{
    61         // TODO: low priority, just a compile-time dependency of binutils
    62         not_implemented();
     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;
    63113}
    64114
  • uspace/lib/posix/sys/wait.h

    rd85a01c r1814ee4d  
    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
     49extern int __posix_wifexited(int status);
     50extern int __posix_wexitstatus(int status);
     51extern int __posix_wifsignaled(int status);
     52extern int __posix_wtermsig(int status);
     53
    4054extern posix_pid_t posix_wait(int *stat_ptr);
    4155extern posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options);
     
    4357#ifndef LIBPOSIX_INTERNAL
    4458        #define wait posix_wait
    45         #define waitpid posix_waitpid
     59        #define waitpid posix_waitpid
    4660#endif
    4761
  • uspace/lib/posix/unistd.c

    rd85a01c r1814ee4d  
    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       
    112119        char *ret = getcwd(buf, size);
    113         if (ret == NULL && errno == EOK) {
    114                 errno = ERANGE;
    115         }
     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       
    116130        return ret;
     131}
     132
     133/**
     134 * Change the current working directory.
     135 *
     136 * @param path New working directory.
     137 */
     138int posix_chdir(const char *path)
     139{
     140        return errnify(chdir, path);
    117141}
    118142
     
    157181        /* There is currently no support for user accounts in HelenOS. */
    158182        return 0;
     183}
     184
     185/**
     186 * Close a file.
     187 *
     188 * @param fildes
     189 * @return 0 on success, -1 on error.
     190 */
     191int posix_close(int fildes)
     192{
     193        return errnify(close, fildes);
    159194}
    160195
     
    169204ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    170205{
    171         int rc = read(fildes, buf, nbyte);
    172         if (rc < 0) {
    173                 errno = -rc;
    174                 return -1;
    175         } else {
    176                 return rc;
    177         }
     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 */
     217ssize_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 */
     227int posix_fsync(int fildes)
     228{
     229        return errnify(fsync, fildes);
     230}
     231
     232int 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 */
     243int posix_rmdir(const char *path)
     244{
     245        return errnify(rmdir, path);
    178246}
    179247
     
    186254int posix_unlink(const char *path)
    187255{
    188         int rc = unlink(path);
    189         if (rc < 0) {
    190                 errno = -rc;
    191                 return -1;
    192         } else {
    193                 return rc;
    194         }
     256        return errnify(unlink, path);
     257}
     258
     259int posix_dup(int fildes)
     260{
     261        return posix_fcntl(fildes, F_DUPFD, 0);
     262}
     263
     264int posix_dup2(int fildes, int fildes2)
     265{
     266        return errnify(dup2, fildes, fildes2);
    195267}
    196268
     
    204276int posix_access(const char *path, int amode)
    205277{
    206         if (amode == F_OK) {
    207                 /* Check file existence by attempt to open it. */
     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                 */
    208284                int fd = open(path, O_RDONLY);
    209                 if (fd != -1) {
    210                         close(fd);
     285                if (fd < 0) {
     286                        errno = -fd;
     287                        return -1;
    211288                }
    212                 return fd;
    213         } else if (amode & (X_OK | W_OK | R_OK)) {
    214                 /* HelenOS doesn't support permissions, return success. */
     289                close(fd);
    215290                return 0;
    216291        } else {
  • uspace/lib/posix/unistd.h

    rd85a01c r1814ee4d  
    6060/* Working Directory */
    6161extern char *posix_getcwd(char *buf, size_t size);
     62extern int posix_chdir(const char *path);
    6263
    6364/* Query Memory Parameters */
     
    6970extern posix_gid_t posix_getgid(void);
    7071
    71 /* File Input/Output */
     72/* File Manipulation */
     73extern int posix_close(int fildes);
     74
    7275extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
     76extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte);
    7377
    74 /* Deleting Files */
     78extern int posix_fsync(int fildes);
     79extern int posix_ftruncate(int fildes, posix_off_t length);
     80
     81extern int posix_rmdir(const char *path);
    7582extern int posix_unlink(const char *path);
     83
     84extern int posix_dup(int fildes);
     85extern int posix_dup2(int fildes, int fildes2);
    7686
    7787/* Standard Streams */
     
    144154
    145155        #define getcwd posix_getcwd
     156        #define chdir posix_chdir
    146157
    147158        #define isatty posix_isatty
     
    154165        #define getgid posix_getgid
    155166
     167        #define close posix_close
    156168        #define read posix_read
    157 
     169        #define write posix_write
     170        #define fsync posix_fsync
     171        #define ftruncate posix_ftruncate
     172        #define rmdir posix_rmdir
    158173        #define unlink posix_unlink
     174        #define dup posix_dup
     175        #define dup2 posix_dup2
    159176
    160177        #define access posix_access
Note: See TracChangeset for help on using the changeset viewer.