Changeset ff381a7 in mainline for uspace/lib/posix/source/stdio.c


Ignore:
Timestamp:
2015-11-02T20:54:19Z (8 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d8513177
Parents:
3feeab2 (diff), 5265eea4 (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 mainline changes.

File:
1 edited

Legend:

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

    r3feeab2 rff381a7  
    5555#include "libc/sys/stat.h"
    5656
    57 
    58 /* not the best of solutions, but freopen and ungetc will eventually
    59  * need to be implemented in libc anyway
    60  */
    61 #include "../../c/generic/private/stdio.h"
    62 
    6357/** Clears the stream's error and end-of-file indicators.
    6458 *
     
    6761void posix_clearerr(FILE *stream)
    6862{
    69         stream->error = 0;
    70         stream->eof = 0;
     63        clearerr(stream);
    7164}
    7265
     
    10194int posix_fputs(const char *restrict s, FILE *restrict stream)
    10295{
    103         int rc = fputs(s, stream);
    104         if (rc == 0) {
    105                 return EOF;
    106         } else {
    107                 return 0;
    108         }
     96        return fputs(s, stream);
    10997}
    11098
     
    118106int posix_ungetc(int c, FILE *stream)
    119107{
    120         uint8_t b = (uint8_t) c;
    121 
    122         bool can_unget =
    123             /* Provided character is legal. */
    124             c != EOF &&
    125             /* Stream is consistent. */
    126             !stream->error &&
    127             /* Stream is buffered. */
    128             stream->btype != _IONBF &&
    129             /* Last operation on the stream was a read operation. */
    130             stream->buf_state == _bs_read &&
    131             /* Stream buffer is already allocated (i.e. there was already carried
    132              * out either write or read operation on the stream). This is probably
    133              * redundant check but let's be safe. */
    134             stream->buf != NULL &&
    135             /* There is still space in the stream to retreat. POSIX demands the
    136              * possibility to unget at least 1 character. It should be always
    137              * possible, assuming the last operation on the stream read at least 1
    138              * character, because the buffer is refilled in the lazily manner. */
    139             stream->buf_tail > stream->buf;
    140 
    141         if (can_unget) {
    142                 --stream->buf_tail;
    143                 stream->buf_tail[0] = b;
    144                 stream->eof = false;
    145                 return (int) b;
    146         } else {
    147                 return EOF;
    148         }
     108        return ungetc(c, stream);
    149109}
    150110
     
    254214    const char *restrict mode, FILE *restrict stream)
    255215{
    256         assert(mode != NULL);
    257         assert(stream != NULL);
    258        
    259         if (filename == NULL) {
    260                 /* POSIX allows this to be imlementation-defined. HelenOS currently
    261                  * does not support changing the mode. */
    262                 // FIXME: handle mode change once it is supported
    263                 return stream;
    264         }
    265        
    266         /* Open a new stream. */
    267         FILE* new = fopen(filename, mode);
    268         if (new == NULL) {
    269                 fclose(stream);
    270                 /* errno was set by fopen() */
    271                 return NULL;
    272         }
    273        
    274         /* Close the original stream without freeing it (ignoring errors). */
    275         if (stream->buf != NULL) {
    276                 fflush(stream);
    277         }
    278         if (stream->sess != NULL) {
    279                 async_hangup(stream->sess);
    280         }
    281         if (stream->fd >= 0) {
    282                 close(stream->fd);
    283         }
    284         list_remove(&stream->link);
    285        
    286         /* Move the new stream to the original location. */
    287         memcpy(stream, new, sizeof (FILE));
    288         free(new);
    289        
    290         /* Update references in the file list. */
    291         stream->link.next->prev = &stream->link;
    292         stream->link.prev->next = &stream->link;
    293        
    294         return stream;
     216        return freopen(filename, mode, stream);
    295217}
    296218
     
    393315int posix_fflush(FILE *stream)
    394316{
    395         int rc = fflush(stream);
    396         if (rc < 0) {
    397                 errno = -rc;
    398                 return EOF;
    399         } else {
    400                 return 0;
    401         }
     317        return negerrno(fflush, stream);
    402318}
    403319
     
    429345{
    430346        ssize_t wr = write(*(int *) fd, str, size);
     347        if (wr < 0)
     348                return errno;
    431349        return str_nlength(str, wr);
    432350}
     
    657575int posix_remove(const char *path)
    658576{
    659         struct stat st;
    660         int rc = stat(path, &st);
    661        
    662         if (rc != EOK) {
    663                 errno = -rc;
    664                 return -1;
    665         }
    666        
    667         if (st.is_directory) {
    668                 rc = rmdir(path);
    669         } else {
    670                 rc = unlink(path);
    671         }
    672        
    673         if (rc != EOK) {
    674                 errno = -rc;
    675                 return -1;
    676         }
    677         return 0;
     577        return negerrno(remove, path);
    678578}
    679579
     
    687587int posix_rename(const char *old, const char *new)
    688588{
    689         return errnify(rename, old, new);
     589        return negerrno(rename, old, new);
    690590}
    691591
Note: See TracChangeset for help on using the changeset viewer.