-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify.c
71 lines (44 loc) · 839 Bytes
/
stringify.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main( int argc, char** argv )
{
int i;
char* infile = argv[1];
char* outfile = argv[2];
int fd = open(infile,O_RDONLY);
struct stat fs;
fstat(fd,&fs);
size_t sz = fs.st_size;
char* buf = (char*)malloc(sz);
read(fd,buf,sz);
close(fd);
fd = creat(outfile,S_IRUSR|S_IWUSR);
char q = '\"';
char b = '\\';
char n = 'n';
write(fd,&q,1);
for(i=0; i<sz; i++) {
char c = buf[i];
if (c=='\n') {
if (i != sz-1) {
write(fd,&b,1);
write(fd,&n,1);
}
write(fd,&q,1);
write(fd,&c,1);
if (i != sz-1)
write(fd,&q,1);
} else if (c==q || c==b) {
write(fd,&b,1);
write(fd,&c,1);
} else {
write(fd,&c,1);
}
}
close(fd);
return(0);
}