LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 705|回复: 2

如何判断文件是符号链结

[复制链接]
发表于 2003-6-21 09:49:11 | 显示全部楼层 |阅读模式
我只能判断一般文件、目录、块设备、字符设备,不知如何判断符号链结
  1. #define T_FILE        0100000
  2. #define T_DIRECTORY        040000
  3. #define T_BLOCKDEVICE        060000
  4. #define T_BYTEDEVICE        020000
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. int main(int argc, char **argv)
  8. {
  9.         struct stat statbuf;
  10.         int tem;
  11.         if(argc<2)
  12.         {
  13.                 printf("usage: isdir filename");
  14.                 exit(0);
  15.         }
  16.         stat(argv[1],&statbuf);
  17.         //printf("%s is 0%o \n",argv[1],statbuf.st_mode);
  18.         tem=statbuf.st_mode&0777000;
  19.         switch(tem)
  20.         {
  21.         case T_FILE:
  22.                 printf("%s is a file!",argv[1]);               
  23.                 //printf("\t tem=0%o\n",tem);
  24.                 break;
  25.         case T_DIRECTORY:
  26.                 printf("%s is a directory!",argv[1]);
  27.                 break;
  28.         case T_BYTEDEVICE:
  29.                 printf("%s is a byte device!",argv[1]);
  30.                 break;
  31.         case T_BLOCKDEVICE:
  32.                 printf("%s is a bolck device!",argv[1]);
  33.                 break;
  34.         default:
  35.                 printf("isdir: %s: No such file or directory!",argv[1]);
  36.                 break;
  37.         }
  38.         //printf("\t tem=0%o\n",tem);
  39. }
复制代码
发表于 2003-6-21 10:26:50 | 显示全部楼层
用sys/stat.h的宏定义

S_ISBLK 块设备
S_ISCHR 字符设备
S_ISDIR 目录
S_ISFIFO  FIFO设备
S_ISREG 普通文件
S_ISLNK 符号连接

eg:

  1. #include<unistd.h>
  2. #include<sys/stat.h>
  3. #include<stdio.h>

  4. struct stat statbuf;
  5. mode_t modes;

  6. int main()
  7. {
  8.     stat("filename",&statbuf);
  9.     modes=statbuf.st_mode;

  10.     if(S_ISLNK(modes))
  11.        printf("filename is link file\n");
  12.     return 0;
  13. }
复制代码
发表于 2003-6-21 13:16:07 | 显示全部楼层
有一个lstat函数可以返回符号链接的有关信息。你man 一下看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表