Hi there,
I'm making a simple shell program in pico using Unix.
I was given most the code from a tutor and added bits to it, it's supposed to go to a prompt allowing user to type but instead it does nothing.
No errors are shown.
Btw: i use gcc -o shell shell.c to try to compile and run it.

Hopefully someone could read what i have below or try it for themselves and point me in the right direction, Having the prompt come up would make thing alot easier for me!

Thanks in advance.

#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
    char buf[1024];
    char *args[64];

    for(;;)
    {
    printf("Command:");

    if(fgets(buf, 100, stdin) == NULL)
    {
    printf("\n");
    exit(0);
    }

    parse(buf,args);
    execute(args);
    }
}

void help()
}
printf("oh no");
printf("oh yes");
}

parse(buf,args)
char *buf;
char **args;
{
    while(*buf != 0)
    {
    while((buf*buf == ' ') || (*buf == '\t'))
        *buf++ = 0;

    *args++ = buf;

    while((*buf!= 0) && (*buf != ' ') && (*buf != '\t'))
        buf++;
    }

    *args = NULL;
}

execute(args)
char **args;
{
    int pid,status;

    if ((pid = fork())<0)
    {
    perror("fork");
    exit(1);
    }

    if (pid == 0)
    {
    if(strcmp(*args,"help")==0)
    {
        help();
    }
    else
    {
        execvp(*args,args);
    }
    perror(*args);
    exit(1);
    }
   
    while (wait(&status) !=pid)
    /*empty*/;
}

Edited 6 Years Ago by thehivetyrant: n/a

No errors are shown.
Btw: i use gcc -o shell shell.c to try to compile and run it.

Then you must be posting a different piece of source code that what you compiled. What you posted has several syntax errors.

Watch the red and the comments

void help()
} /* wrong bracket */
printf("oh no");
printf("oh yes");
}
for(;;)
    {
    printf("Command:"); /* it doesn't have a break line, therefore it is not guaranteed to display when you want */
    fflush(stdout); /* add fflush() to ensure proper prompt display */

    if(fgets(buf, 100, stdin) == NULL) /* buf is 1024, why to limit it to just a teth of it? */
    {
    printf("\n");
    exit(0); /* the exit() function need the header file stdlib.h include */
    }
while(*buf != 0)
    {
    while((buf*buf == ' ') || (*buf == '\t')) /* improper, remove a buf */
        *buf++ = 0;

    *args++ = buf;

Haven't check anything else.

Okay thanks,

The first one is probably my coping skills (since i cant cut and paste)

I'll get around to adjusting my code according to your feedback.

Thankyou :)

This article has been dead for over six months. Start a new discussion instead.