Error[8]: Undefined offset: 532, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

文章目录

题目

job5/sh2.c
实现shell程序,要求在第1版的基础上,增加文件重定向功能

踩坑

[bug 1] 不能用gets()获取字符串 line ,因为它在C++11里面不安全,要用fgets(line,sizeof(line),stdin); //stdin是标准输入
[bug 2] exit 不能跳出,需要已经输入的指令数+1,才能退出

因为fgets()输入没有过滤’\n’,split分割时有’\n’在最后一个字符串里面。


ls\n 找不到指令 , exit != exit\n
【解决方法】main() 里面加上,替换\n为\0,再 split() 分割。


char * find;
find = strchr(line, '\n');          //查找换行符
if(find) 
{                           //如果find不为空指针
    *find = ';'//就把一个空字符放在这里                    }
cc sh2.c
测试

#

代码




include#
include#
include#
include#
include#
include#
include#
includestruct
command int{
	; argcchar
	* [argv10];char
	* ;inputchar
	* ;outputchar
	* ;append}
;int

split (char* ,linechar* ,separatorchar * [word_array])int
{
	= word_count 0 ;char
	* ;word=
	word strtok (,line" ");while
	(!=word NULL )[{
		word_array++word_count]= ; word=
		word strtok (NULL," ");}
	return
	; word_count}
void



parse_command (char* ,linestruct command * )commandint
{
	; wordcchar
	* [wordv10];=
	wordc split (,line" ",)wordv;[
	wordv]wordc= NULL ;=
	command->input NULL ;=
	command->output NULL ;=
	command->append NULL ;if
	([wordv-wordc1][0]=='<')//wc =
	{
		command->input & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}//command->argv ={[wc]}
	else
	if ([wordv-wordc1][0]==&&'>'[wordv-wordc1][1]==)'>' =//echo abc >>log
	{
		command->append & [wordv-wordc1][2];[
		wordv--]wordc= NULL ;}//command->argv = {[echo],[abc]}
	else	
	if ([wordv-wordc1][0]==)'>'=
	{
		command->output & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}
	=
	command->argc ; wordc//	printf("%d:%s\n",wordc,wordv[0]);
for
	(int= i0;<=i;wordc++i)[
	{
		command->argv]i= [ wordv]i;}
//		printf("[%s]",command->argv[i]);
	}
void


child (structcommand ) commandint
{
	; fdif
	(.command!=input NULL )//wc {=
		fd open (.command,input,O_RDONLY0666);dup2
		(,fd0);close
		()fd;}
	if
	(.command!=output NULL )={// echo abc >log
		fd open (.command,output|O_CREAT,O_RDWR0666);dup2
		(,fd1);close
		()fd;}
	if
	(.command!=append NULL )={// echo abc >>log
		fd open (.command,append|O_APPEND|O_CREAT,O_RDWR0777);dup2
		(,fd1);close
		()fd;}
	execvp
	(.command[argv0],.command)argv;}
void
print (structcommand ) commandfor
{
	(int= i0;<i.command;argc++i)printf
	{
		("(%d)[%s]",,i.command[argv]i);}
	}
int

main ()while
{
	(1)printf{
		()"> ";char
		[ line100];fgets
		(,linesizeof()line,stdin);char
		* ;find=
		find strchr (,line'\n');if
		()find*
		{
			=find ';' }struct
		command
		; parse_command command(
		,&line);command//		print(command);if
(
		strcmp(.[command0argv],"cd")==0)chdir(
		{
			.[command1argv]);}else
		if
		( strcmp(.[command0argv],"exit")==0)exit(
		{
//			printf("exit>>>>");
			1);}else
		int
		=
		{
			fork  pid ( );if(
			==0pid)child(
			{
				);command}}
			wait
		(
		NULL);}return
	0
	; }[+++]	
[+++]

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 533, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

文章目录

题目

job5/sh2.c
实现shell程序,要求在第1版的基础上,增加文件重定向功能

踩坑

