Changeset 63088cc1 in mainline


Ignore:
Timestamp:
2008-12-29T13:30:19Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1c1002a
Parents:
04b687b
Message:

fgetc(), fputc(), fputs(), fseek().

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/stdio/stdio1.c

    r04b687b r63088cc1  
    4040        char *file_name = "/readme";
    4141        size_t n;
     42        int c;
    4243
    4344        printf("Open file '%s'\n", file_name);
     
    6162        printf("Read string '%s'.\n", buf);
    6263
    63         fclose(f);
     64        printf("Seek to beginning.\n");
     65        if (fseek(f, 0, SEEK_SET) != 0) {
     66                fclose(f);
     67                return "Failed seeking.";
     68        }
     69
     70        printf("Read using fgetc().\n");
     71        while (true) {
     72                c = fgetc(f);
     73                if (c == EOF) break;
     74
     75                printf("'%c'", c);
     76        }
     77
     78        printf("[EOF]\n");
     79        printf("Closing.\n");
     80
     81        if (fclose(f) != 0)
     82                return "Failed closing.";
    6483
    6584        return NULL;
  • uspace/lib/libc/generic/io/stdio.c

    r04b687b r63088cc1  
    216216}
    217217
     218/** Read character from a stream. */
     219int fgetc(FILE *f)
     220{
     221        unsigned char c;
     222        size_t n;
     223
     224        n = fread(&c, sizeof(c), 1, f);
     225        if (n < 1) return EOF;
     226
     227        return (int) c;
     228}
     229
     230/** Write character to a stream. */
     231int fputc(int c, FILE *f)
     232{
     233        unsigned char cc;
     234        size_t n;
     235
     236        cc = (unsigned char) c;
     237        n = fwrite(&cc, sizeof(cc), 1, f);
     238        if (n < 1) return EOF;
     239
     240        return (int) cc;
     241}
     242
     243/** Write string to a stream. */
     244int fputs(const char *s, FILE *f)
     245{
     246        int rc;
     247
     248        rc = 0;
     249
     250        while (*s && rc >= 0) {
     251                rc = fputc(*s++, f);
     252        }
     253
     254        if (rc < 0) return EOF;
     255
     256        return 0;
     257}
     258
     259/** Seek to position in stream. */
     260int fseek(FILE *f, long offset, int origin)
     261{
     262        off_t rc;
     263
     264        rc = lseek(f->fd, offset, origin);
     265        if (rc == (off_t) (-1)) {
     266                /* errno has been set by lseek. */
     267                return -1;
     268        }
     269
     270        f->eof = 0;
     271
     272        return 0;
     273}
     274
    218275/** @}
    219276 */
  • uspace/lib/libc/include/stdio.h

    r04b687b r63088cc1  
    9090extern void clearerr(FILE *);
    9191
     92extern int fgetc(FILE *);;
     93extern int fputc(int, FILE *);
     94extern int fputs(const char *, FILE *);
     95
     96#define getc fgetc
     97#define putc fputc
     98
     99extern int fseek(FILE *, long, int);
     100
     101#ifndef SEEK_SET
     102        #define SEEK_SET        0
     103        #define SEEK_CUR        1
     104        #define SEEK_END        2
     105#endif
     106
    92107#endif
    93108
  • uspace/lib/libc/include/unistd.h

    r04b687b r63088cc1  
    4545#define getpagesize()     (PAGE_SIZE)
    4646
    47 #define SEEK_SET        0
    48 #define SEEK_CUR        1
    49 #define SEEK_END        2
     47#ifndef SEEK_SET
     48        #define SEEK_SET        0
     49        #define SEEK_CUR        1
     50        #define SEEK_END        2
     51#endif
    5052
    5153extern ssize_t write(int, const void *, size_t);
Note: See TracChangeset for help on using the changeset viewer.