最近在看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 256 uint 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; else ptr[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
#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