gets() vs fgets()

I am totally cursed!

Page 1 of 1

5 Replies - 5882 Views - Last Post: 07 June 2008 - 12:06 AM Rate Topic: -----

#1 clargs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-May 08

gets() vs fgets()

Posted 06 June 2008 - 01:45 AM

Friends,
I'm writing a stupid and simple shell. So far I've finished coding and I've compiled it. It works like a charm. But the thing I'm concerned about is that gcc gives me a warning.

shelltox.c:(.text+0x2a): warning: the `gets' function is dangerous and should not be used.
I've googled this and learned why it was dangerous.

After getting this warning I decided to use fgets(). After the implementation my code doesn't work anymore. It compiles perfectly. After it prints the prompt I write for example "ls" command. Then I get the error "ls command not found". I guess there is a NULL character or something like that which is passed with the command to execvp call thats why it cant find the command.

It's been 39 hours now just for a stupid shell and I really really really want to sleep (had 15 cups of coffee). Please help me out here. I'm totally cursed! :crazy:

I'm posting my code. One with gets() other one with fgets.

I hope your day will be perfect "if you help me"! :pirate:

#define _GNU_SOURCE
//#include <curses.h> /* for coloring? */
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pwd.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/utsname.h>
#include <unistd.h>

void sig_int(int signo)
{
	exit(0);
}

void prompt(void){
	struct passwd *passwd;
	passwd = getpwuid (getuid()); /* Get login name, home directory */
	
	char *homedir = passwd->pw_dir;
	char *currentdir = getcwd(NULL, 0);
	char *loginname = passwd->pw_name;

	//struct utsname *sysinfo; /* Get machine name (didn't work correctly) */
	//int a=uname(sysinfo);
	
	printf("\n%s@%s:%s>", loginname, /*sysinfo->nodename*/ getenv("HOSTNAME"), currentdir);
}

void parse(char *line, char **argv)
{
	while(*line != '\0') { /* if not the end of line ....... */
		while(*line == ' ' || *line == '\t' || *line == '\n'){
			*line++ = '\0'; /* replace white spaces with 0 */
		}
		*argv++=line; /* save the argument position */
		while(*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n'){
			line++; /* skip the argument until ... */
		}
	}
	*argv='\0'; /* mark the end of argument list */
}

void execute(char **argv)
{
	pid_t pid;
	int status;

	if((pid = fork()) < 0){ /* fork a child process */
		printf("*** ERROR: forking child process failed\n");
		exit(1);
	}
	else if(pid == 0){ /* for the child process: */
		if (execvp(*argv, argv) < 0) { /* execute the command */
			printf("kabuk: %s: command not found",argv[0]);
			exit(1);
		}
	}
	else{ /* for the parent: */
		while (wait(&status) != pid); /* wait for completion */
	}
}

void export(void){
	extern char **environ;
	int i=0;
	
	while(environ[i]!='\0'){
		printf("%d %s\n",i,environ[i++]);
	}
}

int main(int argc, char *argv[])
{
	char line[1024];
	char *argva[64]; /* kabuk is limited to take 64 command + arguments in a line */
	
	while (1) {
		prompt(); /* Print the prompt */
		gets(line); /* Get user input */
		parse(line, argva); /* Parse user input */
		
		if(!strcmp(argva[0],"exit")){ 
			exit(0);
		}
		else if(!strcmp(argva[0],"cd")){
			if(argva[1]!='\0')
				if(chdir(argva[1])){printf("kabuk: cd: %s: No such file or directory",argva[1]);}
		}
		else if(!strcmp(argva[0],"cd..")){
			chdir("..");
		}
		else if(!strcmp(argva[0],"cd;")){
			chdir(getenv("HOME"));
		}
		else if(!strcmp(argva[0],"export")){
			export();
		}
		else if(!strcmp(argva[0],"$PATH")||!strcmp(argva[0],"path")){
			printf(getenv("PATH"));
		}
		else {
			execute(argva); /* otherwise, execute the command */
		}
	}
	return( 0 );
}


gets(line);
WORKS

instead of that I wrote fgets(line,sizeof(line),stdin);

now it doesn't work. S.O.S ... --- ... !!!! :ph34r: :blink:

Regards,
clargs

Is This A Good Question/Topic? 0
  • +

Replies To: gets() vs fgets()

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: gets() vs fgets()

Posted 06 June 2008 - 02:07 AM

If that's working with gets then keep it with gets. Or if you still want to go with fgets then first verify what are you getting in the line variable through fgets [may be printf after fgets or something like that.]. I hope your problem will get solved very soon. just take a small break and debug with a fresh mind. You will be through. all the best. :)
Was This Post Helpful? 0
  • +
  • -

#3 clargs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-May 08

Re: gets() vs fgets()

Posted 06 June 2008 - 04:52 AM

Am I expecting the answers to get replied very fast or isn't anyone trying answer it. Well I'm very new to this community but my first impression is that any of the questions are not properly answered except the really basic ones. I'm really suck and even my instructor doesn't know the answer... That's sad. :v: :ph34r: :^: I guess I should quit programming...
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: gets() vs fgets()

Posted 06 June 2008 - 05:03 AM

If gets() works, use that. Amit had a good answer I thought. You could also write a debug function (as he suggested) to assert whether or not your function is working correctly.
Was This Post Helpful? 0
  • +
  • -

#5 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: gets() vs fgets()

Posted 06 June 2008 - 01:31 PM

helllo I have tried to type it all in a compilede it i don't get that warning perhapes it's not a serious one have you tried other compilers ??? that som times help solving the problem
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: gets() vs fgets()

Posted 07 June 2008 - 12:06 AM

Quote

shelltox.c:(.text+0x2a): warning: the `gets' function is dangerous and should not be used.I've googled this and learned why it was dangerous.

After getting this warning I decided to use fgets().

Actually, fgets() is not the replacement for gets(). (Although you can use it though)

The warning actually states that you should use the safer alternative that is not prone to Buffer Overflow Attacks. The function that the compiler is trying to mention is the gets_s() function.

Use the gets_s() function with an extra size parameter and you won't get the warning anymore.

Here's the prototype of the gets_s() function.
char *gets_s( 
   char *buffer,
   size_t sizeInCharacters
);

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1