C unix中的系统调用:目录和文件
嗨,我正在尝试理解系统调用:unix上的目录和文件。。我找到了这个网站,他们用自己的例子解释了一些调用,但不理解这些代码片段C unix中的系统调用:目录和文件,c,unix,file,directory,system-calls,C,Unix,File,Directory,System Calls,嗨,我正在尝试理解系统调用: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)
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 ("-"); ....
void状态(char*文件)
{
结构统计buf;
结构passwd*pw;
结构组*gr;
int i;
if(stat(file,&buf)=-1)
{
佩罗尔(档案);
出口(-1);
}
printf(“文件:%s\n”,archivo);
printf(“\t驻留在设备中:%d,%d\n”,(buf.st_dev&0xFF00)>>8,buf.st_dev&0x00FF);
printf(“\t i-node编号:%d\n”,buf.st\u ino);
printf(“\t类型:”);
开关(buf.st_模式和S_IFMT)
{
案例S_IFREG:printf(“序号”);break;
案例S_IFDIR:printf(“directorio”);break;
案例S_IFCHR:printf(“tipo caracter”);break;
案例S_IFBLK:printf(“tipo bloque”);break;
案例S_IFFO:printf(“FIFO”);中断;
}
如果(buf.st_模式和S_ISUID)printf(“\tSUID activo”);
如果(buf.st_mode&S_ISGID)printf(“\tSGID activo”);
if(buf.st_mode&S_ISVTX)printf(“\tStiky bit activo\n”);
/*权限访问*/
printf(“\t许可:0%o”,buf.st_模式和0777);
对于(i=0;i>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 ("-");
printf(“\tReside en el dispositivo:%d,%d\n”,(buf.st_dev&0xFF00)>>8,
buf.st_dev&0x00FF);
/*权限*/
printf(“\t许可:0%o”,buf.st_模式和0777);
对于(i=0;i>i))printf(“%c”,permisos[(8-i)%3]);
else printf(“-”);
欢迎对双方的比较给予任何帮助或解释
对不起我的英语
链接,其中显示了整个代码示例1,名为estado.c
我想你是说:
(buf.st_dev & 0xFF00)>>8
这不是比较。>
是右移运算符。它将第一个操作数右移第二个操作数指定的位数。此表达式将除buf.st_dev的第9位到第16位以外的所有位置零(这就是&0xFF00
的作用)然后将结果8位下移到第1位到第8位的最低有效位。这将产生一个从0到255的数字。(buf.st_dev&0xFF00)>>8,buf.st_dev&0x00FF
不是比较,而是算术表达式:
&
执行操作数的按位二进制和:
0&0=00&1=0
1&0=0
1&1=1
>
执行按位移位
使用&
是从另一个值中“屏蔽”位的常用方法,以便可以对其进行隔离检查。在第一个表达式中,位置8到15中的八位被隔离并移位到位置0到7,因此,第二个至最低有效位字节似乎显示为独立字节
第二个表达式不进行移位,因为掩蔽后它已经处于最低位位置,因此直接有意义。我们正在尝试访问用于命令(如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_模式成员中第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或a-,I suppse
至于代码中的st_dev部分:
我从来没有真正尝试过操纵那个成员变量,所以我不能说这个成员做了什么。最好的办法是查看它在sys/types.h
中的定义。
至于使用的原因,劳伦斯·贡萨尔维斯试图解释的是相同的
我从手册页上看到的是以下一行:
“st_ino和st_dev字段一起唯一标识系统中的文件。”
希望这些信息对你有所帮助