标准IO完结
This commit is contained in:
65
Chapter13/io/sys/mycpy.c
Normal file
65
Chapter13/io/sys/mycpy.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int sfd, dfd;
|
||||
char buf[BUFSIZE];
|
||||
int len, ret, pos;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <src> <dst>\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sfd = open(argv[1], O_RDONLY);
|
||||
if (sfd < 0)
|
||||
{
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (dfd < 0)
|
||||
{
|
||||
// !!!
|
||||
close(sfd);
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
len = read(sfd, buf, BUFSIZE);
|
||||
if (len < 0)
|
||||
{
|
||||
perror("read");
|
||||
break;
|
||||
}
|
||||
if (len == 0)
|
||||
break;
|
||||
|
||||
pos = 0;
|
||||
while (len > 0)
|
||||
{
|
||||
ret = write(dfd, buf + pos, len);
|
||||
if (ret < 0)
|
||||
{
|
||||
perror("write");
|
||||
exit(1);
|
||||
}
|
||||
pos += ret;
|
||||
len -= ret;
|
||||
}
|
||||
}
|
||||
|
||||
close(dfd);
|
||||
close(sfd);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user