head.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * head implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 compliant */
  23. /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <unistd.h>
  30. #include "busybox.h"
  31. static const char head_opts[] =
  32. "n:"
  33. #ifdef CONFIG_FEATURE_FANCY_HEAD
  34. "c:qv"
  35. #endif
  36. ;
  37. static const char header_fmt_str[] = "\n==> %s <==\n";
  38. int head_main(int argc, char **argv)
  39. {
  40. unsigned long count = 10;
  41. unsigned long i;
  42. #ifdef CONFIG_FEATURE_FANCY_HEAD
  43. int count_bytes = 0;
  44. int header_threshhold = 1;
  45. #endif
  46. FILE *fp;
  47. const char *fmt;
  48. char *p;
  49. int opt;
  50. int c;
  51. int retval = EXIT_SUCCESS;
  52. /* Allow legacy syntax of an initial numeric option without -n. */
  53. if ((argc > 1) && (argv[1][0] == '-')
  54. /* && (isdigit)(argv[1][1]) */
  55. && (((unsigned int)(argv[1][1] - '0')) <= 9)
  56. ) {
  57. --argc;
  58. ++argv;
  59. p = (*argv) + 1;
  60. goto GET_COUNT;
  61. }
  62. while ((opt = getopt(argc, argv, head_opts)) > 0) {
  63. switch(opt) {
  64. #ifdef CONFIG_FEATURE_FANCY_HEAD
  65. case 'q':
  66. header_threshhold = INT_MAX;
  67. break;
  68. case 'v':
  69. header_threshhold = -1;
  70. break;
  71. case 'c':
  72. count_bytes = 1;
  73. /* fall through */
  74. #endif
  75. case 'n':
  76. p = optarg;
  77. GET_COUNT:
  78. count = bb_xgetularg10(p);
  79. break;
  80. default:
  81. bb_show_usage();
  82. }
  83. }
  84. argv += optind;
  85. if (!*argv) {
  86. *--argv = "-";
  87. }
  88. fmt = header_fmt_str + 1;
  89. #ifdef CONFIG_FEATURE_FANCY_HEAD
  90. if (argc - optind <= header_threshhold) {
  91. header_threshhold = 0;
  92. }
  93. #else
  94. if (argc <= optind + 1) {
  95. fmt += 11;
  96. }
  97. /* Now define some things here to avoid #ifdefs in the code below.
  98. * These should optimize out of the if conditions below. */
  99. #define header_threshhold 1
  100. #define count_bytes 0
  101. #endif
  102. do {
  103. if ((fp = bb_wfopen_input(*argv)) != NULL) {
  104. if (fp == stdin) {
  105. *argv = (char *) bb_msg_standard_input;
  106. }
  107. if (header_threshhold) {
  108. bb_printf(fmt, *argv);
  109. }
  110. i = count;
  111. while (i && ((c = getc(fp)) != EOF)) {
  112. if (count_bytes || (c == '\n')) {
  113. --i;
  114. }
  115. putchar(c);
  116. }
  117. if (bb_fclose_nonstdin(fp)) {
  118. bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
  119. retval = EXIT_FAILURE;
  120. }
  121. bb_xferror_stdout();
  122. }
  123. fmt = header_fmt_str;
  124. } while (*++argv);
  125. bb_fflush_stdout_and_exit(retval);
  126. }