[C] 创建目录_mkdir()函数

  在使用面函数的时候,容易出现找不到头文件,可以通过Linux下的find命令查找是否存在其他目录中(find /usr/include -name "io.h"),然后将其拷贝到“/usr/include/”目录下,一般需要root权限。

一、创建目录

共有两种方式,可以选择其中一种,推荐第二种,可以直接设置权限。

1.1 direct.h 头文件

int mkdir(const char *path,mode_t mode);
函数名: mkdir   
功 能: 建立一个目录   
用 法: int mkdir( const char *dirname );   
头文件库:direct.h   
返回值:创建一个目录,若成功则返回0,否则返回-1

1.2 Linux下mkdir函数
头文件库:

#include <sys/stat.h>

#include <sys/types.h>

函数原型: int mkdir(const char *pathname, mode_t mode);

函数说明: mkdir()函数以mode方式创建一个以参数pathname命名的目录,mode定义新创建目录的权限。

返回值: 若目录创建成功,则返回0;否则返回-1,并将错误记录到全局变量errno中。

mode方式:

S_IRWXU 00700权限,代表该文件所有者拥有读,写和执行操作的权限
S_IRUSR(S_IREAD) 00400权限,代表该文件所有者拥有可读的权限
S_IWUSR(S_IWRITE) 00200权限,代表该文件所有者拥有可写的权限
S_IXUSR(S_IEXEC) 00100权限,代表该文件所有者拥有执行的权限
S_IRWXG 00070权限,代表该文件用户组拥有读,写和执行操作的权限
S_IRGRP 00040权限,代表该文件用户组拥有可读的权限
S_IWGRP 00020权限,代表该文件用户组拥有可写的权限
S_IXGRP 00010权限,代表该文件用户组拥有执行的权限
S_IRWXO 00007权限,代表其他用户拥有读,写和执行操作的权限
S_IROTH 00004权限,代表其他用户拥有可读的权限
S_IWOTH 00002权限,代表其他用户拥有可写的权限
S_IXOTH 00001权限,代表其他用户拥有执行的权限

1   用   int   access(const   char   *pathname,   int   mode);   判断有没有此文件或目录 --它区别不出这是文件还是目录
2   用   int   stat(const   char   *file_name,   struct   stat   *buf); 判断该文件或目录是否否存在 ;得到st_mode,然后判断是不是目录文件。 
    stat()系统调用看是否成功,不成功就不存在,成功判断返回的st_mode是否是一个文件夹。

********************************************************************
1.3 linux c关于目录是否存在,新建目录等操作
1. 创建目录

       #include <sys/stat.h>
       #include <sys/types.h>

       int mkdir(const char *pathname, mode_t mode);

运用条件:只能在已存在的目录下建立一级子目录

返回值:  返回0表示成功,返回-1表述出错。

mode 表示新目录的权限,可以取以下值:

其中,mode就用0777,0755这种形式。

 
2. 判断一个目录是否存在

可以使用opendir来判断,这是比较简单的办法。

       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);

       The  opendir()  function  opens  a  directory  stream  corresponding to the directory name, and returns a pointer to the directory

stream.  The stream is positioned at the first entry in the directory.

代码
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <cstddef>
int main()
{
 if(NULL==opendir("/d1/liujian/readdb/adTest/data/html"))
   mkdir("/d1/liujian/readdb/adTest/data/html",0775);
 return 0;
}

以上代码可以测试一个目录是否存在,如果不存在就创建这个目录。

