read_printf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, int loop_on_EINTR)
  47. {
  48. struct pollfd pfd[1];
  49. ssize_t n;
  50. while (1) {
  51. n = loop_on_EINTR ? safe_read(fd, buf, count) : 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. loop_on_EINTR ? safe_poll(pfd, 1, -1) : 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, /*loop_on_EINTR:*/ 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(int fd, size_t *maxsz_p)
  99. {
  100. char *buf;
  101. size_t size, rd_size, total;
  102. size_t to_read;
  103. struct stat st;
  104. to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
  105. /* Estimate file size */
  106. st.st_size = 0; /* in case fstat fails, assume 0 */
  107. fstat(fd, &st);
  108. /* /proc/N/stat files report st_size 0 */
  109. /* In order to make such files readable, we add small const */
  110. size = (st.st_size | 0x3ff) + 1;
  111. total = 0;
  112. buf = NULL;
  113. while (1) {
  114. if (to_read < size)
  115. size = to_read;
  116. buf = xrealloc(buf, total + size + 1);
  117. rd_size = full_read(fd, buf + total, size);
  118. if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
  119. free(buf);
  120. return NULL;
  121. }
  122. total += rd_size;
  123. if (rd_size < size) /* EOF */
  124. break;
  125. if (to_read <= rd_size)
  126. break;
  127. to_read -= rd_size;
  128. /* grow by 1/8, but in [1k..64k] bounds */
  129. size = ((total / 8) | 0x3ff) + 1;
  130. if (size > 64*1024)
  131. size = 64*1024;
  132. }
  133. buf = xrealloc(buf, total + 1);
  134. buf[total] = '\0';
  135. if (maxsz_p)
  136. *maxsz_p = total;
  137. return buf;
  138. }
  139. #ifdef USING_LSEEK_TO_GET_SIZE
  140. /* Alternatively, file size can be obtained by lseek to the end.
  141. * The code is slightly bigger. Retained in case fstat approach
  142. * will not work for some weird cases (/proc, block devices, etc).
  143. * (NB: lseek also can fail to work for some weird files) */
  144. // Read (potentially big) files in one go. File size is estimated by
  145. // lseek to end.
  146. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  147. {
  148. char *buf;
  149. size_t size;
  150. int fd;
  151. off_t len;
  152. fd = open(filename, O_RDONLY);
  153. if (fd < 0)
  154. return NULL;
  155. /* /proc/N/stat files report len 0 here */
  156. /* In order to make such files readable, we add small const */
  157. size = 0x3ff; /* read only 1k on unseekable files */
  158. len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  159. if (len != (off_t)-1) {
  160. xlseek(fd, 0, SEEK_SET);
  161. size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  162. if (len < size)
  163. size = len;
  164. }
  165. buf = xmalloc(size + 1);
  166. size = read_close(fd, buf, size);
  167. if ((ssize_t)size < 0) {
  168. free(buf);
  169. return NULL;
  170. }
  171. buf = xrealloc(buf, size + 1);
  172. buf[size] = '\0';
  173. if (maxsz_p)
  174. *maxsz_p = size;
  175. return buf;
  176. }
  177. #endif
  178. // Read (potentially big) files in one go. File size is estimated
  179. // by stat.
  180. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  181. {
  182. char *buf;
  183. int fd;
  184. fd = open(filename, O_RDONLY);
  185. if (fd < 0)
  186. return NULL;
  187. buf = xmalloc_read(fd, maxsz_p);
  188. close(fd);
  189. return buf;
  190. }
  191. /* Die with an error message if we can't read the entire buffer. */
  192. void FAST_FUNC xread(int fd, void *buf, size_t count)
  193. {
  194. if (count) {
  195. ssize_t size = full_read(fd, buf, count);
  196. if ((size_t)size != count)
  197. bb_error_msg_and_die("short read");
  198. }
  199. }
  200. /* Die with an error message if we can't read one character. */
  201. unsigned char FAST_FUNC xread_char(int fd)
  202. {
  203. char tmp;
  204. xread(fd, &tmp, 1);
  205. return tmp;
  206. }
  207. void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
  208. {
  209. void *buf = xmalloc_open_read_close(filename, maxsz_p);
  210. if (!buf)
  211. bb_perror_msg_and_die("can't read '%s'", filename);
  212. return buf;
  213. }