473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

UNIX command prompt

I'm writing a command prompt for unix and I've run into some problems:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#define EXIT "exit"
#define DELIMITER " "
we are in
int main(argc, argv)
{
//Initilzation of varibles
char **arguments = (char **) malloc (8);
char *command_buffer = (char *) malloc (256);
char *arg_ptr;
int i = 0;
int pid;
int nbytes = 256;
int not_exiting = 1;

do{
//prints prompt and gets line of text
printf("sim> ");
getline(&command_buffer, &nbytes, stdin);

//Special workaround
//replaced the '\n' at the end of the input, which was giving errors
//with '\0'
command_buffer[strlen(command_buffer) -1] = '\0';

//picks line apart and
//replaces spaces with '\0' and adds pointers to argument[]
do{
if(i == 0)
{
arguments[i] = strtok(command_buffer, DELIMITER);
i++;
}
else
{
arg_ptr = strtok(NULL, DELIMITER);

if(arg_ptr != NULL)
{
arguments[i] = arg_ptr;
i++;
}
else
{
i++;
break;
}
}
}while(1);

//are we exiting?
not_exiting = strcmp(command_buffer, EXIT);
if(not_exiting)
{
//create child process
pid = fork();

if(pid == 0) //fork() == 0 means child process
{
execvp(*arguments, arguments);
exit(0);
}
else if(pid > 0) //fork() > 0 means we are in parent process
{

wait(NULL);
}
else //otherwise we have error
{
fprintf(stderr, "fork() returned error");
exit(1);
}
}
else
{
printf("Exiting\n");
}
}while(not_exiting);

return 0;
}

The problems
1. The workaround listed above.
2. One argument commands work fine but multiple don't. Eg. ls -a
works fine, ls -al works fine but ls -a -l doesn't work. Any
subsequent commands that don't have arguments don't work either, eg.
ls works, then ls -a works, but ls again doesn't work.

Any help is greatly appriciated!

Jan 29 '06 #1
5 2301
In article <11**********************@g43g2000cwa.googlegroups .com>,
<re***********@gmail.com> wrote:
I'm writing a command prompt for unix and I've run into some problems:


Try comp.unix.programming . You used several calls which are
not part of standard C (such as fork()), and it is best to take
the questions to people who are familiar with the meaning of those
calls.

comp.lang.c prefers to restrict itself to standard C.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jan 29 '06 #2
Ok thanks, I'll post it up there. But I know the unix calls all work,
just more or less a matter of the reading in from user and handling the
strings, I'm not used to doing that in C.

Jan 29 '06 #3
On 28 Jan 2006 21:23:42 -0800, in comp.lang.c ,
re***********@gmail.com wrote:
Ok thanks, I'll post it up there. But I know the unix calls all work,
just more or less a matter of the reading in from user and handling the
strings, I'm not used to doing that in C.


I suggest you try a simpler example without all the unix-ey bits, just
reading user input and handling some strings. If you get stuck with
that, then post here.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 29 '06 #4
re***********@gmail.com wrote:
I'm writing a command prompt for unix and I've run into some problems:


You're doing as if array of char * was char** (pointer on char *)
see faq 6.19 (thanks Flash Gordon)
//Initilzation of varibles /* instead of */ char **arguments = (char **) malloc (8); /*use this */
char * arguments[8] = {NULL};
char *command_buffer = (char *) malloc (256);
char *arg_ptr;
int i = 0;
int pid;
int nbytes = 256;
int not_exiting = 1;

do{
//prints prompt and gets line of text
printf("sim> ");
getline(&command_buffer, &nbytes, stdin);

//Special workaround
//replaced the '\n' at the end of the input, which was giving errors
//with '\0'
command_buffer[strlen(command_buffer) -1] = '\0';

//picks line apart and
//replaces spaces with '\0' and adds pointers to argument[]
do{
if(i == 0)
{
arguments[i] = strtok(command_buffer, DELIMITER);
i++;
}
else
{
arg_ptr = strtok(NULL, DELIMITER);

if(arg_ptr != NULL)
{
arguments[i] = arg_ptr;
i++;
}
else
{
i++;
break;
}
}
}while(1);

//are we exiting?
not_exiting = strcmp(command_buffer, EXIT);
if(not_exiting)
{ //create child process
pid = fork();

if(pid == 0) //fork() == 0 means child process
{
execvp(*arguments, arguments);
exit(0);
}
else if(pid > 0) //fork() > 0 means we are in parent process
{

wait(NULL); while (i) arguments[i--] = NULL; }
else //otherwise we have error
{
fprintf(stderr, "fork() returned error");
exit(1);
}
}
else
{
printf("Exiting\n");
}
}while(not_exiting);

return 0;
}

The problems
1. The workaround listed above.
2. One argument commands work fine but multiple don't. Eg. ls -a
works fine, ls -al works fine but ls -a -l doesn't work. Any
subsequent commands that don't have arguments don't work either, eg.
ls works, then ls -a works, but ls again doesn't work.

Any help is greatly appriciated!


Fine... but we can't see the command's result...
But... it's not the place here to tell you more on this topic ;-)
Xavier
Jan 29 '06 #5

re***********@gmail.com writes:
I'm writing a command prompt for unix and I've run into some problems:


Three suggestions:
1) Use a debugger;
2) Add debug print statements at appropriate places;
3) Write a small C (or shell or Perl or whatever) program which prints
out its arguments in a way that you know exactly what it received,
then call it in various ways from your shell ("command prompt" to me
sounds like just, well, the prompt).
Jan 30 '06 #6

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

Similar topics

4
by: Jonathan Fine | last post by:
Hello I have written a program that interacts with a command line program. Roughly speaking, it mimics human interaction. (With more speed and accuracy, less intelligence.) It works fine...
5
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard...
3
by: iamsiju | last post by:
Hi, I am facing a problem with the Perl Telnet Object. My Telnet Object hangs just after issueing reboot command on the remote host. Please help me who faced this problem !!! Thanks in...
3
by: surajsingh | last post by:
Hi, I have a perl script which uses Net::Telnet module to open a telnet session with my unix boxes, and executes lot of commands on those boxes. As this module is implemented, when 'cmd' is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.