Changeset 00c2de63 in mainline for uspace/lib/posix/stdio.c


Ignore:
Timestamp:
2011-07-29T14:50:22Z (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:
b6759f4
Parents:
6c69d19 (diff), 7ae249d (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

    r6c69d19 r00c2de63  
    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
Note: See TracChangeset for help on using the changeset viewer.