read_printf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* Suppose that you are a shell. You start child processes.
  11. * They work and eventually exit. You want to get user input.
  12. * You read stdin. But what happens if last child switched
  13. * its stdin into O_NONBLOCK mode?
  14. *
  15. * *** SURPRISE! It will affect the parent too! ***
  16. * *** BIG SURPRISE! It stays even after child exits! ***
  17. *
  18. * This is a design bug in UNIX API.
  19. * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
  20. * will set nonblocking mode not only on _your_ stdin, but
  21. * also on stdin of your parent, etc.
  22. *
  23. * In general,
  24. * fd2 = dup(fd1);
  25. * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
  26. * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
  27. * where duping is done implicitly by fork() etc.
  28. *
  29. * We need
  30. * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
  31. * (note SETFD, not SETFL!) but such thing doesn't exist.
  32. *
  33. * Alternatively, we need nonblocking_read(fd, ...) which doesn't
  34. * require O_NONBLOCK dance at all. Actually, it exists:
  35. * n = recv(fd, buf, len, MSG_DONTWAIT);
  36. * "MSG_DONTWAIT:
  37. * Enables non-blocking operation; if the operation
  38. * would block, EAGAIN is returned."
  39. * but recv() works only for sockets!
  40. *
  41. * So far I don't see any good solution, I can only propose
  42. * that affected readers should be careful and use this routine,
  43. * which detects EAGAIN and uses poll() to wait on the fd.
  44. * Thankfully, poll() doesn't care about O_NONBLOCK flag.
  45. */
  46. ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count)
  47. {
  48. struct pollfd pfd[1];
  49. ssize_t n;
  50. while (1) {
  51. n = safe_read(fd, buf, count);
  52. if (n >= 0 || errno != EAGAIN)
  53. return n;
  54. /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
  55. pfd[0].fd = fd;
  56. pfd[0].events = POLLIN;
  57. /* note: safe_poll pulls in printf */
  58. safe_poll(pfd, 1, -1);
  59. }
  60. }
  61. // Reads one line a-la fgets (but doesn't save terminating '\n').
  62. // Reads byte-by-byte. Useful when it is important to not read ahead.
  63. // Bytes are appended to pfx (which must be malloced, or NULL).
  64. char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
  65. {
  66. char *p;
  67. char *buf = NULL;
  68. size_t sz = 0;
  69. size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  70. goto jump_in;
  71. while (sz < maxsz) {
  72. if ((size_t)(p - buf) == sz) {
  73. jump_in:
  74. buf = xrealloc(buf, sz + 128);
  75. p = buf + sz;
  76. sz += 128;
  77. }
  78. if (nonblock_immune_read(fd, p, 1) != 1) {
  79. /* EOF/error */
  80. if (p == buf) { /* we read nothing */
  81. free(buf);
  82. return NULL;
  83. }
  84. break;
  85. }
  86. if (*p == '\n')
  87. break;
  88. p++;
  89. }
  90. *p = '\0';
  91. if (maxsz_p)
  92. *maxsz_p = p - buf;
  93. p++;
  94. return xrealloc(buf, p - buf);
  95. }
  96. // Read (potentially big) files in one go. File size is estimated
  97. // by stat. Extra '\0' byte is appended.
  98. void* FAST_FUNC xmalloc_read_with_initial_buf(int fd, size_t *maxsz_p, char *buf, size_t total)
  99. {
  100. size_t size, rd_size;
  101. size_t to_read;
  102. struct stat st;
  103. to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
  104. /* Estimate file size */
  105. st.st_size = 0; /* in case fstat fails, assume 0 */
  106. fstat(fd, &st);
  107. /* /proc/N/stat files report st_size 0 */
  108. /* In order to make such files readable, we add small const */
  109. size = (st.st_size | 0x3ff) + 1;
  110. while (1) {
  111. if (to_read < size)
  112. size = to_read;
  113. buf = xrealloc(buf, total + size + 1);
  114. rd_size = full_read(fd, buf + total, size);
  115. if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
  116. free(buf);
  117. return NULL;
  118. }
  119. total += rd_size;
  120. if (rd_size < size) /* EOF */
  121. break;
  122. if (to_read <= rd_size)
  123. break;
  124. to_read -= rd_size;
  125. /* grow by 1/8, but in [1k..64k] bounds */
  126. size = ((total / 8) | 0x3ff) + 1;
  127. if (size > 64*1024)
  128. size = 64*1024;
  129. }
  130. buf = xrealloc(buf, total + 1);
  131. buf[total] = '\0';
  132. if (maxsz_p)
  133. *maxsz_p = total;
  134. return buf;
  135. }
  136. void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
  137. {
  138. return xmalloc_read_with_initial_buf(fd, maxsz_p, NULL, 0);
  139. }
  140. #ifdef USING_LSEEK_TO_GET_SIZE
  141. /* Alternatively, file size can be obtained by lseek to the end.
  142. * The code is slightly bigger. Retained in case fstat approach
  143. * will not work for some weird cases (/proc, block devices, etc).
  144. * (NB: lseek also can fail to work for some weird files) */
  145. // Read (potentially big) files in one go. File size is estimated by
  146. // lseek to end.
  147. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  148. {
  149. char *buf;
  150. size_t size;
  151. int fd;
  152. off_t len;
  153. fd = open(filename, O_RDONLY);
  154. if (fd < 0)
  155. return NULL;
  156. /* /proc/N/stat files report len 0 here */
  157. /* In order to make such files readable, we add small const */
  158. size = 0x3ff; /* read only 1k on unseekable files */
  159. len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  160. if (len != (off_t)-1) {
  161. xlseek(fd, 0, SEEK_SET);
  162. size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  163. if (len < size)
  164. size = len;
  165. }
  166. buf = xmalloc(size + 1);
  167. size = read_close(fd, buf, size);
  168. if ((ssize_t)size < 0) {
  169. free(buf);
  170. return NULL;
  171. }
  172. buf = xrealloc(buf, size + 1);
  173. buf[size] = '\0';
  174. if (maxsz_p)
  175. *maxsz_p = size;
  176. return buf;
  177. }
  178. #endif
  179. // Read (potentially big) files in one go. File size is estimated
  180. // by stat.
  181. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  182. {
  183. char *buf;
  184. int fd;
  185. fd = open(filename, O_RDONLY);
  186. if (fd < 0)
  187. return NULL;
  188. buf = xmalloc_read(fd, maxsz_p);
  189. close(fd);
  190. return buf;
  191. }
  192. /* Die with an error message if we can't read the entire buffer. */
  193. void FAST_FUNC xread(int fd, void *buf, size_t count)
  194. {
  195. if (count) {
  196. ssize_t size = full_read(fd, buf, count);
  197. if ((size_t)size != count)
  198. bb_simple_error_msg_and_die("short read");
  199. }
  200. }
  201. /* Die with an error message if we can't read one character. */
  202. unsigned char FAST_FUNC xread_char(int fd)
  203. {
  204. char tmp;
  205. xread(fd, &tmp, 1);
  206. return tmp;
  207. }
  208. void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
  209. {
  210. void *buf = xmalloc_open_read_close(filename, maxsz_p);
  211. if (!buf)
  212. bb_perror_msg_and_die("can't read '%s'", filename);
  213. return buf;
  214. }