]> git.saurik.com Git - wxWidgets.git/blame - distrib/msw/unix2dos.c
Name conflict correction.
[wxWidgets.git] / distrib / msw / unix2dos.c
CommitLineData
c721300b
JS
1/*****************************************************************************
2dos2unix, unix2dos: translate unix <-> dos text files by
3 -> adding a CR before each LF
4 <- removing the CR from CRLF pairs
5
6Use a modern compiler (gcc); name the executable either unix2dos or dos2unix,
7and link to the other name. A warning will be printed if the input of unix2dos
8contains CR's, as it may have already been a dos file (output still produced).
9The warning is not produced if input is stdin.
10Jim Martino +++++++++++++++++++++++++++++
11jrm@chow.mat.jhu.edu + PLACE COMMERCIAL HERE +
12jmartino@jhunix.hcf.jhu.edu +++++++++++++++++++++++++++++
13Modified by Julian Smart, September 2002
14*****************************************************************************/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#define CR '\015' /* define CR character */
21#define LF '\012' /* define LF character */
22
23char *prog; /* global var: program name as called */
24int warning=0; /* global var: flag for unix2dos if input has CR's */
25
26void usage();
27void translate(FILE *, FILE *, int unix2Dos);
28
29main(int argc, char *argv[])
30{
31 FILE *fp, *outFile;
32 int i;
33 int unix2Dos;
34
35 prog = argv[0]; /* program name as called */
36
37 unix2Dos = 1;
38
39 i = 1;
2c00930d 40 if (argc > 1)
c721300b
JS
41 {
42 if (strcmp(argv[1], "--help") == 0)
43 {
44 usage();
45 }
46 else if (strcmp(argv[1], "--dos2unix") == 0)
47 {
48 unix2Dos = 0;
49 }
50 else if (strcmp(argv[1], "--unix2dos") == 0)
51 {
52 unix2Dos = 1;
53 }
54 else
55 {
56 usage();
57 }
58 }
59 else
60 usage();
61
62 i ++;
63 if (i == argc)
64 translate(stdin, stdout, unix2Dos);
65 else
66 {
67 while (i < argc)
68 {
69 char tmpFile[512];
70 sprintf(tmpFile, "%s.tmp", argv[i]);
71
c69291e9 72 fp = fopen(argv[i], "rb");
2c00930d
JS
73 if (!fp)
74 {
75 fprintf(stderr, "Cannot open %s.\n", argv[i]);
76 i ++;
77 continue;
78 }
c69291e9 79 outFile = fopen(tmpFile, "wb");
c721300b
JS
80 if (!outFile)
81 {
82 fprintf(stderr, "Cannot open %s.\n", tmpFile);
83 exit(1);
84 }
c721300b
JS
85 translate(fp, outFile, unix2Dos);
86
87 if (warning) /* unix2dos acting on a possible DOS file */
88 {
2c00930d
JS
89 fprintf(stderr,"%s: %s may have already been in DOS format. Not converted.\n",
90 prog, argv[i]);
91 warning = 0;
c721300b
JS
92 }
93 fclose(fp);
94 fclose(outFile);
95#ifdef _WINDOWS
96 remove(argv[i]);
97#else
98 unlink(argv[i]);
99#endif
100 rename(tmpFile, argv[i]);
101
102 i ++;
103 }
104 }
105}
106
107/* translate: unix2dos-change LF to CRLF
108dos2unix-strip out CR only before LF */
109
110void translate(FILE *ifp, FILE *ofp, int unix2Dos)
111{
112 int c,d;
113
114 if (!unix2Dos)
c69291e9 115 {
c721300b 116 /* DOS2Unix */
c69291e9
JS
117 while ((c = getc(ifp)) != EOF)
118 {
c721300b 119 if (c == CR)
c69291e9
JS
120 {
121 switch(d = getc(ifp))
122 { /* check to see if LF follows */
123 case LF:
124 putc(d,ofp); /* if so, ignore CR */
125 break;
126 default:
127 putc(c,ofp); /* if not, output CR and following char */
128 putc(d,ofp);
129 }
130 }
131 else
132 putc(c, ofp); /* c is not a CR */
c721300b 133 }
c69291e9
JS
134 }
135 else
136 {
c721300b
JS
137 /* Unix2DOS */
138 while ((c = getc(ifp)) != EOF){
139 if (c == CR)
140 warning = 1; /* set warning flag: input file may be a DOS file */
2c00930d 141 if (c == LF && (warning == 0))
c721300b
JS
142 putc(CR, ofp); /* add CR before each LF */
143 putc(c, ofp);
144 }
c69291e9 145 }
c721300b
JS
146}
147
148void usage()
149{
150 fprintf(stderr, "Usage:\n%s [--dos2unix] [--unix2dos] file1 file2 ...\nor\n%s [--dos2unix] [--unix2dos] < infile > outfile\n", prog, prog);
151 exit(0);
152}