how do i exit from a created shell?

hi everyone, have the following code below in C. i have bin able to figure out ho to come-up with a help program that prints out certain things. please could some show me how or adjust my code for it to support the EXIT COMMAND. using the exit command right now cant exit me from the created shell. thank you all for ur assistance and support in advance.
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
113
114
115
116
117
118
119
120
121
#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");
 	printf ("cp   - copy a file (cp source target)\n");
        printf ("del  - deletes a file del (del file\n");
        printf ("host - prints the hostname of the machine\n");
        printf ("cd   - changes the directory (cd dir1)\n");
        printf ("clr  - clears the screen\n");
        printf ("dir  - lists the contents within a directory\n");
        printf ("help - displays the help associated with your shell\n");
        printf ("quit - exits the shell\n");
        printf ("environ - lists the environment settings\n");
       printf ("echo<comment> - displays the comment followed by a new line\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);
        }

        /*
         * The Child executes the code inside the if
         */
        if (pid == 0)
           {
         
        if (strcmp(*args, "help")== 0)
           {
            help(); /*call the help function*/
         
	   }

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

        }
   /* The parent executes the wait. */

        while (wait (&status) !=pid)
         /*empty */;

}



appreciate any/all suggestions
What? Just check if the user entered "quit" or something as input, and if they did, call exit.
when i type quit in the command line of the new shell. it gives me a "No such file or directory". and exit doesnt do anything.
You don't handle the quit command though.
so any advice on how to handle the quit command
closed account (S6k9GNh0)
May not matter but where is the return type of main, parse, and execute?
Last edited on
@computerquip,
I think he's using pre-ANSI C because in the original C, you declared functions like this:
main(argc, argv) int argc, char** argv;
And if you leave out the return type, it is assumed to be int in pre-ANSI C.

so any advice on how to handle the quit command

Yes. Check if the user entered quit, and if they did... quit.
you should include stdlib.h and at the end exit() or beak....This will end your program without terminating the shell!
for example this is a program that I made recently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a;
    while (1){
    printf("If you are not root, please become...\n\n 1 = What's my mac?               6 = Start interface\n 2 = Change my mac                7 = Start aircrack-ng and hack them all\n 3 = Start capturing IVS          8 = Send DeAth to broadcast\n 4 = Start Reading files          9 = About this prog\n 5 = Stop interface              10 = Exit\n");
    scanf("%int",&a);
    if (a==1) system("/etc/macchanger1.sh");
    else if (a==2) system("/etc/macchanger2.sh");
    else if (a==3) system("/etc/dumpcapt.sh");
    else if (a==4) system("/etc/aireplayread.sh");
    else if (a==5) system("/etc/airmonstop.sh");
    else if (a==6) system("/etc/airmonstart.sh");
    else if (a==7) system("/etc/aircrack.sh");
    else if (a==8) system("/etc/aireplaysenddeath.sh");
    else if (a==9) printf(" This prog was written on February 2010 by Hakermania\n a 15 years old child with purpose to make your life easier\n in hacking yours (and why not your neighborhood's ;)) WEP modems...Cheers!!!\nNote that you must have installed macchanger and aircrack-ng \nContact me: alexsol94@hotmail.com\n");
    else if (a==10) break;
    else printf ("      ERROR, please type a number from 1 to 10\n");
    }
        return 0;

}
First of all, "hakermania?" "A 15 year old child with the purpose of [...] "hacking" WEP modems?" What the hell? Grow up. Anyway, modems are usually associated with wired dialup (serial connections ftw!), not wireless.

Secondly, "hakermania?" What the hell is wrong with you? You don't just say "HAY D00DZ, I KNOW BASIC C++, I AM 1337 HAXX0R NOW, RITE?"

Thirdly, you're actually suggesting that someone should use system()?

Fourth, what is the point of that program? Not only do you assume the locations of all those scripts (hardcoded much), but you also provide a completely pointless command-line interface to these scripts anyway (scripts that you probably didnt write yourself, I might add).

Fifth, you used scanf completely wrong. There is no "%int," to store a decimal integer you use %d.

Sixth, what the hell is up with that formatting? And why do your lines go so far off the screen?
1
2
3
printf("string one"
       "string two"
      );

will be concatenated into
printf("string onestring two");

Finally, "hakermania?" GTFO. Seriously.
Last edited on
Topic archived. No new replies allowed.