-
Notifications
You must be signed in to change notification settings - Fork 0
/
crlf.c
1075 lines (931 loc) · 25.8 KB
/
crlf.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
// CRLF.C - Copyright (c) 2003,2004 by Bob Frazier and S.F.T. Inc.
//
// Distribution and use of this source file and the compiled binary version
// are subject to the BSD-STYLE LICENSE (see below). Other portions of this
// distribution may be covered by the 'GNU General Public License', which is
// included in the 'COPYING' file and also available at the following internet
// address: http://www.gnu.org/copyleft/gpl.html
//
// Specifically, this file, the various text files (README, COPYING, ChangeLog),
// the manual pages (crlf.1 and crlf.interix), and the configuration scripts
// 'Makefile.am' and 'configure.in' are covered by this BSD-STYLE license.
// Other files may be covered by the GPL, such as the various files associated
// with the 'autotools' packages. For any file not specifically covered by the
// GPL, the 'BSD-STYLE LICENSE' applies.
//
// See the included file 'README' for additional information
// See the included file 'COPYING' for specific license information and a copy
// of the GNU PUBLIC LICENSE (GPL).
//
// BSD-STYLE LICENSE
//
// Copyright (c) 2003,2004, Bob Frazier and S.F.T. Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the original author, nor the names of its contri-
// butors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSE-
// QUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE
//
//
// The BSD-STYLE LICENSE is based on licensing guidelines as published at
// http://www.opensource.org/licenses
#ifdef WIN32
#include <windows.h>
#endif // WIN32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
// additional headers and definitions for UNIX
#include <fcntl.h>
#include <fnmatch.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <sys/stat.h>
typedef int HANDLE;
typedef const char *LPCSTR;
typedef unsigned long DWORD;
typedef short BOOL;
#ifndef NAME_MAX
#define NAME_MAX MAXNAMLEN
#endif // NAME_MAX
#define MAX_PATH MAXNAMLEN
#define TRUE (BOOL)(!0)
#define FALSE (BOOL)0
#endif // WIN32
#define _VERSION_ 103
#define _VER_STRING_ "v1.03"
void usage()
{
fprintf(stderr, "crlf " _VER_STRING_ " Copyright (c) 2003,2004,2013 by Bob Frazier and S.F.T. Inc.\n"
" all rights reserved\n\n"
"USAGE: crlf [-[u|m][-[n|b]][-h][-r][-q][-t][-0]] [--] <filename>\n"
" translates '\\n' to '\\r\\n' and vice versa\n\n"
#ifdef WIN32
" -u converts '\\r\\n' to '\\n' (unix style)\n"
" -m (default) converts '\\n' to '\\r\\n' (MS DOS/Windows style)\n"
#else // WIN32
" -u (default) converts '\\r\\n' to '\\n' (unix style)\n"
" -m converts '\\n' to '\\r\\n' (MS DOS/Windows style)\n"
#endif // WIN32
" (-u and -m are mutually exclusive options)\n"
" -n do NOT create a '.bak' backup version of the original\n"
" -b (default)create a '.bak' backup version of the original\n"
" -r recurse sub-directories (only valid if a directory name or\n"
" wildcard filename specification is used\n"
" -t 'trim' mode: remove excess white space at the end of a line\n"
" -0 'allow zero byte' mode - do NOT trim '0' bytes\n"
" -q 'quiet' mode: do not print file/directory names when\n"
" converting, recursing directories, or creating backup files.\n"
" -h prints this 'usage' information\n"
"\n"
);
}
#ifdef HAVE_GETOPT
extern char *optarg; // for getopt
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
#else // HAVE_GETOPT
int optind = 1;
#endif // HAVE_GETOPT
int MainLoop(LPCSTR szFileName, BOOL bUnixFlag, BOOL bBakFile, BOOL bQuietMode, BOOL bTrim, BOOL bAllowZero);
int RecurseLoop(LPCSTR szFileName, BOOL bUnixFlag, BOOL bBakFile, BOOL bRecurse, BOOL bQuietMode, BOOL bTrim, BOOL bAllowZero);
BOOL IsDirectory(LPCSTR szFileName);
int main(int argc, char *argv[])
{
int i1, iRval = -1;
#ifdef WIN32
BOOL bUnixFlag = FALSE;
#else // WIN32
BOOL bUnixFlag = TRUE;
#endif // WIN32
BOOL bRecurse = FALSE;
BOOL bBakFile = TRUE;
BOOL bQuietMode = FALSE;
BOOL bTrim = FALSE;
BOOL bAllowZero = FALSE;
#ifdef WIN32
if(argc < 2)
{
usage();
return(0);
}
#endif // WIN32
#ifndef HAVE_GETOPT
for(optind=1; optind < argc; optind++)
{
if(argv[optind][0] != '-')
break;
if(argv[optind][1] == '-')
{
optind++;
break;
}
if(argv[optind][1] == 'u' || // typically MS-DOS so be case insensitive
argv[optind][1] == 'U')
{
bUnixFlag = TRUE;
}
else if(argv[optind][1] == '0')
{
bAllowZero = TRUE;
}
else if(argv[optind][1] == 'm' ||
argv[optind][1] == 'M')
{
bUnixFlag = FALSE;
}
else if(argv[optind][1] == 'r' ||
argv[optind][1] == 'R')
{
bRecurse = TRUE;
}
else if(argv[optind][1] == 'b' ||
argv[optind][1] == 'B')
{
bBakFile = TRUE;
}
else if(argv[optind][1] == 'n' ||
argv[optind][1] == 'N')
{
bBakFile = FALSE;
}
else if(argv[optind][1] == 'q' ||
argv[optind][1] == 'Q')
{
bQuietMode = TRUE;
}
else if(argv[optind][1] == 'h' ||
argv[optind][1] == 'H' ||
argv[optind][1] == '?')
{
usage();
return(1);
}
else if(argv[optind][1] == 't' ||
argv[optind][1] == 'T')
{
bTrim = TRUE;
}
else
{
fprintf(stderr, "Illegal or unrecognized option\n");
usage();
return(1);
}
}
#else // HAVE_GETOPT
while((i1 = getopt(argc, argv, "humnbrqt0")) != -1)
{
switch(i1)
{
case '0': // allow 0-bytes
bAllowZero = TRUE;
break;
case 'u': // unix-style (strip the CR's)
bUnixFlag = TRUE;
break;
case 'm': // MS style (add the CR's)
bUnixFlag = FALSE;
break;
case 'r': // recurse
bRecurse = TRUE;
break;
case 'n': // no backup
bBakFile = FALSE;
break;
case 'b': // create backup
bBakFile = TRUE;
break;
case 'q': // quiet mode
bQuietMode = TRUE;
break;
case 't': // trim white space
bTrim = TRUE;
break;
default:
fprintf(stderr, "Illegal or unrecognized option\n");
case 'h':
usage();
return(1);
}
}
#endif // HAVE_GETOPT
argc -= optind;
argv += optind;
if(argc <= 0)
{
usage();
return(1);
}
for(i1=0, iRval = 0; i1 < argc; i1++)
{
// check file name for wildcards
if(strchr(argv[i1], '?') ||
strchr(argv[i1], '*') ||
IsDirectory(argv[i1]))
{
iRval = RecurseLoop(argv[i1], bUnixFlag, bBakFile, bRecurse, bQuietMode, bTrim, bAllowZero);
// use a directory listing
if(!bQuietMode && (i1 + 1) < argc)
fprintf(stderr, "\n");
}
else
{
if(!bQuietMode)
fprintf(stderr, "%s ", argv[i1]);
iRval = MainLoop(argv[i1], bUnixFlag, bBakFile, bQuietMode, bTrim, bAllowZero);
if(!bQuietMode)
fprintf(stderr, "\n");
}
if(iRval)
break;
}
return(iRval);
}
int AddStringToBuffer(char **ppBuf, int *pcbBuf, int *pcbBufCur, LPCSTR szString)
{
int iLen = strlen(szString);
if(!*ppBuf || (*pcbBufCur + iLen + 2) < *pcbBufCur)
{
if(!*ppBuf)
{
*ppBuf = malloc(*pcbBuf = 16384); // initial length
*pcbBufCur = 0;
}
else
{
int cbNewSize = (1 + ((*pcbBufCur + iLen + 8192) / 16384)) * 16384;
char *pTemp = realloc(*ppBuf, cbNewSize);
if(!pTemp)
{
fprintf(stderr, "Insufficient memory to continue\n");
return(-1); // error
}
*ppBuf = pTemp;
*pcbBuf = cbNewSize;
}
}
memcpy(*ppBuf + *pcbBufCur, szString, iLen);
*pcbBufCur += iLen;
*(*ppBuf + ((*pcbBufCur)++)) = 0; // to mark end of string
*(*ppBuf + *pcbBufCur) = 0; // to mark end of array
return(0); // success
}
void FreeBuffer(char **ppBuf)
{
if(*ppBuf)
{
free(*ppBuf);
*ppBuf = NULL;
}
}
int RecurseLoop(LPCSTR szFileName, BOOL bUnixFlag, BOOL bBakFile, BOOL bRecurse, BOOL bQuietMode, BOOL bTrim, BOOL bAllowZero)
{
char tbuf[MAX_PATH * 2], tbuf2[MAX_PATH * 2];
char *p1, *pBuf = NULL, *pBuf2 = NULL;
int iRval = 0, cbBuf = 0, cbBufCur = 0, cbBuf2 = 0, cbBufCur2 = 0;
#ifdef WIN32
WIN32_FIND_DATA fd;
HANDLE hFF;
#else // WIN32
DIR *hD;
char de[sizeof(struct dirent) + NAME_MAX + 2];
struct dirent *pD;
struct stat sF;
#endif // WIN32
if((strlen(szFileName) + 4) > sizeof(tbuf))
{
fprintf(stderr, "ERROR: file name too long - %s\n", szFileName);
return(-1);
}
if(!strchr(szFileName, '?') &&
!strchr(szFileName, '*') &&
IsDirectory(szFileName))
{
// a directory name, but no wildcard stuff
strcpy(tbuf, szFileName);
p1 = tbuf + strlen(tbuf); // point at end of string
// at this point, p1 points to the file name or file name spec
// and everything prior to that is the directory name
#ifdef WIN32
if(p1 > tbuf && *(p1 - 1) != '\\')
*(p1++) = '\\'; // point just past the '\' so I can build file names
strcpy(p1, "*.*");
#else // WIN32
if(p1 > tbuf && *(p1 - 1) != '/')
*(p1++) = '/'; // point just past the '/' so I can build file names
*p1 = '*';
*(p1 + 1) = 0;
#endif // WIN32
// this adds the wildcard file spec on the end, dirname/*
}
else
{
// either a wildcard or a file name
strcpy(tbuf, szFileName);
#ifdef WIN32
p1 = strrchr(tbuf, '\\');
#else // WIN32
p1 = strrchr(tbuf, '/');
#endif // WIN32
if(!p1)
{
#ifdef WIN32
p1 = tbuf;
*(p1++) = '.';
*(p1++) = '\\';
#else // WIN32
p1 = tbuf;
*(p1++) = '.';
*(p1++) = '/';
#endif // WIN32
strcpy(p1, szFileName); // build a path of './filename'
}
else
p1++; // points at the file name now
}
#ifdef WIN32
strcpy(tbuf2, p1); // capture original file pattern spec
if((hFF = FindFirstFile(tbuf, &fd))
!= INVALID_HANDLE_VALUE)
{
// note: *nix behavior and WIN32 behavior are identical
do
{
strcpy(p1, fd.cFileName);
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
iRval = AddStringToBuffer(&pBuf, &cbBuf, &cbBufCur, tbuf);
}
} while(!iRval && FindNextFile(hFF, &fd));
FindClose(hFF);
}
if(!iRval && bRecurse)
{
// now we look for sub-directories
strcpy(p1, "*.*"); // and now use a wildcard file spec
if((hFF = FindFirstFile(tbuf, &fd))
!= INVALID_HANDLE_VALUE)
{
do
{
strcpy(p1, fd.cFileName);
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(fd.cFileName[0] != '.' ||
(fd.cFileName[1] &&
(fd.cFileName[1] != '.' || fd.cFileName[2])))
{
if(bRecurse)
{
strcat(tbuf, "\\");
strcat(tbuf, tbuf2); // add file spec to directory name
iRval = AddStringToBuffer(&pBuf2, &cbBuf2, &cbBufCur2, tbuf);
}
}
}
} while(!iRval && FindNextFile(hFF, &fd));
FindClose(hFF);
}
}
#else // WIN32
if(p1 > (tbuf + 1) && *(p1 - 1) == '/')
{
*(p1 - 1) = 0; // temporarily
hD = opendir(tbuf);
*(p1 - 1) = '/';
}
else
hD = opendir("."); // open current directory
if(hD)
{
strcpy(tbuf2, p1); // I need a copy of the pattern
while(!readdir_r(hD, (struct dirent *)&de, &pD) && pD)
{
// skip '.' and '..'
if(pD->d_name[0] == '.' &&
(!pD->d_name[1] ||
(pD->d_name[1] == '.' && !pD->d_name[2])))
{
continue; // no '.' or '..'
}
strcpy(p1, pD->d_name);
// This app differs from the *nix way of doing things in that
// when I recurse a directory, I do not apply the filter to it.
// In this way, using a command 'crlf -r "*.c" ' will recurse
// all directories, and only modify the '.c' files that it finds.
// Other possible uses of '-r' NOT fed by shell 'globbing' are so
// impractical that I won't even bother trying to do it. If you
// want the 'glob' behavior, set globbing and don't quote the name.
if(!stat(tbuf, &sF))
{
if(sF.st_mode & S_IFDIR)
{
if(bRecurse)
{
strcat(tbuf, "/");
strcat(tbuf, tbuf2); // add file spec to directory name
iRval = AddStringToBuffer(&pBuf2, &cbBuf2, &cbBufCur2, tbuf);
}
}
else if(!fnmatch(tbuf2, p1, FNM_PERIOD)) // 'tbuf2' is my pattern
{
iRval = AddStringToBuffer(&pBuf, &cbBuf, &cbBufCur, tbuf);
}
}
else
fprintf(stderr, "Warning: can't 'stat' %s\n", tbuf);
if(iRval)
break;
}
closedir(hD);
}
#endif // WIN32
// at this point 'pBuf' points to a list of strings separated by '\0'
// that match my file spec, and 'pBuf2' points to a similar list of
// strings for the list of directories I need to recurse into. I have
// saved doing the actual work for last so that any changes I make to
// the directory contents as a result of running the 'crlf' change will
// not cause looping problems or skipped files.
if(!iRval)
{
// first do files in THIS directory
if(pBuf)
{
p1 = pBuf;
while(!iRval && *p1)
{
if(!bQuietMode)
fprintf(stderr, "%s ", p1);
iRval = MainLoop(p1, bUnixFlag, bBakFile, bQuietMode, bTrim, bAllowZero);
p1 += strlen(p1) + 1;
if(!bQuietMode)
fprintf(stderr, "\n");
}
}
// next, recurse sub-directories
if(pBuf2 && bRecurse)
{
p1 = pBuf2;
while(!iRval && *p1)
{
if(!bQuietMode)
fprintf(stderr, "\nDirectory: %s\n", p1);
iRval = RecurseLoop(p1, bUnixFlag, bBakFile, TRUE, bQuietMode, bTrim, bAllowZero);
p1 += strlen(p1) + 1;
}
}
}
// free up resources
FreeBuffer(&pBuf);
FreeBuffer(&pBuf2);
return(iRval);
}
const char * DoTrimZeros(const char *lpText, DWORD *pcbLine, char **ppBuf, DWORD *pcbBuf)
{
const char *p1;
char *pBuf = *ppBuf, *p2;
DWORD cb1, cb2, cbLine = *pcbLine;
if(!pBuf || cbLine > *pcbBuf)
{
char *pTemp;
DWORD cbNew = (*pcbLine + 1024 + 512) & ~1023L;
if(!pBuf)
{
pTemp = malloc(cbNew);
}
else
{
pTemp = realloc(pBuf, cbNew);
}
if(pTemp)
{
*ppBuf = pTemp;
pBuf = pTemp;
*pcbBuf = cbNew;
}
else
{
return lpText; // just return this and don't process it
}
}
for(p1=lpText, p2=pBuf, cb2=0, cb1=cbLine; cb1 > 0; cb1--, p1++)
{
if(*p1)
{
*(p2++) = *p1;
cb2++;
}
}
*pcbLine = cb2;
return pBuf;
}
int MainLoop(LPCSTR szFileName, BOOL bUnixFlag, BOOL bBakFile, BOOL bQuietMode, BOOL bTrim, BOOL bAllowZero)
{
HANDLE hFile, hMap, hOut;
LPCSTR lpMap, lpPos, lp1;
DWORD cbFile, cb1, cb2, cbLine;
char tbuf[MAX_PATH * 2];
int iRval = -1;
DWORD cbBuf = 0, cbNew;
char *pBuf = NULL;
const char *pTemp;
#ifdef WIN32
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
#else // WIN32
#ifdef O_SHLOCK
hFile = open(szFileName, O_RDONLY | O_SHLOCK);
#else // O_SHLOCK
hFile = open(szFileName, O_RDONLY);
#endif // O_SHLOCK
#endif // WIN32
#ifdef WIN32
if(hFile == INVALID_HANDLE_VALUE)
#else // WIN32
if(hFile < 0)
#endif // WIN32
{
fprintf(stderr, "Can't open %s\n", szFileName);
return(1);
}
strcpy(tbuf, szFileName);
strcat(tbuf, ".tmp");
#ifdef WIN32
hOut = CreateFile(tbuf, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
#else // WIN32
#ifdef O_EXLOCK
hOut = open(tbuf, O_CREAT | O_RDWR | O_EXLOCK,
#else // O_EXLOCK
hOut = open(tbuf, O_CREAT | O_RDWR,
#endif // O_EXLOCK
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
#endif // WIN32
#ifdef WIN32
if(hOut == INVALID_HANDLE_VALUE)
#else // WIN32
if(hOut < 0)
#endif // WIN32
{
#ifdef WIN32
CloseHandle(hFile);
#else // WIN32
close(hFile);
#endif // WIN32
fprintf(stderr, "Can't create %s\n", tbuf);
return(2);
}
iRval = 999; // will be assigned to zero on success
#ifdef WIN32
cbFile = GetFileSize(hFile, NULL);
#else // WIN32
cbFile = lseek(hFile, 0, SEEK_END);
lseek(hFile, 0, SEEK_SET);
#endif // WIN32
#ifdef WIN32
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if(!hMap)
{
fprintf(stderr, "Unable to create file mapping\n");
iRval = 3;
}
else
{
lpMap = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, cbFile);
#else // WIN32
#ifdef MAP_NOCORE
lpMap = mmap(NULL, cbFile, PROT_READ, MAP_NOCORE | MAP_PRIVATE, hFile, 0);
#else // MAP_NOCORE
lpMap = mmap(NULL, cbFile, PROT_READ, MAP_PRIVATE, hFile, 0);
#endif // MAP_NOCORE
#endif // WIN32
if(!lpMap)
{
fprintf(stderr, "Unable to map view of file\n");
iRval = 4;
}
else
{
lpPos = lpMap;
iRval = 0; // for later (when I finish without error, the return code will be zero)
while(lpPos < (lpMap + cbFile))
{
lp1 = lpPos;
while(lpPos < (lpMap + cbFile) && *lpPos != '\n')
{
lpPos++;
}
if(lpPos < (lpMap + cbFile) && *lpPos == '\n')
{
lpPos++; // so that I point "just past" the newline
}
cb1 = lpPos - lp1;
if(bTrim)
{
cbLine = cb1;
while(cbLine > 0 && lp1[cbLine - 1] <= ' ')
{
cbLine--;
}
}
else
{
cbLine = cb1;
}
if(lpPos > (lpMap + 1) && *(lpPos - 2) == '\r' && *(lpPos - 1) == '\n')
{
// the line ends in CRLF
if(bTrim || bUnixFlag)
{
if(!bTrim) // trimmed lines already exclude the line feed etc.
{
if(cbLine > 2) // write all but the final 2 characters in the line
cbLine -= 2;
else
cbLine = 0;
}
cbNew = cbLine;
if(!bAllowZero)
{
pTemp = DoTrimZeros(lp1, &cbNew, &pBuf, &cbBuf);
}
else
{
pTemp = lp1;
}
if(cbNew/*cbLine*/ > 0 &&
#ifdef WIN32
(!WriteFile(hOut, pTemp/*lp1*/, cbNew/*cbLine*/, &cb2, NULL) ||
#else // WIN32
(!(cb2 = write(hOut, pTemp/*lp1*/, cbNew/*cbLine*/)) ||
#endif // WIN32
(cbNew/*cbLine*/) != cb2))
{
fprintf(stderr, "Write error on output file (b)\n");
iRval = 6;
break;
}
if(lpPos > lpMap && *(lpPos - 1) == '\n') // so any final "no CRLF" section is left as-is
{
const char *pEnd;
if(!bUnixFlag)
{
cb1 = 2;
pEnd = "\r\n";
}
else
{
cb1 = 1;
pEnd = "\n";
}
#ifdef WIN32
if(!WriteFile(hOut, pEnd, cb1, &cb2, NULL) ||
#else // WIN32
if(!(cb2 = write(hOut, pEnd, cb1)) ||
#endif // WIN32
cb2 != cb1)
{
fprintf(stderr, "Write error on output file (c)\n");
iRval = 7;
break;
}
}
}
else
{
cbNew = cb1;
if(!bAllowZero)
{
pTemp = DoTrimZeros(lp1, &cbNew, &pBuf, &cbBuf);
}
else
{
pTemp = lp1;
}
// 'LF' found - write it 'as-is'
if(cbNew > 0)
{
#ifdef WIN32
if(!WriteFile(hOut, pTemp/*lp1*/, cbNew/*cb1*/, &cb2, NULL) ||
#else // WIN32
if(!(cb2 = write(hOut, pTemp/*lp1*/, cbNew/*cb1*/)) ||
#endif // WIN32
cbNew/*cb1*/ != cb2)
{
fprintf(stderr, "Write error on output file (a)\n");
iRval = 5;
break;
}
}
}
}
else
{
// the line ends in LF
if(!bTrim && bUnixFlag)
{
cbNew = cb1;
if(!bAllowZero)
{
pTemp = DoTrimZeros(lp1, &cbNew, &pBuf, &cbBuf);
}
else
{
pTemp = lp1;
}
if(cbNew > 0)
{
// 'just LF' found - write it 'as-is'
#ifdef WIN32
if(!WriteFile(hOut, pTemp/*lp1*/, cbNew/*cb1*/, &cb2, NULL) ||
#else // WIN32
if(!(cb2 = write(hOut, pTemp/*lp1*/, cbNew/*cb1*/)) ||
#endif // WIN32
cbNew/*cb1*/ != cb2)
{
fprintf(stderr, "Write error on output file (a2)\n");
iRval = 5;
break;
}
}
}
else
{
if(!bTrim) // trimmed lines already exclude the line feed etc.
{
if(cbLine > 1) // write all but the final character in the line
cbLine --;
else
cbLine = 0;
}
cbNew = cbLine;
if(!bAllowZero)
{
pTemp = DoTrimZeros(lp1, &cbNew, &pBuf, &cbBuf);
}
else
{
pTemp = lp1;
}
if(cbNew/*cbLine*/ > 0 &&
#ifdef WIN32
(!WriteFile(hOut, pTemp/*lp1*/, cbNew/*cbLine*/, &cb2, NULL) ||
#else // WIN32
(!(cb2 = write(hOut, pTemp/*lp1*/, cbNew/*cbLine*/)) ||
#endif // WIN32
cbNew/*cbLine*/ != cb2))
{
fprintf(stderr, "Write error on output file (b2)\n");
iRval = 6;
break;
}
// write the CRLF now...
if(lpPos > lpMap && *(lpPos - 1) == '\n') // so any final "no LF" section is left as-is
{
const char *pEnd;
if(!bUnixFlag)
{
cb1 = 2;
pEnd = "\r\n";
}
else
{
cb1 = 1;
pEnd = "\n";
}
#ifdef WIN32
if(!WriteFile(hOut, pEnd, cb1, &cb2, NULL) ||
#else // WIN32
if(!(cb2 = write(hOut, pEnd, cb1)) ||
#endif // WIN32
cb2 != cb1)
{
fprintf(stderr, "Write error on output file (c2)\n");
iRval = 7;
break;
}
}
}
}
}
#ifdef WIN32
UnmapViewOfFile(lpMap);
#else // WIN32
munmap((void *)lpMap, cbFile);
#endif // WIN32
}
#ifdef WIN32
CloseHandle(hMap);
}
CloseHandle(hOut);
CloseHandle(hFile);
#else // WIN32
close(hOut);
close(hFile);
#endif // WIN32
if(pBuf)
{
free(pBuf);
pBuf = NULL; // by convention
}
// CLEANUP SECTION
if(iRval)
{
#ifdef WIN32
DeleteFile(tbuf);
#else // WIN32
unlink(tbuf);
#endif // WIN32
}
else if(!bBakFile)
{
#ifdef WIN32
DeleteFile(szFileName); // in preparation for rename