![]() |
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
Quote:
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|