coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/head.c (97 lines, 2.4 KiB) -rw-r--r-- file download

e013c3e Jamozed 2020-07-06 22:38:51
0
// head.c, version 1.0.3
998882e Jamozed 2020-06-27 03:37:26
1
// OMKOV coreutils implementation of POSIX head
998882e Jamozed 2020-06-27 03:37:26
2
// Copyright (C) 2020, Jakob Wakeling
e2140ec Jamozed 2022-03-06 15:27:45
3
// MIT Licence
998882e Jamozed 2020-06-27 03:37:26
4
b181413 Jamozed 2022-02-05 22:32:00
5
#include "util/error.h"
b181413 Jamozed 2022-02-05 22:32:00
6
#include "util/optget.h"
e013c3e Jamozed 2020-07-06 22:38:51
7
998882e Jamozed 2020-06-27 03:37:26
8
#include <stdbool.h>
998882e Jamozed 2020-06-27 03:37:26
9
#include <stdint.h>
998882e Jamozed 2020-06-27 03:37:26
10
#include <stdio.h>
998882e Jamozed 2020-06-27 03:37:26
11
e013c3e Jamozed 2020-07-06 22:38:51
12
#define VERSION "1.0.3"
998882e Jamozed 2020-06-27 03:37:26
13
30cf081 Jamozed 2020-08-28 23:01:02
14
static struct lop lops[] = {
30cf081 Jamozed 2020-08-28 23:01:02
15
	{ "help",    ARG_NUL, 256 },
30cf081 Jamozed 2020-08-28 23:01:02
16
	{ "version", ARG_NUL, 257 },
30cf081 Jamozed 2020-08-28 23:01:02
17
	{ NULL, 0, 0 }
30cf081 Jamozed 2020-08-28 23:01:02
18
};
30cf081 Jamozed 2020-08-28 23:01:02
19
998882e Jamozed 2020-06-27 03:37:26
20
static uintmax_t limit = 10;
998882e Jamozed 2020-06-27 03:37:26
21
static bool      label;
998882e Jamozed 2020-06-27 03:37:26
22
998882e Jamozed 2020-06-27 03:37:26
23
static inline int head(const char *path);
998882e Jamozed 2020-06-27 03:37:26
24
30cf081 Jamozed 2020-08-28 23:01:02
25
static void hlp(void);
30cf081 Jamozed 2020-08-28 23:01:02
26
static void ver(void);
998882e Jamozed 2020-06-27 03:37:26
27
e013c3e Jamozed 2020-07-06 22:38:51
28
int main(int ac, char *av[]) { A0 = av[0];
30cf081 Jamozed 2020-08-28 23:01:02
29
	struct opt opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops;
30cf081 Jamozed 2020-08-28 23:01:02
30
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
998882e Jamozed 2020-06-27 03:37:26
31
	case 'n': {
998882e Jamozed 2020-06-27 03:37:26
32
		register uintmax_t d; register char *p = opt.arg;
998882e Jamozed 2020-06-27 03:37:26
33
		for (limit = 0; *p >= '0' && *p <= '9'; ++p) {
998882e Jamozed 2020-06-27 03:37:26
34
			d = (uintmax_t)*p - '0';
998882e Jamozed 2020-06-27 03:37:26
35
			if (limit > (UINTMAX_MAX - d) / 10) { break; }
998882e Jamozed 2020-06-27 03:37:26
36
			limit = limit * 10 + d;
998882e Jamozed 2020-06-27 03:37:26
37
		}
998882e Jamozed 2020-06-27 03:37:26
38
		
e013c3e Jamozed 2020-07-06 22:38:51
39
		if (*p) { error(1, "%s: invalid line count", opt.arg); }
998882e Jamozed 2020-06-27 03:37:26
40
		break;
998882e Jamozed 2020-06-27 03:37:26
41
	}
30cf081 Jamozed 2020-08-28 23:01:02
42
	case 256: { hlp(); return 0; }
30cf081 Jamozed 2020-08-28 23:01:02
43
	case 257: { ver(); return 0; }
998882e Jamozed 2020-06-27 03:37:26
44
	default: { return 1; }
998882e Jamozed 2020-06-27 03:37:26
45
	}
998882e Jamozed 2020-06-27 03:37:26
46
	
998882e Jamozed 2020-06-27 03:37:26
47
	bool warned = false;
998882e Jamozed 2020-06-27 03:37:26
48
	
