[英]Get information about a file with c in linux
当我运行时:
ls -l /proc/123/fd/80
我可以看到例如:
89-> socket:[38676]
如何在 C 中获取此信息? 我怎么知道这个fd
指向一个套接字/管道并获取括号内的数字( [ ]
之间的38676
)
顺便说一下,括号之间的数字是缓冲区套接字的大小吗?
man fstat将为您提供所需的所有文档。
手册页提供了一个您可以轻松适应的示例:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
if (S_IFSOCK != (sb.st_mode & S_IFMT)) {
printf("Not a socket.\n");
exit(EXIT_FAILURE);
}
printf("socket: %ld\n", (long) sb.st_ino);
exit(EXIT_SUCCESS);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.