head.c (3154B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "config.h" 9 #include "util.h" 10 11 static void 12 head(FILE *fp, const char *fname, size_t n) 13 { 14 char *buf = NULL; 15 size_t i = 0, size = 0; 16 ssize_t len; 17 18 while (i < n && (len = getline(&buf, &size, fp)) > 0) { 19 fwrite(buf, 1, len, stdout); 20 i += (len && (buf[len - 1] == '\n')); 21 } 22 free(buf); 23 if (ferror(fp)) 24 eprintf("getline %s:", fname); 25 } 26 27 #if FEATURE_HEAD_NEGATIVE 28 // gnu extension, -n -N: everything but the last n lines. a line is held 29 // back until n others came after it 30 static void 31 head_but(FILE *fp, const char *fname, size_t n) 32 { 33 char *buf = NULL, **held = NULL; 34 size_t i, size = 0, nheld = 0, at = 0; 35 ssize_t len; 36 37 while ((len = getline(&buf, &size, fp)) > 0) { 38 if (nheld < n) { 39 held = ereallocarray(held, nheld + 1, sizeof(*held)); 40 held[nheld++] = estrdup(buf); 41 continue; 42 } 43 fputs(held[at], stdout); 44 free(held[at]); 45 held[at] = estrdup(buf); 46 at = (at + 1) % n; 47 } 48 for (i = 0; i < nheld; i++) 49 free(held[i]); 50 free(held); 51 free(buf); 52 if (ferror(fp)) 53 eprintf("getline %s:", fname); 54 } 55 #endif 56 57 static void 58 usage(void) 59 { 60 eprintf("usage: %s [-num | -n num] [file ...]\n", argv0); 61 } 62 63 // ?man head: output first part of files 64 // ?man arguments: file ... 65 // ?man print the first lines or bytes of files 66 int 67 main(int argc, char *argv[]) 68 { 69 size_t n = 10; 70 FILE *fp; 71 int ret = 0, newline = 0, many = 0; 72 #if FEATURE_HEAD_NEGATIVE 73 char *arg; 74 int drop = 0; 75 #endif 76 77 ARGBEGIN 78 { 79 // ?man -n:num: print line numbers or counts 80 case 'n': 81 #if FEATURE_HEAD_NEGATIVE 82 arg = EARGF(usage()); 83 drop = arg[0] == '-' && arg[1]; 84 n = estrtonum( 85 arg + drop, 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 86 ); 87 #else 88 n = estrtonum( 89 EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 90 ); 91 #endif 92 break; 93 // ?man ARGNUM: specify RGNUM option 94 ARGNUM: 95 n = ARGNUMF(); 96 break; 97 default: 98 usage(); 99 } 100 ARGEND 101 102 #if FEATURE_HEAD_NEGATIVE 103 // dropping none is printing all 104 if (drop && !n) { 105 drop = 0; 106 n = SIZE_MAX; 107 } 108 #endif 109 110 if (!argc) { 111 #if FEATURE_HEAD_NEGATIVE 112 if (drop) 113 head_but(stdin, "<stdin>", n); 114 else 115 #endif 116 head(stdin, "<stdin>", n); 117 } else { 118 many = argc > 1; 119 for (newline = 0; *argv; argc--, argv++) { 120 if (!strcmp(*argv, "-")) { 121 *argv = "<stdin>"; 122 fp = stdin; 123 } else if (!(fp = fopen(*argv, "r"))) { 124 weprintf("fopen %s:", *argv); 125 ret = 1; 126 continue; 127 } 128 if (many) { 129 if (newline) 130 putchar('\n'); 131 printf("==> %s <==\n", *argv); 132 } 133 newline = 1; 134 #if FEATURE_HEAD_NEGATIVE 135 if (drop) 136 head_but(fp, *argv, n); 137 else 138 #endif 139 head(fp, *argv, n); 140 if (fp != stdin && fshut(fp, *argv)) 141 ret = 1; 142 } 143 } 144 145 ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); 146 147 return ret; 148 }