
请有各位大虾帮我分析下面关于LINUX文件系统编程的代码,然后回答几个问题?
代码如下:#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<fcntl.h>#includ...
代码如下:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
#include<stdlib.h>
#define MAX 128
int chmd();
int chmd ()
{
int c;
mode_t mode=S_IWUSR;
printf(" 0. 0700\n 1. 0400\n 2. 0200 \n 3. 0100\n ");
printf("Please input your choice(0-3):");
scanf("%d",&c);
switch(c)
{
case 0: chmod("file1",S_IRWXU);break;
case 1: chmod("file1",S_IRUSR);break;
case 2: chmod("file1",S_IWUSR);break;
case 3: chmod("file1",S_IXUSR);break;
default:printf("You have a wrong choice!\n");
}
return(0);
}
main()
{
int fd;
int num;
int choice;
char buffer[MAX];
struct stat buf;
char* path="/bin/ls";
char* argv[4]={"ls","-l","file1",NULL};
while(1)
{
printf("********************************\n");
printf("0. 退出\n");
printf("1. 创建新文件\n");
printf("2. 写文件\n");
printf("3. 读文件\n");
printf("4. 修改文件权限\n");
printf("5. 查看当前文件的权限修改文件权限\n");
printf("********************************\n");
printf("Please input your choice(0-6):");
scanf("%d",&choice);
switch(choice)
{
case 0:close(fd);
exit(0);
case 1:
fd=open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
if(fd==-1)
printf("File Create Failed!\n");
else
printf("fd = %d\n",fd);
break;
case 2:
num=read(0,buffer,MAX);
write(fd,buffer,num);
break;
case 3:
read(fd,buffer,MAX);
write(1,buffer,num);
break;
case 4:
chmd ();
printf("Change mode success!\n");
break;
case 5:
execv(path,argv);
break;
default:
printf("You have a wrong choice!\n");
}
}
}
问:(1)num=read(0,buffer,MAX); write(fd,buffer,num);代码的含义?
(2)系统调用execv()的用途? 展开
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
#include<stdlib.h>
#define MAX 128
int chmd();
int chmd ()
{
int c;
mode_t mode=S_IWUSR;
printf(" 0. 0700\n 1. 0400\n 2. 0200 \n 3. 0100\n ");
printf("Please input your choice(0-3):");
scanf("%d",&c);
switch(c)
{
case 0: chmod("file1",S_IRWXU);break;
case 1: chmod("file1",S_IRUSR);break;
case 2: chmod("file1",S_IWUSR);break;
case 3: chmod("file1",S_IXUSR);break;
default:printf("You have a wrong choice!\n");
}
return(0);
}
main()
{
int fd;
int num;
int choice;
char buffer[MAX];
struct stat buf;
char* path="/bin/ls";
char* argv[4]={"ls","-l","file1",NULL};
while(1)
{
printf("********************************\n");
printf("0. 退出\n");
printf("1. 创建新文件\n");
printf("2. 写文件\n");
printf("3. 读文件\n");
printf("4. 修改文件权限\n");
printf("5. 查看当前文件的权限修改文件权限\n");
printf("********************************\n");
printf("Please input your choice(0-6):");
scanf("%d",&choice);
switch(choice)
{
case 0:close(fd);
exit(0);
case 1:
fd=open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
if(fd==-1)
printf("File Create Failed!\n");
else
printf("fd = %d\n",fd);
break;
case 2:
num=read(0,buffer,MAX);
write(fd,buffer,num);
break;
case 3:
read(fd,buffer,MAX);
write(1,buffer,num);
break;
case 4:
chmd ();
printf("Change mode success!\n");
break;
case 5:
execv(path,argv);
break;
default:
printf("You have a wrong choice!\n");
}
}
}
问:(1)num=read(0,buffer,MAX); write(fd,buffer,num);代码的含义?
(2)系统调用execv()的用途? 展开
4个回答
展开全部
(1)num=read(0,buffer,MAX); write(fd,buffer,num);
表示从标准输入设备中(键盘输入) 读取数据放入buffer 再写到“file1”中
0代表标准输入设备, 1代表标准输出设备 , 2,代表错误输出设备
(2)execv( path , argv ) 用来执行path字符串所代表的文件路径文 argv是执行命令
总的来说就是执行 ls -l file1这个命令 这个命令执行后 会列出file1的文件属性
包括访问权限
表示从标准输入设备中(键盘输入) 读取数据放入buffer 再写到“file1”中
0代表标准输入设备, 1代表标准输出设备 , 2,代表错误输出设备
(2)execv( path , argv ) 用来执行path字符串所代表的文件路径文 argv是执行命令
总的来说就是执行 ls -l file1这个命令 这个命令执行后 会列出file1的文件属性
包括访问权限
展开全部
num=read(0,buffer,MAX);获取标准输入文件的类容,方到buffer离去!MAX是大小
write(fd,buffer,num);把buffer里面的内容写道fd(文件file1)里面去,大小num
The execv() functions provide an array of pointers to
null-terminated strings that represent the argument list available to
the new program. The first argument, by convention, should point to
the filename associated with the file being executed. The array of
pointers must be terminated by a NULL pointer.
write(fd,buffer,num);把buffer里面的内容写道fd(文件file1)里面去,大小num
The execv() functions provide an array of pointers to
null-terminated strings that represent the argument list available to
the new program. The first argument, by convention, should point to
the filename associated with the file being executed. The array of
pointers must be terminated by a NULL pointer.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
read和write就是读写文件,不过是底层调用
num=read(0,buffer,MAX);
num:实际读取的字符数
0:文件标识符
buffer:缓存变量
max:想读取的字符数
execv():开启执行另一个进程。
num=read(0,buffer,MAX);
num:实际读取的字符数
0:文件标识符
buffer:缓存变量
max:想读取的字符数
execv():开启执行另一个进程。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这样,到一些主流的网络安全网站上很了解计算机的安全、特征及方法,掌握了这些,再学习如何颠履他的安全。
不用着急,慢慢来。
但是,如果想成为一个特别好的黑客,第一,要有一些计算机知识;第二,要刻苦;第三,要勤于思考;第四,要有耐心。
任何一个黑客都不能保证每次成功,所以,一两次甚至若干次的失败都不说明水平不够。
有的时候当黑客也是需要缘份的。
兄弟,祝你成功。
不用着急,慢慢来。
但是,如果想成为一个特别好的黑客,第一,要有一些计算机知识;第二,要刻苦;第三,要勤于思考;第四,要有耐心。
任何一个黑客都不能保证每次成功,所以,一两次甚至若干次的失败都不说明水平不够。
有的时候当黑客也是需要缘份的。
兄弟,祝你成功。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询