日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2023/12/13 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)上重現了這個問題,并建議問題在修訂版本中是固定的(但測試只能顯示錯誤的存在,從不缺席)。

(因此,您有權將此代碼用于出于任何目的而使用歸因。)

總結

以上是生活随笔為你收集整理的linux中创建目录树,如何在C/Linux中创建目录树?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。