-
Notifications
You must be signed in to change notification settings - Fork 1
/
addcr.c
48 lines (46 loc) · 1.1 KB
/
addcr.c
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 <stdlib.h>
main(int ac, char **av) {
FILE *infile, *outfile;
int i, outfilefd;
char *name, *temp;
while (name = *++av) {
if (temp = malloc(strlen(name) + 7)) {
if ((infile = fopen(name, "r")) == (FILE *)0) {
fprintf(stderr, "addcr: can't open \"%s\" for reading\n");
exit(1);
}
strcpy(temp, name);
strcat(temp, "XXXXXX");
if ((outfilefd = mkstemp(temp)) == -1
|| (outfile = fdopen(outfilefd, "w")) == (FILE *)0) {
fprintf(stderr, "addcr: can't create temporary file\n");
exit(1);
}
} else {
fprintf(stderr, "addcr: out of memory\n");
exit(1);
}
while ((i = getc(infile)) != EOF) {
if (i == '\n')
putc('\r', outfile);
putc(i, outfile);
}
if (ferror(infile) || (fclose(infile) == EOF)) {
fprintf(stderr, "addcr: read error\n");
unlink(temp);
exit(1);
}
if (ferror(outfile) || (fclose(outfile) == EOF)) {
fprintf(stderr, "addcr: write error\n");
unlink(temp);
exit(1);
}
if (rename(temp, name) == -1) {
fprintf(stderr, "addcr: rename error\n");
unlink (temp);
exit(1);
}
unlink(temp);
}
}