[cpp] view plain copy
 
 print?
    1. ***********************************  
    2.   
    3. #include<stdio.h>  
    4. #include<string.h>  
    5. #include<errno.h>  
    6.   
    7. #include<unistd.h>  
    8.   
    9. #include<dirent.h>  
    10. #include<sys/types.h>  
    11. #include<sys/stat.h>  
    12.   
    13. extern int errno;  
    14.   
    15. #define MODE (S_IRWXU | S_IRWXG | S_IRWXO)  
    16.   
    17. int mk_dir(char *dir)  
    18. {  
    19.     DIR *mydir = NULL;  
    20.     if((mydir= opendir(dir))==NULL)//判断目录   
    21.     {  
    22.       int ret = mkdir(dir, MODE);//创建目录  
    23.       if (ret != 0)  
    24.       {  
    25.           return -1;  
    26.       }  
    27.       printf("%s created sucess!/n", dir);  
    28.     }  
    29.     else  
    30.     {  
    31.         printf("%s exist!/n", dir);  
    32.     }  
    33.   
    34.     return 0;  
    35. }  
    36.   
    37. int mk_all_dir(char *dir)  
    38. {  
    39.     bool flag = true;  
    40.     char *pDir = dir;  
    41.     while (flag)  
    42.     {  
    43.         char *pIndex = index(pDir, '/');  
    44.         if (pIndex != NULL && pIndex != dir)  
    45.         {  
    46.             char buffer[512] = {0};  
    47.             int msg_size = pIndex - dir;  
    48.             memcpy(buffer, dir, msg_size);  
    49.             int ret = mk_dir(buffer);  
    50.             if (ret < 0)  
    51.             {  
    52.                 printf("%s created failed!/n", dir);  
    53.             }  
    54.         }  
    55.         else if (pIndex == NULL && pDir == dir)  
    56.         {  
    57.             printf("dir is not directory!/n");  
    58.             return -1;  
    59.         }  
    60.         else if (pIndex == NULL && pDir != dir)  
    61.         {  
    62.             int ret = mk_dir(dir);  
    63.             if (ret < 0)  
    64.             {  
    65.                 printf("%s created failed!/n", dir);  
    66.             }  
    67.   
    68.             break;  
    69.         }  
    70.   
    71.         pDir = pIndex+1;  
    72.   
    73.     }  
    74.   
    75.     return 0;  
    76. }  
    77.   
    78.    
    79.   
    80. int main()  
    81. {  
    82.     char buffer[512] = {0};  
    83.     printf("please input path mane/n");  
    84.     fgets(buffer, sizeof(buffer), stdin);  
    85.       
    86.     char *pIndex = index(buffer, '/n');  
    87.     if (pIndex != NULL)  
    88.     {  
    89.         *pIndex = '/0';  
    90.     }  
    91.   
    92.     printf("check path mane %s/n", buffer);  
    93.   
    94.     int ret = mk_all_dir(buffer);  
    95.     if (ret < 0)  
    96.     {  
    97.         printf("% mkdir failed!/n", buffer);  
    98.         return -1;  
    99.     }  
    100.   
    101.     return 0;  
    102. }  

二、更改权限

相关函数:fchmod, stat, open, chown

头文件:#include <sys/types.h>   #include <sys/stat.h>
定义函数:int chmod(const char * path, mode_t mode);
函数说明:chmod()会依参数mode 权限来更改参数path 指定文件的权限。

返回值:权限改变成功返回0, 失败返回-1, 错误原因存于errno.

参数 mode 有下列数种组合:
1、S_ISUID 04000 文件的 (set user-id on execution)位
2、S_ISGID 02000 文件的 (set group-id on execution)位
3、S_ISVTX 01000 文件的sticky 位
4、S_IRUSR (S_IREAD) 00400 文件所有者具可读取权限
5、S_IWUSR (S_IWRITE)00200 文件所有者具可写入权限
6、S_IXUSR (S_IEXEC) 00100 文件所有者具可执行权限
7、S_IRGRP 00040 用户组具可读取权限
8、S_IWGRP 00020 用户组具可写入权限
9、S_IXGRP 00010 用户组具可执行权限
10、S_IROTH 00004 其他用户具可读取权限
11、S_IWOTH 00002 其他用户具可写入权限
12、S_IXOTH 00001 其他用户具可执行权限

注:只有该文件的所有者或有效用户识别码为0,才可以修改该文件权限。

基于系统安全,如果欲将数据写入一执行文件,而该执行文件具有S_ISUID 或S_ISGID 权限,则这两个位会被清除。如果一目录具有S_ISUID 位权限,表示在此目录下只有该文件的所有者或root 可以删除该文件。


错误代码:
1、EPERM 进程的有效用户识别码与欲修改权限的文件拥有者不同, 而且也不具root 权限.
2、EACCESS 参数path 所指定的文件无法存取.
3、EROFS 欲写入权限的文件存在于只读文件系统内.
4、EFAULT 参数path 指针超出可存取内存空间.
5、EINVAL 参数mode 不正确
6、ENAMETOOLONG 参数path 太长
7、ENOENT 指定的文件不存在
8、ENOTDIR 参数path 路径并非一目录
9、ENOMEM 核心内存不足
10、ELOOP 参数path 有过多符号连接问题.
11、EIO I/O 存取错误

范例
/* 将/etc/passwd 文件权限设成S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH */
#include <sys/types.h>
#include <sys/stat.h>
main()
{
    chmod("/etc/passwd", S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
}

原文地址:https://www.cnblogs.com/xiaofeiIDO/p/6695459.html