디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 83 추천 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
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 끝까지 다 본 걸 후회하게 만든 용두사미 드라마는? 운영자 25/07/07 - -
345283 cplusplus.com 이거 C++이 만든사이트임? [4] 부스타(14.36) 13.02.07 117 0
345282 [질문] 난수 출력 문제 [7] MUFF갤로그로 이동합니다. 13.02.07 111 0
345280 jsp로 웹프로그래머가 꿈인데 리눅스책을샀다. 이유는? [5] ㅇㅇ(112.153) 13.02.07 187 0
345277 내일 출근 하는사람 잇음? [2] ㅁㅁ(175.223) 13.02.07 76 0
345273 귀신 사진2 에어로홍갤로그로 이동합니다. 13.02.07 65 0
345272 내일 오전근무만 하는 횽들 있음??? [1] 멍청닭(182.211) 13.02.07 57 0
345271 아직도 모집공고의 뜻을 모르는 뉴비들을 위한 가이드라인 [6] 멍청닭(182.211) 13.02.07 275 2
345270 한참mfc를 봤는데 도저히 못찾겠어서.. mfc잘하시는분들 도움의 손길점 고릴리아갤로그로 이동합니다. 13.02.07 59 0
345269 어휴...... 경제가 진짜 개판인가보다 [1] 멍청닭(182.211) 13.02.07 99 0
345268 채용 공고에 석식 지원, 야간교통비 지급 이거 어떻게 생각해야되냐? [6] classical(175.200) 13.02.07 178 0
345266 청마린 소희빠아님? 로레알갤로그로 이동합니다. 13.02.07 37 0
345265 세탁소에 컴퓨터세탁이라고써있는거 물세탁이냐 [5] 로레알갤로그로 이동합니다. 13.02.07 119 0
345264 리닉스 초짜 질문 좀 드립니다ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ [3] ㅇㅇ(121.162) 13.02.07 97 0
345263 C++ STL에 정규식 넣으면 참 좋을텐데.. [2] ㅇㅇ(58.102) 13.02.07 121 0
345261 리버싱이라는것의 정확한 뜻이뭐냐?? [4] 엽산.갤로그로 이동합니다. 13.02.07 158 0
345260 교육원같은데 나오면 취직됨? [1] (168.126) 13.02.07 89 0
345259 역시 프겔은 여자 청정 지역이군 G컵녀 말이 없네? [5] 멍청닭(182.211) 13.02.07 347 0
345258 통신병가면 뭔일하냐?? 컴터관련일하나??? [4] 엽산.갤로그로 이동합니다. 13.02.07 120 0
345257 프로그래밍카페에서 디씨말투로질문햇는데 욕먹엇다; [10] 엽산.갤로그로 이동합니다. 13.02.07 257 0
345256 알고리즘 공부할려고 하는데 이산수학에서 꼭 필요한부분 캐치좀... [5] 이산수학(211.104) 13.02.07 352 0
345255 장의력하믄 백왕이제~~ [2] 코불(211.172) 13.02.07 76 0
345252 야 프로그래밍 공부 넘 열심히 하다 이명현상 걸렸다 ㅡㅜ [2] 상상(175.123) 13.02.07 115 0
345251 야 SA는 어떠냐 ㅇㅇ(58.102) 13.02.07 75 0
345250 실존하는 소스관리 안하는 회사이야기 [5] 123(61.72) 13.02.07 233 0
345249 Direct3D device가 뭐야? ㅁㄴㄻㄹ갤로그로 이동합니다. 13.02.07 164 0
345248 m&s 분야 어떤가여? topandbottom갤로그로 이동합니다. 13.02.07 47 0
345246 inet_ntoa 로 strcmp 비교했을때 좀 문제가있는데.. [3] 늘모(175.208) 13.02.07 90 0
345245 소스 관리 도구 안쓰는 회사는 망해야 한다. [10] 물속의다이아갤로그로 이동합니다. 13.02.07 268 0
345244 필드 가보면 다소 황당한 경우들이 많은데 [4] ㅇㅇ(58.102) 13.02.07 158 0
345243 언어별 소수연산 벤치마킹 [4] 123(210.103) 13.02.07 204 0
345240 내가 윈도우 모바일(ce,wm)쪽 프로그래머인데 닷넷을 싫어하는 이유가. [12] ㅇㅇ(58.102) 13.02.07 228 0
345239 우와 오늘 춥다! 외계달팽갤로그로 이동합니다. 13.02.07 43 0
345238 UI는 WPF가 최고 최강 이죠^^. [26] 123(210.103) 13.02.07 341 0
345237 아쉽다. 청마린 떡밥을 못 물었네 ㅇㅇ(58.102) 13.02.07 45 0
345236 제가 머리가 나빠서 하드웨어 팝니다. [3] d861ch(211.207) 13.02.07 117 0
345235 비주얼 베이직6 학원댕기는데... [6] 연금술사(220.149) 13.02.07 261 0
345231 7급공무원, 아이리스에 댓글알바 요원 왜 안나오고 총쏘면서 개폼잡습니까? 에어로홍갤로그로 이동합니다. 13.02.07 78 0
345230 출근길 지하철에선 앉아가면 안되는듯 싶다 S-GUN갤로그로 이동합니다. 13.02.07 85 0
345229 현직자들 있어?? 고민상담좀... [4] sk(112.170) 13.02.07 152 0
345227 요즘 디씨 불경기냐 [1] 땡칠도사갤로그로 이동합니다. 13.02.07 84 0
345226 성님들 [3] ㄴㄴ(1.240) 13.02.07 66 0
345225 전문대생이 궁금해서 물어봐요 [15] ㅁㅅㅋ(183.106) 13.02.07 279 0
345223 리버싱 [7] 0000(203.226) 13.02.07 140 0
345222 형들 이것좀 봐줘바.. c++ [9] cout(61.34) 13.02.07 136 0
345221 화가들이 노닥거리는 곳에 와선 [1] sfuh(59.7) 13.02.07 53 0
345219 vs2012 깔아서 코드 몇개 디버그해봤는데 [3] d861ch(211.207) 13.02.07 130 0
345218 심심해서 만들어보는... [7] 땡칠도사갤로그로 이동합니다. 13.02.07 223 0
345217 자라 루비•‿•갤로그로 이동합니다. 13.02.07 63 0
345216 프갤에서 정모하면... [2] 93년생미필(1.242) 13.02.07 123 0
345215 낚시류 甲 프로그래머로써 이렇게 살지는 말자. ㅇㅇ(39.119) 13.02.07 254 1
뉴스 수지, 뷔와 프랑스 파리에서 만나… 달콤한 눈맞춤 ‘포착’ 디시트렌드 07.09
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2