/trunk/uspace/app/tester/stdio/stdio1.c |
---|
39,6 → 39,7 |
FILE *f; |
char *file_name = "/readme"; |
size_t n; |
int c; |
printf("Open file '%s'\n", file_name); |
errno = 0; |
60,7 → 61,25 |
buf[n] = '\0'; |
printf("Read string '%s'.\n", buf); |
printf("Seek to beginning.\n"); |
if (fseek(f, 0, SEEK_SET) != 0) { |
fclose(f); |
return "Failed seeking."; |
} |
printf("Read using fgetc().\n"); |
while (true) { |
c = fgetc(f); |
if (c == EOF) break; |
printf("'%c'", c); |
} |
printf("[EOF]\n"); |
printf("Closing.\n"); |
if (fclose(f) != 0) |
return "Failed closing."; |
return NULL; |
} |
/trunk/uspace/lib/libc/include/stdio.h |
---|
89,7 → 89,22 |
extern int ferror(FILE *); |
extern void clearerr(FILE *); |
extern int fgetc(FILE *);; |
extern int fputc(int, FILE *); |
extern int fputs(const char *, FILE *); |
#define getc fgetc |
#define putc fputc |
extern int fseek(FILE *, long, int); |
#ifndef SEEK_SET |
#define SEEK_SET 0 |
#define SEEK_CUR 1 |
#define SEEK_END 2 |
#endif |
#endif |
/** @} |
*/ |
/trunk/uspace/lib/libc/include/unistd.h |
---|
44,9 → 44,11 |
#define getpagesize() (PAGE_SIZE) |
#ifndef SEEK_SET |
#define SEEK_SET 0 |
#define SEEK_CUR 1 |
#define SEEK_END 2 |
#endif |
extern ssize_t write(int, const void *, size_t); |
extern ssize_t read(int, void *, size_t); |
/trunk/uspace/lib/libc/generic/io/stdio.c |
---|
215,5 → 215,62 |
f->error = 0; |
} |
/** Read character from a stream. */ |
int fgetc(FILE *f) |
{ |
unsigned char c; |
size_t n; |
n = fread(&c, sizeof(c), 1, f); |
if (n < 1) return EOF; |
return (int) c; |
} |
/** Write character to a stream. */ |
int fputc(int c, FILE *f) |
{ |
unsigned char cc; |
size_t n; |
cc = (unsigned char) c; |
n = fwrite(&cc, sizeof(cc), 1, f); |
if (n < 1) return EOF; |
return (int) cc; |
} |
/** Write string to a stream. */ |
int fputs(const char *s, FILE *f) |
{ |
int rc; |
rc = 0; |
while (*s && rc >= 0) { |
rc = fputc(*s++, f); |
} |
if (rc < 0) return EOF; |
return 0; |
} |
/** Seek to position in stream. */ |
int fseek(FILE *f, long offset, int origin) |
{ |
off_t rc; |
rc = lseek(f->fd, offset, origin); |
if (rc == (off_t) (-1)) { |
/* errno has been set by lseek. */ |
return -1; |
} |
f->eof = 0; |
return 0; |
} |
/** @} |
*/ |