디시인사이드 갤러리

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

갤러리 본문 영역

형들 좀 도와줘 ㅠ

...(183.106) 2010.10.05 14:20:49
조회 76 추천 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/06/24 - -
225789 파이썬으로 프로그래밍 할 때 홑따옴표랑 쌍따옴표 어떻게 구분해서 써? [4] 페이퍼(112.153) 10.12.19 253 0
225788 프로그래머에 대한 망상 [4] 컴맹늅늅이(121.135) 10.12.19 325 0
225787 프로그래머로써 대성하고 싶은가.. [3] 홍어(218.51) 10.12.19 298 0
225786 후로그래밍 취업하면 노가다라고하던데 [6] 금고래갤로그로 이동합니다. 10.12.19 329 0
225785 포트란은 도대체 뭐하는 언어인고.... [5] 컴맹늅늅이(121.135) 10.12.19 179 0
225784 SI할바에야.. 80만원 알바나 하겠다. [4] 홍어(218.51) 10.12.19 368 0
225783 점심에 황태구이 정식을 먹고왔는데... [4] Rei@디씨갤로그로 이동합니다. 10.12.19 147 0
225782 Fuck that world 허파맛삼치(110.15) 10.12.19 71 0
225781 통계학과인데 컴공 부전공할까 생각중인데 [7] AAA(211.108) 10.12.19 351 0
225780 님들 후로그래밍과나오면 보통 취업 얼루하나영? [14] 금고래갤로그로 이동합니다. 10.12.19 302 0
225779 열혈강의c 정수 표현 방식에서 [16] Hello,World갤로그로 이동합니다. 10.12.19 236 0
225778 노트북 좋은거 하나 사서 오래 써볼려고 하는데 얼마나 갈까? [7] ㅇㅇ(203.241) 10.12.19 195 0
225777 근데 왜 굳이 프로그래밍 입문자에게 C를 가르치려 할까 [21] 컴맹늅늅이(121.135) 10.12.19 385 0
225775 네트워크 다시 질문점... [1] ㄹㅇㄴㄹ(119.196) 10.12.19 76 0
225773 가입하고 헤일로2게임 받아가세요 ~ 가입행(114.206) 10.12.19 132 0
225771 C에서 진수 변환 질문점 [4] Hello,World갤로그로 이동합니다. 10.12.19 98 0
225768 Bye, World! [3] elwlwlwk갤로그로 이동합니다. 10.12.19 106 0
225767 혹시나해서 여기다도 올림 형들아 나 컴퓨터가 심각해 도와줘 ㅠㅠ [10] 이교도갤로그로 이동합니다. 10.12.19 142 0
225766 Reading ZIP File Format #3, 로컬 파일 헤더[임시] [1] DMW(125.138) 10.12.19 105 0
225765 작업환경 종결 [1] 밀맛시럽(110.15) 10.12.19 168 0
225762 html 짝이 안맞다네.. 원인이 뭘까 [9] BTTTS!갤로그로 이동합니다. 10.12.19 128 0
225760 으아 부왘!!!!!!!!!!!!!!!!!!!11 [3] 테징징(121.146) 10.12.19 2534 0
225759 주문해버렸다... [2] --;;(124.153) 10.12.19 182 0
225758 프갤횽들 늊비의 오류를 해결해줘요 [4] 테징징(121.146) 10.12.19 87 0
225756 횽들 산업자동화기사는 왜 산업기사만 있어요?? 도도(114.202) 10.12.19 66 0
225755 컴파일러랑 운영체제 책 살려고 하는데... [2] --;;(124.153) 10.12.19 162 0
225754 오늘 하루 계획 [2] DMW(125.138) 10.12.19 153 0
225752 visual studio 2010 한글정품 구하는방법 [3] 개초보1단계(124.56) 10.12.19 342 0
225750 이제막 배우는 중인데요. 대문자랑 소문자 [3] 개초보1단계(124.56) 10.12.19 81 0
225749 프갤 리셋 했었나요?? 호돌이(110.76) 10.12.19 48 0
225748 얍! 기초 물리학 실험 1등했다 ㅋㅋㅋㅋㅋㅋㅋㅋㅋ [3] 로레알갤로그로 이동합니다. 10.12.19 206 0
225747 밤새서 간츠 20권까지 다봄 [3] 꿀레갤로그로 이동합니다. 10.12.19 260 0
225746 C++/Java/O-C만 배운 학생입니다. [3] Mayu갤로그로 이동합니다. 10.12.19 162 0
225745 이게 뭔 소리일까요 [3] -(218.148) 10.12.19 149 0
225744 구글맵좀 뛰워볼려다 하루가 다가네 홍어(218.51) 10.12.19 46 0
225743 아ㅏㅏ 도와주세욤 ㅠ_ㅠ [3] 긔요미(124.137) 10.12.19 70 0
225742 혹시 퇴마요새 하던 갤러 있냐? [1] 유리한갤로그로 이동합니다. 10.12.19 251 0
225741 오랜만에 던파를 했어 [4] 홍어(218.51) 10.12.19 93 0
225740 횽아들 KMP알고리즘 경계정보 계산 질문이야 [11] 앙아앙아(61.100) 10.12.19 296 0
225739 소켓에서 받아온걸 CString에 넣으니 어째 깔끔하게 안되는 느낌? [1] 르하소갤로그로 이동합니다. 10.12.19 60 0
225738 오늘 시험보고와서 할일. [9] Minryu갤로그로 이동합니다. 10.12.19 225 0
225737 내일은 기말고사 유리한갤로그로 이동합니다. 10.12.19 77 0
225736 징징짜지 말고 차라리 어그로를 올려라. ㅁㄴㅇㄹ(175.200) 10.12.19 102 0
225733 c++에서도 memset써? [3] 르하소갤로그로 이동합니다. 10.12.19 170 0
225731 디시에 악성코드 심어졌음? [2] ㅇㅇㅇ(211.198) 10.12.19 142 0
225730 신난다! [3] 꿀레갤로그로 이동합니다. 10.12.19 98 0
225729 형아들 되고싶어 [1] 미클갤로그로 이동합니다. 10.12.19 120 0
225728 형아들 살고싶어 [3] 아주아슬갤로그로 이동합니다. 10.12.19 123 0
225727 형아들 죽고싶어 [12] 르하소갤로그로 이동합니다. 10.12.19 221 0
225726 먹튀하는 새끼 개새끼 아주아슬갤로그로 이동합니다. 10.12.19 128 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2