天天看點

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

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

函數原型: 

#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 

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); 

}

fp2 = fopen(filename2,"wb"); 

if(fp2==NULL) 

printf("open %s failed /n",filename2); 

fclose(fp1); 

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(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 <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。

錯誤代碼

    EROFS 欲測試寫入權限的檔案存在于隻讀檔案系統内。

    EFAULT 參數pathname指針超出可存取記憶體空間。

    EINVAL 參數mode 不正确。

    ENAMETOOLONG 參數pathname太長。

    ENOTDIR 參數pathname為一目錄。

    ENOMEM 核心記憶體不足    

    ELOOP 參數pathname有過多符号連接配接問題。

    EIO I/O 存取錯誤。

附加說明

    使用access()作使用者認證方面的判斷要特别小心,例如在access()後再做open()的空檔案可能會造成系統安全上的問題。

本文轉自張昺華-sky部落格園部落格,原文連結:http://www.cnblogs.com/sky-heaven/p/7047720.html,如需轉載請自行聯系原作者

繼續閱讀