程序4-1 对每个命令行参数打印文件类型

 1 //http://blog.chinaunix.net/uid-24549279-id-71355.html
 2 /*
 3  ============================================================================
 4  Name        : test.c
 5  Author      : blank
 6  Version     :
 7  Copyright   : Your copyright notice
 8  Description : 程序4-1 对每个命令行参数打印文件类型
 9  ============================================================================
10 */
11 
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include "ourhdr.h"
15 
16 #define BUFFSIZE 4096
17 
18 int main(int argc, char *argv[])
19 {
20     int         i;
21     struct stat buf;
22     char        *ptr;
23 
24     for (i=1; i<argc; i++){
25         printf("%s: ", argv[i]);
26         if (lstat(argv[i], &buf) < 0){
27             err_ret("lstat error");
28             continue;
29         }
30 
31         if (S_ISREG(buf.st_mode)){
32             ptr = "regular";
33         }else if(S_ISDIR(buf.st_mode)){
34             ptr = "directory";
35         }else if(S_ISCHR(buf.st_mode)){
36             ptr = "character special";
37         }else if(S_ISLNK(buf.st_mode)){
38             ptr = "symbolic link";
39         }else if (S_ISSOCK(buf.st_mode)){
40             ptr = "socket";
41         }else if(S_ISBLK(buf.st_mode)){
42             ptr = "block special";
43         }else if(S_ISFIFO(buf.st_mode)){
44             ptr = "FIFO";
45         }else{
46             ptr = "***unknown mode***";
47         }
48         printf("%s\n", ptr);
49     }
50     exit(0);
51 }

 

posted @ 2014-04-27 11:36  blankqdb  阅读(230)  评论(0编辑  收藏  举报