[bug 1] 不能用gets()获取字符串 line ,因为它在C++11里面不安全,要用fgets(line,sizeof(line),stdin); //stdin是标准输入
[bug 2] exit 不能跳出,需要已经输入的指令数+1,才能退出

因为fgets()输入没有过滤’\n’,split分割时有’\n’在最后一个字符串里面。


ls\n 找不到指令 , exit != exit\n
【解决方法】main() 里面加上,替换\n为\0,再 split() 分割。


char * find;
find = strchr(line, '\n');          //查找换行符
if(find) 
{                           //如果find不为空指针
    *find = ';'//就把一个空字符放在这里                    }
cc sh2.c
测试

#

代码




include#
include#
include#
include#
include#
include#
include#
includestruct
command int{
	; argcchar
	* [argv10];char
	* ;inputchar
	* ;outputchar
	* ;append}
;int

split (char* ,linechar* ,separatorchar * [word_array])int
{
	= word_count 0 ;char
	* ;word=
	word strtok (,line" ");while
	(!=word NULL )[{
		word_array++word_count]= ; word=
		word strtok (NULL," ");}
	return
	; word_count}
void



parse_command (char* ,linestruct command * )commandint
{
	; wordcchar
	* [wordv10];=
	wordc split (,line" ",)wordv;[
	wordv]wordc= NULL ;=
	command->input NULL ;=
	command->output NULL ;=
	command->append NULL ;if
	([wordv-wordc1][0]=='<')//wc =
	{
		command->input & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}//command->argv ={[wc]}
	else
	if ([wordv-wordc1][0]==&&'>'[wordv-wordc1][1]==)'>' =//echo abc >>log
	{
		command->append & [wordv-wordc1][2];[
		wordv--]wordc= NULL ;}//command->argv = {[echo],[abc]}
	else	
	if ([wordv-wordc1][0]==)'>'=
	{
		command->output & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}
	=
	command->argc ; wordc//	printf("%d:%s\n",wordc,wordv[0]);
for
	(int= i0;<=i;wordc++i)[
	{
		command->argv]i= [ wordv]i;}
//		printf("[%s]",command->argv[i]);
	}
void


child (structcommand ) commandint
{
	; fdif
	(.command!=input NULL )//wc {=
		fd open (.command,input,O_RDONLY0666);dup2
		(,fd0);close
		()fd;}
	if
	(.command!=output NULL )={// echo abc >log
		fd open (.command,output|O_CREAT,O_RDWR0666);dup2
		(,fd1);close
		()fd;}
	if
	(.command!=append NULL )={// echo abc >>log
		fd open (.command,append|O_APPEND|O_CREAT,O_RDWR0777);dup2
		(,fd1);close
		()fd;}
	execvp
	(.command[argv0],.command)argv;}
void
print (structcommand ) commandfor
{
	(int= i0;<i.command;argc++i)printf
	{
		("(%d)[%s]",,i.command[argv]i);}
	}
int

main ()while
{
	(1)printf{
		()"> ";char
		[ line100];fgets
		(,linesizeof()line,stdin);char
		* ;find=
		find strchr (,line'\n');if
		()find*
		{
			=find ';' }struct
		command
		; parse_command command(
		,&line);command//		print(command);if
(
		strcmp(.[command0argv],"cd")==0)chdir(
		{
			.[command1argv]);}else
		if
		( strcmp(.[command0argv],"exit")==0)exit(
		{
//			printf("exit>>>>");
			1);}else
		int
		=
		{
			fork  pid ( );if(
			==0pid)child(
			{
				);command}}
			wait
		(
		NULL);}return
	0
	; }	
[+++]

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
【 *** 作系统 实验五】--- 重定向输入、输出、追加_C_内存溢出

【 *** 作系统 实验五】--- 重定向输入、输出、追加

【 *** 作系统 实验五】--- 重定向输入、输出、追加,第1张

文章目录
    • 题目
    • 踩坑
    • 测试
    • 代码

题目

