得到的问题 [英] problem with gets
问题描述
#include< stdio.h>
#include< unistd.h>
#include< string.h>
#include< stdlib.h>
#define MAX 256
char * valid_cmds =" ls ps df" ;;
int main(void){
char line_input [MAX],the_cmd [CMD_MAX];
char * new_args [CMD_MAX],* cp;
int i;
while(1){
printf(" cmd>");
if(gets(line_input)!= NULL){
cp = line_input;
i = 0;
if ((new_args [i] = strtok(cp,""))!= NULL){
sprintf(the_cmd,"%s",new_args [i]);
if((strstr(valid_cmds,the_cmd) - valid_cmds)%4 == 1){
do {
++ i;
cp = NULL;
new_args [i] = strtok(cp,"");
} while(i< CMD_MAX -1& & new_args [i]!= NULL);
new_args [i] = NULL;
开关(fork()){
case 0:
execvp(new_args [0],new_args);
perror(exec failure);
exit(1);
case -1:
perror(fork failure);
break;
默认值:
;
}
}否则
printf(" huh?\ n");
}
}
}
}
上面的代码取自unix系统编程书,我编译了
red hat 9中的代码和错误消息出来了。有人可以告诉我
如何使代码工作任何帮助将不胜感激。
make -k shell
cc shell .c -o shell
/tmp/ccOmPIMu.o(.text+0x2f):在函数main中:
:`gets''函数很危险不应该使用。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
#define CMD_MAX 10
char *valid_cmds = " ls ps df ";
int main (void) {
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while(1) {
printf ("cmd> ");
if (gets(line_input ) != NULL) {
cp = line_input;
i = 0;
if ((new_args[i] =strtok(cp, " " )) != NULL) {
sprintf(the_cmd, "%s ", new_args[i]);
if((strstr(valid_cmds,the_cmd) - valid_cmds) % 4 == 1) {
do {
++i;
cp = NULL;
new_args[i] = strtok(cp, " ");
} while (i < CMD_MAX -1 && new_args[i] != NULL);
new_args[i] =NULL;
switch (fork( )) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
break;
default:
;
}
} else
printf("huh?\n");
}
}
}
}
the code above is taken from a unix system programming book, i compile
the code in red hat 9 and error message came out. can someone tell me
how to make the code work any help will be appreciated.
make -k shell
cc shell.c -o shell
/tmp/ccOmPIMu.o(.text+0x2f): In function `main'':
: the `gets'' function is dangerous and should not be used.
推荐答案
< jo **** @ yahoo.com>在消息中写道
新闻:e9 ************************** @ posting.google.c om ...
<jo****@yahoo.com> wrote in message
news:e9**************************@posting.google.c om...
Re:获取
的问题
''gets()''*是*问题。不要用它。
无法安全使用它。
请改用''fgets()'。
#include< stdio.h>
#include< unistd.h>
#include< string.h>
#include< stdlib.h>
#define MAX 256
#define CMD_MAX 10
char * valid_cmds =" ls ps df" ;;
int main(void){
char_input [MAX],the_cmd [CMD_MAX];
char * new_args [CMD_MAX],* cp;
int i;
while(1){
printf(" cmd>");
if(gets(line_input)!= NULL){
cp = line_input;
i = 0;
if((new_args [i] = strtok(cp,""))!= NULL){
sprintf(the_cmd,"%s",new_args [ i]);
if((strstr(valid_cmds,the_cmd) - valid_cmds)%4 == 1){
做{
++ i;
cp = NULL;
new_args [i] = strtok(cp,"");
} while(i< CMD_MAX -1&& new_args [i]!= NULL);
new_args [ i] = NULL;
switch(fork()){
case 0:
execvp(new_args [0],new_args);
perror(" exec failure");
退出(1);
案例-1:
perror(fork failure);
break;
默认值:
;
}
}否则
printf(" huh?\ n");
}
}
}
上面的代码取自unix系统编程书,我编译红帽9中的代码并出错消息传出来了。谁能告诉我
如何使代码工作任何帮助将不胜感激。
我不认为这是''错误''消息,而是''警告''
消息。编译器是否拒绝翻译你的代码?
make -k shell
cc shell.c -o shell
/tmp/ccOmPIMu.o(.text+0x2f):In函数`main'':
:`gets''函数很危险,不应该使用。
Re: problem with gets
''gets()'' *is* the problem. Don''t use it.
There is no way to use it safely.
Use ''fgets()'' instead.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
#define CMD_MAX 10
char *valid_cmds = " ls ps df ";
int main (void) {
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while(1) {
printf ("cmd> ");
if (gets(line_input ) != NULL) {
cp = line_input;
i = 0;
if ((new_args[i] =strtok(cp, " " )) != NULL) {
sprintf(the_cmd, "%s ", new_args[i]);
if((strstr(valid_cmds,the_cmd) - valid_cmds) % 4 == 1) {
do {
++i;
cp = NULL;
new_args[i] = strtok(cp, " ");
} while (i < CMD_MAX -1 && new_args[i] != NULL);
new_args[i] =NULL;
switch (fork( )) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
break;
default:
;
}
} else
printf("huh?\n");
}
}
}
}
the code above is taken from a unix system programming book, i compile
the code in red hat 9 and error message came out. can someone tell me
how to make the code work any help will be appreciated.
I don''t think that''s an ''error'' message, but a ''warning''
message. Did the compiler refuse to translate your code?
make -k shell
cc shell.c -o shell
/tmp/ccOmPIMu.o(.text+0x2f): In function `main'':
: the `gets'' function is dangerous and should not be used.
这是一个准确的评估。请使用''fgets()''。
-Mike
This is an accurate assessment. Use ''fgets()'' instead.
-Mike
< jo **** @ yahoo.com>在消息中写道
<jo****@yahoo.com> wrote in message
:`gets''函数很危险,不应该使用。
: the `gets'' function is dangerous and should not be used.
用fgets替换gets() )交换未定义的行为为不正确的
行为,这将关闭编译器(因为它不知道该程序的
目的),但将帮助没有人。
您需要一个策略,如果输入的行太长,则会警告用户
长 - 大概是它的长度超过256个字符必须是垃圾 -
并丢弃它。
Just replacing gets() with fgets() swaps undefined behaviour for incorrect
behaviour, which will shut up the compiler (since it doesn''t know the
purpose of the program) but will help no-one.
You need a strategy that will warn the user if the line entered is too
long - presumably if its over 256 characters in length in must be garbage -
and discard it.
尽管你不应该使用gets函数你仍然可以运行程序
$ b $当我使用gets函数时,bi得到相同的消息。
我用它只是为了看看我用suse 8.2会发生什么
all though you should not use the gets function you can still run the program
i get the same message when i use the gets function.
I use it just to see what happens I use suse 8.2
这篇关于得到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!