-
Notifications
You must be signed in to change notification settings - Fork 0
/
nl.c
109 lines (86 loc) · 2.16 KB
/
nl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "stdlib.h"
#include "stdio.h"
#include "getopt.h"
#include "regex.h"
void number_lines(FILE* file, const char* format, const regex_t* regex, size_t* offset);
int main (int argc, char** argv)
{
const char* regex_str = ".*[ -~].*";
const char* format = "% 5d %s\n";
regex_t regex;
int i;
int argfail = 0;
int m = 0;
while ((m = getopt(argc, argv,"b:n:")) != -1) {
switch(m) {
case 'b':
switch(optarg[0]) {
case 't':
regex_str = ".*[ -~].*";
if (optarg[1] != '\0') argfail = 1;
break;
case 'a':
regex_str = ".*";
if (optarg[1] != '\0') argfail = 1;
break;
case 'n':
regex_str = "(?!.*)";
if (optarg[1] != '\0') argfail = 1;
break;
case 'p':
if (optarg[1] == '\0') argfail = 1;
regex_str = optarg+1;
break;
default:
argfail = 1;
}
break;
case 'n':
if (optarg[0]=='l' && optarg[1]=='n' && optarg[2] == '\0') format = "%- 5d %s\n";
else if (optarg[0]=='r' && optarg[1]=='n' && optarg[2] == '\0') format = "% 5d %s\n";
else if (optarg[0]=='r' && optarg[1]=='z' && optarg[2] == '\0') format = "%05d %s\n";
else argfail = 1;
break;
case '?':
default:
argfail = 1;
}
if (argfail == 1) {
fprintf(stderr, "Wait a sec, are you trying to trick me again?\n");
return 1;
}
}
regcomp(®ex, regex_str, REG_EXTENDED);
argv = argv + optind;
argc = argc - optind;
size_t offset = 1;
if (argc == 0) {
number_lines(stdin, format, ®ex, &offset);
} else {
for (i = 0; i < argc; ++i) {
FILE* file = fopen(argv[i], "r");
if (file == NULL) {
fprintf(stderr, "Where the fuck is %s?\n", argv[i]);
return 1;
}
number_lines(file, format, ®ex, &offset);
fclose(file);
}
}
regfree(®ex);
return 0;
}
void number_lines(FILE* file, const char* format, const regex_t* regex, size_t* offset)
{
char* line = NULL;
size_t len = 0;
while(getline(&line, &len, file) > 0) {
if (regexec(regex, line, 0, NULL, REG_EXTENDED) == 0) {
printf(format, *offset, line);
++(*offset);
} else {
printf(" %s", line);
}
}
if (line != NULL) free(line);
}