#include <head.h>
int main(int argc,const char * argv[])
{
DIR *dr;
struct dirent* dt;
struct stat st;
int flag =0;
if (argc != 3) {
fprintf(stderr, "input error,try again\n");
fprintf(stderr, "usage:./a.out filename directoryname\n");
return -1;
}
if ((dr = opendir(argv[2])) == NULL)
PRINT_ERR("open dir error");
while ((dt=readdir(dr))!=NULL)
{
if(!strcmp(dt->d_name,argv[1])){
printf("存在\n");
flag=1;
if (lstat(argv[1], &st))
PRINT_ERR("get file stat error");
switch (st.st_mode & __S_IFMT) {
case __S_IFSOCK:
printf("这是套接字文件\n");
break;
case __S_IFBLK:
printf("这是块设备文件\n");
break;
case __S_IFLNK:
printf("这是软连接文件\n");
break;
case __S_IFDIR:
printf("这是目录文件\n");
break;
case __S_IFCHR:
printf("这是字符设备文件\n");
break;
case __S_IFIFO:
printf("这是管道文件\n");
break;
case __S_IFREG:
printf("这是普通文件\n");
break;
}
printf("ino = %ld\nmode=%#o\nhardlink=%ld\nuid=%s\ngid=%s\nsize=%ld\n",
st.st_ino, st.st_mode & 0777, st.st_nlink,
getpwuid(st.st_uid)->pw_name, //根据uid获取用户名
getgrgid(st.st_gid)->gr_name,
st.st_size);
}
}
if(flag==0){
printf("该文件不存在\n");
}
closedir(dr);
return 0;
}