Ignore:
File:
1 edited

Legend:

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

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