unix中的系统调用:目录和文件

发布于 2024-08-14 19:02:18 字数 2334 浏览 2 评论 0原文

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

    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

链接显示了名为 estado.c 的整个代码示例 1

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

hi I'm trying to understand system calls: directories and files on unix, .. I found this website where they explain some calls with their own examples, but do not understand these code snippets ..

    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 ("-"); ....

I do not understand the comparison to find out which device file is missing .. someone could help me understand? specifically here ..

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 ("-");

would welcome any help or explanation of the comparisons that are made on both sides
PD: sorry for my english =P

link where it appears the entire code example 1 called 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-08-21 19:02:18

我假设您的意思是:

(buf.st_dev & 0xFF00)>>8

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

I'm assuming you mean this:

(buf.st_dev & 0xFF00)>>8

That isn't a comparison. >> is the right-shift operator. It shifts the first operand to the right by the number of bits specified by the second operand. This expression is zeroing all but the 9th through 16th bits of buf.st_dev (That's what & 0xFF00 does) and then shifting the resulting 8 bits down to the 1st through 8th least-significant bits. This will result in a number from 0 to 255.

幽蝶幻影 2024-08-21 19:02:18

<代码>(buf.st_dev & 0xFF00)>>8, buf.st_dev & 0x00FF 不是比较,而是算术表达式:

& 对操作数执行按位二进制 AND:

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

>> 执行按位移位

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

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

(buf.st_dev & 0xFF00)>>8, buf.st_dev & 0x00FF are not comparisons but arithmetic expressions:

& performs a bitwise binary AND of the operands:

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

>> performs a bitwise shift

Using & is a common way to "mask" bits out of another value so they can be inspected in isolation. In the first expression, the eight bits in positions 8 through 15 are isolated and shifted to be in positions 0 through 7, so it's as though the second-to-least significant byte is displayed as a standalone byte.

The second expression doesn't do the shift because after masking it is already in the lowest bit position, and so makes sense directly.

冬天旳寂寞 2024-08-21 19:02:18

我们正在尝试访问用于 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

我认为开发人员在您的代码片段中手动执行相同的操作并显示 rwx 或 - ,我猜。

至于代码的 st_dev 部分:

我从未真正尝试过操作该成员变量,所以我不能真正说出该成员做了什么。找出答案的最佳方法是查看它在 sys/types.h 中的定义。
至于使用的原因与劳伦斯·贡萨尔维斯试图解释的相同。

我从手册页中看到的只是以下行:
“st_ino 和 st_dev 字段一起唯一地标识系统内的文件。”

希望这个信息对你有帮助。

We are trying to access the stat struscture used for commands such as stat and lstat here:

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 */
 };

for the line:

buf.st_mode & (0400>>i)

I think they are trying to extract the permissions of the file which is bits 5-16 in the st_mode member of the above structure.

I think we can also extract the info using the macro S_IFMT:

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

I think the developer is doin the samethin manually in your snippet and displaying rwx or a - , I suppse.

As for the st_dev part of the code:

I have never really tried to manipulate that member variable, so I cant really say wat the member does. The best way to find out is look at its definition in sys/types.h .
As for the reason of the usage is same as what Laurence Gonsalves is trying to explain.

All I see from the man pages is the following line:
"The st_ino and st_dev fields taken together uniquely identify the file within the system."

Hope this info helped u.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文