blob: e678e994be3a84fb63c3f0d364bd8f607a01fe6c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#define BUF_SIZE 256
void
usage(char *cmd)
{
printf("%s\n", cmd);
char *base = basename(cmd);
fprintf(stderr, "Usage: %s infile [outfile]\n", base);
}
int
main(int argc, char *argv[])
{
FILE *infile, *outfile;
char buffer[BUF_SIZE];
if (argc < 2 || argc > 3)
{
usage(argv[0]);
return 1;
}
infile = fopen(argv[1], "r");
if (argc < 3)
{
outfile = stdout;
}
else
{
outfile = fopen(argv[2], "w");
}
while (fgets(buffer, BUF_SIZE, infile) != NULL)
{
int len;
// Step on any CR LF at end of line
len = strlen(buffer);
if (buffer[len-1] == '\n' || buffer[len-1] == '\r')
buffer[len-1] = 0;
if (buffer[len-2] == '\n' || buffer[len-2] == '\r')
buffer[len-2] = 0;
fprintf(outfile, "\"%s\\n\"\n", buffer);
}
close(infile);
close(outfile);
}
|