2 * Copy a text file with end-of-line character transformation (EOL)
4 * Usage: rline input-file output-file
11 char* convert_path(char* origpath)
16 newpath = strdup(origpath);
19 while (newpath[i] != 0)
22 if (newpath[i] == '\\')
28 if (newpath[i] == '/')
45 if (fh < 0 || fstat (fh, &st) < 0)
47 return (int) st.st_size;
50 int main(int argc, char* argv[])
65 /* Terminate the line with windows EOL characters (CRLF) */
71 fprintf(stderr, "Wrong argument count\n");
75 path1 = convert_path(argv[1]);
76 path2 = convert_path(argv[2]);
78 in = fopen(path1, "rb");
81 perror("Cannot open input file");
86 in_buf = malloc(in_size);
89 perror("Not enough free memory");
94 out = fopen(path2, "wb");
97 perror("Cannot open output file");
103 n_in = fread(in_buf, 1, in_size, in);
106 while (in_ptr < in_size)
110 while ((in_ptr + linelen < in_size) && (in_buf[in_ptr + linelen] != '\r') && (in_buf[in_ptr + linelen] != '\n'))
116 n_out = fwrite(&in_buf[in_ptr], 1, linelen, out);
119 /* Terminate the line */
120 n_out = fwrite(&eol_buf[0], 1, sizeof(eol_buf), out);
122 if ((in_ptr < in_size) && (in_buf[in_ptr] == '\r'))
127 if ((in_ptr < in_size) && (in_buf[in_ptr] == '\n'))