linux之lstat函数解析

本文详细解析了Linux命令lstat的功能及其在获取文件类型方面的应用,包括普通文件、目录、字符特殊文件、块特殊文件、符号链接及套接字等不同类型的文件标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[lingyun@localhost lstat]$ cat lstat.c 
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  stat.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/02/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "08/02/2013 12:38:43 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.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)
        {
            perror("lstat");
            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_ISLNK(buf.st_mode))
            ptr = "symbolic link";
        else if(S_ISSOCK(buf.st_mode))
            ptr = "socket";
        else
            "** unknown mode **";
        printf("%s\n",ptr);
    }
    exit(0);
}


[lingyun@localhost lstat]$ gcc lstat.c 
[lingyun@localhost lstat]$ ./a.out /etc/passwd /etc /dev/log /dev/tty /dev/cdrom /dev/sda
/etc/passwd: regular
/etc: directory
/dev/log: socket
/dev/tty: character special
/dev/cdrom: symbolic link
/dev/sda: block special
[lingyun@localhost lstat]$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fulinux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值