티스토리 툴바

달력

052012  이전 다음

  •  
  •  
  • 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
  •  
  •  

'컴과학생/UNIX'에 해당되는 글 2건

  1. 2009/11/01 [UNIX Study] rm,mv 명령어 만들기 (5)
  2. 2009/11/01 [UNIX Study] CP 명령어 만들기 (2)
크리에이티브 커먼즈 라이선스
Creative Commons License
rm과 mv 명령어도 만들어 보았다
rm은 unlink 만 해주면 되고
mv는 기존에 만들었던 cp명령어와 rm을 혼합해주면된다 ( 복사후 기존 파일을 지우면 됨)

rm 명령어 (remove.c)

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>

int main(int argc, char* argv[])
{
    unlink(argv[1]);
    return 0;   
}


실행
$./remove filename

mv 명령어 (move.c)

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

int main(int argc, char* argv[])
{
    int fd1,fd2;
    int r_size,w_size;
    char buf[100];
    fd1 = open(argv[1], O_RDONLY);
    fd2 = open(argv[2], O_RDWR|O_CREAT|O_EXCL, 0664);
   
    r_size = read(fd1,buf,100);
    w_size = write(fd2,buf,r_size);
    while(r_size == 100)
    {
        r_size=read(fd1,buf,100);
        w_size=write(fd2,buf,r_size);
    }

    unlink(argv[1]);
    return 0;   
}


실행
$./move filename1 filename2

저작자 표시 비영리 변경 금지
Posted by 만솜
크리에이티브 커먼즈 라이선스
Creative Commons License
cp 명령어 만들기 (copy.c)

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

int main(int argc, char* argv[])
{
    int fd1,fd2;
    int r_size,w_size;
    char buf[100];
    fd1 = open(argv[1], O_RDONLY);
    fd2 = open(argv[2], O_RDWR|O_CREAT|O_EXCL, 0664);
   
    r_size = read(fd1,buf,100);
    w_size = write(fd2,buf,r_size);
    while(r_size == 100)
    {
        r_size=read(fd1,buf,100);
        w_size=write(fd2,buf,r_size);
    }


    return 0;   
}

컴파일 후
./copy filename1 filename2

라고 하면 된다. (cp filename1 filename2와 같음.)

예외처리는 하지 않은 simple 한 코드.

음 근데 아직도 fopen과 open 이 헷갈린다.
(더불어 fopen을 사용해서 fgets등을 하는 것과 read&write등을 하는 것도 ㅜ0ㅜ,, 뭐,, 더 공부 하고 찾아봐야겠다.)
이제 remove 하러 가야지!


돌아다니다 보니 비슷한 코드도 있었다 (실행은 안해봤지만 역시 잘 돌아갈것 같다)
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main()
{
char c;
int in, out;

in = open(“file.in”, O_RDONLY);

out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR);
while(read(in, &c, 1) ==1 )
write(out, &c, 1);

exit(0);
}



저작자 표시 비영리 변경 금지
Posted by 만솜