]> git.saurik.com Git - wxWidgets.git/blob - distrib/msw/unix2dos.c
Added build instructions, readme, and TODO
[wxWidgets.git] / 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
5
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 *****************************************************************************/
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
23 char *prog; /* global var: program name as called */
24 int warning=0; /* global var: flag for unix2dos if input has CR's */
25
26 void usage();
27 void translate(FILE *, FILE *, int unix2Dos);
28
29 main(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;
40 if (argc > 1)
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
72 fp = fopen(argv[i], "r");
73 if (!fp)
74 {
75 fprintf(stderr, "Cannot open %s.\n", argv[i]);
76 i ++;
77 continue;
78 }
79 outFile = fopen(tmpFile, "w");
80 if (!outFile)
81 {
82 fprintf(stderr, "Cannot open %s.\n", tmpFile);
83 exit(1);
84 }
85 translate(fp, outFile, unix2Dos);
86
87 if (warning) /* unix2dos acting on a possible DOS file */
88 {
89 fprintf(stderr,"%s: %s may have already been in DOS format. Not converted.\n",
90 prog, argv[i]);
91 warning = 0;
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
108 dos2unix-strip out CR only before LF */
109
110 void translate(FILE *ifp, FILE *ofp, int unix2Dos)
111 {
112 int c,d;
113
114 if (!unix2Dos)
115 /* DOS2Unix */
116 while ((c = getc(ifp)) != EOF){
117 if (c == CR)
118 switch(d = getc(ifp)){ /* check to see if LF follows */
119 case LF:
120 putc(d,ofp); /* if so, ignore CR */
121 break;
122 default:
123 putc(c,ofp); /* if not, output CR and following char */
124 putc(d,ofp);
125 } else putc(c, ofp); /* c is not a CR */
126 }
127
128 else
129 /* Unix2DOS */
130 while ((c = getc(ifp)) != EOF){
131 if (c == CR)
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 */
135 putc(c, ofp);
136 }
137 }
138
139 void usage()
140 {
141 fprintf(stderr, "Usage:\n%s [--dos2unix] [--unix2dos] file1 file2 ...\nor\n%s [--dos2unix] [--unix2dos] < infile > outfile\n", prog, prog);
142 exit(0);
143 }