Linux C 創建目錄函數mkdir相關【轉】


轉自:http://blog.csdn.net/fallenink/article/details/8480483

原文地址:http://sharp2wing.iteye.com/blog/1280802

————————————————————————————————————————————————

I.Linux C 創建目錄函數mkdir的mode設置問題 

函數原型: 

#include <sys/stat.h> 

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

參數: 

path是目錄名 

mode是目錄權限 

返回值: 

返回0 表示成功, 返回 -1表示錯誤,並且會設置errno值。 

mode模式位: 

mode 表示新目錄的權限,可以取以下值: 

S_IRUSR 
S_IREAD 

S_IWUSR 
S_IWRITE 
S_IXUSR 
S_IEXEC 
S_IRWXU 
This is equivalent to (S_IRUSR | S_IWUSR | S_IXUSR). 
S_IRGRP 
Read permission bit for the group owner of the file. Usually 040. 
S_IWGRP 
Write permission bit for the group owner of the file. Usually 020. 
S_IXGRP 
Execute or search permission bit for the group owner of the file. Usually 010. 
S_IRWXG 
This is equivalent to (S_IRGRP | S_IWGRP | S_IXGRP). 
S_IROTH 
Read permission bit for other users. Usually 04. 
S_IWOTH 
Write permission bit for other users. Usually 02. 
S_IXOTH 
Execute or search permission bit for other users. Usually 01. 
S_IRWXO 
This is equivalent to (S_IROTH | S_IWOTH | S_IXOTH). 
S_ISUID 
This is the set-user-ID on execute bit, usually 04000. See How Change Persona. 
S_ISGID 
This is the set-group-ID on execute bit, usually 02000. See How Change Persona. 
S_ISVTX 
This is the sticky bit, usually 01000. 

【man 2 mkdir可以查看下】


例子: 

#include <sys/types.h> #include <sys/stat.h> 
int status; 

status = mkdir("/home/newdir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 

這樣就創建了一個newdir目錄,權限通過ls -al 查看為 

drwxr-xr-x 

跟用linux命令mkdir創建的目錄權限位一致。 



II. linux下C語言創建多級目錄 

int   CreateDir(const   char   *sPathName)  
  {  
  char   DirName[256];  
  strcpy(DirName,   sPathName);  
  int   i,len   =   strlen(DirName);  
  if(DirName[len-1]!='/')  
  strcat(DirName,   "/");  
   
  len   =   strlen(DirName);  
   
  for(i=1;   i<len;   i++)  
  {  
  if(DirName[i]=='/')  
  {  
  DirName[i]   =   0;  
  if(  access(DirName,   NULL)!=0   )  
  {  
      if(mkdir(DirName,   0755)==-1)  
      {   
                      perror("mkdir   error");   
                      return   -1;   
      }  
  }  
  DirName[i]   =   '/';  
  }  
  }  
   
  return   0;  
  } 

III.linux c 編程:創建一個線程,監視某個目錄,一旦目錄里出現新的文件,就將文件轉移到指定的目錄里去。 
/* 
頭文件 
*/ 
#define SRCPATH "srcpath/" 
#define DSTPATH "dstpath/" 

int movefile() 
{
DIR *dir; 
struct dirent *dt; 
FILE *fp1,*fp2; 
char filename1[256],filename2[256]; 
char buf[1024]; 
int readsize,writesize; 

if((dir = opendir(SRCPATH)) == NULL) 

printf("opendir %s error\n",SRCPATH); 
return -1; 

memset(filename1,0,sizeof(filename1)); 
strcpy(filename1,SRCPATH); 
memset(filename2,0,sizeof(filename2)); 
strcpy(filename2,DSTPATH); 
while(1) 

while((dt = readdir(dir)) != NULL) 

if(strcmp(dt->d_name,".")==0||strcmp(dt->d_name,"..")==0) 

continue; 

//如果這個目錄里 還有目錄,可以在這加判斷 
//這里假設初始為空目錄 
strcat(filename1,dt->d_name); 
strcat(filename2,dt->d_name); 
//如果進程資源較少可以直接用linux系統命令 

fp1 = fopen(filename1,"rb"); 
if(fp1==NULL) 

printf("open %s failed /n",filename1); 
return -1; 
}

fp2 = fopen(filename2,"wb"); 
if(fp2==NULL) 

printf("open %s failed /n",filename2); 
fclose(fp1); 
return -1; 


while((readsize = fread(buf,sizeof(buf),1,fp1))>0) 

//total += readsize; 
memset(buf,0,sizeof(buf)); 
writesize = fwrite(buf,sizeof(buf),1,fp2); 
if(writesize!==readsize) 

printf("write error"); 
return -2; 
fclose(fp1); 
fclose(fp2); 
}

fclose(fp1); 
fclose(fp2); 
rmdir(filename2); 


}

int main(int argc,char **argv) 

pthread_t id1; 
int ret; 
ret = pthread_create(&id1, NULL, (void*)movefile, NULL); 
return ret; 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

1. 創建目錄 

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

       int mkdir(const char *pathname, mode_t 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.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

access():判斷是否具有存取文件的權限

相關函數
    stat,open,chmod,chown,setuid,setgid
表頭文件
    #include<unistd.h>
定義函數
    int access(const char * pathname, int mode);
函數說明
    access()會檢查是否可以讀/寫某一已存在的文件。參數mode有幾種情況組合, R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK與X_OK用來檢查文件是否具有讀取、寫入和執行的權限。F_OK則是用來判斷該文件是否存在。由於access()只作權限的核查,並不理會文件形態或文件內容,因此,如果一目錄表示為“可寫入”,表示可以在該目錄中建立新文件等操作,而非意味此目錄可以被當做文件處理。例如,你會發現DOS的文件都具有“可執行”權限,但用execve()執行時則會失敗。
返回值
    若所有欲查核的權限都通過了檢查則返回0值,表示成功,只要有一權限被禁止則返回-1。
錯誤代碼
    EACCESS 參數pathname 所指定的文件不符合所要求測試的權限。
    EROFS 欲測試寫入權限的文件存在於只讀文件系統內。
    EFAULT 參數pathname指針超出可存取內存空間。
    EINVAL 參數mode 不正確。
    ENAMETOOLONG 參數pathname太長。
    ENOTDIR 參數pathname為一目錄。
    ENOMEM 核心內存不足    
    ELOOP 參數pathname有過多符號連接問題。
    EIO I/O 存取錯誤。
附加說明
    使用access()作用戶認證方面的判斷要特別小心,例如在access()后再做open()的空文件可能會造成系統安全上的問題。

 

還可以參考:linux access()函數和readdir()函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM