当前目录的权限

 waxw 发布于 2023-01-12 14:19

我正在制作一个在linux shell中执行的程序,它接受一个参数(一个文件)并显示它的inode编号,权限,文件大小等,如果没有给出参数,则应该读取权限,inode编号,大小目录等.

我正在使用stat来查找文件的这些内容,但我不确定如何检查当前目录中的这些信息.

这是我的代码:

#define _BSD_SOURCE
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void inodeNumber(struct stat info)
{

  //printf("I-node number: %ld\n", (long)info.st_ino);
  printf("%ld ", (long) info.st_ino);

}

void filePermissions(struct stat info)
{

  //printf("File Permissions: ");
  printf((S_ISDIR(info.st_mode)) ? "d" : "-");
  printf((info.st_mode & S_IRUSR) ? "r" : "-");
  printf((info.st_mode & S_IWUSR) ? "w" : "-");
  printf((info.st_mode & S_IXUSR) ? "x" : "-");
  printf((info.st_mode & S_IRGRP) ? "r" : "-");
  printf((info.st_mode & S_IWGRP) ? "w" : "-");
  printf((info.st_mode & S_IXGRP) ? "x" : "-");
  printf((info.st_mode & S_IROTH) ? "r" : "-");
  printf((info.st_mode & S_IWOTH) ? "w" : "-");
  printf((info.st_mode & S_IXOTH) ? "x" : "-");
  //printf("\n");
  printf(" ");

}

void numberOfLinks(struct stat info)
{

  //printf("Link number: %ld\n", (long)info.st_nlink);
  printf("%ld ", (long) info.st_nlink);
  //printf(info.st_nlink);

}

void fileSize(struct stat info)
{

  //printf("File Size: %ld\n", (long)info.st_size);
  printf("%ld ", (long) info.st_size);

}

int main(int argc, char *argv[])
{

  struct stat info;
  char cwd[256];

  if (argc == 1)
  {

    //current directory
    printf("No Arguments\n");

    if (getcwd(cwd, sizeof(cwd)) == NULL )
    {

      perror("getcwd() error");
    }
    else
    {
      //find current directory
      printf("current working dir is: %s\n", cwd);

    }
    //move up directory 1 place, then test the file we were just in
    //  chdir("..");

  }

  if (stat(argv[1], &info) == -1)
  {
    perror("stat");
    exit(EXIT_FAILURE);
  }

  if (argc == 2)
  {

    inodeNumber(info);
    filePermissions(info);
    numberOfLinks(info);
    fileSize(info);
    //printf("File Name Is: %s\n", argv[1]);
    printf("%s", argv[1]);
  }

  return 0;

}

Yeraze.. 5

fstat()在文件和目录上工作相同.对于当前目录,只需使用".".

1 个回答
  • fstat()在文件和目录上工作相同.对于当前目录,只需使用".".

    2023-01-12 14:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有