쉘 프로그래밍 하는 내용입니다. 예제로 받은건데.. 이해가 되질 않아서 질문 드립니다.

tlqkdto89의 이미지

#include <string.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define MAX_CMD_ARG 10 
 
 const char *prompt = "myshell> "; 
char* cmdvector[MAX_CMD_ARG]; 
char  cmdline[BUFSIZ]; 
 
 void fatal(char *str) 
{ 
    perror(str); 
    exit(1); 
} 
 
 int makelist(char *s, const char *delimiters, char** list, int MAX_LIST) 
{     
      int i = 0; 
      int numtokens = 0; 
      char *snew = NULL; 
 
       if((s==NULL) || (delimiters==NULL)) 
    { 
        return -1; 
    } 
 
     snew = s + strspn(s, delimiters);     
      if((list[numtokens] = strtok(snew, delimiters)) == NULL ) 
        { 
        return numtokens; 
    } 
 
      numtokens = 1; 
 
       while(1) 
    { 
             if((list[numtokens] = strtok(NULL, delimiters)) == NULL) 
        { 
            break; 
        } 
             if(numtokens == (MAX_LIST-1)) 
        {  
             return -1; 
        }         
             numtokens++; 
      } 
      return numtokens; 
} 
 
 int main(int argc, char**argv) 
{ 
      int i = 0; 
      pid_t pid; 
      while (1)  
     {       
           fputs(prompt, stdout); 
        fgets(cmdline, BUFSIZ, stdin); 
        cmdline[ strlen(cmdline) -1] ='\0'; 
 
         switch(pid=fork()) 
        { 
        case 0: 
            makelist(cmdline, " \t", cmdvector, MAX_CMD_ARG); 
            execvp(cmdvector[0], cmdvector); 
            fatal("main()"); 
        case -1: 
              fatal("main()"); 
        default: 
            wait(NULL); 
        } 
      } 
      return 0; 
}


쉘 프로그래밍 하는 소스인데 예제로 받았습니다. 명령어를 입력받아서 그것을 cmdline에 저장해
makelist함수로 넘기는 부분까지는 이해가 되는데 이것을 실행시켜 myshell> 이 시작되면
ls, rm, touch, gedit같은 부분은 실행되는데 옵션이나 다른 명령어 예를들면 mkdir 등의 명령어는
먹히지 않습니다... 이 소스에 대해서 해설을 도움받고 싶습니다.. 도와주세요 ㅠ
shint의 이미지

//http://blog.naver.com/window__han?Redirect=Log&logNo=80143615309
fputs는 prompt("my shell >>") 문장을 stdout에 출력합니다.

//http://www.cyworld.com/schasert/8195597
fgets는 cmdline의 입력받고 싶은 문자열을 입력받습니다.

//fork는 자신과 동일한 프로세스를 생성 및 실행한다고 합니다.
http://blog.naver.com/hottodayok?Redirect=Log&logNo=50153777775

//execvp는 파일을 실행해서 인자를 넘겨줍니다.
http://bbolmin.tistory.com/35

//strspn은 중복되는 문자의 갯수를 알려줍니다.
http://itguru.tistory.com/97

//strtok는 문자열을 잘라줍니다.
http://blog.naver.com/kyuhgmi?Redirect=Log&logNo=40123745720

그리고. 필요한 부분은 각각의 줄마다. printf("----\n"); 로 디버깅 해보세요.

가상 PC에서 Fedora13으로 이 소스 넣고. gcc -o test test.c 한후에. 직접 해봤는데
./test
shell >> mkdir tmp
shell >> rmdir dd
shell >> man top
shell >> ls -al

다 됐습니다. (Ctrl + ^Z 누르면 나옵니다.)

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

익명 사용자의 이미지

저같은 경우도 mkdir은 동작합니다.

제 경우도 mkdir 이 실행 됐습니다. 실행 파일이 /bin 에 존재했기 때문이죠. (Ubuntu 기준)

익명 사용자의 이미지

일덴 기본적으로 이 쉘이 하는 기능은 실행가능한 명령어가 입력이 들어오면 실행합니다.

execvp 함수가 하는일을 찾아보세요. 실행파일이 있는 프로그램에 대해서 실행을 해주게 되는데.. 앞서 말한 명령어들 ls rm 등은 이미 실행파일들이 bin 밑이나 usr/bin 같은곳에 (PATH) 위치해서 접근할 수 있죠.

말씀하신 cd 같은 명령어는 외부명령어가 아니라 쉘자체가 지원하는 명령어 입니다. 즉 사용자가 쉘에서 내부명령어들을 입력하면

쉘은 그 명령어들을 수행할 프로그램을 찾는 것이 아니라 쉘 내부적으로 그 명령어를 처리합니다.

예를 들면 cd를 입력을 하면 내부적으로 디렉토리 구조를 옮겨다니는 일을 하게 되지요. (외부의 프로그램을 실행하는 것이 아니라..)

즉 이것들을 지원하시려면 직접 내용을 구현하셔야 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.