stdio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stddef.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. /* Required constants */
  24. /* For file I/O*/
  25. #define EOF 0xFFFFFFFF
  26. #define BUFSIZ 0x1000
  27. /* For lseek */
  28. #define SEEK_SET 0
  29. #define SEEK_CUR 1
  30. #define SEEK_END 2
  31. /* Required variables */
  32. FILE* stdin;
  33. FILE* stdout;
  34. FILE* stderr;
  35. FILE* __list;
  36. void __init_io()
  37. {
  38. __list = NULL;
  39. stdin = calloc(1, sizeof(FILE));
  40. stdin->fd = STDIN_FILENO;
  41. stdin->bufmode = O_RDONLY;
  42. stdin->buflen = 1;
  43. stdin->buffer = calloc(2, sizeof(char));
  44. stdout = calloc(1, sizeof(FILE));
  45. stdout->fd = STDOUT_FILENO;
  46. stdout->bufmode = O_WRONLY;
  47. stdout->buflen = 512;
  48. stdout->buffer = calloc(514, sizeof(char));
  49. stderr = calloc(1, sizeof(FILE));
  50. stderr->fd = STDERR_FILENO;
  51. stderr->bufmode = O_WRONLY;
  52. stderr->buflen = 512;
  53. stderr->buffer = calloc(514, sizeof(char));
  54. }
  55. /* Flush all IO on exit */
  56. int fflush(FILE* stream);
  57. void __kill_io()
  58. {
  59. fflush(stdout);
  60. fflush(stderr);
  61. while(NULL != __list)
  62. {
  63. fflush(__list);
  64. __list = __list->next;
  65. }
  66. }
  67. /* Standard C functions */
  68. /* Getting */
  69. int read(int fd, char* buf, unsigned count);
  70. int fgetc(FILE* f)
  71. {
  72. /* Only read on read buffers */
  73. if(O_WRONLY == f->bufmode) return EOF;
  74. /* Deal with stdin */
  75. if(STDIN_FILENO == f->fd)
  76. {
  77. f->bufpos = 0;
  78. int r = read(f->fd, f->buffer, 1);
  79. /* Catch special case of STDIN gets nothing (AN EOF) */
  80. if(0 == r) return EOF;
  81. }
  82. /* Catch EOF */
  83. if(f->buflen <= f->bufpos) return EOF;
  84. /* Deal with standard case */
  85. int ret = f->buffer[f->bufpos];
  86. f->bufpos = f->bufpos + 1;
  87. /* Ensure 0xFF doesn't return EOF */
  88. return (ret & 0xFF);
  89. }
  90. size_t fread( void* buffer, size_t size, size_t count, FILE* stream )
  91. {
  92. if(0 == size) return 0;
  93. if(0 == count) return 0;
  94. long n = size + count - 1;
  95. char* p = buffer;
  96. long i;
  97. unsigned c;
  98. for(i = 0; i < n; i = i + 1)
  99. {
  100. c = fgetc(stream);
  101. if(EOF == c) return (i/size);
  102. p[i] = c;
  103. }
  104. return (i/size);
  105. }
  106. int getchar()
  107. {
  108. return fgetc(stdin);
  109. }
  110. char* fgets(char* str, int count, FILE* stream)
  111. {
  112. int i = 0;
  113. int ch;
  114. while(i < count)
  115. {
  116. ch = fgetc(stream);
  117. if(EOF == ch) {
  118. /* Return null if EOF is first char read */
  119. if (i == 0) return NULL;
  120. break;
  121. }
  122. str[i] = ch;
  123. i = i + 1;
  124. if('\n' == ch) break;
  125. }
  126. return str;
  127. }
  128. /* Putting */
  129. void fputc(char s, FILE* f)
  130. {
  131. /* Only write on write buffers */
  132. if(O_RDONLY == f->bufmode) return;
  133. /* Add to buffer */
  134. f->buffer[f->bufpos] = s;
  135. f->bufpos = f->bufpos + 1;
  136. /* Flush if full or '\n' */
  137. if(f->bufpos == f->buflen) fflush(f);
  138. else if(('\n' == s) && (2 >= f->fd)) fflush(f);
  139. }
  140. size_t fwrite(void const* buffer, size_t size, size_t count, FILE* stream )
  141. {
  142. long n = size * count;
  143. if(0 == n) return 0;
  144. char* p = buffer;
  145. int c;
  146. long i;
  147. for(i=0; i < n; i = i + 1)
  148. {
  149. c = p[i];
  150. fputc(c, stream);
  151. }
  152. return (i/size);
  153. }
  154. void putchar(char s)
  155. {
  156. fputc(s, stdout);
  157. }
  158. int fputs(char const* str, FILE* stream)
  159. {
  160. while(0 != str[0])
  161. {
  162. fputc(str[0], stream);
  163. str = str + 1;
  164. }
  165. return 0;
  166. }
  167. int puts(char const* str)
  168. {
  169. fputs(str, stdout);
  170. fputc('\n', stdout);
  171. return 0;
  172. }
  173. int lseek(int fd, int offset, int whence);
  174. /* File management */
  175. FILE* fopen(char const* filename, char const* mode)
  176. {
  177. int f;
  178. FILE* fi = calloc(1, sizeof(FILE));
  179. fi->next = __list;
  180. if(NULL != __list) __list->prev = fi;
  181. __list = fi;
  182. int size;
  183. if('w' == mode[0]) f = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 00600);
  184. else f = open(filename, 0, 0); /* Everything else is a read */
  185. /* Negative numbers are error codes */
  186. if(0 > f)
  187. {
  188. return 0;
  189. }
  190. if('w' == mode[0])
  191. {
  192. /* Buffer as much as possible */
  193. fi->buffer = malloc(BUFSIZ * sizeof(char));
  194. fi->buflen = BUFSIZ;
  195. fi->bufmode = O_WRONLY;
  196. }
  197. else
  198. {
  199. /* Get enough buffer to read it all */
  200. size = lseek(f, 0, SEEK_END);
  201. fi->buffer = malloc((size + 1) * sizeof(char));
  202. fi->buflen = size;
  203. fi->bufmode = O_RDONLY;
  204. /* Now read it all */
  205. lseek(f, 0, SEEK_SET);
  206. read(f, fi->buffer, size);
  207. }
  208. fi->fd = f;
  209. return fi;
  210. }
  211. FILE* fdopen(int fd, char* mode)
  212. {
  213. FILE* fi = calloc(1, sizeof(FILE));
  214. fi->next = __list;
  215. if(NULL != __list) __list->prev = fi;
  216. __list = fi;
  217. int size;
  218. if('w' == mode[0])
  219. {
  220. /* Buffer as much as possible */
  221. fi->buffer = malloc(BUFSIZ * sizeof(char));
  222. fi->buflen = BUFSIZ;
  223. fi->bufmode = O_WRONLY;
  224. }
  225. else
  226. {
  227. /* Get enough buffer to read it all */
  228. size = lseek(fd, 0, SEEK_END);
  229. fi->buffer = malloc((size + 1) * sizeof(char));
  230. fi->buflen = size;
  231. fi->bufmode = O_RDONLY;
  232. /* Now read it all */
  233. lseek(fd, 0, SEEK_SET);
  234. read(fd, fi->buffer, size);
  235. }
  236. fi->fd = fd;
  237. return fi;
  238. }
  239. int write(int fd, char* buf, unsigned count);
  240. int fflush(FILE* stream)
  241. {
  242. /* We only need to flush on writes */
  243. if(O_RDONLY == stream->bufmode) return 0;
  244. /* If nothing to flush */
  245. if(0 ==stream->bufpos) return 0;
  246. /* The actual flushing */
  247. int error = write(stream->fd, stream->buffer, stream->bufpos);
  248. /* Keep track of position */
  249. stream->file_pos = stream->file_pos + stream->bufpos;
  250. stream->bufpos = 0;
  251. return error;
  252. }
  253. int close(int fd);
  254. int fclose(FILE* stream)
  255. {
  256. /* Deal with STDIN, STDOUT and STDERR */
  257. /* No close for you */
  258. if(2 >= stream->fd) return 0;
  259. /* We only need to flush on writes */
  260. if(O_WRONLY == stream->bufmode)
  261. {
  262. fflush(stream);
  263. }
  264. /* Need to keep the File Descriptor for a moment */
  265. int fd = stream->fd;
  266. /* Remove from __list */
  267. if(NULL != stream->prev) stream->prev->next = stream->next;
  268. if(NULL != stream->next) stream->next->prev = stream->prev;
  269. /* Deal with special case of first node in __list */
  270. if (__list == stream) __list = __list->next;
  271. /* Free up the buffer and struct used for FILE */
  272. free(stream->buffer);
  273. free(stream);
  274. /* Do the actual closing */
  275. return close(fd);
  276. }
  277. int unlink(char* filename);
  278. /* File Removal */
  279. int remove(char *pathname)
  280. {
  281. return unlink(pathname);
  282. }
  283. /* File Positioning */
  284. int ungetc(int ch, FILE* stream)
  285. {
  286. /* Deal with STDIN, STDOUT and STDERR */
  287. /* No ungetc for you */
  288. if(2 >= stream->fd) return EOF;
  289. /* You can't unget on a write stream! */
  290. if(O_WRONLY == stream->bufmode) return EOF;
  291. /* Don't underflow */
  292. if(0 == stream->bufpos) return EOF;
  293. /* Don't let crap be shoved into read stream */
  294. if(stream->buffer[stream->bufpos - 1] != ch) return EOF;
  295. stream->bufpos = stream->bufpos - 1;
  296. return ch;
  297. }
  298. long ftell(FILE* stream)
  299. {
  300. /* Deal with STDIN, STDOUT and STDERR */
  301. /* No ftell for you */
  302. if(2 >= stream->fd) return 0;
  303. /* Deal with buffered output */
  304. if(O_WRONLY == stream->bufmode) return stream->file_pos + stream->bufpos;
  305. /* Deal with read */
  306. return stream->bufpos;
  307. }
  308. int fseek(FILE* f, long offset, int whence)
  309. {
  310. /* Deal with STDIN, STDOUT and STDERR */
  311. /* No seek and destroy missions */
  312. if(2 >= f->fd) return 0;
  313. /* Deal with ugly case */
  314. if(O_WRONLY == f->bufmode)
  315. {
  316. fflush(f);
  317. return lseek(f->fd, offset, whence);
  318. }
  319. /* Deal with read mode */
  320. int pos;
  321. if(SEEK_SET == whence)
  322. {
  323. pos = offset;
  324. }
  325. else if(SEEK_CUR == whence)
  326. {
  327. pos = f->bufpos + offset;
  328. }
  329. else if(SEEK_END == whence)
  330. {
  331. pos = f->buflen + offset;
  332. }
  333. else return -1;
  334. if(pos < 0) return -1;
  335. if(pos > f->buflen) return -1;
  336. f->bufpos = pos;
  337. return pos;
  338. }
  339. void rewind(FILE* f)
  340. {
  341. fseek(f, 0, SEEK_SET);
  342. }