디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 80 추천 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/06/30 - -
337377 프리 월봉 1천 어캐 넘기냐 [1] 천회장(118.131) 12.11.15 112 0
337376 지금 과제하다가 머리 터질듯... [7] ㅇㅇ(128.134) 12.11.15 155 0
337374 일거리 들어왔따 [3] 정수정•‿•갤로그로 이동합니다. 12.11.15 107 0
337373 구직 하다 안되면 창업이나 할까염 구직중(182.211) 12.11.15 44 0
337372 풀어 ㅁㅁ(165.246) 12.11.15 28 0
337371 아~!!!! 미치겠어!!! [3] 코더(112.216) 12.11.15 60 0
337370 일 더하기 일은~ 귀 (1.214) 12.11.15 35 0
337369 형들잇자나여 [3] WannaParty갤로그로 이동합니다. 12.11.15 46 0
337368 횽들 이거 C++인데 좀 봐봐 [2] ...(210.182) 12.11.15 69 0
337367 거지 무시하지 마세연. [2] ☎2.51™갤로그로 이동합니다. 12.11.15 62 0
337366 그렇게 죶같으면 왜함? [2] 고랭?갤로그로 이동합니다. 12.11.15 83 0
337365 프로그래밍 개발자가 얼마나 인기가 없는지 알수 있다 [1] 구직중(182.211) 12.11.15 131 0
337364 경력 시즌 지나간거야? 그런거야? [1] 프갤러(49.1) 12.11.15 51 0
337363 친구들이 개발한다고 하니 다들 깜놀한다. [4] 구직중(182.211) 12.11.15 176 0
337362 아는 꼬맹이가 비트간다던데 [2] 농농이랑마군갤로그로 이동합니다. 12.11.15 158 0
337361 아오 모르겠다 그냥 이력서 전부 다 써야지 ㅎㅎㅎ [3] 구직중(182.211) 12.11.15 54 0
337360 형들 이코드가 어떤식으로 돌아가는거야? [45] WannaParty갤로그로 이동합니다. 12.11.15 211 0
337359 db고수님들 erp가 설계분야인가요? [4] ㅁㄴㅇㅁ(59.11) 12.11.15 88 0
337358 VS 2012의 문제점 [4] TUNA갤로그로 이동합니다. 12.11.15 123 0
337357 씨발 진지빨고 글쓰는데 IT쪽으로 온거 존나 후회스럽네... [3] 멘탈붕괴_갤로그로 이동합니다. 12.11.15 122 0
337356 아무리 생각해도 로우레벨로 가야겠다 [4] 구직중(182.211) 12.11.15 84 0
337353 linux 64bit vs 32bit 에 관련해서 잘 정리된 문서 없을까 [2] vF(175.113) 12.11.15 36 0
337352 사장님 기분이 안좋아보인다 [9] 정수정•‿•갤로그로 이동합니다. 12.11.15 108 0
337351 PHP 하나 물어봐도 됨? [4] 농농이랑마군갤로그로 이동합니다. 12.11.15 59 0
337350 100바이트 패킷 한번 보내기 vs 50바이트 패킷 2번 보내기 멘탈붕괴_갤로그로 이동합니다. 12.11.15 44 0
337349 요즘 저 이거에 중독됨 [1] Kyle(220.244) 12.11.15 76 0
337348 지금 닥치는대로 이력서 쓰고 돌아왔음 구직중(182.211) 12.11.15 31 0
337347 형들 프로그래머 중소기업가면 초봉 2500도 힘듬? [3] doragee(183.101) 12.11.15 324 0
337346 우와~ 지금 취업하려고 쭉 검색 중인데 [1] 구직중(182.211) 12.11.15 78 0
337344 혹시 임베디드 아두이노쪽 아시는분 계신가요... [2] doragee(183.101) 12.11.15 119 0
337343 요즘 구직활동 중인데 피해야할 회사를 알려주세요~ [2] 구직중(182.211) 12.11.15 84 0
337342 현재까지 희망퇴직을 실시하기로 한 대기업들. [2] ☎2.51™갤로그로 이동합니다. 12.11.15 104 0
337339 대통령 후보들에게 TV 토론회 때 할 질문을 올릴수 있데 선거(182.211) 12.11.15 30 0
337338 안드로이드 개발도구 많이 편해졌내?? [2] 고랭?갤로그로 이동합니다. 12.11.15 136 0
337337 신입 SI가 나음? 솔루션업체가 나음? [4] 고민(211.60) 12.11.15 193 0
337336 텍사스 악기상 OMAP 포기 dot(59.5) 12.11.15 46 0
337335 내년에 구조조정 엄청나게 한다는군 구직중(182.211) 12.11.15 55 0
337334 마지막 프로젝트는 내맘대로 설계. 씨발라드세요갤로그로 이동합니다. 12.11.15 35 0
337333 제로보드 보안관련 질문 [1] appllee갤로그로 이동합니다. 12.11.15 53 0
337332 빨갛 빨갛 하게 해줄까? [4] 때릴꺼야?(116.40) 12.11.15 67 0
337331 횽들이 생각하기엔 IT 분야에서 어느게 제일 좋을꺼 같어?? [5] 구직중(182.211) 12.11.15 347 0
337330 4년제 전공이면 최소 연봉이 얼마임?? [3] 구직중(182.211) 12.11.15 192 0
337328 링크 걸어줄때.. 부끄곰갤로그로 이동합니다. 12.11.15 42 0
337326 모의해킹 하는 애들 메타스플로잇 쓰냐 [6] ㅁㅁ(183.99) 12.11.15 211 0
337325 삼성 엘지에서 개발자들 쭉쭉 흡수하고 있다는게 사실임?? [1] 대기업(182.211) 12.11.15 135 0
337324 요새 호주 비자 잘 안준다메? [1] asdasd(59.11) 12.11.15 58 0
337323 경력이 지저분하고 많이 쉬었는데 연봉을 얼마 정도로 때리면 좋을까?? [1] 경력고민(182.211) 12.11.15 116 0
337322 안녕하세요? 오랜만입니다 HarlemLuiah갤로그로 이동합니다. 12.11.15 43 0
337321 형들 급해 ㅠㅠ CentOs에서 vi쓸때 블록지정 어떻게 해?? [1] ㅎㅇㅇ(121.128) 12.11.15 45 0
337319 니들도 호주로 가라 ㅇㅇㅇㅇㅇㅇㅇ [6] asdasd(59.11) 12.11.15 192 0
뉴스 [티처스2] 조정식, 솔루션 최초 실패! 결국 재도전 사태...“영어에서 희망 보이지 않아” 충격! 디시트렌드 06.29
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2