---
title: "lstat(传入路径参数，&一种struct)返回0或1"
author: "mactonish"
date: "2011-11-08T00:15:57Z"
tags: []
categories: ["编程语言"]
slug: "lstat-struct-0-1"
draft: false
lastmod: "2011-11-08T00:20:11Z"
id: 1240887
url: "https://mactonish.iteye.com/blog/1240887"
---

apue 04.pdf

S _ I S L N K ( )
S _ I S S O C K ( )
符号连接( P O S I X . 1 或 S V R 4无此类型) 套接字( P O S I X . 1 或 S V R 4无此类型)
为了跨平台，代码写成#ifdef这种

```c
#include "apue.h"

int
main(int argc, char *argv[])
{
    int         i;
    struct stat buf;
    char        *ptr;

    for (i = 1; i < argc; i++) {
        printf("%s: ", argv[i]);
        if (lstat(argv[i], &buf) < 0) {
            err_ret("lstat error");
            continue;
        }
        if (S_ISREG(buf.st_mode))
            ptr = "regular";
        else if (S_ISDIR(buf.st_mode))
            ptr = "directory";
        else if (S_ISCHR(buf.st_mode))
            ptr = "character special";
        else if (S_ISBLK(buf.st_mode))
            ptr = "block special";
        else if (S_ISFIFO(buf.st_mode))
            ptr = "fifo";
#ifdef S_ISLNK
        else if (S_ISLNK(buf.st_mode))
            ptr = "symbolic link";
#endif
#ifdef S_ISSOCK      
        else if (S_ISSOCK(buf.st_mode))
            ptr = "socket";
#endif
        else
            ptr = "** unknown mode **";
        printf("%s\n", ptr);
    }
    exit(0);
}
```

这样定义的
#define S_ISDIR (mode) (((mode) & S_IFMT) == S_IFDIR)