|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +int fgetc(FILE *stream) { |
| 4 | + int fd = fileno(stream); |
| 5 | + unsigned char buf; |
| 6 | + ssize_t read_byte = read(fd, &buf, 1); |
| 7 | + if (read_byte == 1) { |
| 8 | + return buf; |
| 9 | + } else { |
| 10 | + return EOF; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +int getc(FILE *stream) { |
| 15 | + int fd = fileno(stream); |
| 16 | + unsigned char buf; |
| 17 | + ssize_t read_byte = read(fd, &buf, 1); |
| 18 | + if (read_byte == 1) { |
| 19 | + return buf; |
| 20 | + } else { |
| 21 | + return EOF; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +size_t fread(void *buffer, size_t size, size_t count, FILE *stream) { |
| 26 | + int fd = fileno(stream); |
| 27 | + ssize_t read_byte = read(fd, buffer, size * count); |
| 28 | + if (read_byte == -1) { |
| 29 | + return 0; |
| 30 | + } |
| 31 | + return read_byte / size; |
| 32 | +} |
| 33 | + |
| 34 | +char* fgets(char *s, int n, FILE *stream) |
| 35 | +{ |
| 36 | + char *p = s; |
| 37 | + if (s == NULL || n <= 0 || ferror(stream) || feof(stream)) { |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + |
| 41 | + int c = 0; |
| 42 | + while (--n > 0 && (c = getc(stream)) != EOF) { |
| 43 | + if ((*p++ = c) == '\n') { |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + if (ferror(stream) || (c == EOF && p == s)) { |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + *p = '\0'; |
| 51 | + return s; |
| 52 | +} |
| 53 | + |
| 54 | +int getchar(void) { |
| 55 | + return getc(stdin); |
| 56 | +} |
| 57 | + |
| 58 | +char* gets(char *s) |
| 59 | +{ |
| 60 | + char *p = s; |
| 61 | + if (s == NULL || ferror(stdin) || feof(stdin)) { |
| 62 | + return NULL; |
| 63 | + } |
| 64 | + |
| 65 | + int c = 0; |
| 66 | + while ((c = getchar()) != EOF) { |
| 67 | + if (c == '\n') { |
| 68 | + break; |
| 69 | + } |
| 70 | + *p++ = c; |
| 71 | + } |
| 72 | + if (ferror(stdin) || (c == EOF && p == s)) { |
| 73 | + return NULL; |
| 74 | + } |
| 75 | + *p = '\0'; |
| 76 | + return s; |
| 77 | +} |
| 78 | + |
| 79 | +int fputc(int c, FILE *stream) { |
| 80 | + int fd = fileno(stream); |
| 81 | + unsigned char symb = c; |
| 82 | + int write_byte = write(fd, &symb, 1); |
| 83 | + if (write_byte == 1) { |
| 84 | + return c; |
| 85 | + } else { |
| 86 | + return EOF; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +int putc(int c, FILE *stream) { |
| 91 | + int fd = fileno(stream); |
| 92 | + unsigned char symb = c; |
| 93 | + int write_byte = write(fd, &symb, 1); |
| 94 | + if (write_byte == 1) { |
| 95 | + return c; |
| 96 | + } else { |
| 97 | + return EOF; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream) { |
| 102 | + int fd = fileno(stream); |
| 103 | + void *cop_buf = buffer; |
| 104 | + int write_byte = write(fd, cop_buf, size * count); |
| 105 | + if (write == -1) { |
| 106 | + return 0; |
| 107 | + } |
| 108 | + return write_byte / size; |
| 109 | +} |
| 110 | + |
| 111 | +int fputs(const char *str, FILE *stream) { |
| 112 | + if (str == NULL) { |
| 113 | + return EOF; |
| 114 | + } |
| 115 | + |
| 116 | + while (*str != '\0') { |
| 117 | + unsigned char c = *str; |
| 118 | + fputc(c, stream); |
| 119 | + str++; |
| 120 | + } |
| 121 | + |
| 122 | + return 1; |
| 123 | +} |
| 124 | + |
| 125 | +int putchar(int c) { |
| 126 | + return putc(c, stdout); |
| 127 | +} |
| 128 | + |
| 129 | +int puts(const char *str) { |
| 130 | + int write_code = fputs(str, stdout); |
| 131 | + if (write_code == EOF) { |
| 132 | + return EOF; |
| 133 | + } |
| 134 | + write_code = putchar('\n'); |
| 135 | + if (write_code == EOF) { |
| 136 | + return EOF; |
| 137 | + } |
| 138 | + return 1; |
| 139 | +} |
0 commit comments