-
Notifications
You must be signed in to change notification settings - Fork 0
/
eol.c
642 lines (554 loc) · 20 KB
/
eol.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* eol.c - Set or scan the end-of-line characters in a file. */
/* C language version. */
/* ************************************************************************* */
/*
******************************************************************************
Greg Messer - 12/10/2004
Description:
This program will either set the end-of-line characters in a file to the
ones specified on the command line, or scan the file for end-of-line
characters and report which ones were found.
Setting the end-of-line characters:
The existing end-of-line characters from the input file are ignored and the
specified end-of-line characters are written to the output file.
When setting the end-of-line characters, the program writes its output to a
temporary file. When finished processing the input file, it renames the
temporary file to the same name as the input file. It does not make a
backup copy of the input file.
Scanning for end-of-line characters:
When scanning for end-of-line characters, the program does not alter the
input file. The program reports the total number of line ends found and
the number of line ends of each supported type found.
------------------------------------------------------------------------------
Usage:
eol [-?] [-v] [-d | -m | -u] [-s] [files]
Argument Result
--------------- ------------------------------------------------
-? or no args Produce a usage message
-v Produce verbose progress messages
-d Set DOS CR/LF end-of-line characters in files
-m Set Macintosh CR end-of-line character in files
-u Set UNIX LF end-of-line character in files
-s Scan and report end-of-line characters in files
files
Use the -s option to scan for end-of-line characters.
Scan does not change the end-of-line, it reads the files
and reports which end-of-line characters were found.
******************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
/* Parse the commandline. */
int parse_commandline(int argc, char *argv[]);
/* Set EOL characters. */
unsigned long set_eol();
/* Scan for EOL characters. */
unsigned long scan_eol();
/* Processes */
enum EOL_OPERATIONS {EOL_NO_OPERATION, EOL_SET_OPERATION, EOL_SCAN_OPERATION};
char *operation_description[] = {"Invalid operation",
"Set end-of-line characters",
"Scan for end-of-line characters"};
/* Output Formats */
enum EOL_OUTPUT_FORMATS {EOL_NO_OUTPUT_FORMAT, EOL_UNIX_OUTPUT_FORMAT, EOL_MSDOS_OUTPUT_FORMAT, EOL_MAC_OUTPUT_FORMAT};
char *output_format_description[] = {"Invalid output format",
"UNIX (LF)",
"MS-DOS (CR+LF)",
"Macintosh (CR)"};
/* Global Variables */
int operation = EOL_NO_OPERATION;
int output_format = EOL_NO_OUTPUT_FORMAT;
unsigned long cnt_eol;
unsigned long cnt_msdos;
unsigned long cnt_mac;
unsigned long cnt_unix;
double cnt_grand_total;
FILE *file_in = 0;
FILE *file_out = 0;
/*
------------------------------------------------------------------------------
main()
------------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
int i, result;
int err = 0;
int verbose = 0;
int argcnt = 1;
char *pgm = 0;
char *fname = 0;
char eol_fname[512];
char *eolfextension = ".EOL_TEMP_FILE"; /* extension of temporary file */
/* Set a pointer to the command name. */
pgm = argv[0];
/* Process command-line options. */
for(i = 1; i < argc; i++)
{
if(argv[i][0] == '-' && argv[i][1] != '\0')
{
argcnt++;
switch(argv[i][1])
{
case 'd':
case 'D':
/* MS-DOS */
operation = EOL_SET_OPERATION;
output_format = EOL_MSDOS_OUTPUT_FORMAT;
break;
case 'm':
case 'M':
/* Macintosh */
operation = EOL_SET_OPERATION;
output_format = EOL_MAC_OUTPUT_FORMAT;
break;
case 'u':
case 'U':
/* UNIX */
operation = EOL_SET_OPERATION;
output_format = EOL_UNIX_OUTPUT_FORMAT;
break;
case 's':
case 'S':
/* Scan for EOL */
operation = EOL_SCAN_OPERATION;
output_format = EOL_UNIX_OUTPUT_FORMAT;
break;
case 'v':
case 'V':
/* Verbose mode */
verbose++;
break;
default:
/* Treat all other options as an error. */
err++;
break;
}
}
}
/* End of for loop processing each commandline argument. */
/*
Show the usage message if:
an invalid command line option was found, or
no operation was set, or
the EOL_SET_OPERATION operation was set but no format was set.
*/
if(err || operation == EOL_NO_OPERATION || (operation == EOL_SET_OPERATION && output_format == EOL_NO_OUTPUT_FORMAT))
{
fprintf(stderr,
"\n"
"This program will either set the end-of-line characters\n"
"in files or scan for end-of-line characters in files.\n"
"\n"
"Usage: %s [-d | -m | -u] [-s] [-v] [-?] [files]\n"
"\n"
"Output format options:\n"
" -d set %s end-of-line characters,\n"
" -m set %s end-of-line characters,\n"
" -u set %s end-of-line characters.\n"
" If multiple formats are specified, the last one is used.\n"
"\n"
"Use -s to scan for end-of-line characters.\n"
" Scan does not change the end-of-line, it reads the files\n"
" and reports which end-of-line characters were found.\n"
"Use -v or -V to produce verbose messages.\n"
"\n",
pgm,
output_format_description[EOL_MSDOS_OUTPUT_FORMAT],
output_format_description[EOL_MAC_OUTPUT_FORMAT],
output_format_description[EOL_UNIX_OUTPUT_FORMAT]);
return 1;
}
/* Show the operation for this execution of the program. */
if (verbose)
{
fprintf(stderr, "\nOperation: %s.\n",
operation_description[operation]);
}
cnt_grand_total = 0L;
/* If no files were specified on the command line, use stdin. */
if (argcnt == argc)
{
if(operation == EOL_SET_OPERATION)
{
if (verbose)
{
fprintf(stderr,
"\nstdin: Setting %s end-of-line characters.\n",
output_format_description[output_format]);
}
file_in = stdin;
file_out = stdout;
cnt_eol = set_eol();
if (verbose)
{
fprintf(stderr, "stdin: Processed %lu line ends.\n", cnt_eol);
}
}
else if(operation == EOL_SCAN_OPERATION)
{
if (verbose)
{
fprintf(stderr,
"\nstdin: Scanning for end-of-line characters.\n");
}
cnt_msdos = 0L;
cnt_mac = 0L;
cnt_unix = 0L;
file_in = stdin;
cnt_eol = scan_eol();
cnt_grand_total += cnt_eol;
fprintf(stderr, "stdin: Found %lu total line ends.\n", cnt_eol);
if(cnt_msdos > 0L)
{
fprintf(stderr, "stdin: %lu %s line ends.\n",
cnt_msdos,
output_format_description[EOL_MSDOS_OUTPUT_FORMAT]);
}
if(cnt_mac > 0L)
{
fprintf(stderr, "stdin: %lu %s line ends.\n",
cnt_mac,
output_format_description[EOL_MAC_OUTPUT_FORMAT]);
}
if(cnt_unix > 0L)
{
fprintf(stderr, "stdin: %lu %s line ends.\n",
cnt_unix,
output_format_description[EOL_UNIX_OUTPUT_FORMAT]);
}
}
else
{
/* Bad operation - do nothing. */
}
return 0;
}
/* End if processing stdin with no files on commandline. */
/* Process end-of-line for each file given on the command line. */
for(i = 1; i < argc; i++)
{
/* ignore any args with leading '-', they are options */
if (argv[i][0] != '-')
{
/* Store the input filename. */
fname = argv[i];
/* Open the input file. */
file_in = fopen(fname, "rb");
if (file_in == NULL)
{
fprintf(stderr,
"Error: Cannot open input file %s.\n"
" Reason: %s.\n",
fname, strerror(errno));
err = 1;
continue;
}
if(operation == EOL_SET_OPERATION)
{
/* Create and store the temporary output filename. */
strcpy(eol_fname, fname);
strcat(eol_fname, eolfextension);
/* Open the output file. */
file_out = fopen(eol_fname, "wb");
if (file_out == NULL)
{
fprintf(stderr,
"Error: Cannot open temporary output file %s.\n"
" Reason: %s.\n",
eol_fname, strerror(errno));
err = 1;
continue;
}
}
}
if ((argv[i][0] == '-' && argv[i][1] == '\0') ||
(argv[i][0] != '-'))
{
if(operation == EOL_SET_OPERATION)
{
if (verbose)
{
fprintf(stderr,
"\n%s: Setting %s end-of-line characters.\n",
fname, output_format_description[output_format]);
}
cnt_eol = set_eol(file_in, file_out);
if (verbose)
{
fprintf(stderr,
"%s: Processed %lu line ends.\n",
fname, cnt_eol);
}
}
else if(operation == EOL_SCAN_OPERATION)
{
if (verbose)
{
fprintf(stderr,
"\n%s: Scanning for end-of-line characters.\n",
fname);
}
cnt_msdos = 0L;
cnt_mac = 0L;
cnt_unix = 0L;
cnt_eol = scan_eol(file_in);
cnt_grand_total += cnt_eol;
fprintf(stderr, "%s: Found %lu total line ends.\n",
fname, cnt_eol);
if(cnt_msdos > 0L)
{
fprintf(stderr, "%s: %lu %s line ends.\n",
fname,
cnt_msdos,
output_format_description[EOL_MSDOS_OUTPUT_FORMAT]);
}
if(cnt_mac > 0L)
{
fprintf(stderr, "%s: %lu %s line ends.\n",
fname,
cnt_mac,
output_format_description[EOL_MAC_OUTPUT_FORMAT]);
}
if(cnt_unix > 0L)
{
fprintf(stderr, "%s: %lu %s line ends.\n",
fname,
cnt_unix,
output_format_description[EOL_UNIX_OUTPUT_FORMAT]);
}
}
else
{
/* Bad operation - do nothing. */
}
/* Close files and rename output. */
if (file_in != stdin)
{
fclose(file_in);
if(operation == EOL_SET_OPERATION)
{
if (file_out != stdout)
{
fclose(file_out);
}
/* Initialize the result variable. */
result = 0;
#ifdef MS_WIN32_COMPILER
/*
The rename() function only works in MS VC++ if the new
filename doesn't already exist.
In VC++, must remove() the original file first, then
rename() the temporary file.
*/
if(result == 0)
result = remove(fname);
if(result != 0)
{
fprintf(stderr, "remove() return: %d\n", result);
fprintf(stderr,
"Error: Cannot remove original file %s.\n"
" Reason: %s\n",
fname, strerror(errno));
}
#endif /* MS_WIN32_COMPILER */
/*
The rename() function works in GNU C++, but the error
handling logic below does not work for GNU C++. The
rename() function in GNU C++ returns non-zero, indicating
that an error occurred, even though the file gets renamed
correctly.
*/
if(result == 0)
result = rename(eol_fname, fname);
#ifndef GNU_WIN32_COMPILER
/* Check the result of renaming the temporary file. */
if(result != 0)
{
fprintf(stderr, "rename() return: %d\n", result);
fprintf(stderr,
"Error: Cannot rename temporary output %s\n"
" to the original input name %s.\n"
" Reason: %s\n",
eol_fname, fname, strerror(errno));
}
#endif /* #ifndef GNU_WIN32_COMPILER */
}
}
}
}
/* End of for loop processing each commandline argument. */
if(cnt_grand_total > 0L)
{
fprintf(stderr, "Grand Total: %g line ends.\n",
cnt_grand_total);
}
/* Return the number of errors as the exit code to the OS. */
return err;
}
/*
------------------------------------------------------------------------------
set_eol() - Set EOL characters.
IN FMT OUT Eat
-- --- --- ---
CR EOL_MSDOS_OUTPUT_FORMAT CR + LF single LF after the CR, if any
CR EOL_MAC_OUTPUT_FORMAT CR single LF after the CR, if any
CR EOL_UNIX_OUTPUT_FORMAT LF single LF after the CR, if any
LF EOL_MSDOS_OUTPUT_FORMAT CR + LF nothing
LF EOL_MAC_OUTPUT_FORMAT CR nothing
LF EOL_UNIX_OUTPUT_FORMAT LF nothing
------------------------------------------------------------------------------
*/
unsigned long set_eol(FILE *file_in, FILE *file_out)
{
int ch;
const int ch_CR = '\r';
const int ch_LF = '\n';
unsigned long nl;
nl = 0L;
/* Read the file one character at a time. */
while((ch = getc(file_in)) != EOF)
{
/* Process the character. */
if(ch == ch_CR)
{
/* CR. Could be CR alone, or CR followed by LF. */
/* Count the line. */
nl++;
switch(output_format)
{
case EOL_MSDOS_OUTPUT_FORMAT:
putc(ch_CR, file_out);
putc(ch_LF, file_out);
break;
case EOL_MAC_OUTPUT_FORMAT:
putc(ch_CR, file_out);
break;
case EOL_UNIX_OUTPUT_FORMAT:
putc(ch_LF, file_out);
break;
default:
/* shouldn't happen - output the input character */
putc(ch, file_out);
break;
}
/* See if the character was CR alone, or CR followed by LF. */
/* Read the next character after the CR. */
ch = getc(file_in);
/* See what it is. Must eat a LF following a CR. */
if(ch == ch_LF)
{
/* LF after CR: Eat it. */
}
else if(ch == EOF)
{
/* EOF: Break from the read loop. Finished this file. */
break;
}
else
{
/* Other: Put it back so the next read loop will get it. */
ungetc(ch, file_in);
}
}
else if(ch == ch_LF)
{
/* LF. */
/* Count the line. */
nl++;
switch(output_format)
{
case EOL_MSDOS_OUTPUT_FORMAT:
putc(ch_CR, file_out);
putc(ch_LF, file_out);
break;
case EOL_MAC_OUTPUT_FORMAT:
putc(ch_CR, file_out);
break;
case EOL_UNIX_OUTPUT_FORMAT:
putc(ch_LF, file_out);
break;
default:
/* shouldn't happen - output the input character */
putc(ch, file_out);
break;
}
}
else
{
/* Regular character. Just write it. */
putc(ch, file_out);
}
}
/* End of while loop reading input file. */
/* Return the number of end-of-lines processed. */
return nl;
}
/*
------------------------------------------------------------------------------
scan_eol() - Scan for EOL characters.
IN FMT
------- ------------
CR + LF MS-DOS
CR Macintosh
LF UNIX
------------------------------------------------------------------------------
*/
unsigned long scan_eol(FILE *file_in)
{
int ch;
const int ch_CR = '\r';
const int ch_LF = '\n';
unsigned long nl;
nl = 0L;
/* Read the file one character at a time. */
while((ch = getc(file_in)) != EOF)
{
/* Process the character. */
if(ch == ch_CR)
{
/* CR. Could be CR alone, or CR followed by LF. */
/* Count the line. */
nl++;
/* See if the character was CR alone, or CR followed by LF. */
/* Read the next character after the CR. */
ch = getc(file_in);
/* See what it is. */
if(ch == ch_LF)
{
/* LF after CR: Count it as MS-DOS. */
cnt_msdos++;
}
else
{
/* No LF after CR: Count it as Macintosh. */
cnt_mac++;
}
if(ch == EOF)
{
/* EOF: Break from the read loop. Finished this file. */
break;
}
}
else if(ch == ch_LF)
{
/* LF. */
/* Count the line. */
nl++;
/* Count it as UNIX. */
cnt_unix++;
}
else
{
/* Regular character. */
}
}
/* End of while loop reading input file. */
/* Return the number of end-of-lines processed. */
return nl;
}
/* ************************************************************************* */
/* end of eol.c */