#include "command.h" #include "lslib.h" #include static struct { int count; bool lines; bool print_headers; bool dont_print_headers; } flags; static void head_file_lines(FILE* file) { size_t len = 0; char* line = NULL; int count = flags.count; while(count > 0 && getline(&line, &len, file) != -1) { printf("%s", line); count--; } free(line); fclose(file); } static void head_file_chars(FILE* file) { char c; int count = flags.count; while(count > 0 && (c = getc(file)) != EOF) { putchar(c); count--; } fclose(file); } static void help(void) { printf("Usage: head [OPTIONS] [FILE]...\n\n"); printf("Print first 10 lines of FILEs (or stdin)\n"); printf("With more than one FILE, precede each with a filename header.\n\n"); printf("\t-c [+]N[bkm]\tPrint first N bytes\n"); printf("\t-n N[bkm]\tPrint first N lines\n"); printf("\t\t\t(b:*512 k:*1024 m:*1024^2)\n"); printf("\t-q\t\tNever print headers\n"); printf("\t-v\t\tAlways print headers\n"); } static void print_header(char* path, bool many) { if (flags.dont_print_headers) return; if (!many && !flags.print_headers) return; if (streql("-", path)) { printf("\n==> standard input <==\n"); } else { printf("\n=>> %s <==\n", path); } } static void head_file(char* path, bool many) { FILE* file = get_file(path, "r"); print_header(path, many); if (flags.lines) { head_file_lines(file); } else { head_file_chars(file); } } static int short_arg(char c, char* next) { switch(c) { case 'c': { long int bkm; flags.lines = false; check_arg(next); bkm = get_blkm(next); if (bkm < 1) { error("bkm cannot be less than 1"); } flags.count = bkm; return ARG_USED; } case 'n': { long int bkm; flags.lines = true; check_arg(next); bkm = get_blkm(next); if (bkm < 1) { error("bkm cannot be less than 1"); } flags.count = bkm; return ARG_USED; } case 'q': flags.dont_print_headers = true; break; case 'v': flags.print_headers = true; break; default: return ARG_INVALID; } return ARG_UNUSED; } COMMAND(head) { int start, count, i; flags.count = 10; flags.lines = true; flags.print_headers = false; flags.dont_print_headers = false; start = parse_args(argc, argv, help, short_arg, NULL); count = argc - start; if (count < 1) { head_file_lines(stdin); return EXIT_SUCCESS; } if (count == 1) { head_file(argv[start], false); return EXIT_SUCCESS; } for (i = 0; i < count; i++) { head_file(argv[start + i], true); } return EXIT_SUCCESS; }