Design and implementation of a shell

i was able to create this shell below. how can i make this shell support commands like cp, del, cd, host, quit etc. i have tried the help calling the function of help (lines 38 - 43). please would be highly greatful if any assistance or suggestions can be shown. thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

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

        for (;;)
        {
        /*
         *prompt for and read a command.
         */
        printf ("trial_shell:");

        if (gets(buf) == NULL)
        {
        printf ("\n");
        exit (0);
        }

        /*
         * Split the string into Arguments.
         */
        parse (buf, args);

        /*
         * Execute the command.
 	*/
        execute (args);
        } // endof for loop
}

/*Simple help function that dumps help text to the screen*/

void help()

        {
        printf ("*******trial_shell usage/help options********/n");
        printf ("help - prints the list of commands supported by trial_shell/n");
        }

/* parse -- Splits the command in buf into individual arguments */
parse (buf, args)
        char *buf;
        char **args;
        {
          while ( *buf != NULL)
        {
        /*
         *Strip White Spaces. Use Nulls, so
         *that the previous argument is terminated automatically
         */
          while ((*buf == ' ') || (*buf == '\t'))
                *buf++ = NULL;

        /* Save the Argument. */

     *args++ = buf;

        /* Skip over the argument. */
         while ((* buf != NULL) && (*buf != ' ' ) && (*buf != '\t'))
         buf++;
        }

        *args= NULL;
}

/* execute -- spawns a child process and execute the program */
execute (args)
        char **args;
        {
        int pid,status;

        /* Get a child process */

        if ((pid= fork()) <0)
        {
        perror("fork");
        exit (1);
        /* NOTE. perror() produces a short error message on the standard
           error describing the last error encountered during a call to a
           system or library function */
        }

        /*
         * The Child executes the code inside the if
         */
        if (pid == 0)
           {
            execvp (*args, args);
            perror (*args);
            exit (1);

        if (strcmp(*args, "help")== 0)
           {
            help(); /*call the help function*/
           }

	   else
           {
          execvp (*args, args);
        }
          perror (*args);
          exit (1);
       }

        while (wait (&status) !=pid)
         /*empty */;
}
Last edited on
Are you using pre-ANSI C for this perchance?
1
2
execute (args)
        char **args;


Anyway, I don't get what you're trying to do. You just want to print that help message when someone types "help" or you want to get it as a command line argument? If it's the former, use strcmp. If it's the latter, use getopt_long.

getopt_long(3): http://linux.die.net/man/3/getopt_long
Last edited on
thank you so much. yes when someone types help; the help message displays. would be greatful
If you don't know how to compare strings, how are you meant to know how to write a shell?
I'd learn how to do things like string manipulation first.
Topic archived. No new replies allowed.