유닉스 내부 명령어 mkdir 구현 질문입니다.

bexu의 이미지

#include
#include
#include
#include
#include
#include
#include
#include

#define SZ_STR_BUF 256

char *cmd;
char cmd_line[SZ_STR_BUF];

typedef struct {
char *cmd;
void (*func)();
} cmd_disp_t;

cmd_disp_t cmd_disp[] = {
//{ "cp", cp },
//{ "echo", echo },
//{ "help", help},
//{ "pwd", pwd },
//{ "quit", quit },
//{ "rm", rm },
//{ "cat", cat },
//{ "cd", cd },
{ "mkdir, mkdir },
//{ "rmdir, rmdir },
};

int num_cmd = sizeof(cmd_disp) / sizeof(cmd_disp[0]);

void
proc_cmd(void)
{
int k;

cmd = strtok(cmd_line, " \t\n");
if (cmd == NULL)
return;
for (k = 0; k < num_cmd; ++k)
if (!strcmp(cmd, cmd_disp[k].cmd)) {
cmd_disp[k].func();
break;
}
if (k == num_cmd)
printf("%s : Command not found.\n", cmd);
}

int
main(int argc, char *argv[])
{
char buf[1024];

struct stat st_buf;

setbuf(stdout, NULL);
setbuf(stderr, NULL);

fstat(STDIN_FILENO, &st_buf);

for (;;) {
if (S_ISCHR(st_buf.st_mode))
getcwd(buf, 1024);
printf("[%s]: ", buf);
gets(cmd_line);
proc_cmd();
}
}

이렇게 메인만 선언해 놓구
mkdir 함수를 따로 만들어서 호출해서 쓸려고 하는데.. 잘 안되네요

머릿속에서는 대충 그려지는거 같은데.. 손이 안잡히네요
어떤식으로 만들면 좋을지 조언좀 부탁드립니다.

cinsk의 이미지

묻고자 하는 게 뭔지 모르겠군요.

mkdir을 구현하고 싶다고 했는데, 코드를 보니, shell 기능을 하는 프로그램을 작성하는 것 같은데, 간단히 mkdir(2)을 호출하면 되지 않을까요?

참고로 mkdir(1)은 mkdir(2)로 구현되어 있을테고, mkdir(2)는 system call이니, 사용자가 그 내부를 구현할 수 없습니다.

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

댓글 달기

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