#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main(int argc,char **argv){
char buf[512];
memset(buf,0,sizeof(buf));
if(argc!=2){
printf("usage: %s version|devices\n",argv[0]);
return -1;
}
// strcmp 如果相等 返回值为 0
if(!strcmp(argv[1],"-v")){
// 读取proc/vesion;
int fd = open("/proc/version",O_RDONLY);
if(fd < 0){
perror("open /proc/version");
}
read(fd,buf,sizeof(buf));
printf("结果是 %s",buf);
}else if(!strcmp(argv[1],"-d")){
//读取 proc/devices;
int fd = open("/proc/devices",O_RDONLY);
if(fd < 0){
perror("open /proc/devices");
}
read(fd,buf,sizeof(buf));
printf("结果是 %s",buf);
}
return 0;
}
效果如图: