디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 74 추천 0 댓글 4

#include <string.h>
#include "minishell.h"
#include <stdio.h>
#include "stdbool.h"
#include <unistd.h>
#include <stdlib.h>

void lookupPath(char** argv, char** dirs, char* result);
int parseCommand(char *buffer, struct command_t *cmd);
int parsePath(char **dirs);
void printPrompt();
void readCommand(char *buffer);

int main()
{
        /*Variable Declaration*/
         int pid, numChildren=0;
         int status;
         char cmd[MAX_LINE_LEN];
         struct command_t command;
         char *pathv[MAX_PATHS];
         char commandLine[MAX_LINE_LEN];
         char result[MAX_PATH_LEN];
 
         /* shell initialization*/

         /*Get directory paths form PATH*/
         parsePath(pathv);

         while(true)
         {
                  printPrompt();

                  /*Read the command line and parse it*/
                  readCommand(commandLine);

                  parseCommand(commandLine, &command);
                  command.argv[command.argc] = NULL;

                  /*Get the full pathname for the file*/
                  lookupPath(command.argv, pathv, result);
                  command.name = result;
                  if(command.name == NULL)
                  {
                           /*Report error*/
                           continue;
                  }

                  /*Create child and execute the command*/
                  pid = fork();
                  if(pid<0)
                  {
                           //error occurred
                           fprintf(stderr, "Fork Failed");
                           exit(-1);
                    }
                  else if(pid==0)
                  {
                           execvp(command.name, command.argv);
                  }
                  else
                  {
                           wait(&status);
                  }
                  /*Wait for the child to terminate*/
          }

         /*Shell termination*/
         return 0;
}

int parsePath(char **dirs)
{
         /*This function reads the PATH variable for this environment, then
          * builds an array, dirs[], of the directories in PATH
          */
         char *pathEnvVar;
         char *thePath;
         int i=0,j=0;
         char **Path;

         for(i=0; i<MAX_PATHS; i++)
          dirs[i] = \'\\0\';
         pathEnvVar = (char*) getenv("PATH");
         thePath = (char*) malloc(strlen(pathEnvVar)+1);
         strcpy(thePath, pathEnvVar);
         Path=&thePath;

         /* Loop to parse thePath. Look for a \':\'
          * delimiter between each path time.
          */
         for(j=0; j<MAX_PATHS; j++)
          dirs[j]=strsep(Path, ":");

         return 0;
}

