=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/head/head.c,v retrieving revision 1.22 retrieving revision 1.23 diff -c -r1.22 -r1.23 *** src/usr.bin/head/head.c 2021/10/10 15:57:25 1.22 --- src/usr.bin/head/head.c 2022/01/29 00:19:04 1.23 *************** *** 1,4 **** ! /* $OpenBSD: head.c,v 1.22 2021/10/10 15:57:25 cheloha Exp $ */ /* * Copyright (c) 1980, 1987 Regents of the University of California. --- 1,4 ---- ! /* $OpenBSD: head.c,v 1.23 2022/01/29 00:19:04 cheloha Exp $ */ /* * Copyright (c) 1980, 1987 Regents of the University of California. *************** *** 37,42 **** --- 37,43 ---- #include #include + int head_file(const char *, long, int); static void usage(void); /* *************** *** 49,57 **** main(int argc, char *argv[]) { const char *errstr; ! FILE *fp; ! long cnt; ! int ch, firsttime; long linecnt = 10; int status = 0; --- 50,56 ---- main(int argc, char *argv[]) { const char *errstr; ! int ch; long linecnt = 10; int status = 0; *************** *** 81,113 **** } argc -= optind, argv += optind; ! for (firsttime = 1; ; firsttime = 0) { ! if (!*argv) { ! if (!firsttime) ! exit(status); ! fp = stdin; ! if (pledge("stdio", NULL) == -1) ! err(1, "pledge"); ! } else { ! if ((fp = fopen(*argv, "r")) == NULL) { ! warn("%s", *argv++); ! status = 1; ! continue; ! } ! if (argc > 1) { ! if (!firsttime) ! putchar('\n'); ! printf("==> %s <==\n", *argv); ! } ! ++argv; ! } ! for (cnt = linecnt; cnt && !feof(fp); --cnt) ! while ((ch = getc(fp)) != EOF) ! if (putchar(ch) == '\n') ! break; ! fclose(fp); } ! /*NOTREACHED*/ } --- 80,125 ---- } argc -= optind, argv += optind; ! if (argc == 0) { ! if (pledge("stdio", NULL) == -1) ! err(1, "pledge"); ! ! status = head_file(NULL, linecnt, 0); ! } else { ! for (; *argv != NULL; argv++) ! status |= head_file(*argv, linecnt, argc > 1); } ! ! return status; ! } ! ! int ! head_file(const char *path, long count, int need_header) ! { ! FILE *fp; ! int ch; ! static int first = 1; ! ! if (path != NULL) { ! fp = fopen(path, "r"); ! if (fp == NULL) { ! warn("%s", path); ! return 1; ! } ! if (need_header) { ! printf("%s==> %s <==\n", first ? "" : "\n", path); ! first = 0; ! } ! } else ! fp = stdin; ! ! for (; count > 0 && !feof(fp); --count) ! while ((ch = getc(fp)) != EOF) ! if (putchar(ch) == '\n') ! break; ! fclose(fp); ! ! return 0; }