forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.5.3.c
51 lines (45 loc) · 989 Bytes
/
14.5.3.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
#include <ctype.h>
#include <fcntl.h>
#include "apue.h"
#define BSZ 4096
unsigned char buf[BSZ];
unsigned char translate(unsigned char c) {
if (isalpha(c)) {
if (c >= 'n') {
c -= 13;
} else if (c >= 'a') {
c += 13;
} else if (c >= 'N') {
c -= 13;
} else {
c += 13;
}
}
return (c);
}
int main(int argc, char* argv[]) {
int ifd, ofd, i, n, nw;
if (argc != 3) {
err_quit("usage: rot13 infile outfile");
}
if ((ifd = open(argv[1], O_RDONLY)) < 0) {
err_sys("can't open %s", argv[1]);
}
if ((ofd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0) {
err_sys("can't create %s", argv[2]);
}
while ((n = read(ifd, buf, BSZ)) > 0) {
for (i = 0; i < n; i++) {
buf[i] = translate(buf[i]);
}
if ((nw = write(ofd, buf, n)) != n) {
if (nw < 0) {
err_sys("write failed");
} else {
err_quit("short write (%d/%d)", nw, n);
}
}
}
fsync(ofd);
exit(0);
}