디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 81 추천 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 - -
AD 휴대폰 바꿀까? 특가 구매 찬스! 운영자 25/07/02 - -
공지 프로그래밍 갤러리 이용 안내 [88] 운영자 20.09.28 45102 65
2869232 Gemini VS ChatGPT VS Claude VS Cursor ㅂㅂ(116.82) 21:43 7 0
2869231 대강 윈 7에서 파이썬으로 키움 힘들던게 [6] ㅆㅇㅆ(124.216) 21:36 47 0
2869229 아니 들어봐 내가 실력이 없어서 못만든게 아님... [22] ㅆㅇㅆ(124.216) 21:13 84 0
2869227 진지하게 저보다 인생 못난 사람이 존재하긴 할까요?? [1] ㅇㅇ(223.38) 21:11 19 0
2869225 한투 행님들 API 문서화해둔거 깔쌈하시네 진짜. [7] ㅆㅇㅆ(124.216) 21:02 42 0
2869223 아프리카티비는 문재인 이후부터 갑자기 좇나 재미없어졌음 뒷통수한방(1.213) 21:01 16 0
2869221 와 근데 한투 얘네 대단하다 ㅆㅇㅆ찡갤로그로 이동합니다. 20:57 26 0
2869220 모의 CRC 만들어서 우회하는 실습했다 [2] 루도그담당(58.239) 20:57 53 0
2869219 유심 복제 및 스와핑 해킹 사기 조심해라 ㅇㅇ(211.246) 20:56 26 1
2869218 윈도우10에서 업그레이드 절대안하는이유 프갤러(1.213) 20:52 18 0
2869217 디자이너랑 30만원 내기함 누구 말이 맞는지 봐주라 [1] ㅇㅇ(211.235) 20:49 26 3
2869215 마소 ceo가 윈도우설치하면 제일 먼저 하는 일 프갤러(106.241) 20:36 23 0
2869214 유튜브 쇼츠 이거 틱톡하고 존나 똑같네 뒷통수한방(1.213) 20:33 14 0
2869213 내가 윈도우 커널을 건드려가지고 이스라엘 방공망이 뚫렸네... [2] 넥도리아(121.139) 20:29 36 0
2869212 친구 아버지 장례식 왔는데 [7] 아스카영원히사랑해갤로그로 이동합니다. 20:11 63 0
2869210 당연히 내가 맞지. 나는 원문 들고와서 이야기하는데 반박하는 애들은 [2] ㅆㅇㅆ(124.216) 19:59 49 0
2869208 ㅆㅇㅆ랑 루비 논쟁 지피티 결과 ㅇㅇ(61.75) 19:45 32 0
2869206 여름철 내몸냄새확인법 ㅇㅇㅇㅇ(115.144) 19:44 23 0
2869205 짤녀는 1분 후 어떻게 될거같음? [1] 매쿠이료갤로그로 이동합니다. 19:41 35 0
2869203 싸운거 궁금해서 지피티한테 물어보니까 ㅆㅇㅆ가 맞는말이라는데 [2] ㅇㅇ(61.75) 19:39 50 0
2869201 추억의 만찐두빵⭐+ ♥냥덩이♥갤로그로 이동합니다. 19:33 11 0
2869199 흠.. 잘하면 올해내로 끝낼수 있겠군 [2] ♥냥덩이♥갤로그로 이동합니다. 19:26 22 0
2869198 내가 코드 짜는 방법과 지하철 요금 150원 인상 프갤러(121.172) 19:25 39 0
2869196 그 보석: "오픈소스 감놔라 배놔라 해대서 힘들었습니다." [2] 프갤러(27.169) 19:12 33 0
2869195 [애니뉴스][공지] 문서공간 상품화 방법 프갤러(121.172) 19:08 32 0
2869193 신입 인턴인데 사수가 프갤하는거 봤는데 프갤러(106.101) 19:05 32 0
2869192 반팔티 긴바지 가위로 잘라서 나시하고 반바지만듬 뒷통수한방(1.213) 19:04 11 0
2869191 보석이 임베디드도 모르고 동적 링크도 모르네 프갤러(27.169) 19:02 24 0
2869190 [애니뉴스] 트루 티어즈! - 오리지널 하렘 프갤러(121.172) 19:01 22 0
2869188 훗, 그런 도발에는 안 넘어간다- 프갤러(121.172) 19:00 24 1
2869187 레즈 = 레이즈 몰이 [1] 프갤러(121.172) 18:58 33 1
2869185 그 새끼가 하도 나대서 님프 찾아봤는데 프갤러(27.169) 18:53 29 0
2869183 RxFramework의 위대함- [3] 프갤러(121.172) 18:50 38 0
2869182 그 보석 새끼:"리눅스 커널은 동적링크를 써서 크기가 작습니다" 프갤러(223.33) 18:50 15 0
2869180 나는 예전부터 저런 타입이 이해가 안감. 학벌이야 잘날 수 있지. [2] ㅆㅇㅆ(124.216) 18:46 35 0
2869179 야 121.172야 SOLID는 실무서 나온거고 SRP는 학회서 나온거임 [2] ㅆㅇㅆ(124.216) 18:40 40 0
2869178 SRP - OOP 반박글 [3] 프갤러(121.172) 18:37 51 1
2869177 남성들은 이국적인 외모를 안좋아하는구나 [5] 루도그담당(58.239) 18:36 68 0
2869176 문명 도중 친구 아버지 돌아가셔서 장례식 가는 중 [11] 아스카영원히사랑해갤로그로 이동합니다. 18:32 54 0
2869175 저 석사 다녔다는놈 기억남 SRP없어도 OOP 성립한다고 하던놈 [1] ㅆㅇㅆ(124.216) 18:31 38 0
2869174 이 사람 이쁘지않냐? [2] 루도그담당(58.239) 18:30 53 0
2869173 앱 키워드 유료 효과좀 있냐? 프갤러(223.38) 18:29 10 0
2869172 오늘도 고생하셨습니다. [2] 개멍청한유라갤로그로 이동합니다. 18:28 29 0
2869171 [대한민국] 윤석열 대통령이 만든 업적! 체코 원전 계약 24조! 프갤러(121.172) 18:25 22 0
2869170 얼씨구 이년봐라? [2] 개멍청한유라갤로그로 이동합니다. 18:23 34 0
2869169 대체 나랑 키배떴다는 애는 뭘로 키배떴단거냐 [1] ㅆㅇㅆ찡갤로그로 이동합니다. 18:21 29 0
2869168 입이 딱 벌어지지 썅년들아 [7] 개멍청한유라갤로그로 이동합니다. 18:21 40 0
2869167 도커 빌드는 어떻게 하는거냐... ㅇㅇ(112.187) 18:17 22 0
2869166 인생 별거 있냐?? 뭐 대단한 인생이라고 이렇게 고민이 많을까... [1] ㅇㅇ(223.38) 18:16 17 0
뉴스 블랙아이드필승 라도, KIA 홈경기 시구 출격! 고향팀 KIA 타이거즈 승리 기원 디시트렌드 07.01
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2