void lookupPath(char **argv, char **dirs, char* result)
{
         /* This function searches the directories identified by the dirs
          * argument to see if argv[0] (the file name) appears there.
          * Allocate a new string, place the full path name in it, then
          * return the string.
          */
         char pName[MAX_PATH_LEN];
         int i=0;

         //Check to see if file name is already an absolute path name
         if(*argv[0] == \'/\')
         {
                  if(access(*dirs, 0)==0)
                   {
                           fprintf(stderr, "%s: command not found\\n", argv[0]);
                           return;
                   }


          sprintf(result,"%s",*argv[0]);
         }

         // Look in PATH directories
         // Use access()to see if the file is in a dirs/
         for(i=0;i<MAX_PATHS;i++)
         {
                  if(*dirs[i]==\'\\0\')
                           break;
                  if(access(*dirs, 0)==0)
                  {
                           fprintf(stderr, "%s: command not found\\n", *argv[0]);
                           break;
                  }
                  sprintf(pName,"%s/%s",*dirs[i],*argv[0]);
                  strcpy(result, pName);
         }

         //File name not found in any path variable

}

int parseCommand(char *cLine, struct command_t *cmd)
{
         /* Initialization*/
         int argc;
         char **clPtr;

         /*cLine is the command line*/
         clPtr = &cLine;

         /*Fill argv[]*/
         argc=0;
         while((cmd->argv[argc] = strsep(clPtr,WHITESPACE)) != NULL)
         { 
                  argc++; 
         }

         /* Set the command name and argc*/
         cmd->argc = argc-1;
         cmd->name = (char *) malloc(MAX_ARG_LEN);
         strcpy(cmd->name, cmd->argv[0]);
         return 1;
}


void readCommand(char *buffer)
{
         /* This code uses any set of I/O functions, such as those in
          * the stdio library to read the entire command line into
          * the buffer. This implementation is greatly simplified,
          * but it does the job.
          */

         gets(buffer);
}

void printPrompt()
{
         /* Build the prompt string to have the machine name,
          * current directory, or other desired information
          */
         char promptString[12]="Lee@shell\\n";
         printf("%s ", promptString);
}

간단한 shell 만들기가 과젠데... 컴파일 오류도 없는데, 포인터에 오류가 있대,

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
등록순정렬 기준선택
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 비난 여론에도 뻔뻔하게 잘 살 것 같은 스타는? 운영자 24/06/03 - -
222749 쉘소트에서 궁금하거 있어여 [2] ㅇㅇ(211.169) 10.12.06 63 0
222748 고수분들!!!C언어나 프로그래밍 잘하시는 분들 제발 도와주세요!ㅜ_ㅜ!! [16] 순두부(183.107) 10.12.06 218 0
222747 능력자 형님들 부탁이있습니다. 이거 만들면 당신은 신이 됩니다. [4] 6년차(116.38) 10.12.06 123 0
222746 님들 컴퓨터 이론쪽 공부조하려고하는데 좋은 책좀 추처해주세요 [2] 53553(124.80) 10.12.06 84 0
222744 시발 그래서 그래서 그래서 [1] 53553(124.80) 10.12.06 57 0
222743 요세 느끼는건데 빌게이츠가 천재긴 천재다 윈도우3.1 혼자 만들고 [3] 53553(124.80) 10.12.06 135 0
222742 nc 소프트 회장색끼가 리니지 혼자서 만든거임? [4] 53553(124.80) 10.12.06 132 0
222741 님들 만약 컴퓨터야 너는 섹스가 가능해 이런문장을 쓴다면 [3] 53553(124.80) 10.12.06 93 0
222738 대쪽같은 정신 [5] ㅇㅇㅃ갤로그로 이동합니다. 10.12.06 95 0
222737 님들 만약에 말야 내가 리니지를 만든다면 일단 컴퓨터 언어를 안다는가정하 [1] 53553(124.80) 10.12.06 66 0
222735 컴퓨터 분석 설계론 아는 훃들만 봐봐 아니 아무나 봐봐 ㄱㄱ(124.80) 10.12.06 51 0
222734 컴퓨터학원 3개월째 [2] 팰트로우갤로그로 이동합니다. 10.12.06 125 0
222732 컴퓨터 언어 이런건 잼있는데 이론 나오는건 정말 지루해 미치겠네 [2] ㄱㄱ(124.80) 10.12.06 64 0
222731 자바 텍스트 파일 생성인데 간단한 소스니까 한번 봐줘염 HERMES갤로그로 이동합니다. 10.12.06 299 0
222729 횽들 it 빅3가 어디양?? [6] 살려줭(121.144) 10.12.06 140 0
222728 오늘 과제를 하면서 [1] ㅇㅇㅃ갤로그로 이동합니다. 10.12.06 39 0
222727 근데 비트프로젝트 책보면... 불꽃(124.153) 10.12.06 85 0
222726 C언어 공부한 형님들 이거 정말 병신같은 질문인거 아는데... [3] 수학쨔응하악하악갤로그로 이동합니다. 10.12.06 85 0
222725 님들 컴퓨터 분석 설계론 이거 꼭 공부해야됨? ㄱㄱ(124.80) 10.12.06 46 0
222724 이사람 남자야? [6] Vita500갤로그로 이동합니다. 10.12.06 157 0
222722 자바에서 입력한 걸 텍스트 파일로 뽑아낼수 있나염 [2] HERMES갤로그로 이동합니다. 10.12.06 62 0
222721 오늘치 시험 끝. Rei@디씨갤로그로 이동합니다. 10.12.06 52 0
222720 교촌 오리지날이 진리 아니냐? [2] 불꽃(124.153) 10.12.06 74 0
222718 자바는 왜 protected 가 엑세스를 패키지 영역에도 허용함? [3] ㅏㅏㅏㅗㅗㅗ(219.157) 10.12.06 117 0
222714 비아냥 갤러리에 오신 것을 환영합니다. [7] 아잉따잉갤로그로 이동합니다. 10.12.06 90 0
222712 C 떡밥에만 반응하는 물고기들 [1] 홍다(114.199) 10.12.06 103 0
222711 트리 공부시작 [3] 유리한추종자(118.91) 10.12.06 64 0
222709 형들아 자바 관련해서 잠깐 질문좀;; ImageIcon gif 띄우기 [2] 형들아(220.73) 10.12.06 171 0
222708 c언어 컴파일러를 c언어로 만든게 맞나요??? [5] 컴돌이(58.77) 10.12.06 263 0
222707 으잌ㅋㅋ 시험시작 8분 남았다 Rei@디씨갤로그로 이동합니다. 10.12.06 92 0
222704 ㅇ ㅐ들아~ 밥먹게 들어와라~ [5] 쭈중이갤로그로 이동합니다. 10.12.06 74 0
222703 헐 학교 갔다왓는데 아직도 장은성 있네;; [1] 일광면(119.198) 10.12.06 96 0
222702 구글 이 씨발 개샛키들 [3] 눈팅용.갤로그로 이동합니다. 10.12.06 132 0
222701 오류질문이요ㅠㅠlink1120 외부참조ㅠㅠ [5] ㅇㄷ(116.37) 10.12.06 1689 3
222700 애정 표현을 좀 과격하게 했어요 세브(222.239) 10.12.06 109 0
222699 프로그램 언어도 스스로 개발하지 못하지만 언어에 대한 유행은 민감... [19] 장은성(220.121) 10.12.06 281 0
222698 테트리스 알고리즘은 아는데..비베 언어를 잘 몰라서 [17] 장은성(220.121) 10.12.06 1520 1
222697 장은성 횽아와 관련해서 한가지는 분명한 거 같다 [3] 분당살람갤로그로 이동합니다. 10.12.06 104 0
222696 내가 좀 짜증날 만한 상황인가..?? [9] 111(218.55) 10.12.06 133 0
222695 공비 안심심하게 해줄께... [4] 장은성(220.121) 10.12.06 133 0
222694 집에 가고싶은 아이의 질문 [4] 집에가고싶어(175.217) 10.12.06 76 0
222693 게임 프로그래밍 하려면 API다음 트리가 뭐임 Direct draw임? [5] 자리비움갤로그로 이동합니다. 10.12.06 150 0
222692 it crew가 아니라 it croud 아닌가요??? [1] 컴돌이(58.77) 10.12.06 62 0
222691 궁금한거 질문 elwlwlwk갤로그로 이동합니다. 10.12.06 34 0
222690 실버라이트 공부할수있는 카페나 홈페이지 없을까요? [1] Witch Doctor갤로그로 이동합니다. 10.12.06 79 0
222689 심심해 [1] elwlwlwk갤로그로 이동합니다. 10.12.06 72 0
222688 장은성이 낫냐 내가 낫냐?? [2] 개쉛기갤로그로 이동합니다. 10.12.06 164 0
222686 냥옹잉 대마법서오즈갤로그로 이동합니다. 10.12.06 83 0
222685 장은성 글보다가 기분 나쁜거 [1] 퓨리님(211.48) 10.12.06 95 0
222682 크로우 카드의 낌새가 느껴져! [2] 세브(211.117) 10.12.06 147 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2