[완료]mkdir 명령어 구현 질문 입니다.

svsummon의 이미지

항상 여기 도움 받는 학생입니다 'ㅅ';;

과제를 하다가 또 막히는 부분이 생겨서 질문 드립니다 ㅠ

mkdir을 구현하는 것이 과제인데..

그냥 mkdir ./asd 이런 것은 그냥 asd 폴더를 만들면 되니까 쉬운데..

문제는 ./asd/qw 이런식의 폴더를 만들때 asd 폴더도 존재하지 않고 qw도 존재 하지 않는다면

연속된 디렉토리 생성에 오류가 생기더라구요..

지금 이 문제 때문에 과제 진행이 안된다는 ㅠㅠ

현재 생각 나는 것은.. '/'를 기준으로 문자열을 잘라서 chdir 후 만드는 것을 작업을 반복하는 건데..

이 경우 './' 에서 '.' 이 문제가 되더라구요..

이부분을 좀더 쉽게 할수 있는 알고리즘을 알고 계시면 답변 주시면 감사하겠습니다 (__)

답변 부탁드립니다 (__)

drinkme의 이미지

이러면 되지 않을까요?

int mkdir_p(char *path)
{
	char *tmp, *P;
	struct stat s;
 
	P = strdup(path);
	tmp = dirname(p);
 
	if (stat(tmp, &s) == 0)
	{
		if (S_ISDIR(s.st_mode) != 0)
			return -1;
	}
	else
		if (mkdir_p(tmp) == -1)
			return -1;
	free(p);
	mkdir(path);
}
ahsan의 이미지

if (!(S_ISDIR(s.st_mode))) <= 바꿔야 할 것 같습니다...

svsummon의 이미지

답변 감사합니다!

알려주신 코드를 가지고 실험을 해봤는데요..

오류가 나더라구요 =_=;;

 
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <unistd.h>
  6 #include <string.h>
  7 
  8 int mkdir_p(char *path)
  9 {
 10     char *tmp, *P;
 11     struct stat s;
 12     
 13     P = strdup(path);
 14     tmp = dirname(P);
 15     
 16     if(stat(tmp, &s) == 0)
 17     {
 18         if(!(S_ISDIR(s.st_mode)))
 19             return -1;
 20     }       
 21     else
 22         if(mkdir_p(tmp) == -1)
 23             return -1;
 24 
 25     free(P);
 26     mkdir(path);
 27 }
 28 
 29 main(int argc, char* argv[])
 30 {
 31     mkdir_p(argv[1]);
 32 }

위가 코드이고, 오류는 다음과 같이 뜨네요..

[asd@root 0928]$ gcc -o mkdir_p mkdir_p.c 
mkdir_p.c: In function ‘mkdir_p’:
mkdir_p.c:14: warning: assignment makes pointer from integer without a cast
mkdir_p.c:26: error: too few arguments to function ‘mkdir’

도움 부탁드려요 ㅠ

-----------

아 그리고.. 제가 지금 테스트중인데 이상한점이 한개 더 나왔네요 'ㅅ';;

dirname("./asd/qw");

이렇게 하면 dirname한 결과값이 ./asd 로 나오더라구요

아마 qw를 확장자가 없는 파일로 봐서 그런거 같은데..

만약 제가 asd와 qw 둘다 폴더로 가정하고 만드려면 어떻게 해야 될까요..?

계속 질문만 하는거 같아서 죄송합니다 ㅠㅠ

ahsan의 이미지

14번은 header file을 인크루드 안해서 생기는 것으로 보이고
26번은 아마도 mkdir 에 접근권환을 넣지 않아서 일겁니다.
mkdir("/asd/qw", 0755);

svsummon의 이미지

감사드립니다 ~_~)//

댓글 달기

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