디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 82 추천 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 - -
340349 자바스크립트의 렉시컬 특성이라는 의미가 [3] ㅁㄴㅇ(119.202) 12.12.11 84 0
340348 요즘 시대는 모바일, 임베디드 시대 아니냐? ㅁㄴㅇ(119.202) 12.12.11 90 0
340347 역시 자료구조는 [1] sadasd(220.120) 12.12.11 82 1
340346 컴공이라면 이런거 만져야지 [8] ㅁㄴㅇ(59.11) 12.12.11 305 1
340344 앞으로는 가상화 랑 클라우드 컴퓨팅이 미래다 [11] PandaMango갤로그로 이동합니다. 12.12.11 149 0
340342 DB에서 select into 문 점 알려주세요ㅠ [2] DB공부중(121.65) 12.12.11 49 0
340340 님들 이노래 제목 뭐임? [2] Kyle(220.244) 12.12.11 63 0
340338 [java교육/it교육]오라클DB기반 자바프레임워크 국비지원 자바취업교육 hanbit(115.94) 12.12.11 54 0
340336 컴퓨터 망가졌어 ㅠㅠ [8] Kyle(220.244) 12.12.11 161 0
340335 Buffer Overflow [2] ㅁㅁ(210.121) 12.12.11 58 0
340333 횽들 svn migration 질문좀 tmp(61.43) 12.12.11 33 0
340332 20000바이트짜리 이미지 udp로 전송하려면 어떻게 해야함요 ㅠ [3] 펔끄유갤로그로 이동합니다. 12.12.11 77 0
340331 횽들아 뮴뮴이 왔어염 ^ ㅅ^ [1] 뮴뮴이 ^ ㅅ^(59.25) 12.12.11 88 0
340330 횽들아 우리 뮴뮴이가 나오지 않아여 ㅠㅠ [3] 뮴뮴이 ㅠㅠ(59.25) 12.12.11 76 0
340329 홀펀칭 및 LTE, 패킷 사이즈에 관해서 질문 [2] 펔끄유갤로그로 이동합니다. 12.12.11 432 0
340328 오늘도 프갤은 평화롭습니다. (1.214) 12.12.11 37 0
340327 서양음식의 비밀 2가지. 1탄 [4] Kyle(220.244) 12.12.11 191 1
340326 공인ip에서 사설ip로 udp전송할때 홀펀칭 쓰는거 맞지? [4] 펔끄유갤로그로 이동합니다. 12.12.11 136 0
340325 webrtc가 온다 [1] 고랭?갤로그로 이동합니다. 12.12.11 83 0
340323 이런 형태의 암호화는 뭥미? [2] 아토케어갤로그로 이동합니다. 12.12.11 192 0
340319 으하핳 내일모레면 소멤 접수일이다 [2] 123(220.124) 12.12.11 142 0
340316 컴퓨터공학 학부생인데요. 현직자님들 복수전공 질문 좋다고소고기사묵겠지갤로그로 이동합니다. 12.12.11 187 0
340314 SI전망이 어떤가요??? [5] 좋다고소고기사묵겠지갤로그로 이동합니다. 12.12.11 244 0
340311 프로그래밍을 하려면 수학을 어느정도 알아야 한다고 하던데 '최대' [2] 컴맹새끼(221.155) 12.12.11 177 0
340310 일 가르쳐주는 사람이 없으니 실력이 안 는다 [3] 밍키맨(112.171) 12.12.10 77 0
340309 7년만에 찾아왔다. 7년전에 넥슨 입사문제 물어보고 도움받아 합격한 1인 [3] 쿵남(220.80) 12.12.10 306 1
340307 nhn 예비과정이라는것에 합격했는데 어찌 공부해야할까요 [6] 하잇후힝(211.243) 12.12.10 1557 0
340303 형들 제발 살려줘.. [2] 이현우(222.236) 12.12.10 67 0
340302 보면 은근히 웹개발자는 프로그래밍 살짝 못하는것처럼 보는 애들있음 ㅋ [3] Kyle(220.244) 12.12.10 281 0
340301 레노미나 [2] 육군야전출신(223.62) 12.12.10 84 0
340300 형들 c언어 이거 좀 부탁해욧!!?? [6] 질전문폭파범갤로그로 이동합니다. 12.12.10 170 0
340298 형들 나 이직됐어 [5] +어게인갤로그로 이동합니다. 12.12.10 159 0
340297 asp 데이터베이스 연동 아는사람 [3] ㅁㅁㅁㅁ(112.145) 12.12.10 58 0
340296 형들 데브로 컴파일했는데 에러뜸 이거 왜이런지좀알려주셈 ㅠㅠ [4] ㅁㅁ(218.156) 12.12.10 58 0
340293 전산관리... 어떤 일을 하는건가요? 또 어떤 능력이 필요한가요? [3] 학자갤로그로 이동합니다. 12.12.10 136 0
340291 리버싱님은 진리입니다. [2] 징기스칸4(125.182) 12.12.10 109 0
340290 유니티 배울려하는데 c#기본 전부다 마스터해야 유니티 제대로 사용할수있? 123123(175.215) 12.12.10 244 0
340289 공인ip에서 사설ip로 UDP패킷 전송 어떻게해요? [11] 펔끄유갤로그로 이동합니다. 12.12.10 114 0
340288 프갤 리눅스쓰는 형들, 이 문제좀 알려줘 [2] 태사웅(119.206) 12.12.10 64 0
340287 자바 예외조건 만드는 구문 이상한지좀봐줘 쎾쓰(175.197) 12.12.10 44 0
340286 데스크탑은 모바일리티가 갑 아닌가요? [4] ㅁㄴㅇ(59.25) 12.12.10 64 0
340285 3dmax 아무것도 모르는 초짜가 이책보고 배울려하는데 어떤지 좀 봐주셈 [2] 호구(175.215) 12.12.10 75 0
340284 다른업종은 뭐 잘나가는줄아냐 [2] ㅁㄴㅇㅁㄴㅇ(59.11) 12.12.10 110 0
340283 노트북은 당연히 휴대성..이 .. 아닐까 생각합니다.. [6] 123(61.106) 12.12.10 195 0
340282 내일 면접보는데요.. [2] 123(219.250) 12.12.10 87 0
340281 레티나맥북이 화면 분리까지되면 레전드 [2] 초보리버서(49.1) 12.12.10 105 0
340280 웹만들때 이렇게배울건데 이러면됨? [4] 잉잉1(175.215) 12.12.10 110 0
340279 내가 컴퓨터 IT 계열의 객관적인 현실을 알려주마. [6] 현실(14.45) 12.12.10 8182 14
340278 개발자 노트북은 당연히 맥북이지! [10] 123(61.106) 12.12.10 376 0
340277 나 자바 이것좀 도와줘!! 답 알려주면 사례함 치킨도쏠수있음 [20] 쎾쓰(175.197) 12.12.10 193 0
뉴스 방탄소년단, 애플뮤직 '10년 최다 재생곡' 34위 디시트렌드 07.08
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2