* 파일명 : mycp.c
* 파일설명 : copyfile -- name1을 name2로 복사한다. */
#include <stdio.h>
#include <fcntl.h>
#define PERMS 0666
main(int argc, char *argv[])
{
void fatal(char *);
int source, object;
int read_number;
char buff[BUFSIZ];
if(argc < 2)
{
puts("mycp: 인수가 너무 적습니다.\n 더 많은 정보를 얻으러면 `mycp --help'명령을 하십시오.");
exit(0);
}
if(argc < 3) //--help를 정확히 어떻게 구현하는지 몰라 argc를 하나 느려 --help를 구현 하지만 다른 인자를 넣어서 나오는 문제점 발생
{
puts("작동법:mycp source object\n mycp test.c test.txt\n 궁금한 사항이나 버그는 xxx@xxxx.com 으로 연락주세요");
exit(0);
}
if((source = open(argv[1], O_RDONLY)) < 0)
fatal("read open");
if((object = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, PERMS)) < 0)
fatal("write open");
while((read_number = read(source, buff, BUFSIZ)) > 0)
{
if(write(object, buff, read_number) < read_number)
{
close(source);
close(object);
exit(1);
}
}
close(source);
close(object);
}
void fatal(char *error_name)
{
perror(error_name);
exit(1);
}
|