stdio_file.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Ring-3 FILE layer.
  2. *
  3. * Files are read-only and whole-file: fopen() stats the path, allocates one
  4. * buffer and reads it through SYS_READ_FILE. stdout/stderr are console
  5. * streams. This is enough for the command-line tools that read whole files.
  6. *
  7. * TODO(fd-layer): replace whole-file FILE with SYS_OPEN/READ/WRITE/LSEEK/
  8. * CLOSE and offset-capable fs_ops when a real fd layer lands.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <limits.h>
  15. #include <ucmd_lib.h>
  16. #define FILE_MAX (4u * 1024u * 1024u)
  17. struct __libc_file {
  18. int kind; /* 0 = empty/stdin, 1 = console, 2 = whole-file */
  19. int owned;
  20. int eof;
  21. int error;
  22. unsigned char *data;
  23. unsigned int size;
  24. unsigned int pos;
  25. };
  26. static struct __libc_file console_out = { 1, 0, 0, 0, 0, 0, 0 };
  27. static struct __libc_file console_err = { 1, 0, 0, 0, 0, 0, 0 };
  28. static struct __libc_file stdin_file = { 0, 0, 0, 0, 0, 0, 0 };
  29. FILE *stdin = &stdin_file;
  30. FILE *stdout = &console_out;
  31. FILE *stderr = &console_err;
  32. static int map_sys_error(int r)
  33. {
  34. switch (r) {
  35. case SYS_ENOENT: return ENOENT;
  36. case SYS_ENODEV: return ENODEV;
  37. case SYS_ENOFS: return ENODEV;
  38. case SYS_EINVAL: return EINVAL;
  39. case SYS_EROFS: return EROFS;
  40. case SYS_ENOSPC: return ENOSPC;
  41. case SYS_EWRITE: return EIO;
  42. case SYS_EIO: return EIO;
  43. default: return EIO;
  44. }
  45. }
  46. FILE *fopen(const char *path, const char *mode)
  47. {
  48. struct sys_stat st;
  49. struct __libc_file *f;
  50. int r;
  51. if (!path || !mode) {
  52. errno = EINVAL;
  53. return 0;
  54. }
  55. if (!(mode[0] == 'r' && (mode[1] == '\0' || mode[1] == 'b'))) {
  56. /* TODO(fd-layer): writable FILE objects need the kernel fd layer. */
  57. errno = EROFS;
  58. return 0;
  59. }
  60. r = u_syscall2(SYS_FS_STAT, (int)path, (int)&st);
  61. if (r != 0) {
  62. errno = map_sys_error(r);
  63. return 0;
  64. }
  65. if (st.is_dir) {
  66. errno = EISDIR;
  67. return 0;
  68. }
  69. if (st.size > FILE_MAX) {
  70. errno = EFBIG;
  71. return 0;
  72. }
  73. f = (struct __libc_file *)malloc(sizeof(*f));
  74. if (!f) {
  75. errno = ENOMEM;
  76. return 0;
  77. }
  78. memset(f, 0, sizeof(*f));
  79. f->kind = 2;
  80. f->owned = 1;
  81. f->data = (unsigned char *)malloc(st.size ? st.size : 1);
  82. if (!f->data) {
  83. free(f);
  84. errno = ENOMEM;
  85. return 0;
  86. }
  87. if (st.size) {
  88. r = u_syscall3(SYS_READ_FILE, (int)path, (int)f->data,
  89. (int)st.size);
  90. if (r < 0) {
  91. free(f->data);
  92. free(f);
  93. errno = map_sys_error(r);
  94. return 0;
  95. }
  96. f->size = (unsigned int)r;
  97. }
  98. return f;
  99. }
  100. int fclose(FILE *stream)
  101. {
  102. struct __libc_file *f = (struct __libc_file *)stream;
  103. if (!f)
  104. return EOF;
  105. if (f->owned) {
  106. free(f->data);
  107. free(f);
  108. }
  109. return 0;
  110. }
  111. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  112. {
  113. struct __libc_file *f = (struct __libc_file *)stream;
  114. size_t total;
  115. size_t avail;
  116. size_t got;
  117. if (!f || !ptr || size == 0 || nmemb == 0)
  118. return 0;
  119. if (size && nmemb > (size_t)-1 / size) {
  120. errno = EOVERFLOW;
  121. return 0;
  122. }
  123. if (f->kind == 0) {
  124. f->eof = 1;
  125. return 0;
  126. }
  127. if (f->kind != 2) {
  128. errno = EBADF;
  129. return 0;
  130. }
  131. total = size * nmemb;
  132. avail = (size_t)f->size - f->pos;
  133. if (total > avail)
  134. total = avail;
  135. got = total / size;
  136. if (got) {
  137. memcpy(ptr, f->data + f->pos, got * size);
  138. f->pos += (unsigned int)(got * size);
  139. }
  140. if (got < nmemb)
  141. f->eof = 1;
  142. return got;
  143. }
  144. static int console_write(FILE *stream, const void *ptr, size_t len)
  145. {
  146. const unsigned char *p = (const unsigned char *)ptr;
  147. size_t i;
  148. (void)stream;
  149. for (i = 0; i < len; i++)
  150. u_putc((char)p[i]);
  151. return 0;
  152. }
  153. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  154. {
  155. struct __libc_file *f = (struct __libc_file *)stream;
  156. if (!f || !ptr || size == 0 || nmemb == 0)
  157. return 0;
  158. if (size && nmemb > (size_t)-1 / size) {
  159. errno = EOVERFLOW;
  160. return 0;
  161. }
  162. if (f->kind != 1) {
  163. errno = EBADF;
  164. return 0;
  165. }
  166. console_write(stream, ptr, size * nmemb);
  167. return nmemb;
  168. }
  169. int fputc(int c, FILE *stream)
  170. {
  171. unsigned char ch = (unsigned char)c;
  172. if (fwrite(&ch, 1, 1, stream) != 1)
  173. return EOF;
  174. return ch;
  175. }
  176. int fgetc(FILE *stream)
  177. {
  178. struct __libc_file *f = (struct __libc_file *)stream;
  179. int c;
  180. if (!f || f->kind == 0) {
  181. if (f)
  182. f->eof = 1;
  183. return EOF;
  184. }
  185. if (f->kind != 2) {
  186. errno = EBADF;
  187. return EOF;
  188. }
  189. if (f->pos >= f->size) {
  190. f->eof = 1;
  191. return EOF;
  192. }
  193. c = f->data[f->pos++];
  194. return c;
  195. }
  196. int getchar(void)
  197. {
  198. return fgetc(stdin);
  199. }
  200. char *fgets(char *s, int size, FILE *stream)
  201. {
  202. int n = 0;
  203. if (!s || size <= 0)
  204. return 0;
  205. while (n + 1 < size) {
  206. int c = fgetc(stream);
  207. if (c == EOF) {
  208. if (n == 0)
  209. return 0;
  210. break;
  211. }
  212. s[n++] = (char)c;
  213. if (c == '\n')
  214. break;
  215. }
  216. s[n] = '\0';
  217. return s;
  218. }
  219. int fputs(const char *s, FILE *stream)
  220. {
  221. size_t n;
  222. if (!s)
  223. return EOF;
  224. n = strlen(s);
  225. if (fwrite(s, 1, n, stream) != n)
  226. return EOF;
  227. return 0;
  228. }
  229. int fseek(FILE *stream, long offset, int whence)
  230. {
  231. struct __libc_file *f = (struct __libc_file *)stream;
  232. long base;
  233. long target;
  234. if (!f || f->kind != 2) {
  235. errno = ESPIPE;
  236. return -1;
  237. }
  238. if (whence == SEEK_SET)
  239. base = 0;
  240. else if (whence == SEEK_CUR)
  241. base = (long)f->pos;
  242. else if (whence == SEEK_END)
  243. base = (long)f->size;
  244. else {
  245. errno = EINVAL;
  246. return -1;
  247. }
  248. if (offset > LONG_MAX - base || offset < LONG_MIN + base) {
  249. errno = EOVERFLOW;
  250. return -1;
  251. }
  252. target = base + offset;
  253. if (target < 0 || (unsigned long)target > f->size) {
  254. errno = EINVAL;
  255. return -1;
  256. }
  257. f->pos = (unsigned int)target;
  258. f->eof = 0;
  259. return 0;
  260. }
  261. long ftell(FILE *stream)
  262. {
  263. struct __libc_file *f = (struct __libc_file *)stream;
  264. if (!f || f->kind != 2) {
  265. errno = ESPIPE;
  266. return -1;
  267. }
  268. return (long)f->pos;
  269. }
  270. void rewind(FILE *stream)
  271. {
  272. (void)fseek(stream, 0, SEEK_SET);
  273. clearerr(stream);
  274. }
  275. int fflush(FILE *stream)
  276. {
  277. (void)stream;
  278. return 0;
  279. }
  280. int feof(FILE *stream)
  281. {
  282. struct __libc_file *f = (struct __libc_file *)stream;
  283. return f ? f->eof : 0;
  284. }
  285. int ferror(FILE *stream)
  286. {
  287. struct __libc_file *f = (struct __libc_file *)stream;
  288. return f ? f->error : 0;
  289. }
  290. void clearerr(FILE *stream)
  291. {
  292. struct __libc_file *f = (struct __libc_file *)stream;
  293. if (f) {
  294. f->eof = 0;
  295. f->error = 0;
  296. }
  297. }
  298. int fileno(FILE *stream)
  299. {
  300. (void)stream;
  301. errno = EBADF;
  302. return -1;
  303. }