【问题标题】:System call in unix: directories and filesunix中的系统调用:目录和文件
【发布时间】:2010-12-26 06:54:32
【问题描述】:

嗨,我正在尝试理解系统调用:unix 上的目录和文件,.. 我找到了这个网站,他们用自己的示例解释了一些调用,但不理解这些代码 sn-ps ..

    void state (char *file) 
    {
    struct stat    buf;
    struct passwd *pw;
    struct group  *gr;
    int i;

    if (stat(file, &buf)==-1)
    {
    perror(file);
    exit(-1);
    }

    printf ("file: %s\n", archivo);
    printf ("\t resides in the device: %d, %d\n",(buf.st_dev & 0xFF00)>>8,            buf.st_dev   &   0x00FF);
    printf ("\t  i-node number: %d\n", buf.st_ino);
    printf ("\t type: ");
    switch (buf.st_mode & S_IFMT)
    {
    case S_IFREG: printf ("ordinario\n");     break;
    case S_IFDIR: printf ("directorio\n");    break;
    case S_IFCHR: printf ("tipo caracter\n"); break;
    case S_IFBLK: printf ("tipo bloque\n");   break;
    case S_IFIFO: printf ("FIFO\n");          break;
    }

  if (buf.st_mode & S_ISUID) printf ("\tSUID activo");
  if (buf.st_mode & S_ISGID) printf ("\tSGID activo");
  if (buf.st_mode & S_ISVTX) printf ("\tStiky bit activo\n");

  /* Permissions access */
  printf ("\tPermission: 0%o ",buf.st_mode & 0777);
  for (i=0; i<9; i++)
      if (buf.st_mode & (0400>>i)) printf ("%c", permisos[(8-i)%3]);
      else  printf ("-"); ....

我不明白比较以找出缺少哪个设备文件..有人可以帮助我理解吗?特别是这里..

printf ("\tReside en el dispositivo: %d, %d\n", (buf.st_dev & 0xFF00)>>8,
buf.st_dev & 0x00FF);



/* Permissions */
  printf ("\tPermission: 0%o ",buf.st_mode & 0777);
  for (i=0; i<9; i++)
      if (buf.st_mode & (0400>>i)) printf ("%c", permisos[(8-i)%3]);
      else  printf ("-");

欢迎对双方进行的比较提供任何帮助或解释 PD:对不起我的英语=P

链接显示整个代码示例 1 的位置,称为 estado.c

http://translate.googleusercontent.com/translate_c?hl=es&ie=UTF-8&sl=es&tl=en&u=http://malicia.super.unam.mx/wiki/index.php/Llamadas_al_Sistema:_Directorios_y_Archivos&prev=_t&rurl=translate.google.co.ve&twu=1&usg=ALkJrhhwwFSx-UiPs4rtgSJADbrZy13v7A

【问题讨论】:

    标签: c unix file directory system-calls


    【解决方案1】:

    我假设你的意思是:

    (buf.st_dev & 0xFF00)>>8
    

    这不是比较。 &gt;&gt; 是右移运算符。它将第一个操作数向右移动第二个操作数指定的位数。该表达式将除 buf.st_dev 的第 9 位到第 16 位之外的所有位归零(这就是 &amp; 0xFF00 所做的),然后将生成的 8 位向下移动到第 1 到第 8 个最低有效位。这将产生一个从 0 到 255 的数字。

    【讨论】:

      【解决方案2】:

      (buf.st_dev &amp; 0xFF00)&gt;&gt;8, buf.st_dev &amp; 0x00FF 不是比较,而是算术表达式:

      &amp; 执行操作数的按位二进制与:

      0 & 0 = 0
      0 & 1 = 0
      1 & 0 = 0
      1 & 1 = 1

      &gt;&gt; 执行位移

      使用&amp; 是从另一个值“屏蔽”位的常用方法,以便可以单独检查它们。在第一个表达式中,位置 8 到 15 的 8 位被隔离并移位到位置 0 到 7,因此就好像倒数第二个有效字节显示为独立字节。

      第二个表达式不进行移位,因为在屏蔽后它已经在最低位位置,因此直接有意义。

      【讨论】:

        【解决方案3】:

        我们正在尝试在此处访问用于 stat 和 lstat 等命令的 stat 结构:

        struct stat {
             dev_t     st_dev;     /* ID of device containing file */
             ino_t     st_ino;     /* inode number */
             mode_t    st_mode;    /* protection */
             nlink_t   st_nlink;   /* number of hard links */
             uid_t     st_uid;     /* user ID of owner */
             gid_t     st_gid;     /* group ID of owner */
             dev_t     st_rdev;    /* device ID (if special file) */
             off_t     st_size;    /* total size, in bytes */
             blksize_t st_blksize; /* blocksize for file system I/O */
             blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
             time_t    st_atime;   /* time of last access */
             time_t    st_mtime;   /* time of last modification */
             time_t    st_ctime;   /* time of last status change */
         };
        

        换行:

        buf.st_mode & (0400>>i)
        

        我认为他们正在尝试提取上述结构的 st_mode 成员中位 5-16 的文件的权限。

        我认为我们也可以使用宏 S_IFMT 提取信息:

        file_type = statbuf.st_mode&S_IFMT;  // for bits 1-4
        file_perm = statbuf.st_mode&~S_IFMT  // for bits 5-16
        

        我认为开发人员在您的 sn-p 中手动执行相同的操作并显示 rwx 或 - ,我想。

        至于st_dev部分代码:

        我从来没有真正尝试过操纵那个成员变量,所以我不能说这个成员做了什么。找出答案的最好方法是查看它在 sys/types.h 中的定义。 至于使用的原因和劳伦斯·贡萨尔维斯试图解释的一样。

        我从手册页中看到的是以下行: "st_ino 和 st_dev 字段共同唯一标识系统中的文件。"

        希望这些信息对你有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-10
          • 2011-04-19
          • 2021-06-21
          • 1970-01-01
          • 2015-09-24
          • 2012-12-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多