.

wlsgh604의 이미지
익명 사용자의 이미지

1. <code> 블럭을 써주세요. 안 그러면 #include 헤더 날아가고, indentation 날아가고, syntax highlighting은 꿈도 못 꾸게 됩니다.

올리신 글을 직접 보세요. 누가 이렇게 올라온 코드를 읽고 싶겠어요?

2. if (strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0)

왜 갑자기 뜬금없이 argv[0]와 비교하나요? 거기에 과연 어떤 문자열이 들어 있었을까요?

wlsgh604의 이미지

.

wlsgh604의 이미지

.

익명 사용자의 이미지

그렇게까지 급하게 과정 다 생략하고 결론으로 달릴 필요는 없을 텐데요.

과제물처럼 보이는데, 이런 과제의 경우 잘 동작하는 결과물 자체는 그다지 중요한 게 아닙니다.
잘 동작하는 프로그램을 만들기 위한 훈련이 중요하지요.
차근차근 해 봅시다.

사용자 입력이 exit이나 quit과 같은지 확인해야 할 텐데, 그럼 사용자 입력은 어디에 있을까요?
argv[0]은 왜 답이 아닐까요?

사용자가 " exit", "exit " 따위를 입력하면 어떻게 해야 할까요?
그런 경우도 처리할 수 있도록 구현하려면 어떻게 해야 할까요?

문제를 잘 이해하고, 작은 문제로 쪼개고, 하나씩 해결해 보면 됩니다.

wlsgh604의 이미지

.

익명 사용자의 이미지

답변이 달리니까 이제 질문 내용을 삭제하는 등의 먹튀를 하시네요?

익명 사용자의 이미지

어떤 코드가 있었더라...
기억나는 대로 한번 써 보죠.

#include <string.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <wait.h>
#define MAX_CMD_ARG 10 
 
const char *prompt = "(sp@resl$) ";
char* cmdvector[MAX_CMD_ARG];
char  cmdline[BUFSIZ];
 
void fatal(char *str)
{
	perror(str);
	exit(1);
}
 
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';
		if (strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0)
		{
			printf("EXIT\n");
			exit(0);
		}
		else
		{
			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;
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.