9
static void create_file(const char *path, const char *buffer, int mode) {
10
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
13
int err = write(fd, buffer, sizeof(char) * strlen(buffer));
14
assert(err == (sizeof(char) * strlen(buffer)));
20
create_file("file.txt", "cd", 0666);
32
file = fopen("file.txt", "r");
35
// pushing EOF always returns EOF
37
err = ungetc(EOF, file);
40
// ungetc should return itself
41
err = ungetc('a', file);
42
assert(err == (int)'a');
44
// push two chars and make sure they're read back in
45
// the correct order (both by fgetc and fread)
50
assert(err == (int)'a');
51
int r = fread(buffer, sizeof(char), sizeof(buffer), file);
54
assert(!strcmp(buffer, "bcd"));
56
// rewind and fseek should reset anything that's been
57
// pushed to the stream
61
assert(err == (int)'c');
63
fseek(file, 0, SEEK_SET);
65
assert(err == (int)'c');
67
// fgetc, when nothing is left, should return EOF
68
fseek(file, 0, SEEK_END);
74
// ungetc should reset the EOF indicator
86
signal(SIGABRT, cleanup);