디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 73 추천 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/05/27 - -
218880 오늘은 힘이 안나 [6] 해일리갤로그로 이동합니다. 10.11.19 86 0
218879 프겔이 처음이라면 이글부터 봐라 !!! [3] 칭송받는자(210.118) 10.11.19 123 0
218878 네임드가 되고 싶은 찌질이 입니다. [5] 칭송받는자(210.118) 10.11.19 98 0
218877 오락실에서 쳐맞는 이유 [3] Ordinary Day갤로그로 이동합니다. 10.11.19 129 0
218876 프갤러가 되려면 어떻게해야해용? [2] 나는scv갤로그로 이동합니다. 10.11.19 68 0
218875 리눅스jsp 웹호스팅할라는데 리눅스 몰라도됨? [4] 핸드메이드(58.233) 10.11.19 102 0
218874 운영체제 공부하기 좋은책점 [10] 고칼로리갤로그로 이동합니다. 10.11.19 195 0
218873 아아 좋은 [2] 땡칠도사갤로그로 이동합니다. 10.11.19 82 0
218872 힘든 하루였따. [2] Gromit갤로그로 이동합니다. 10.11.19 92 0
218871 무언가 작품을 시연할때의 미스테리. [4] 연화갤로그로 이동합니다. 10.11.19 117 0
218870 오랜만에 프갤에 글을 싸본다. [16] 연화갤로그로 이동합니다. 10.11.19 152 0
218867 오디오 테스트에 관해서 [17] Vita500갤로그로 이동합니다. 10.11.19 212 0
218864 이바보 저리가! [20] Minryu갤로그로 이동합니다. 10.11.19 208 0
218863 19금 [9] rntjr갤로그로 이동합니다. 10.11.19 186 0
218862 19금 [10] Ordinary Day갤로그로 이동합니다. 10.11.19 159 0
218861 X,Y쌍 찾는 거 O(n)이야. [13] Gromit갤로그로 이동합니다. 10.11.19 160 0
218860 리브로50% 책이 오늘 드디어 다 왔다 ㅎㅎ [11] 김늅늅(61.40) 10.11.19 115 0
218858 아 배고파 [17] monoless갤로그로 이동합니다. 10.11.19 101 0
218856 회사로의 rollback 그 후 [19] 해일리갤로그로 이동합니다. 10.11.19 166 0
218853 시험을 무사히 끝내고 온 나 자신에게 선물을 사왔어. [16] Minryu갤로그로 이동합니다. 10.11.19 198 0
218852 폐가 썩어들어가고있어,,,,,, [9] 키사수(115.93) 10.11.19 101 0
218850 혹시 지금 깨어있는 횽들 있어? [17] 물속의다이아갤로그로 이동합니다. 10.11.19 108 0
218848 수익5:5다 쇼핑몰같은거 만드는능력자없냐 [12] 칸토캣(차단)갤로그로 이동합니다. 10.11.19 112 0
218847 쇼핑몰같은거 만들수있는 능력자없냐?? 급하다; [9] 칸토캣(차단)갤로그로 이동합니다. 10.11.19 125 0
218846 어휴 힘들다 [9] 유리한갤로그로 이동합니다. 10.11.19 96 0
218845 뇌가 마비된 느낌 [10] 땡칠도사갤로그로 이동합니다. 10.11.19 167 0
218844 비전공 백지상태에서 SNS 만들려면 최소 6개월은 걸린다. [7] 홍어(58.233) 10.11.19 180 0
218843 자바 입문하고픈데 괜츈한데 없음? [10] ㄱㄱ(119.192) 10.11.19 90 0
218840 [질문]병렬처리 프로그래밍 [10] 940(125.178) 10.11.19 130 0
218839 혹시 버퍼오버플로어 잘하시는 분 있으신가요??ㅠㅠㅠ [17] 전설의대지갤로그로 이동합니다. 10.11.19 245 0
218837 저도 이만 퇴갤요 [1] 별가사리(122.40) 10.11.19 67 0
218834 포인터 공부하는데 오버플로우가 씹썌끼 [2] 311123113(211.172) 10.11.19 89 0
218832 수능난이도가 상승했나봐 [1] 이모군(1.225) 10.11.19 67 0
218829 님들 스스로 코딩하는 프로그램 개발좀 [3] c(211.205) 10.11.19 229 0
218827 수능 망친 꼬꼬마들을 위한 서비스 [2] 별가사리(122.40) 10.11.19 186 0
218826 도코모도 안드로이드 마켓 열었네 ㅋ [2] 푸른숲갤로그로 이동합니다. 10.11.19 100 0
218825 아 너무 쳐잤다... [6] 별가사리(122.40) 10.11.19 116 0
218824 아키텍트 학벌영향 많이 받나용? [9] 마타버터갤로그로 이동합니다. 10.11.19 169 0
218823 퇴근했다 [1] Rei@디씨갤로그로 이동합니다. 10.11.18 47 0
218821 아나 서보모터 시발... [2] 생각놀이갤로그로 이동합니다. 10.11.18 90 0
218820 할일없는놈 이거나 증명좀해줘 [10] ㄴㅇㄹ(112.187) 10.11.18 79 0
218819 부산을 정ㅋ벜 [3] 초밥술사갤로그로 이동합니다. 10.11.18 92 0
218815 프갤님들... 이거 한번만 봐주세요... [6] Stygia갤로그로 이동합니다. 10.11.18 128 0
218812 그녀를 찾아주세요. [3] Vita500갤로그로 이동합니다. 10.11.18 123 0
218811 지금 잠이 옵니까? [2] 분당살람갤로그로 이동합니다. 10.11.18 104 0
218810 비주얼스튜디오 2008 서비스팩 말이죠 [10] 웅왘(115.137) 10.11.18 218 0
218808 오늘을 기점으로 또 경쟁자가 수만명 생겨나겠군.. [7] ㅇㅇㅇㅇ(112.171) 10.11.18 168 2
218804 유리한 보아라 [2] 유리한추종자(118.91) 10.11.18 98 0
218803 잡힐 듯 잡힐 듯 안 잡히는 JAVA [4] gisman갤로그로 이동합니다. 10.11.18 161 3
218802 sql쿼리 튜닝때문에 집에 가다가 회사로 다시 rollback [7] 해일리갤로그로 이동합니다. 10.11.18 107 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2