에러가 나는 원인이 궁금합니다..

tlqkdto89의 이미지

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#define MAX_CMD_ARG 10
 
const char *prompt = "myshell> ";
char* cmdvector[MAX_CMD_ARG];
char  cmdline[BUFSIZ];
 
void fatal(char *str)
{
	perror(str);
	exit(1);
}
 
void command_cd(char* path)
{
	chdir(path);
}
 
int makelist(char *s, const char *delimiters, char** list, int MAX_LIST)
{	
  	int i = 0;
  	int numtokens = 0;
  	char *snew = NULL;
 
  	if((s==NULL) || (delimiters==NULL))
	{
		return -1;
	}
 
	snew = s + strspn(s, delimiters);	
  	if((list[numtokens] = strtok(snew, delimiters)) == NULL )
    	{
		return numtokens;
	}
 
  	numtokens = 1;
 
  	while(1)
	{
	     	if((list[numtokens] = strtok(NULL, delimiters)) == NULL)
		{
			break;
		}
     		if(numtokens == (MAX_LIST-1))
		{ 
			return -1;
		}		
     		numtokens++;
  	}	
 
	return numtokens;
}
 
int main(int argc, char**argv)
{
  	int i = 0;
  	pid_t pid;
  	while (1) 
	{      
  		fputs(prompt, stdout);
		fgets(cmdline, BUFSIZ, stdin);
		cmdline[ strlen(cmdline) -1] ='\0';
 
		makelist(cmdline, " \t", cmdvector, MAX_CMD_ARG);
 
		if(!strcmp(cmdline, "exit"))
		{
			exit(1);
		}
		if(!strcmp(cmdvector[0], "cd"))
		{
			command_cd(cmdvector[1]);
		}
 
		switch(pid=fork())
		{
		case 0:
			makelist(cmdline, " \t", cmdvector, MAX_CMD_ARG);
			execvp(cmdvector[0], cmdvector);
			fatal("main()");
		case -1:
  			fatal("main()");
		default:
			wait(NULL);
		}
 
  	}
  	return 0;
}

exit, cd를 구현하였는데 cd를 실행하면 디렉토리 이동은 가능한데 fatal("main()"); 이 부분이 실행됩니다..
혹시 제가 구현한 것이 어떤 문제가 있는지 궁금합니다.. 감사합니다.
raymundo의 이미지

cd 처리를 한 후에도 여전히 fork 하고 case 0 에 들어가서 execvp 를 호출하니깐요.

그러나 cd 는 외부 프로그램이 아니다보니 찾지를 못해서 exec 이 실패하고, 그 다음 fatal()을 호출하죠.

command_cd 처리한 후 continue 로 바로 루프를 재반복시키면 될 것 같습니다.

좋은 하루 되세요!

익명 사용자의 이미지

실행하다 보면 cd를 구현한 뒤로는 mkdir rmdir touch 같은 내장명령어가
실행되지 않습니다.. 왜 그런지 알 수 있을까요.?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.