]> git.saurik.com Git - wxWidgets.git/blame - distrib/msw/unix2dos.c
Added unix2dos.c utility
[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;
40 if (i > argc)
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 outFile = fopen(tmpFile, "w");
74 if (!outFile)
75 {
76 fprintf(stderr, "Cannot open %s.\n", tmpFile);
77 exit(1);
78 }
79 if (!fp)
80 {
81 fprintf(stderr, "Cannot open %s.\n", argv[i]);
82 exit(1);
83 }
84 translate(fp, outFile, unix2Dos);
85
86 if (warning) /* unix2dos acting on a possible DOS file */
87 {
88 fprintf(stderr,"%s: %s may have already been in DOS format.\n",
89 prog, argv[i]);
90 }
91 fclose(fp);
92 fclose(outFile);
93#ifdef _WINDOWS
94 remove(argv[i]);
95#else
96 unlink(argv[i]);
97#endif
98 rename(tmpFile, argv[i]);
99
100 i ++;
101 }
102 }
103}
104
105/* translate: unix2dos-change LF to CRLF
106dos2unix-strip out CR only before LF */
107
108void translate(FILE *ifp, FILE *ofp, int unix2Dos)
109{
110 int c,d;
111
112 if (!unix2Dos)
113 /* DOS2Unix */
114 while ((c = getc(ifp)) != EOF){
115 if (c == CR)
116 switch(d = getc(ifp)){ /* check to see if LF follows */
117 case LF:
118 putc(d,ofp); /* if so, ignore CR */
119 break;
120 default:
121 putc(c,ofp); /* if not, output CR and following char */
122 putc(d,ofp);
123 } else putc(c, ofp); /* c is not a CR */
124 }
125
126 else
127 /* Unix2DOS */
128 while ((c = getc(ifp)) != EOF){
129 if (c == CR)
130 warning = 1; /* set warning flag: input file may be a DOS file */
131 if (c == LF)
132 putc(CR, ofp); /* add CR before each LF */
133 putc(c, ofp);
134 }
135}
136
137void usage()
138{
139 fprintf(stderr, "Usage:\n%s [--dos2unix] [--unix2dos] file1 file2 ...\nor\n%s [--dos2unix] [--unix2dos] < infile > outfile\n", prog, prog);
140 exit(0);
141}