LuvSea

Linux cp 명령어를 만들어보자. 본문

sTudy

Linux cp 명령어를 만들어보자.

사랑海 2009. 4. 30. 17:43
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

여기서 Cmd[0]은 명령어, Cmd[1]은 복사할 대상파일, Cmd[2]는 복사 후 생성 파일

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


int main(int iNum, char * Cmd[3])
{
 char c;
 int in, out;

 
 if(3 != iNum)
 {
  printf("사용법 : 명령 원본 사본 \n");
  return 0;
 }
 in = open(Cmd[1], O_RDONLY);

 // 파일 읽기 예외처리
 if(-1 == in)
 {
  printf("%s파일이  없거나 사용법이 틀렸습니다!\n",Cmd[1]);
  return 0;
 }
 

 out = open(Cmd[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

 // 파일 쓰기 예외 처리
 if(-1 == out)
 {
  printf("%s 파일에 쓸 수 없습니다.\n", Cmd[2]);
  return 0;
 }
 
 
 while(read(in, &c, 1) == 1)
  write(out, &c, 1);
 
 
 close(in);
 close(out);
 return 0;
}

Comments