file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* $NetBSD: file.c,v 1.7 2011/04/18 22:46:48 joerg Exp $ */
  2. /* $FreeBSD: head/usr.bin/grep/file.c 211496 2010-08-19 09:28:59Z des $ */
  3. /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
  4. /*-
  5. * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
  6. * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
  7. * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #if HAVE_NBTOOL_CONFIG_H
  32. #include "nbtool_config.h"
  33. #endif
  34. #include <sys/cdefs.h>
  35. __RCSID("$NetBSD: file.c,v 1.7 2011/04/18 22:46:48 joerg Exp $");
  36. #include <sys/param.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <err.h>
  40. #include <errno.h>
  41. #include <fcntl.h>
  42. #include <stddef.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include <wchar.h>
  47. #include <wctype.h>
  48. #include "grep.h"
  49. #define MAXBUFSIZ (32 * 1024)
  50. #define LNBUFBUMP 80
  51. static unsigned char buffer[MAXBUFSIZ];
  52. static unsigned char *bufpos;
  53. static size_t bufrem;
  54. static unsigned char *lnbuf;
  55. static size_t lnbuflen;
  56. static inline int
  57. grep_refill(struct file *f)
  58. {
  59. ssize_t nr;
  60. bufpos = buffer;
  61. bufrem = 0;
  62. nr = read(f->fd, buffer, MAXBUFSIZ);
  63. if (nr < 0)
  64. return (-1);
  65. bufrem = nr;
  66. return (0);
  67. }
  68. static inline int
  69. grep_lnbufgrow(size_t newlen)
  70. {
  71. if (lnbuflen < newlen) {
  72. lnbuf = grep_realloc(lnbuf, newlen);
  73. lnbuflen = newlen;
  74. }
  75. return (0);
  76. }
  77. char *
  78. grep_fgetln(struct file *f, size_t *lenp)
  79. {
  80. unsigned char *p;
  81. char *ret;
  82. size_t len;
  83. size_t off;
  84. ptrdiff_t diff;
  85. /* Fill the buffer, if necessary */
  86. if (bufrem == 0 && grep_refill(f) != 0)
  87. goto error;
  88. if (bufrem == 0) {
  89. /* Return zero length to indicate EOF */
  90. *lenp = 0;
  91. return ((char *)bufpos);
  92. }
  93. /* Look for a newline in the remaining part of the buffer */
  94. if ((p = memchr(bufpos, line_sep, bufrem)) != NULL) {
  95. ++p; /* advance over newline */
  96. ret = (char *)bufpos;
  97. len = p - bufpos;
  98. bufrem -= len;
  99. bufpos = p;
  100. *lenp = len;
  101. return (ret);
  102. }
  103. /* We have to copy the current buffered data to the line buffer */
  104. for (len = bufrem, off = 0; ; len += bufrem) {
  105. /* Make sure there is room for more data */
  106. if (grep_lnbufgrow(len + LNBUFBUMP))
  107. goto error;
  108. memcpy(lnbuf + off, bufpos, len - off);
  109. off = len;
  110. if (grep_refill(f) != 0)
  111. goto error;
  112. if (bufrem == 0)
  113. /* EOF: return partial line */
  114. break;
  115. if ((p = memchr(bufpos, line_sep, bufrem)) == NULL)
  116. continue;
  117. /* got it: finish up the line (like code above) */
  118. ++p;
  119. diff = p - bufpos;
  120. len += diff;
  121. if (grep_lnbufgrow(len))
  122. goto error;
  123. memcpy(lnbuf + off, bufpos, diff);
  124. bufrem -= diff;
  125. bufpos = p;
  126. break;
  127. }
  128. *lenp = len;
  129. return ((char *)lnbuf);
  130. error:
  131. *lenp = 0;
  132. return (NULL);
  133. }
  134. static inline struct file *
  135. grep_file_init(struct file *f)
  136. {
  137. /* Fill read buffer, also catches errors early */
  138. if (grep_refill(f) != 0)
  139. goto error;
  140. /* Check for binary stuff, if necessary */
  141. if (!nulldataflag && binbehave != BINFILE_TEXT &&
  142. memchr(bufpos, '\0', bufrem) != NULL)
  143. f->binary = true;
  144. return (f);
  145. error:
  146. close(f->fd);
  147. free(f);
  148. return (NULL);
  149. }
  150. /*
  151. * Opens a file for processing.
  152. */
  153. struct file *
  154. grep_open(const char *path)
  155. {
  156. struct file *f;
  157. f = grep_malloc(sizeof *f);
  158. memset(f, 0, sizeof *f);
  159. if (path == NULL) {
  160. /* Processing stdin implies --line-buffered. */
  161. lbflag = true;
  162. f->fd = STDIN_FILENO;
  163. } else if ((f->fd = open(path, O_RDONLY)) == -1) {
  164. free(f);
  165. return (NULL);
  166. }
  167. return (grep_file_init(f));
  168. }
  169. /*
  170. * Closes a file.
  171. */
  172. void
  173. grep_close(struct file *f)
  174. {
  175. close(f->fd);
  176. /* Reset read buffer and line buffer */
  177. bufpos = buffer;
  178. bufrem = 0;
  179. free(lnbuf);
  180. lnbuf = NULL;
  181. lnbuflen = 0;
  182. }