linux之lstat函数解析
[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 ",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]$
/*********************************************************************************
* 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 ",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]$
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。