The UNIX and Linux Forums  
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell program nivas Shell Programming and Scripting 8 02-21-2008 05:23 AM
shell program jyotiardeshana Shell Programming and Scripting 4 01-03-2006 06:51 AM
shell program rameshparsa Shell Programming and Scripting 1 11-17-2005 01:18 PM
C shell Program Reza Nazarian Shell Programming and Scripting 2 07-28-2003 03:52 PM
shell program nageshrc UNIX for Advanced & Expert Users 2 11-27-2001 12:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2005
TexasGuy TexasGuy is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 6
Shell Program

I am programming the following simple shell program. It works for the most part, things like 'ls' and 'ps' work just fine. However when I add options, (example, ls -l) it does not execute the command. Also, I've been trying to add an "exit" command, so that I don't have to use the iterrupt; but every time I try..' if (buf == "exit") exit(0) ' in the child it doesn't seem to read the array for some reason. Any suggestions?

Thanks
Code:
#include        <sys/types.h>
#include        <sys/wait.h>
#include        <signal.h>
#include        <stdio.h>
#include        <stdlib.h>

static void     sig_int(int);           /*signal-catching function */
int MAXLINE = 4096;

int
main(void)
{
        char    buf[MAXLINE];
        pid_t   pid;
        int             status;

        if (signal(SIGINT, sig_int) == SIG_ERR)
                printf("signal error");

        printf("shell%% ");  /* print prompt */

        while (fgets(buf, MAXLINE, stdin) != NULL) {
                buf[strlen(buf) - 1] = 0;       /* replace newline with null */

                if ( (pid = fork()) < 0)
                        printf("fork error");

                else if (pid == 0) { /*child*/
                        execlp(buf, buf, (char *) 0);
                        printf("couldn't execute: %s", buf);
                        exit(127);
                }

                /* parent */
                if ( (pid = waitpid(pid, &status, 0)) < 0)
                        printf("waitpid error");
                printf("shell%% ");
        }
        exit(0);
}

void
sig_int(int signo)
{
        printf("interrupt\n");

Last edited by Perderabo; 03-11-2005 at 09:11 PM.. Reason: Add code tags for readability
  #2 (permalink)  
Old 03-11-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
For one thing: if (buf == "exit")
is not useful. You can't compare a string like that. See "man strcmp".
  #3 (permalink)  
Old 03-12-2005
TexasGuy TexasGuy is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 6
Quote:
Originally Posted by Perderabo
For one thing: if (buf == "exit")
is not useful. You can't compare a string like that. See "man strcmp".
Thanks, that is actually what I used, what I didn't realize was that the return value 0 is true, so my if statement wasn't working correctly. I am relatively new to fork() and the exec commands, which is why I am playing with this. My problem with the options still remains, and I can't seem to figure out why. ls works just fine, but ls -l doesn't execute at all.
  #4 (permalink)  
Old 03-12-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
Well you're not very close to anything that will work right. execlp doesn't work like that. A correct call would be more like:
execlp ("ls", "ls", "-l", (char *)0)
but this would be used if happened to know that that want to invoke ls -l. You could also get it to work if you're willing to support a single argument. But a shell must really support a variable number of arguments. So you will need something more like....

char *cmd[] = { "ls", "-l", (char *)0 };
execvp ("ls", cmd);

except that you must parse the command line from the user and build the cmd array dynamically. Considering that you're having trouble comparing two strings, this may be more than you're ready for at present. You need to walk before you can run.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0