473,614 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(&comman d_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(*argumen ts, 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_exit ing);

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 2314
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
<re***********@ gmail.com> wrote:
I'm writing a command prompt for unix and I've run into some problems:


Try comp.unix.progr amming . 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***********@g mail.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***********@g mail.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(&comman d_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(*argumen ts, 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_exit ing);

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***********@g mail.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
5913
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 under Linux, using select().
5
2710
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 most of the time unix programmers are C and C++ programmers. please advise. thanks!!
3
4008
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 advance Siju Maliakkal
3
2045
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 used, this module waits for the 'prompt' to decide whether the command is finished or not. Now in case the 'timeout' is reached before the 'prompt' appears, the module directly throws error (even if the command might actually be running on the remote...
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7093
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.