这里有一个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 = '';
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): %sn",
(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: %sn", 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)上重现了这个问题,并建议问题在修订版本中是固定的(但测试只能显示错误的存在,从不缺席)。
(因此,您有权将此代码用于出于任何目的而使用归因。)
最后
以上就是冷酷灯泡最近收集整理的关于linux中创建目录树,如何在C/Linux中创建目录树?的全部内容,更多相关linux中创建目录树内容请搜索靠谱客的其他文章。
发表评论 取消回复