遍曆檔案(linux)

來源:互聯網
上載者:User

         最近在看unix環境進階編程,一開始看的是電子版,覺得在電腦上看這個,真的不是特別的舒服,就節約了生活費買了一本,花了我¥67.6,真是有點貴啊,758頁我女朋友說這太厚了點吧!不管了,希望它能發揮它的作用。

       看來關於unix的檔案部分,發現unix的檔案許可權真的管得很不錯,不過也真的很煩鎖,看了一邊基本上很快就忘記了.唉,看來是不能理解啊,準備新書到了再看一篇!

       雖然看的不怎麼樣,可是不妨礙我寫程式,這章有一個程式好像是完成關於檔案遍曆的問題,覺得蠻有意思的,所以也模仿的寫一下。有一個很壞壞的習慣,我覺得這個應該很多也有這樣的習慣。當我們接受到一個項目或者是問題的時候,總是喜歡去網上先找找有沒有類似的或者是相似的。由於網路異常的發達,很多的問題或者項目是可以直接搜尋到的,更甚的是連code都是有的。我不知道這是好還是不好,我也差不多,很多的問題都是從網上去找來的,包括代碼!

       可是自己總是對這樣的做法不是很贊同!尤其是當你看到他的源code的時候,很容易很容易讓自己沒有思考的餘地,然後然後你最後的代碼就是和他基本差不多的。思路一樣,連code也一樣。可是如果不去網上找的話,不是很合適,畢竟一個人不可能什麼都懂,什麼都會,網路可以提供一種平台讓人集思廣意,所以怎麼樣面對這樣的狀況我很想有人能告訴我。

       包括自己這次的程式,我基本上是看了書上的然後寫的,不過在寫的過程中自己很努力的去思考的,從開頭到最後很努力的思考程式的邏輯,不過總是看過程式的,很多很多地方很像很像......


      下面就是貼code了,哈哈,湊一個字數吧!代碼的正確性我還真不知道,感覺很像是蠻正確的,尤其是輸入根目錄的時候,啟動並執行不少時間....哈哈哈,大家可以試試看,有什麼bug可以說說...

/*這個程式重要是想使用unix中的一些I/O操作 * 比如stat,S_ISDIR opendir readdir等一系列的函數 * 從而完成一系列的檔案的統計 * nreg   表示 普通檔案 * ndir   表示 目錄檔案 * nchar  表示 字元檔案 * nblk   表示 塊檔案 * nslink 表示 符號串連 * nsock  表示 通訊端 * nfifo  表示 管道 * */#include "headfile.h"#include "test.h"#define unsigned int uint;#define MAX 256uint nreg = 0;uint ndir = 0;uint nchar = 0;uint nblk = 0;uint nslink= 0;uint nsock = 0;uint nfifo = 0;int dopath(char *pathname);int classify(char *pathname,struct stat *buf,int flag);int main(){    char dir[MAX];    printf("please input your pathname!\n");    scanf("%s",dir);    if(dopath(dir) == -1)    {printf("error!\n");    }    printf("nreg is %u\n",nreg);    printf("ndir is %u\n",ndir);    printf("nchar is %u\n",nchar);    printf("nblk is %u\n",nblk);    printf("nslink is %u\n",nslink);    printf("nsock is %u\n",nsock);    printf("nfifo is %u\n",nfifo);    return 0;}/*dopath 是一個遞迴調用的函數 * 1.判斷這個pathname是不是可以被stat的 * 2.判斷它是不是檔案而不是路徑 * 3.判斷如果是路進就遞迴調用dopath * 4.這裡有一個ptr這個指標是永遠指向pathname的'\0'的這個位置 * 5.最後有一個是ptr的賦值的是因為要恢複現場,不然前面啟動並執行會帶在pathname上 * */int dopath(char *pathname){    int ret = 0;    struct stat buf;    DIR *dir = NULL;    struct dirent *dent = NULL;    char *ptr = NULL;    char flag = 0;    return_val_if_fail(pathname);        if(lstat(pathname,&buf) == -1)    {fprintf(stderr,"lstat is fail,this is name is %s,and this error is %s\n",pathname,strerror(errno));return classify(pathname,&buf,S_FILE_NS);    }    if(S_ISDIR(buf.st_mode) == 0)    {return classify(pathname,&buf,S_FILE);    }    classify(pathname,&buf,S_DIR);        ptr = pathname;    ptr = pathname + strlen(pathname);    if(*(ptr - 1) != '/')    {*ptr++ = '/';flag = 1;    }    *ptr = 0;         //printf("%s\n",pathname);    if((dir = opendir(pathname)) == NULL)    {fprintf(stderr,"opendir is fail,this is name is %s,and this error is %s\n",pathname,strerror(errno));return classify(pathname,&buf,S_FILE_NR);    }    while((dent = readdir(dir)) != NULL)    {if(strcmp(dent->d_name,".") == 0 || strcmp(dent->d_name,"..") == 0){    continue;}else{    strcpy(ptr,dent->d_name);    if((ret = dopath(pathname)) != 0)    {return -1;    }}    }    if(flag)ptr[-1] = 0;    elseptr[0] = 0;        closedir(dir);    return ret;}int classify(char *pathname,struct stat *buf,int flag){    return_val_if_fail(pathname)    switch(flag)    {case S_FILE:     { switch(S_IFMT & buf->st_mode) {     case S_IFREG: nreg++; return 0;     case S_IFCHR: nchar++; return 0;     case S_IFBLK: nblk++; return 0;     case S_IFLNK: nslink++; return 0;     case S_IFIFO: nfifo++; return 0;     case S_IFSOCK: nsock++; return 0;     default: printf("you create a new file type!\n"); return -1; }     }case S_DIR:     ndir++;     return 0;case S_FILE_NR:     printf("S_FILE_NR\n");     return -1;case S_FILE_NS:     printf("S_FILE_NS\n");     return -1;default:     printf("this file is not know!\n");     return -1;    }}

headfile.h

#ifndef HEAD_H#define HEAD_H#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <string.h>#include <errno.h>#define S_FILE 1#define S_DIR 2#define S_FILE_NR 3#define S_FILE_NS 4#endif

test.h

#ifndef TEST_H#define TEST_H#define return_if_fail(thiz) if(!(thiz)) \    printf("%s,%d warning:"#thiz"failed.\n", \__func__,__LINE__);return;#define return_val_if_fail(thiz) if(!(thiz)) \{printf("%s,%d warning:"#thiz"failed.\n",\    __func__,__LINE__);return -1;}#endif
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.