linux中创建目录树,如何在C/Linux中创建目录树?

这里有一个C函数可以用C编译器编译。

/*

@(#)File: $RCSfile: mkpath.c,v $

@(#)Version: $Revision: 1.13 $

@(#)Last changed: $Date: 2012/07/15 00:40:37 $

@(#)Purpose: Create all directories in path

@(#)Author: J Leffler

@(#)Copyright: (C) JLSS 1990-91,1997-98,2001,2005,2008,2012

*/

/*TABSTOP=4*/

#include "jlss.h"

#include "emalloc.h"

#include

#ifdef HAVE_UNISTD_H

#include

#endif /* HAVE_UNISTD_H */

#include

#include "sysstat.h" /* Fix up for Windows - inc mode_t */

typedef struct stat Stat;

#ifndef lint

/* Prevent over-aggressive optimizers from eliminating ID string */

const char jlss_id_mkpath_c[] = "@(#)$Id: mkpath.c,v 1.13 2012/07/15 00:40:37 jleffler Exp $";

#endif /* lint */

static int do_mkdir(const char *path, mode_t mode)

{

Stat st;

int status = 0;

if (stat(path, &st) != 0)

{

/* Directory does not exist. EEXIST for race condition */

if (mkdir(path, mode) != 0 && errno != EEXIST)

status = -1;

}

else if (!S_ISDIR(st.st_mode))

{

errno = ENOTDIR;

status = -1;

}

return(status);

}

/**

** mkpath - ensure all directories in path exist

** Algorithm takes the pessimistic view and works top-down to ensure

** each directory in path exists, rather than optimistically creating

** the last element and working backwards.

*/

int mkpath(const char *path, mode_t mode)

{

char *pp;

char *sp;

int status;

char *copypath = STRDUP(path);

status = 0;

pp = copypath;

while (status == 0 && (sp = strchr(pp, '/')) != 0)

{

if (sp != pp)

{

/* Neither root nor double slash in path */

*sp = '\0';

status = do_mkdir(copypath, mode);

*sp = '/';

}

pp = sp + 1;

}

if (status == 0)

status = do_mkdir(path, mode);

FREE(copypath);

return (status);

}

#ifdef TEST

#include

/*

** Stress test with parallel running of mkpath() function.

** Before the EEXIST test, code would fail.

** With the EEXIST test, code does not fail.

**

** Test shell script

** PREFIX=mkpath.$$

** NAME=./$PREFIX/sa/32/ad/13/23/13/12/13/sd/ds/ww/qq/ss/dd/zz/xx/dd/rr/ff/ff/ss/ss/ss/ss/ss/ss/ss/ss

** : ${MKPATH:=mkpath}

** ./$MKPATH $NAME &

** [...repeat a dozen times or so...]

** ./$MKPATH $NAME &

** wait

** rm -fr ./$PREFIX/

*/

int main(int argc, char **argv)

{

int i;

for (i = 1; i < argc; i++)

{

for (int j = 0; j < 20; j++)

{

if (fork() == 0)

{

int rc = mkpath(argv[i], 0777);

if (rc != 0)

fprintf(stderr, "%d: failed to create (%d: %s): %s\n",

(int)getpid(), errno, strerror(errno), argv[i]);

exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);

}

}

int status;

int fail = 0;

while (wait(&status) != -1)

{

if (WEXITSTATUS(status) != 0)

fail = 1;

}

if (fail == 0)

printf("created: %s\n", argv[i]);

}

return(0);

}

#endif /* TEST */

宏STRDUP()和FREE()是错误检查版本的strdup()和free(),在emalloc.h中声明(并在emalloc.c和estrdup.c中实现)。 “sysstat.h”头处理< sys / stat.h>的破坏版本。并且可以被< sys / stat.h>在现代的Unix系统(但在1990年有很多问题)。和“jlss.h”声明mkpath()。

v1.12(上一个)和v1.13(上面)之间的更改是对do_mkdir()中的EEXIST的测试。这是指出必要的Switch – 谢谢你,Switch。测试代码已经升级,并在MacBook Pro(2.3GHz Intel Core i7,运行Mac OS X 10.7.4)上重现了这个问题,并建议问题在修订版本中是固定的(但测试只能显示错误的存在,从不缺席)。

(因此,您有权将此代码用于出于任何目的而使用归因。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值