프로그램상에서 디렉토리를 만들때

soylove의 이미지

현재 경로에 디렉토리 만드는 프로그램을 짰습니다. main의 두번째 줄처럼 현재의 경로에 디렉토리를 만드는 것은 잘 되는데 첫번째줄처럼 아직 생기지 않은 test 디렉토리안에 t1이 생성되있는 이런 소스를 수행하니 에러가 납니다.
<에러메세지>
mkdir("./test/t1") returns -1: errno= 2 [No such file or directory]

커맨드에서는 mkdir 하고 -p옵션을 주면 된다고하는데 프로그래밍상에서는 이것을 어떻게 할 수 잇을지 조언 구합니다.
아래는 프로그램 소스입니다.(혹시몰라 인클루드파일까지 올립니다)

#include<unistd.h>
#include <errno.h>
#include <sys/stat.h>
main()
{
make_folder("./test/t1");
make_folder("./test"); //현재경로에 test디렉토리생김
}

void make_folder(const char *path)
{
int retcode;
errno = 0;

retcode = mkdir(path, 0755);
printf("mkdir(\"%s\") returns %d: errno= %d [%s]\n",
path, retcode, errno, strerror(errno));
}

sangwoo의 이미지

mkdir(1) 프로그램과 mkdir(2) 시스템 콜은 다릅니다. 물론 mkdir(1) 프로그램은 mkdir(2) 를 사용하여 구현되어 있긴 하지만요.
mkdir(1)의 -p옵션 처리는 프로그램 내부에서 argument를 파싱한 뒤에 필요한 디렉토리를 순서대로 만들도록 되어 있습니다. 즉 여러번의 mkdir(2) 콜을 하게 됩니다.

----
Let's shut up and code.

익명 사용자의 이미지

그럼 / 로 나누어진부분을 짤라서 제가 직접 디렉토리를 만들도록 구현을 해줘야 한다는 말씀이신지요?

purluno의 이미지

그냥 간단하게 하시려면

#include <stdlib.h>

system("mkdir -p -m 755 ./test/t1");
도 가능합니다.
sangwoo의 이미지

soy wrote:
그럼 / 로 나누어진부분을 짤라서 제가 직접 디렉토리를 만들도록 구현을 해줘야 한다는 말씀이신지요?

네.. 그게 올바른 방법 같습니다. system()을 쓰셔도 되겠지만 제 가 좋아하는 방법은 아니라서 :-/
여기에 FreeBSD의 mkdir 소스가 있으니 참고하세요~
http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/bin/mkdir/mkdir.c?rev=1.32&content-type=text/plain

----
Let's shut up and code.

댓글 달기

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