0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
|
// head.c, version 1.0.3
// OMKOV coreutils implementation of POSIX head
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence
#include "util/error.h"
#include "util/optget.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#define VERSION "1.0.3"
static struct lop lops[] = {
{ "help", ARG_NUL, 256 },
{ "version", ARG_NUL, 257 },
{ NULL, 0, 0 }
};
static uintmax_t limit = 10;
static bool label;
static inline int head(const char *path);
static void hlp(void);
static void ver(void);
int main(int ac, char *av[]) { A0 = av[0];
struct opt opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops;
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
case 'n': {
register uintmax_t d; register char *p = opt.arg;
for (limit = 0; *p >= '0' && *p <= '9'; ++p) {
d = (uintmax_t)*p - '0';
if (limit > (UINTMAX_MAX - d) / 10) { break; }
limit = limit * 10 + d;
}
if (*p) { error(1, "%s: invalid line count", opt.arg); }
break;
}
case 256: { hlp(); return 0; }
case 257: { ver(); return 0; }
default: { return 1; }
}
bool warned = false;
if (opt.ind == ac) { head("-"); return 0; }
if (opt.ind + 1 != ac) { label = true; }
for (char **p = &av[opt.ind]; *p; ++p) if (head(*p)) {
warn("%s: %s", *p, serr()); warned = true;
}
return warned;
}
static inline int head(const char *file) {
char stdbuf[BUFSIZ * 16]; FILE *fi;
register uintmax_t l = 0;
if (file[0] == '-' && file[1] == 0) { fi = stdin; }
else if (!(fi = fopen(file, "r"))) { return 1; }
if (label) { static bool spc = false;
if (spc) { fputc('\n', stdout); } else { spc = true; }
printf("==> %s <==\n", file);
}
if (limit == 0) { goto end; }
for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), fi));) {
for (register size_t i = 0; i != c; ++i) {
fputc(stdbuf[i], stdout);
if (stdbuf[i] == '\n' && ++l == limit) { goto end; }
}
}
end:
if (fi != stdin) { fclose(fi); } return 0;
}
static void hlp(void) {
puts("head - output the first part of files\n");
puts("usage: head [-n number] [file...]\n");
puts("options:");
puts(" -n number Number of lines to output for each file");
puts(" --help Display help information");
puts(" --version Display version information");
}
static void ver(void) {
puts("OMKOV coreutils head, version " VERSION);
puts("Copyright (C) 2020, Jakob Wakeling");
puts("MIT Licence (https://opensource.org/licenses/MIT)");
}
|