473,471 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

shell programming problem

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

/*limit the lenght of input to 256 character and number
of argument to 10*/
#define MAX 256
#define CMAX 10
char logout[]= "logout";

int spawn(char* program, char** arg_list)
{
pid_t child_pid;
if(!strcmp(program,logout))
{
exit(0);
}
if ((child_pid=fork())==0)
{
execvp(program,arg_list);
perror("execvp");
exit(1);
}
}

int main()
{
char input[MAX];
char *p;
char *arg_list[CMAX+1];
int i;
while(1)
{
printf("Shell> ");
if (fgets(input,sizeof input, stdin)!=NULL)
{
if(strchr(input,'\n'))
*strchr(input,'\n')=0;
else
{
while (!strchr(input,'\n'))
fgets(input, sizeof input,stdin);
exit(1);
}
}
p=strtok(input," ");
for(i=0;i<CMAX;++i)
{
arg_list[i]=p;
p=strtok(NULL," ");
}
arg_list[i]=0;

spawn(arg_list[0], arg_list);
}
return 0;
}

the above program can accept commnand from user and execute it, i need
advice on how to expand its function to include pipe function. other
thing is where should i place code to exit when logout is enter, i
have try checking in spawn process it works but the program will
return segmentation error when click enter with not other command.
thanks in advance.
Nov 13 '05 #1
1 2057
jo****@yahoo.com wrote:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Use of this header (which is not a standard C header) makes me
suspect that your question is off topic here and that should take
your question to:

news:comp.unix.programmer
#include <sys/types.h>
Hmmm, I'm starting to think my suspicion is correct.
#include <string.h>

/*limit the lenght of input to 256 character and number
of argument to 10*/
#define MAX 256
#define CMAX 10
char logout[]= "logout";

int spawn(char* program, char** arg_list)
{
pid_t child_pid;
if(!strcmp(program,logout))
{
exit(0);
}
if ((child_pid=fork())==0)
{
execvp(program,arg_list);
perror("execvp");
exit(1);
}
}

int main()
{
char input[MAX];
char *p;
char *arg_list[CMAX+1];
int i;
while(1)
{
printf("Shell> ");
if (fgets(input,sizeof input, stdin)!=NULL)
{
if(strchr(input,'\n'))
*strchr(input,'\n')=0;
else
{
while (!strchr(input,'\n'))
fgets(input, sizeof input,stdin);
exit(1);
}
}
p=strtok(input," ");
for(i=0;i<CMAX;++i)
{
arg_list[i]=p;
p=strtok(NULL," ");
}
arg_list[i]=0;

spawn(arg_list[0], arg_list);
}
return 0;
}

the above program can accept commnand from user and execute it, i need
advice on how to expand its function to include pipe function. other
thing is where should i place code to exit when logout is enter, i
have try checking in spawn process it works but the program will
return segmentation error when click enter with not other command.
thanks in advance.


Yup.
Off topic here.

news:comp.unix.programmer is the place for you to ask.

--ag
--
Artie Gold -- Austin, Texas

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ken Miller | last post by:
Hi all. I'm fairly new to programming, and have started using visual basic.net, just found this place, so hope someone can help as I'm a little stuck :) I'm trying to write a small front end to...
9
by: none | last post by:
Hello all, I wrote a shell program a few years ago in VB6 that needs to be modified. The problem I have is this: The SysAdmin uses this shell in place of Explorer, so there is no taskbar. When...
10
by: Daven Nair | last post by:
Hi, I would like to know if Python supports codes similar to shell scripts: count=`ps -ef|grep "pattern"|wc -l` for count in `echo $count` do done fi
2
by: Francesco | last post by:
Hello Pythonnian's (sorry for crossposting) I have written on the base of Miro Rajic a (still) small filemanager in wxPython. Now I want to add (for Windows XP) a explorer shell context menu....
5
by: Javaman59 | last post by:
Hi all, I'm a Unix guy from way back, who's enjoying learning C# and Windows programming. Overall, it's not such a difficult transition (as long as one keeps an open mind :) ), but there is one...
2
by: Sin | last post by:
Hello everyone, I'm totally stumped at how little info I can find in MSDN or on the web concerning this. It's almost as if only microsoft personel hold the key to these secrets or something!!! ...
5
by: gjuro kladaric | last post by:
it was not possible to make a shell extension from within VB2003, I believe has anything changed since then, can I (easily) write a VB code that would function as a shell extension thank you ...
3
by: Mark | last post by:
Hello, What I need to know is if there is a better method to run/edit modules on my pc. I'm currently running the IDLE shell under Python 2.5, on Windows XP. Every time I edit my .txt or .py...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
1
by: kumarboston | last post by:
Hi all, I am trying to copy couple of directories and their files to another place. Each directory have 25-30 files and numbered accordingly, the problem I am facing is, I have to copy files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.