how can i quit/exit from a create shell. - Unix

This is a discussion on how can i quit/exit from a create shell. - Unix ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: how can i quit/exit from a create shell.

  1. how can i quit/exit from a create 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 QUIT/EXIT COMMAND. using the quit/exit command right now cant exit me from the created shell. thank you all for ur assistance and support in advance.
    Code:
    #include 
    #include 
    #include 
    #include 
    
    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 - 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

  2. Thumbs up Re: how can i quit/exit from a create shell.

    I have worked out your code.I found few mistakes there ,

    To check the exit or quit command from the user just you just check using the buf content using strcmp.
    Also when I execute your code I got some warnings .You used gets() to get the input,but it is not a good practice always use fgets().Then you checked the single character with the NULL.to check the single character use '\0'.

    Here is the code .

    Code:
           #include 
    #include 
    #include
    main ()
    {
            char buf [1024];
            char *args [64];
    
            for (;;)
            {
                    /*
                     *prompt for and read a command.
                     */
                    printf ("trial_shell:");
    
                    //   if (gets(buf) == NULL)
                    if(fgets(buf,sizeof(buf),stdin)==NULL) // It is better than fgets
                    {
                            printf ("\n");
                            exit (0);
                    }
    
                    /*
                     * Split the string into Arguments.
                     */
                    buf[strlen(buf)-1]='\0'; // To remove the new line
    
                    parse (buf, args);
    
                    /*
                     * Execute the command.
                     */
    
                    /* Check it here ,if it is exit or quit come out from the shell */
                    if((strcmp(*args,"exit")==0)||(strcmp(*args,"quit")==0))
                    {
                            exit(0);
                    }
    
                    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 - 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 != '\0') // Since *buf has single character compare it with '\0'
            {
                    /*
                     *Strip White Spaces. Use Nulls, so
                     *that the previous argument is terminated automatically
                     */
                    while ((*buf == ' ') || (*buf == '\t'))
                            *buf++ = '\0';
    
                    /* Save the Argument. */
    
                    *args++ = buf;
    
                    /* Skip over the argument. */
                    while ((* buf != '\0') && (*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 */;
    
    }
    Thanks

+ Reply to Thread