-
Notifications
You must be signed in to change notification settings - Fork 0
/
eolfix.c
3001 lines (2550 loc) · 70.5 KB
/
eolfix.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eolfix - Report & fix end-of-line characters
Copyright (c) 2002-2015 Ross Smith II
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "compiler.h"
#include <stdio.h> /* fprintf() */
#include <assert.h> /* assert() */
#include <ctype.h> /* iscntrl() */
#include <errno.h> /* errno() */
#include <limits.h> /* PATH_MAX */
#include <signal.h> /* signal() */
#include <stdarg.h> /* vfprintf() */
#include <sys/stat.h> /* stat() */
#include <sys/types.h> /* stat() */
#ifdef HAVE_CONIO_H
#include <conio.h> /* getch() */
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h> /* opendir() (on Watcom) */
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h> /* opendir() */
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h> /* O_RDWR, etc. */
#endif
#ifdef HAVE_FNMATCH_H
#ifdef _POSIX_SOURCE
#define _POSIX_SOURCE_SAVE _POSIX_SOURCE
#undef _POSIX_SOURCE
#endif
#include <fnmatch.h> /* fnmatch() */
#ifdef _POSIX_SOURCE_SAVE
#define _POSIX_SOURCE _POSIX_SOURCE_SAVE
#undef _POSIX_SOURCE_SAVE
#endif
#else
#include "fnmatch.h" /* local version */
#endif
#ifdef HAVE_GETOPT_LONG
#include <getopt.h> /* getopt() */
#else
#include "getopt.h" /* local version */
#endif
#ifdef HAVE_IO_H
#include <io.h> /* open() */
#endif
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else /* !HAVE_STDBOOL_H */
#ifndef HAVE__BOOL
#ifdef __cplusplus
typedef bool _Bool;
#else
typedef unsigned char _Bool;
#endif
#endif
#define bool _Bool
#define false 0
#define true 1
#define __bool_true_false_are_defined 1
#endif /* HAVE_STDBOOL_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* NULL, EXIT_SUCCESS */
#else
extern void exit ();
#endif
#ifdef HAVE_STRING_H
#include <string.h> /* strlen */
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h> /* strlen */
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h> /* O_RDWR, etc. */
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h> /* struct timeval */
#endif
#ifdef HAVE_SYS_UTIME_H
#include <sys/utime.h> /* utime() */
#endif
#ifdef HAVE_TIME_H
#include <time.h> /* struct timeval */
#endif
#ifdef HAVE_UTIME_H
#include <utime.h> /* utime() */
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* open() */
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h> /* GetSystemTimeAsFileTime(), OutputDebugString() */
#endif
#ifndef longlong_t
#if defined(SIZEOF_LONG) && (SIZEOF_LONG >= 8)
#define longlong_t long
#elif (defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG >= 8)) || defined(__GNUC__)
#define longlong_t long long
#else
#define longlong_t long
#endif
#endif /* longlong_t */
#define ulonglong_t unsigned longlong_t
#ifndef BUFSIZ
#define BUFSIZ 512
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef PATH_MAX
#ifdef _MAX_PATH
#define PATH_MAX _MAX_PATH
#else
#ifdef FILENAME_MAX
#define PATH_MAX FILENAME_MAX
#else
#define PATH_MAX 260
#endif
#endif
#endif
#ifndef HAVE_SSIZE_T
#define ssize_t int
#endif
#ifndef S_IRUSR
#ifdef S_IREAD
#define S_IRUSR S_IREAD
#endif
#endif
#ifndef S_IWUSR
#ifdef S_IWRITE
#define S_IWUSR S_IWRITE
#endif
#endif
#define COPYRIGHT "Copyright (c) 2002-2015, Ross Smith II. MIT Licensed."
#undef CONV_SUPPORT_ENABLED
/* unix EOF marker */
#define CTRL_D '\x04'
/* Line feed character */
#define CTRL_J '\x0a'
/* Carriage return character */
#define CTRL_M '\x0d'
/* dos EOF marker */
#define CTRL_Z '\x1a'
#define EOLFIX_ENVVAR "EOLFIX"
#define TEMP_FILE_TEMPLATE "/eoXXXXXXX.tmp"
#define TEMP_FILE_TEMPLATE_SUFFIX_LEN 4
#define UNIMPLEMENTED_OPTION "Unimplemented option"
#define PROCESSING_OPTION "Processing option"
static char *progname;
/* input file format */
enum input_e
{
INPUT_NONE = 0,
INPUT_UNIX = 1,
INPUT_DOS = 2,
INPUT_MAC = 4,
INPUT_VARIED = 8,
INPUT_BINARY = 16,
INPUT_UNSET = 32
};
typedef enum input_e input_t;
#define INPUT_ALL_ASCII ((input_t) (INPUT_UNIX | INPUT_DOS | INPUT_MAC | INPUT_VARIED))
#define INPUT_ALL ((input_t) (INPUT_ALL_ASCII | INPUT_BINARY))
/* output file format */
enum output_e
{
OUTPUT_LEAVE = 0,
OUTPUT_UNIX = 1,
OUTPUT_DOS = 2,
OUTPUT_MAC = 4,
OUTPUT_SKIP = 32
};
typedef enum output_e output_t;
#ifdef __DOS__
#define NATIVE "DOS/Windows"
#define OUTPUT_NATIVE OUTPUT_DOS
#elif defined(__MAC__)
/* how to determine? CodeWarrior compiler? */
#define NATIVE "Macintosh"
#define OUTPUT_NATIVE OUTPUT_MAC
#else
#define NATIVE "Unix"
#define OUTPUT_NATIVE OUTPUT_UNIX
#endif
#define T_UNCHANGED "unchanged"
#define T_UNIX "unix"
#define T_DOS "dos"
#define T_MAC "mac"
#define T_VARIED "varied"
#define T_NONE "none"
#define T_BINARY "binary"
#define T_ALL "all"
static char *input_desc[] = {
T_NONE, /* no cr/lf's found */
T_UNIX, /* unix */
T_DOS, /* dos */
T_VARIED, /* dos & unix */
T_MAC, /* mac */
T_VARIED, /* mac & unix */
T_VARIED, /* mac & dos */
T_VARIED /* mac & dos & unix */
};
static char *input_descv[] = {
T_NONE, /* no cr/lf's found */
T_UNIX, /* unix */
T_DOS, /* dos */
T_DOS "/" T_UNIX, /* dos & unix */
T_MAC, /* mac */
T_MAC "/" T_UNIX, /* mac & unix */
T_DOS "/" T_MAC, /* mac & dos */
T_DOS "/" T_MAC "/" T_UNIX /* mac & dos & unix */
};
static char *output_desc[] = {
T_UNCHANGED, /* OUTPUT_LEAVE (0) */
T_UNIX, /* OUTPUT_UNIX (1) */
T_DOS, /* OUTPUT_DOS (2) */
NULL, /* not used */
T_MAC /* OUTPUT_MAC (4) */
};
struct _options
{
bool abort;
bool backup;
char bak_ext[PATH_MAX];
bool compress;
bool dry_run;
bool force;
int include; /* input_t */
bool in_place;
char output_filename[PATH_MAX];
output_t output_format;
bool preserve;
bool quiet;
bool recursive;
char temp_dir[PATH_MAX];
int verbose;
bool ignore_case;
bool std_in;
bool std_out;
bool wildcards_used;
#ifdef CONV_SUPPORT_ENABLED
bool conv_mode;
bool _auto;
#endif /* CONV_SUPPORT_ENABLED */
};
typedef struct _options options_t;
#define BACKUP_EXT ".bak"
#ifdef IS_CASE_INSENSITIVE_FILESYSTEM
#define IGNORE_CASE (true)
#else
#define IGNORE_CASE (false)
#endif
static options_t opt = {
false, /* abort */
false, /* backup */
BACKUP_EXT, /* backup-ext */
false, /* compress */
false, /* dry_run */
false, /* force */
INPUT_UNSET, /* include */
false, /* in_place */
"", /* output_filename */
OUTPUT_LEAVE, /* output_format */
false, /* preserve */
false, /* quiet */
false, /* recursive */
"", /* temp_dir */
1, /* verbose */
IGNORE_CASE, /* ignore_case */
false, /* stdin */
false, /* stdout */
false, /* wildcard_found */
#ifdef CONV_SUPPORT_ENABLED
, false, /* _auto */
false /* conv_mode */
#endif /* CONV_SUPPORT_ENABLED */
};
static char *input_filename;
static char temp_filename[PATH_MAX] = "";
static int output_handle = -1;
static int exit_value = 0;
#ifdef CONV_SUPPORT_ENABLED
#define CONV_MODE_SWITCHES "DMU"
#define FORCE_OPTION "force-rewrite"
#define FORCEOPTION "forcerewrite"
#define NO_FORCE_OPTION "no-force-rewrite"
#define NOFORCEOPTION "noforcerewrite"
#else
#define CONV_MODE_SWITCHES
#define FORCE_OPTION "force"
#define FORCEOPTION "force"
#define NO_FORCE_OPTION "no-force"
#define NOFORCEOPTION "noforce"
#endif /* CONV_SUPPORT_ENABLED */
#define UNDOCUMENTED_SWITCHES "wRh"
#define NEGATE_SWITCHES "ABCFJKOPQRTVYZ"
static char *short_options =
"abcde:fg:i:jk:lmno:pqrst:uvyz?-" NEGATE_SWITCHES UNDOCUMENTED_SWITCHES
CONV_MODE_SWITCHES;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with UCHAR_MAX + 1. */
enum opt_e
{
OPT_LICENSE = UCHAR_MAX + 1,
OPT_VERSION
#ifdef CONV_SUPPORT_ENABLED
, OPT_AUTO,
OPT_FORCE,
OPT_SAFE,
OPT_NO_AUTO,
OPT_NO_FORCE
#endif /* CONV_SUPPORT_ENABLED */
};
static struct option long_options[] = {
{"abort", no_argument, 0, 'a'},
{"backup", no_argument, 0, 'b'},
{"backup-ext", required_argument, 0, 'k'},
{"compress", no_argument, 0, 'c'},
{"config", required_argument, 0, 'g'},
{"dos", no_argument, 0, 'd'},
{"dry-run", no_argument, 0, 'y'},
{"exclude", required_argument, 0, 'e'},
{FORCE_OPTION, no_argument, 0, 'f'},
{"help", no_argument, 0, '?'},
{"ignore-case", no_argument, 0, 'z'},
{"include", required_argument, 0, 'i'},
{"in-place", no_argument, 0, 'j'},
{"leave", no_argument, 0, 'l'},
{"license", no_argument, 0, OPT_LICENSE},
{"mac", no_argument, 0, 'm'},
{"native", no_argument, 0, 'n'},
{"output", required_argument, 0, 'o'},
{"preserve", no_argument, 0, 'p'},
{"quiet", no_argument, 0, 'q'},
{"recursive", no_argument, 0, 'r'},
{"skip", no_argument, 0, 's'},
{"temp-dir", required_argument, 0, 't'},
{"unix", no_argument, 0, 'u'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, OPT_VERSION},
/* negate options */
{"no-abort", no_argument, 0, 'A'},
{"no-backup", no_argument, 0, 'B'},
{"no-backup-ext", no_argument, 0, 'K'},
{"no-compress", no_argument, 0, 'C'},
{"no-dry-run", no_argument, 0, 'Y'},
{NO_FORCE_OPTION, no_argument, 0, 'F'},
{"no-ignore-case", no_argument, 0, 'Z'},
{"no-in-place", no_argument, 0, 'J'},
{"no-output", no_argument, 0, 'O'},
{"no-preserve", no_argument, 0, 'P'},
{"no-quiet", no_argument, 0, 'Q'},
{"no-recursive", no_argument, 0, 'R'},
{"no-temp-dir", no_argument, 0, 'T'},
{"no-tmp-dir", no_argument, 0, 'T'},
{"no-verbose", no_argument, 0, 'V'},
/* undocumented alternatives */
/* abort */
{"abort-on-error", no_argument, 0, 'a'},
/* backup-ext */
{"backupext", required_argument, 0, 'k'},
/* dos */
{"crlf", no_argument, 0, 'd'},
{"msdos", no_argument, 0, 'd'},
{"u2d", no_argument, 0, 'd'},
{"unix2dos", no_argument, 0, 'd'},
{"m2d", no_argument, 0, 'd'},
{"mac2dos", no_argument, 0, 'd'},
{"win", no_argument, 0, 'd'},
{"windows", no_argument, 0, 'd'},
/* dry-run */
{"dryrun", no_argument, 0, 'y'},
/* force */
{FORCEOPTION, no_argument, 0, 'f'},
/* help */
{"help", no_argument, 0, '?'},
{"usage", no_argument, 0, '?'},
/* ignore-case */
{"ignorecase", no_argument, 0, 'Z'},
/* in-place */
{"inplace", no_argument, 0, 'j'},
/* leave */
{"leave-alone", no_argument, 0, 'l'},
/* license */
{"licence", no_argument, 0, OPT_LICENSE},
/* mac */
{"cr", no_argument, 0, 'm'},
{"macintosh", no_argument, 0, 'm'},
{"d2m", no_argument, 0, 'u'},
{"dos2mac", no_argument, 0, 'u'},
{"u2m", no_argument, 0, 'u'},
{"unix2mac", no_argument, 0, 'u'},
/* no-change */
{"nochange", no_argument, 0, 'n'},
/* recursive */
{"recurse", no_argument, 0, 'r'},
/* temp-dir */
{"tempdir", required_argument, 0, 't'},
{"tmp-dir", required_argument, 0, 't'},
{"tmpdir", required_argument, 0, 't'},
/* unix */
{"lf", no_argument, 0, 'u'},
{"nl", no_argument, 0, 'u'},
{"d2u", no_argument, 0, 'u'},
{"dos2unix", no_argument, 0, 'u'},
{"m2u", no_argument, 0, 'u'},
{"mac2unix", no_argument, 0, 'u'},
/* negate options */
{"noabort", no_argument, 0, 'A'},
{"nobackup", no_argument, 0, 'B'},
{"nobackupext", no_argument, 0, 'K'},
{"nocaseinsensitive", no_argument, 0, 'Z'},
{"nodryrun", no_argument, 0, 'Y'},
{NOFORCEOPTION, no_argument, 0, 'F'},
{"noignorecase", no_argument, 0, 'Z'},
{"noinplace", no_argument, 0, 'J'},
{"nooutput", no_argument, 0, 'O'},
{"nopreserve", no_argument, 0, 'P'},
{"noquiet", no_argument, 0, 'Q'},
{"norecursive", no_argument, 0, 'R'},
{"notempdir", no_argument, 0, 'T'},
{"notmpdir", no_argument, 0, 'T'},
{"noverbose", no_argument, 0, 'V'}
#ifdef CONV_SUPPORT_ENABLED
, {"auto", no_argument, 0, OPT_AUTO},
{"dos", no_argument, 0, 'D'},
{"force", no_argument, 0, OPT_FORCE},
{"mac", no_argument, 0, 'M'},
{"safe", no_argument, 0, OPT_SAFE},
{"unix", no_argument, 0, 'U'},
{"no-auto", no_argument, 0, OPT_NO_AUTO},
{"no-force", no_argument, 0, OPT_NO_FORCE},
{"noauto", no_argument, 0, OPT_NO_AUTO},
{"noforce", no_argument, 0, OPT_NO_FORCE}
#endif /* CONV_SUPPORT_ENABLED */
};
#ifdef CONV_SUPPORT_ENABLED
#define CONV_USAGE \
"-A | --auto Convert to UNIX if DOS, Mac, or varied, or DOS if UNIX\n" \
"-D | --u2d [files] Convert text files to have DOS/Windows line endings\n" \
" --unix2dos [files] Convert text files to have DOS/Windows line endings\n" \
"-U | --d2u [files] Convert text files to have UNIX line endings\n" \
" --dos2unix [files] Convert text files to have UNIX line endings\n" \
" --force Force conversion of binary files\n" \
" --safe Do not convert binary files (default)\n"
#else
#define CONV_USAGE
#endif /* CONV_SUPPORT_ENABLED */
/****************************************************************************/
static
#if !defined(_DEBUG) && !defined(__LCC__)
/* Workaround for LCC's: Error eolfix.c: 2168 operands of = have illegal types 'pointer to char' and 'int' */
inline
#endif
void
_DBG (char *fmt, ...)
{
#ifdef _DEBUG
static char buffer[65535];
va_list args;
va_start (args, fmt);
vsprintf (buffer, fmt, args);
strcat (buffer, "\n");
#if defined(__WINDOWS__)
OutputDebugString (buffer);
#endif
fprintf (stdout, "%s", buffer);
fflush (stdout);
va_end (args);
#endif
}
/****************************************************************************/
static void
usage (int exit_value)
{
printf ("Usage: %s [options] [files...]\n" "Options:\n", progname);
#ifdef CONV_SUPPORT_ENABLED
if (opt.conv_mode)
{
printf (CONV_USAGE);
printf ("\n");
}
#endif /* CONV_SUPPORT_ENABLED */
/*2345678901234567890123456789012345678901234567890123456789012345678901234567890*/
printf
("-u | --unix [files] Convert text files to have UNIX line endings\n"
"-d | --dos [files] Convert text files to have DOS/Windows line endings\n"
"-m | --mac [files] Convert text files to have Macintosh line endings\n"
"-n | --native [files] Convert text files to have %s line endings\n"
"-l | --leave [files] Report on the type of file, but do not convert (default)\n"
"-s | --skip [files] Skip these files entirely\n"
"-i | --include type Include types: unix, dos, mac, varied, binary and all\n"
" Default is to include unix, dos, mac and varied files\n"
"-e | --exclude type Exclude types: unix, dos, mac, varied, binary and all\n"
"-a | --abort Abort processing on the first error encountered\n"
"-b | --backup Backup each file before converting it\n"
"-k | --backup-ext .ext Use <.ext> for backup file extension (default is %s)\n"
"-c | --compress Compress doubled-spaced files to be single-spaced (todo)\n"
"-g | --config file Process options in <file>\n"
"-y | --dry-run Skip conversion, but show what would have been converted\n"
"-f | --%-15s Convert every file, even if no change would result\n"
"-z | --ignore-case Perform case-insensitive filename matching\n"
"-j | --in-place Convert text files in-place (slower)\n"
"-o | --output file Send output to <file> (use - for stdout)\n"
"-p | --preserve Preserve file ownership and times\n"
"-r | --recursive Recurse into directories\n"
"-t | --temp-dir dir Use <dir> for temporary files (default is file's dir)\n",
NATIVE, opt.bak_ext, FORCE_OPTION);
#ifdef HAVE_GETCH
if (isatty (fileno (stdin)) && isatty (fileno (stdout)))
{
printf ("Press any key: ");
while (!getch ())
;
printf ("\r");
}
#endif
printf
("-v | --verbose Report on every file, even if it would not be changed\n"
"-q | --quiet Only report errors\n"
"-? | --help Show this help message and quit\n"
" --version Display version information and quit\n"
" --license Display licensing information\n"
" -- Stop processing options\n");
#ifdef CONV_SUPPORT_ENABLED
if (!opt.conv_mode)
{
printf ("\n");
printf (CONV_USAGE);
}
#endif /* CONV_SUPPORT_ENABLED */
exit (exit_value);
}
/****************************************************************************/
#ifndef HAVE_BASENAME
/****************************************************************************/
/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+basename */
static char *
basename (char *s)
{
char *rv;
if (!s || !*s)
return ".";
rv = s + strlen (s) - 1;
do
{
if (IS_PATH_SEPARATOR (*rv))
return rv + 1;
--rv;
}
while (rv >= s);
return s;
}
#define HAVE_BASENAME 1
#endif /* ! HAVE_BASENAME */
/****************************************************************************/
#ifndef HAVE_DIRNAME
/****************************************************************************/
/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+dirname */
static char *
dirname (char *path)
{
static char rv[PATH_MAX];
char *p;
unsigned int i = 0;
if (path == NULL || *path == '\0')
return ".";
i = strlen (path) - 1;
p = path + i;
while (IS_PATH_SEPARATOR (*p))
{
if (p == path)
return path;
*p-- = '\0';
--i;
}
while (p >= path && !IS_PATH_SEPARATOR (*p))
{
--p;
--i;
}
if (p < path)
{
return ".";
}
if (p == path)
{
return "/";
}
if (i > PATH_MAX - 1)
{
return "";
}
strncpy (rv, path, i);
rv[i] = '\0';
return rv;
/*
int i;
static char rv[PATH_MAX];
int at_end = 1;
if (s) {
for (i = strlen(s) - 1; i >= 0; --i) {
if (IS_PATH_SEPARATOR(s[i])) {
if (!at_end) {
if (i > 0) {
if (i > PATH_MAX - 1)
return NULL;
strncpy(rv, s, i);
}
rv[i] = '\0';
return rv;
}
} else
at_end = 0;
}
}
return ".";
*/
}
#define HAVE_DIRNAME 1
#endif /* ! HAVE_DIRNAME */
/****************************************************************************/
#ifndef HAVE_GETTIMEOFDAY
/****************************************************************************/
#if !defined(HAVE_STRUCT_TIMEVAL) && defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__)
#include <winsock.h> /* struct timeval */
#endif
#define MAKE_ULONGLONG(hi, lo) ((((ulonglong_t) hi) << 32) + ((ulonglong_t) lo))
static int
gettimeofday (struct timeval *tv, void *timezone)
{
/*
FILETIME time;
double timed;
GetSystemTimeAsFileTime(&time);
timed = ((time.dwHighDateTime * 4294967296e-7) - 11644473600.0) + (time.dwLowDateTime * 1e-7);
tv->tv_sec = (long) timed;
tv->tv_usec = (long) ((timed - tv->tv_sec) * 1e6);
*/
SYSTEMTIME nowst;
FILETIME nowft;
static ulonglong_t then = 0;
ulonglong_t now;
ulonglong_t diff;
if (!then)
{
SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; /* 00:00:00 Jan 1st 1970 */
FILETIME thenft;
SystemTimeToFileTime (&thenst, &thenft);
then = MAKE_ULONGLONG (thenft.dwHighDateTime, thenft.dwLowDateTime);
}
GetLocalTime (&nowst);
SystemTimeToFileTime (&nowst, &nowft);
now = MAKE_ULONGLONG (nowft.dwHighDateTime, nowft.dwLowDateTime);
diff = now - then; /* time from 00:00:00 Jan 1st 1970 to now in 100 nanos */
/* 100 nanos / 10 = 1 micro / 1000 = 1 milli / 1000 = 1 second */
tv->tv_sec = (long) (diff / 10000000);
diff %= 10000000;
tv->tv_usec = (long) (diff * 100);
return 0;
}
#define HAVE_GETTIMEOFDAY 1
#endif /* ! HAVE_GETTIMEOFDAY */
/****************************************************************************/
#ifndef HAVE_MKSTEMPS
/****************************************************************************/
#ifdef HAVE_PROCESS_H
#include <process.h> /* getpid() */
#endif /* HAVE_PROCESS_H */
#ifndef TMP_MAX
#define TMP_MAX 32767
#endif
static int
mkstemps (char *templat, int suffix_len)
{
static const char letters[] =
#ifndef IS_CASE_INSENSITIVE_FILESYSTEM
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#endif
"abcdefghijklmnopqrstuvwxyz0123456789";
static ulonglong_t value;
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
char *XXXXXX;
size_t len;
int count;
len = strlen (templat);
if ((int) len < 6 + suffix_len
|| strncmp (&templat[len - 6 - suffix_len], "XXXXXX", 6))
return -1;
XXXXXX = &templat[len - 6 - suffix_len];
#ifdef HAVE_GETTIMEOFDAY
gettimeofday (&tv, NULL);
value += ((ulonglong_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
#else
value += getpid ();
#endif
for (count = 0; count < TMP_MAX; ++count)
{
ulonglong_t v = value;
int fd;
/* Fill in the random bits. */
XXXXXX[0] = letters[(unsigned int) (v % sizeof (letters))];
v /= sizeof (letters);
XXXXXX[1] = letters[(unsigned int) (v % sizeof (letters))];
v /= sizeof (letters);
XXXXXX[2] = letters[(unsigned int) (v % sizeof (letters))];
v /= sizeof (letters);
XXXXXX[3] = letters[(unsigned int) (v % sizeof (letters))];
v /= sizeof (letters);
XXXXXX[4] = letters[(unsigned int) (v % sizeof (letters))];
v /= sizeof (letters);
XXXXXX[5] = letters[(unsigned int) (v % sizeof (letters))];
fd = open (templat, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
if (fd >= 0)
return fd;
value += 7777;
}
templat[0] = '\0';
return -1;
}
#define HAVE_MKSTEMPS 1
#endif /* ! HAVE MKSTEMPS */
/****************************************************************************/
#ifndef HAVE_OPENDIR
/****************************************************************************/
struct dirent
{
char *d_name;
};
struct _dir
{
long findfirst_handle;
struct _finddata_t *finddata;
struct dirent *direntry;
struct dirent *firstentry;
};
typedef struct _dir DIR;
static void
dh_free (DIR * dh)
{
free (dh->finddata);
free (dh->direntry);
free (dh);
dh = NULL;
}
/* http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+opendir */
static DIR *
opendir (const char *dir)
{
char path[PATH_MAX];
DIR *dh;
struct stat statbuf;
if (!dir)
{
errno = EFAULT;
return (DIR *) NULL;
}
if (!*dir)
{
errno = ENOTDIR;
return (DIR *) NULL;
}
if (stat (dir, &statbuf) == -1)
return (DIR *) NULL;
if (!(statbuf.st_mode & S_IFDIR))
{
errno = ENOTDIR;
return (DIR *) NULL;
}
dh = (DIR *) malloc (sizeof (DIR));
if (!dh)
{
errno = ENOMEM;
return (DIR *) NULL;
}
dh->finddata = (struct _finddata_t *) malloc (sizeof (struct _finddata_t));
if (!dh->finddata)
{
free (dh);
errno = ENOMEM;
return (DIR *) NULL;
}
dh->direntry = (struct dirent *) malloc (sizeof (struct dirent));
if (!dh->direntry)
{
free (dh);
free (dh->finddata);
errno = ENOMEM;
return (DIR *) NULL;
}
if (strlen (dir) + 2 > sizeof (path) - 1)
{
dh_free (dh);
errno = ENAMETOOLONG;
return (DIR *) NULL;
}
sprintf (path, "%s/%s", dir, "*");
dh->findfirst_handle = _findfirst (path, dh->finddata);
if (dh->findfirst_handle == -1)
{
errno = ENOENT;
dh_free (dh);
return (DIR *) NULL;
}
dh->firstentry = dh->direntry;
dh->direntry->d_name = dh->finddata->name;
return dh;
}
/* http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+readdir */
static struct dirent *
readdir (DIR * dh)