]>
git.saurik.com Git - wxWidgets.git/blob - distrib/msw/unix2dos.c
1 /*****************************************************************************
2 dos2unix, unix2dos: translate unix <-> dos text files by
3 -> adding a CR before each LF
4 <- removing the CR from CRLF pairs
6 Use a modern compiler (gcc); name the executable either unix2dos or dos2unix,
7 and link to the other name. A warning will be printed if the input of unix2dos
8 contains CR's, as it may have already been a dos file (output still produced).
9 The warning is not produced if input is stdin.
10 Jim Martino +++++++++++++++++++++++++++++
11 jrm@chow.mat.jhu.edu + PLACE COMMERCIAL HERE +
12 jmartino@jhunix.hcf.jhu.edu +++++++++++++++++++++++++++++
13 Modified by Julian Smart, September 2002
14 *****************************************************************************/
20 #define CR '\015' /* define CR character */
21 #define LF '\012' /* define LF character */
23 char *prog
; /* global var: program name as called */
24 int warning
=0; /* global var: flag for unix2dos if input has CR's */
27 void translate(FILE *, FILE *, int unix2Dos
);
29 main(int argc
, char *argv
[])
35 prog
= argv
[0]; /* program name as called */
42 if (strcmp(argv
[1], "--help") == 0)
46 else if (strcmp(argv
[1], "--dos2unix") == 0)
50 else if (strcmp(argv
[1], "--unix2dos") == 0)
64 translate(stdin
, stdout
, unix2Dos
);
70 sprintf(tmpFile
, "%s.tmp", argv
[i
]);
72 fp
= fopen(argv
[i
], "r");
75 fprintf(stderr
, "Cannot open %s.\n", argv
[i
]);
79 outFile
= fopen(tmpFile
, "w");
82 fprintf(stderr
, "Cannot open %s.\n", tmpFile
);
85 translate(fp
, outFile
, unix2Dos
);
87 if (warning
) /* unix2dos acting on a possible DOS file */
89 fprintf(stderr
,"%s: %s may have already been in DOS format. Not converted.\n",
100 rename(tmpFile
, argv
[i
]);
107 /* translate: unix2dos-change LF to CRLF
108 dos2unix-strip out CR only before LF */
110 void translate(FILE *ifp
, FILE *ofp
, int unix2Dos
)
116 while ((c
= getc(ifp
)) != EOF
){
118 switch(d
= getc(ifp
)){ /* check to see if LF follows */
120 putc(d
,ofp
); /* if so, ignore CR */
123 putc(c
,ofp
); /* if not, output CR and following char */
125 } else putc(c
, ofp
); /* c is not a CR */
130 while ((c
= getc(ifp
)) != EOF
){
132 warning
= 1; /* set warning flag: input file may be a DOS file */
133 if (c
== LF
&& (warning
== 0))
134 putc(CR
, ofp
); /* add CR before each LF */
141 fprintf(stderr
, "Usage:\n%s [--dos2unix] [--unix2dos] file1 file2 ...\nor\n%s [--dos2unix] [--unix2dos] < infile > outfile\n", prog
, prog
);