-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
nin.c
177 lines (155 loc) · 3.57 KB
/
nin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* ninc - Narthex incrementor
*
* By Michael Constantine Dimopoulos https://mcdim.xyz <[email protected]>
* License: GNU GPL v3
*
* ninc will iterate over stdin and after printing the dictionary as is
* it will reprint it but will also multiply each line with the
* difference of max-min, and will append n to each line, where n is
* increased after every line from min to max inclusive. (I know, I know.
* Just try to use it and it will make more sense).
*
* * * * * * * *
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define VERSION "v1.2.2"
#define BUFFER_SIZE 256
static void
help(char *exename)
{
printf( "ninc - Narthex incrementor %s\n"
"By Michael Constantine Dimopoulos <[email protected]>\n\n"
"-d use delimiters (specified in a string)\n"
"-n increment numerical lines as well\n"
"-h print this panel & exit\n"
"-v print current version & exit\n\n"
"Usage: cat [FILENAME] | %s [MIN] [MAX] [OPTIONS]\n",
VERSION, exename);
exit(EXIT_SUCCESS);
}
static inline void
die(char *str)
{
printf("%s\n", str);
exit(EXIT_SUCCESS);
}
static FILE *
save_stdin(FILE *f)
{
FILE *f2 = tmpfile();
char buffer[BUFFER_SIZE];
while (fgets(buffer, sizeof(buffer), f) != NULL) {
fprintf(f2, "%s", buffer);
}
fclose(f);
return f2;
}
static int
isnumber(char *str)
{
for (int i = 0; str[i] != '\0'; i++) {
if (isdigit(str[i]) != 0)
return 1;
}
return 0;
}
static inline int
check_num(int num, char * buffer)
{
return ((num == 0 && isnumber(buffer) == 0) || num == 1);
}
static void
ninc(FILE *f, int min, int max, int numerical, char del)
{
char buffer[BUFFER_SIZE];
while (fgets(buffer, sizeof(buffer), f) != NULL) {
strtok(buffer, "\n");
for (int i = min; i <= max; i++) {
if (check_num(numerical, buffer) == 1) {
if (del == ' ')
printf("%s%d\n", buffer, i);
else
printf("%s%c%d\n", buffer, del, i);
}
}
}
}
static void
print_only(FILE *f)
{
char buffer[BUFFER_SIZE];
while (fgets(buffer, sizeof(buffer), f) != NULL) {
printf("%s",buffer);
}
}
void
main(int argc, char * argv[])
{
int min = 1;
int max = 10;
int d = 0;
char del[10];
int numerical = 0;
int index;
int c;
opterr = 0;
while ((c = getopt(argc, argv, "d:nvh")) != -1 )
switch (c) {
case 'v':
die(VERSION);
case 'h':
help(argv[0]);
case 'd':
d=1;
strncpy(del, optarg, 10);
break;
case 'n':
numerical=1;
break;
case '?':
exit(EXIT_FAILURE);
break;
}
if (optind < argc) {
min = atoi(argv[optind]);
max = atoi(argv[optind+1]);
} else {
fprintf(stderr, "%s: wrong number of arguments\n", argv[0]);
exit(EXIT_FAILURE);
}
if (min > max) {
int temp = max;
max = min;
min = temp;
}
FILE *f;
f = save_stdin(stdin);
rewind(f);
print_only(f);
rewind(f);
ninc(f, min, max, numerical, ' ');
for (int i = 0; i < strlen(del); i++) {
rewind(f);
ninc(f, min, max, numerical, del[i]);
}
exit(EXIT_SUCCESS);
}