Hey,
check out this code... I tried running it with "fgets" as it states that "gets" is a dangerous code.... however, its not executing the linux commands... what could be the problem, or what in this code needs modification??
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
#define MAX 1024
void parse(char*buf, char ** args);
void execute(char**args);
int main()
{
char buf[MAX];
char *args[64];
for(;
{
/*Prompts for and reads a command*/
printf("Command: ");
if(fgets(buf, MAX, stdin) == '\0'){
printf("\n");
exit(0);
}
/*Splits the strings into arguments*/
parse(buf,args);
/*Executes the command*/
execute(args);
}
}
/* Parse: - Splits the commands in buf into individual arguments */
void parse(char*buf,char **args)
//char * buf;
//char ** args;
{
while(*buf != '\0'){
/*Strips the whitespace with the
use of null so previous argument
is terminated automatically*/
while(*buf == ' '|| *buf == '\t' || buf == '\n')
*buf++ = '\0';
/*Saves the argument*/
*args++ = buf;
/*Skips over the argument*/
while((*buf != '\0') && (buf != ' ') && (*buf != '\t') && (*buf != '\n'))
buf++;
}
*args = '\0';
}
/*Execute spawns the child process
and execute the program*/
void execute(char **args)
//char ** args;
{
pid_t pid;
int status;
/*Get a child process*/
if((pid = fork()) < 0){
perror("ERROR executing fork\n");
exit(1);
}
/*Child executes the code inside the if statement*/
if(pid == 0){
execvp(*args, args);
perror(*args);
exit(1);
}
/*The parent executes the wait*/
while (wait(&status) != pid)
/* empty */ ;
}
Thankx in advance.......