- Disable commented out define-check which i had only commented to check building.
[reactos.git] / reactos / tools / rline.c
1 /*
2 * Copy a text file with end-of-line character transformation (EOL)
3 *
4 * Usage: rline input-file output-file
5 */
6 #include <sys/stat.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10
11 char* convert_path(char* origpath)
12 {
13 char* newpath;
14 int i;
15
16 newpath = strdup(origpath);
17
18 i = 0;
19 while (newpath[i] != 0)
20 {
21 #ifdef UNIX_PATHS
22 if (newpath[i] == '\\')
23 {
24 newpath[i] = '/';
25 }
26 #else
27 #ifdef DOS_PATHS
28 if (newpath[i] == '/')
29 {
30 newpath[i] = '\\';
31 }
32 #endif
33 #endif
34 i++;
35 }
36 return(newpath);
37 }
38
39 int
40 fsize (FILE * f)
41 {
42 struct stat st;
43 int fh = fileno (f);
44
45 if (fh < 0 || fstat (fh, &st) < 0)
46 return -1;
47 return (int) st.st_size;
48 }
49
50 int main(int argc, char* argv[])
51 {
52 char* path1;
53 char* path2;
54 FILE* in;
55 FILE* out;
56 char* in_buf;
57 int in_size;
58 int in_ptr;
59 int linelen;
60 int n_in;
61 int n_out;
62 char eol_buf[2];
63
64 /* Terminate the line with windows EOL characters (CRLF) */
65 eol_buf[0] = '\r';
66 eol_buf[1] = '\n';
67
68 if (argc != 3)
69 {
70 fprintf(stderr, "Wrong argument count\n");
71 exit(1);
72 }
73
74 path1 = convert_path(argv[1]);
75 path2 = convert_path(argv[2]);
76
77 in = fopen(path1, "rb");
78 if (in == NULL)
79 {
80 perror("Cannot open input file");
81 exit(1);
82 }
83
84 in_size = fsize(in);
85 in_buf = malloc(in_size);
86 if (in_buf == NULL)
87 {
88 perror("Not enough free memory");
89 fclose(in);
90 exit(1);
91 }
92
93 out = fopen(path2, "wb");
94 if (out == NULL)
95 {
96 perror("Cannot open output file");
97 fclose(in);
98 exit(1);
99 }
100
101 /* Read it all in */
102 n_in = fread(in_buf, 1, in_size, in);
103
104 in_ptr = 0;
105 while (in_ptr < in_size)
106 {
107 linelen = 0;
108
109 while ((in_ptr + linelen < in_size) && (in_buf[in_ptr + linelen] != '\r') && (in_buf[in_ptr + linelen] != '\n'))
110 {
111 linelen++;
112 }
113 if (linelen > 0)
114 {
115 n_out = fwrite(&in_buf[in_ptr], 1, linelen, out);
116 in_ptr += linelen;
117 }
118 /* Terminate the line */
119 n_out = fwrite(&eol_buf[0], 1, sizeof(eol_buf), out);
120
121 if ((in_ptr < in_size) && (in_buf[in_ptr] == '\r'))
122 {
123 in_ptr++;
124 }
125
126 if ((in_ptr < in_size) && (in_buf[in_ptr] == '\n'))
127 {
128 in_ptr++;
129 }
130 }
131
132 free(in_buf);
133 fclose(in);
134
135 exit(0);
136 }