/usr.bin/head/head.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 182 lines · 130 code · 18 blank · 34 comment · 42 complexity · 99019213985945ee06def305f9aeb9b2 MD5 · raw file

  1. /*
  2. * Copyright (c) 1980, 1987, 1992, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. static const char copyright[] =
  31. "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
  32. The Regents of the University of California. All rights reserved.\n";
  33. #endif /* not lint */
  34. #ifndef lint
  35. #if 0
  36. static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
  37. #endif
  38. #endif /* not lint */
  39. #include <sys/cdefs.h>
  40. __FBSDID("$FreeBSD$");
  41. #include <sys/types.h>
  42. #include <ctype.h>
  43. #include <err.h>
  44. #include <inttypes.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. /*
  50. * head - give the first few lines of a stream or of each of a set of files
  51. *
  52. * Bill Joy UCB August 24, 1977
  53. */
  54. static void head(FILE *, int);
  55. static void head_bytes(FILE *, off_t);
  56. static void obsolete(char *[]);
  57. static void usage(void);
  58. int
  59. main(int argc, char *argv[])
  60. {
  61. int ch;
  62. FILE *fp;
  63. int first, linecnt = -1, eval = 0;
  64. off_t bytecnt = -1;
  65. char *ep;
  66. obsolete(argv);
  67. while ((ch = getopt(argc, argv, "n:c:")) != -1)
  68. switch(ch) {
  69. case 'c':
  70. bytecnt = strtoimax(optarg, &ep, 10);
  71. if (*ep || bytecnt <= 0)
  72. errx(1, "illegal byte count -- %s", optarg);
  73. break;
  74. case 'n':
  75. linecnt = strtol(optarg, &ep, 10);
  76. if (*ep || linecnt <= 0)
  77. errx(1, "illegal line count -- %s", optarg);
  78. break;
  79. case '?':
  80. default:
  81. usage();
  82. }
  83. argc -= optind;
  84. argv += optind;
  85. if (linecnt != -1 && bytecnt != -1)
  86. errx(1, "can't combine line and byte counts");
  87. if (linecnt == -1 )
  88. linecnt = 10;
  89. if (*argv) {
  90. for (first = 1; *argv; ++argv) {
  91. if ((fp = fopen(*argv, "r")) == NULL) {
  92. warn("%s", *argv);
  93. eval = 1;
  94. continue;
  95. }
  96. if (argc > 1) {
  97. (void)printf("%s==> %s <==\n",
  98. first ? "" : "\n", *argv);
  99. first = 0;
  100. }
  101. if (bytecnt == -1)
  102. head(fp, linecnt);
  103. else
  104. head_bytes(fp, bytecnt);
  105. (void)fclose(fp);
  106. }
  107. } else if (bytecnt == -1)
  108. head(stdin, linecnt);
  109. else
  110. head_bytes(stdin, bytecnt);
  111. exit(eval);
  112. }
  113. static void
  114. head(FILE *fp, int cnt)
  115. {
  116. char *cp;
  117. size_t error, readlen;
  118. while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
  119. error = fwrite(cp, sizeof(char), readlen, stdout);
  120. if (error != readlen)
  121. err(1, "stdout");
  122. cnt--;
  123. }
  124. }
  125. static void
  126. head_bytes(FILE *fp, off_t cnt)
  127. {
  128. char buf[4096];
  129. size_t readlen;
  130. while (cnt) {
  131. if ((uintmax_t)cnt < sizeof(buf))
  132. readlen = cnt;
  133. else
  134. readlen = sizeof(buf);
  135. readlen = fread(buf, sizeof(char), readlen, fp);
  136. if (readlen == 0)
  137. break;
  138. if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
  139. err(1, "stdout");
  140. cnt -= readlen;
  141. }
  142. }
  143. static void
  144. obsolete(char *argv[])
  145. {
  146. char *ap;
  147. while ((ap = *++argv)) {
  148. /* Return if "--" or not "-[0-9]*". */
  149. if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
  150. return;
  151. if ((ap = malloc(strlen(*argv) + 2)) == NULL)
  152. err(1, NULL);
  153. ap[0] = '-';
  154. ap[1] = 'n';
  155. (void)strcpy(ap + 2, *argv + 1);
  156. *argv = ap;
  157. }
  158. }
  159. static void
  160. usage(void)
  161. {
  162. (void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
  163. exit(1);
  164. }