e013c3e Jamozed 2020-07-06 22:38:51
49
	if (opt.ind == ac) { head("-"); return 0; }
e013c3e Jamozed 2020-07-06 22:38:51
50
	if (opt.ind + 1 != ac) { label = true; }
e013c3e Jamozed 2020-07-06 22:38:51
51
	for (char **p = &av[opt.ind]; *p; ++p) if (head(*p)) {
e013c3e Jamozed 2020-07-06 22:38:51
52
		warn("%s: %s", *p, serr()); warned = true;
998882e Jamozed 2020-06-27 03:37:26
53
	}
998882e Jamozed 2020-06-27 03:37:26
54
	
998882e Jamozed 2020-06-27 03:37:26
55
	return warned;
998882e Jamozed 2020-06-27 03:37:26
56
}
998882e Jamozed 2020-06-27 03:37:26
57
998882e Jamozed 2020-06-27 03:37:26
58
static inline int head(const char *file) {
e2d04da Jamozed 2020-07-02 15:20:02
59
	char stdbuf[BUFSIZ * 16]; FILE *fi;
e2d04da Jamozed 2020-07-02 15:20:02
60
	register uintmax_t l = 0;
998882e Jamozed 2020-06-27 03:37:26
61
	
998882e Jamozed 2020-06-27 03:37:26
62
	if (file[0] == '-' && file[1] == 0) { fi = stdin; }
998882e Jamozed 2020-06-27 03:37:26
63
	else if (!(fi = fopen(file, "r"))) { return 1; }
998882e Jamozed 2020-06-27 03:37:26
64
	
998882e Jamozed 2020-06-27 03:37:26
65
	if (label) { static bool spc = false;
998882e Jamozed 2020-06-27 03:37:26
66
		if (spc) { fputc('\n', stdout); } else { spc = true; }
998882e Jamozed 2020-06-27 03:37:26
67
		printf("==> %s <==\n", file);
998882e Jamozed 2020-06-27 03:37:26
68
	}
998882e Jamozed 2020-06-27 03:37:26
69
	
998882e Jamozed 2020-06-27 03:37:26
70
	if (limit == 0) { goto end; }
998882e Jamozed 2020-06-27 03:37:26
71
	for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), fi));) {
998882e Jamozed 2020-06-27 03:37:26
72
		for (register size_t i = 0; i != c; ++i) {
998882e Jamozed 2020-06-27 03:37:26
73
			fputc(stdbuf[i], stdout);
998882e Jamozed 2020-06-27 03:37:26
74
			if (stdbuf[i] == '\n' && ++l == limit) { goto end; }
998882e Jamozed 2020-06-27 03:37:26
75
		}
998882e Jamozed 2020-06-27 03:37:26
76
	}
998882e Jamozed 2020-06-27 03:37:26
77
	
998882e Jamozed 2020-06-27 03:37:26
78
end:
998882e Jamozed 2020-06-27 03:37:26
79
	if (fi != stdin) { fclose(fi); } return 0;
998882e Jamozed 2020-06-27 03:37:26
80
}
998882e Jamozed 2020-06-27 03:37:26
81
30cf081 Jamozed 2020-08-28 23:01:02
82
static void hlp(void) {
998882e Jamozed 2020-06-27 03:37:26
83
	puts("head - output the first part of files\n");
998882e Jamozed 2020-06-27 03:37:26
84
	puts("usage: head [-n number] [file...]\n");
998882e Jamozed 2020-06-27 03:37:26
85
	puts("options:");
998882e Jamozed 2020-06-27 03:37:26
86
	puts("  -n number  Number of lines to output for each file");
998882e Jamozed 2020-06-27 03:37:26
87
	puts("  --help     Display help information");
998882e Jamozed 2020-06-27 03:37:26
88
	puts("  --version  Display version information");
998882e Jamozed 2020-06-27 03:37:26
89
}
998882e Jamozed 2020-06-27 03:37:26
90
30cf081 Jamozed 2020-08-28 23:01:02
91
static void ver(void) {
e2d04da Jamozed 2020-07-02 15:20:02
92
	puts("OMKOV coreutils head, version " VERSION);
998882e Jamozed 2020-06-27 03:37:26
93
	puts("Copyright (C) 2020, Jakob Wakeling");
e2140ec Jamozed 2022-03-06 15:27:45
94
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
998882e Jamozed 2020-06-27 03:37:26
95
}
96