job5/sh2.c
实现shell程序,要求在第1版的基础上,增加文件重定向功能

  • 重定向输入
  • 重定向输出
  • 重定向追加
踩坑

[bug 1] 不能用gets()获取字符串 line ,因为它在C++11里面不安全,要用fgets(line,sizeof(line),stdin); //stdin是标准输入
[bug 2] exit 不能跳出,需要已经输入的指令数+1,才能退出

因为fgets()输入没有过滤’\n’,split分割时有’\n’在最后一个字符串里面。


ls\n 找不到指令 , exit != exit\n
【解决方法】main() 里面加上,替换\n为\0,再 split() 分割。


char * find;
find = strchr(line, '\n');          //查找换行符
if(find) 
{                           //如果find不为空指针
    *find = ';'//就把一个空字符放在这里                    }
cc sh2.c
测试

#

代码




include#
include#
include#
include#
include#
include#
include#
includestruct
command int{
	; argcchar
	* [argv10];char
	* ;inputchar
	* ;outputchar
	* ;append}
;int

split (char* ,linechar* ,separatorchar * [word_array])int
{
	= word_count 0 ;char
	* ;word=
	word strtok (,line" ");while
	(!=word NULL )[{
		word_array++word_count]= ; word=
		word strtok (NULL," ");}
	return
	; word_count}
void



parse_command (char* ,linestruct command * )commandint
{
	; wordcchar
	* [wordv10];=
	wordc split (,line" ",)wordv;[
	wordv]wordc= NULL ;=
	command->input NULL ;=
	command->output NULL ;=
	command->append NULL ;if
	([wordv-wordc1][0]=='<')//wc =
	{
		command->input & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}//command->argv ={[wc]}
	else
	if ([wordv-wordc1][0]==&&'>'[wordv-wordc1][1]==)'>' =//echo abc >>log
	{
		command->append & [wordv-wordc1][2];[
		wordv--]wordc= NULL ;}//command->argv = {[echo],[abc]}
	else	
	if ([wordv-wordc1][0]==)'>'=
	{
		command->output & [wordv-wordc1][1];[
		wordv--]wordc= NULL ;}
	=
	command->argc ; wordc//	printf("%d:%s\n",wordc,wordv[0]);
for
	(int= i0;<=i;wordc++i)[
	{
		command->argv]i= [ wordv]i;}
//		printf("[%s]",command->argv[i]);
	}
void


child (structcommand ) commandint
{
	; fdif
	(.command!=input NULL )//wc {=
		fd open (.command,input,O_RDONLY0666);dup2
		(,fd0);close
		()fd;}
	if
	(.command!=output NULL )={// echo abc >log
		fd open (.command,output|O_CREAT,O_RDWR0666);dup2
		(,fd1);close
		()fd;}
	if
	(.command!=append NULL )={// echo abc >>log
		fd open (.command,append|O_APPEND|O_CREAT,O_RDWR0777);dup2
		(,fd1);close
		()fd;}
	execvp
	(.command[argv0],.command)argv;}
void
print (structcommand ) commandfor
{
	(int= i0;<i.command;argc++i)printf
	{
		("(%d)[%s]",,i.command[argv]i);}
	}
int

main ()while
{
	(1)printf{
		()"> ";char
		[ line100];fgets
		(,linesizeof()line,stdin);char
		* ;find=
		find strchr (,line'\n');if
		()find*
		{
			=find ';' }struct
		command
		; parse_command command(
		,&line);command//		print(command);if
(
		strcmp(.[command0argv],"cd")==0)chdir(
		{
			.[command1argv]);}else
		if
		( strcmp(.[command0argv],"exit")==0)exit(
		{
//			printf("exit>>>>");
			1);}else
		int
		=
		{
			fork  pid ( );if(
			==0pid)child(
			{
				);command}}
			wait
		(
		NULL);}return
	0
	; }	


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/621722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-15
下一篇 2022-04-15

发表评论

登录后才能评论

评论列表(0条)

保存