阅读背景:

使用lstat()判断文件类型 分类: H_HISTORY 2013-02-24 11:48 703人阅读 评论(0) 收藏

来源:互联网 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#define _GNU_SOURCE

int main (int argc, char *argv[]){

    struct stat buf;

    for(int i = 1; i < argc; i++){
        if(lstat(argv[i], &buf) < 0){
            printf("lstat invoke error.");
            continue;
        }
        printf("%s is a ", argv[i]);
        if (S_ISREG(buf.st_mode)){
            printf("regular file.");
        }else if(S_ISDIR(buf.st_mode)){
            printf("direcotry.");
        }else if(S_ISBLK(buf.st_mode)){
            printf("block device.");
        }else if(S_ISCHR(buf.st_mode)){
            printf("character device.");
        }else if(S_ISFIFO(buf.st_mode)){
            printf("FIFO.");
        }else if(S_ISLNK(buf.st_mode)){
            printf("soft link.");
        }else if(S_ISSOCK(buf.st_mode)){
            printf("socket.");
        }else
            printf("Unkown type.");
     }

        printf("\n");

        exit(0);
}#include <stdio.h>
#include <unistd.h>
#incl



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: