-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrepla.c
300 lines (279 loc) · 7.2 KB
/
grepla.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#define L_strtmp 2048
int main(int, char**);
int chkistext(void);
int cerca(void);
void sostituisci(void);
int apri(void);
int canreplace(char *,char *);
char *strnstr(char *,char *,int);
void cambia(char *);
void chiudi(void);
int opzioni(int, char**);
void heading(void);
int queries = 0, ignorecase = 0, showlines = 0, beginofline = 0, endofline = 0;
int noreplace = 0, redo = 0;
int tabstops = 8;
char _ftmpname[L_tmpnam+1];
char *fname,*ftmpname;
FILE *ftosearch,*ftemp;
struct stat finfo;
char *strnone = "";
char *tosearch, *toreplace;
int ltosearch,ltoreplace;
char *strtmp;
long linee;
void heading(void)
{
printf("GREP & REPLACE for TEXT files\n");
}
int opzioni(argc,argv)
int argc;
char **argv;
{
int i,l,c,help = 0;
char *s;
argc--;
argv = &argv[1];
c = 0;
fname = strnone;
tosearch = strnone;
toreplace = strnone;
for (i = 0; i < argc; i++) {
s = argv[i];
l = strlen(s);
if ((s[0] == '-') && (s[1] != '-')) {
while (l-- > 0) {
switch (s[l]) {
case 'b': beginofline = !0; break;
case 'e': endofline = !0; break;
case 'i': ignorecase = !0; break;
case 'q': queries = !0; break;
case 's': showlines = !0; break;
case 'h':
printf("Usage: grepla -bihqs <filename> <tosearch> <toreplace>\n\n");
printf("Options:\n");
printf(" -b Replace at Beginning of line (can't use with -e option)\n");
printf(" -e Replace at End of line (can't use with -b option)\n");
printf(" -i Ignore Case (Default = Case Sensitive)\n");
printf(" -q Query user before replace (Default = Replace all)\n");
printf(" -s Show lines while changing (Default = No Screen Output)\n");
help = !0;
break;
}
}
} else if (l > 0) {
if (s[0] == '-') {
s = &s[1]; l--;
}
switch (++c) {
case 1: fname = s; break;
case 2: tosearch = s; ltosearch = l; break;
case 3: toreplace = s; ltoreplace = l; break;
}
}
}
if (beginofline && endofline) {
printf("Command-line error: You can't use -b and -e option togheter!\n");
c = 0;
}
if ((c < 3) && !help) {
printf("(Use -h for help)...\n");
}
return ((c >= 3) && !help);
}
int apri(void)
{
int apriOK,e = 0;
ftosearch = fopen(fname,"r");
apriOK = (ftosearch != NULL);
if (apriOK) {
apriOK = (stat(fname,&finfo) == 0);
} else if (!e) {
printf("Error: can't open '%s'.\n",fname);
e = !0;
}
if (apriOK) {
ftmpname = tmpnam(NULL);
apriOK = (ftmpname != NULL);
} else if (!e) {
printf("Error: '%s': can't get file informations.\n",fname);
e = !0;
}
if (apriOK) {
ftemp = fopen(ftmpname,"w");
apriOK = (ftemp != NULL);
if (apriOK) {
strtmp = calloc(L_strtmp+1,sizeof(char));
apriOK = (strtmp != NULL);
if (!apriOK) {
printf("Error: Memory allocation request fault.\n");
remove(ftmpname);
}
} else if (!e) {
printf("Error: can't create temporary file named '%s'.\n",ftmpname);
}
} else if (!e) {
printf("Error: can't create temporary file name.\n");
}
return apriOK;
}
int canreplace(str,s)
char *str,*s;
{
int c,i = strlen(str) - strlen(s),can;
if (noreplace) {
can = 0;
} else if (queries) {
printf("%07ld:%s",linee,str);
for (c = 0; c < i; c++) {
if (str[c] == '\t') i += tabstops-1;
}
i += 8;
while (i-- > 0) putchar(' ');
i = ltosearch; while (i-- > 0) putchar('^');
printf("\nReplace (Yes/No/All/Stop replacing/Redo from start) ? Y\b");
c = toupper(getchar()); fflush(stdin);
can = 0;
switch (c) {
case 'A': queries = 0;
case '\n':;
case 'Y': can = !0; break;
case 'S': queries = 0; noreplace = !0; break;
case 'R': redo = !0; noreplace = !0; break;
}
} else {
can = !0;
}
return can;
}
char *strnstr(s1,s2,ls2)
char *s1,*s2;
int ls2;
{
int c,i,j,l = strlen(s1);
l = (beginofline ? (l>0 ? 1 : 0) : (l - ls2 + 1));
i = ((endofline && (l>1)) ? l-2 : 0);
c = 0;
while (i < l) {
if (ignorecase) {
for (j = 0, c = !0; (j < ls2) && c; j++) {
c = (toupper(s1[i+j]) == toupper(s2[j]));
}
} else {
c = !strncmp(&s1[i],s2,ls2);
}
if (c) break;
i++;
}
return (c ? &s1[i] : NULL);
}
void cambia(str)
char *str;
{
char *s,*spark;
s = noreplace ? strnone : str;
while ((s = strnstr(s,tosearch,ltosearch)) != NULL) {
spark = calloc(strlen(s)-ltosearch+1,sizeof(char));
if (spark == NULL) {
printf("WARNING: Memory allocation error.\n");
s = &s[ltosearch];
} else if (canreplace(str,s)) {
strcpy(spark,&s[ltosearch]);
strcpy(s,toreplace);
strcat(s,spark);
free(spark);
s = &s[ltoreplace];
} else {
s = &s[1];
}
}
}
int chkistext(void)
{
int isOK = !0,c;
struct stat finfo;
FILE *f = fopen(fname,"rb");
if (f != NULL) {
stat(fname,&finfo);
while (ftell(f) < finfo.st_size) {
if (fgetc(f) == 0) {
isOK = 0;
break;
}
}
fclose(f);
if (!isOK) {
printf("\b\bWARNING!: '%s'\n");
printf("This file doesn't appear to be a text file.\n");
printf("Do You want to continue anyway ? Y\b");
c = toupper(getchar()); fflush(stdin);
isOK = (c == 'Y') || (c == '\n');
if (!isOK) {
printf("(GREPLA interrupted by user)...\n");
}
}
}
return isOK;
}
void chiudi(void)
{
size_t blocksize = 2048,r;
FILE *ff,*ft;
char *buf;
free(strtmp);
fclose(ftosearch);
fclose(ftemp);
remove(fname);
buf = calloc(blocksize,sizeof(char));
ff = fopen(ftmpname,"rb");
ft = fopen(fname,"wb");
if ((ff != NULL) && (ft != NULL) && (buf != NULL)) {
do {
r = fread(buf,sizeof(char),blocksize,ff);
fwrite(buf,sizeof(char),r,ft);
} while (!feof(ff));
fclose(ff);
fclose(ft);
chmod(fname,finfo.st_mode);
} else {
printf("File open error! - See in:\n %s\nfor changed file.\n",ftmpname);
}
}
int main(argc,argv)
int argc;
char **argv;
{
int mainOK;
heading();
mainOK = opzioni(argc,argv);
if (mainOK) {
mainOK = apri();
}
if (mainOK) {
mainOK = chkistext();
}
if (mainOK) {
linee = 0;
while (ftell(ftosearch) < finfo.st_size) {
fgets(strtmp,L_strtmp,ftosearch); ++linee;
cambia(strtmp);
if (showlines) printf("%s",strtmp);
fputs(strtmp,ftemp);
if (redo) {
rewind(ftosearch);
fclose(ftemp);
ftemp = fopen(ftmpname,"w");
redo = 0; noreplace = 0; linee = 0;
}
}
printf("GREPLA: Ok.\n");
chiudi();
}
return !mainOK;
}