472,993 Members | 3,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

writing a shell in c

for my operating systems design class i am supposed to write a shell in c. i have no idea where to start and our teacher hasn't given us much info. anyone know of any good tutorials?

Nov 14 '05 #1
5 34714
L337Hax0r wrote:
for my operating systems design class i am supposed to write a shell in c.
i have no idea where to start and our teacher hasn't given us much info.
anyone know of any good tutorials?


Here is an example shell (well, it's more of a skeleton, really), which does
not actually execute any internal commands - it just pretends to. On the
other hand, it /will/ execute external commands if they don't happen to
have the same name as the internal commands it understands, so use with
care!

Note that it is case-sensitive. If you're in Windows, MKDIR foo, for
example, will be treated as an external command and will actually create a
foo directory, whereas mkdir foo will be treated as internal, and will
merely /pretend/ to make a directory.

/* shelldemo.c
* Richard Heathfield, 2003
* Contact: bi****@eton.powernet.co.uk
* Permission is granted to copy this file and to modify it,
* provided that credit to the author is given where appropriate
* and modifications are clearly marked as such. In other words,
* don't pretend you wrote my bits, and don't pretend I wrote
* your bits.
* This code is intended merely to be an expression of an idea for
* matching up requests to function calls, and was written very
* quickly. It was not intended to be considered a model of
* exemplary programming practice. Witness the large main(), and
* the use of "magic numbers".
* Retain this comment. Thank you.
* - rjh 8 Jan 2003.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmpstr(const void *vs1, const void *vs2)
{
char * const *s1 = vs1;
char * const *s2 = vs2;
return strcmp(*s1, *s2);
}

int rjhchdir(char *args)
{
if(args != NULL)
{
printf("You changed directory to %s.\n", args);
}
else
{
printf("You changed to your home directory.\n");
}
return 0;
}

int rjhclear(void)
{
printf("You cleared the display.\n");
return 0;
}

int rjhdf(void)
{
printf("You asked for disk usage info.\n");
return 0;
}

int rjhdu(char *args)
{
printf("You asked for directory usage info.\n");
if(args != NULL)
{
printf("Specifically, you wanted the option %s\n", args);
}
return 0;
}

int rjhls(char *args)
{
printf("You asked for a directory listing.\n");
if(args != NULL)
{
printf("Options: %s\n", args);
}
return 0;
}

int rjhmkdir(char *args)
{
if(args != NULL)
{
printf("You created a directory, %s\n", args);
}
else
{
printf("You have to say what "
"directory you want to create.\n");
}
return 0;
}

int rjhsu(char *args)
{
if(args != NULL)
{
printf("You switched user to %s\n", args);
}
else
{
printf("You are now root.\n");
}
return 0;
}

int main(void)
{
/* ensure this list is sorted! */
char *command[] =
{
"cd",
"clear",
"df",
"du",
"exit",
"ls",
"mkdir",
"su"
};
size_t numcommands = sizeof command / sizeof command[0];
char line[4096] = {0};
char safeline[4096] = {0};
int done = 0;
int i = 0;
char *s = line;
char **t = NULL;
char *prompt = ">";
char *args = NULL;
char *nl = NULL;

while(!done)
{
printf("%s", prompt);
fflush(stdout);
if(NULL == fgets(line, sizeof line, stdin))
{
t = NULL;
done = 1;
}
else
{
nl = strchr(line, '\n');
if(nl != NULL)
{
*nl = '\0';
strcpy(safeline, line);
}
else
{
int ch;
printf("Line too long! Ignored.\n");
while((ch = getchar()) != '\n' && ch != EOF)
{
continue;
}
if(ch == EOF)
{
done = 1;
}
}
args = strchr(line, ' ');
if(args != NULL)
{
*args++ = '\0';
}
t = bsearch(&s,
command,
numcommands,
sizeof command[0],
cmpstr);
}

if(!done && t != NULL)
{
i = (int)(t - command);
switch(i)
{
case 0: rjhchdir(args); break;
case 1: rjhclear(); break;
case 2: rjhdf(); break;
case 3: rjhdu(args); break;
case 4: done = 1; break;
case 5: rjhls(args); break;
case 6: rjhmkdir(args); break;
case 7: rjhsu(args); break;
default: break;
}
}
else
{
printf("%s is not an internal command. "
"Trying to execute it instead.\n", line);
system(safeline);
}
}

return 0;
}


--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #2
L337Hax0r <ku****@yahoo.com> spoke thus:
for my operating systems design class i am supposed to write a shell in c. i
have no idea where to start and our teacher hasn't given us much info.
anyone know of any good tutorials?


0) Get clarification from the instructor if you don't understand the
requirements. No one else can help you until you at least are
clear on what you are to do.
1) Check Google if you haven't.
2) You may need to look elsewhere for help. The system() library call
is germane here, but all system calls are topics for other
newsgroups.
3) In any case, come up with some code first (standard C only, please)
and post here for help. Starting from scratch is *your* job.
4) I'm sure that at least one of the links below will prove useful to
you, if Google wasn't as illuminating as you'd like:

http://www.csclub.uwaterloo.ca/u/dj3...lc-welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

Have fun with your systems class. Make an effort to give it a go
yourself - newsgroups are no substitute for your own initiative.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
Richard Heathfield <do******@address.co.uk.invalid> writes:
/* ensure this list is sorted! */
char *command[] =
{
"cd",
"clear",
"df",
"du",
"exit",
"ls",
"mkdir",
"su"
};


You're going to have a hard time keeping it sorted when sort
order varies from one platform to the next. You're already
calling bsearch(); why not call qsort() at the beginning too?
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 14 '05 #4
Ben Pfaff wrote:
Richard Heathfield <do******@address.co.uk.invalid> writes:
/* ensure this list is sorted! */
char *command[] =
{
"cd",
"clear",
"df",
"du",
"exit",
"ls",
"mkdir",
"su"
};


You're going to have a hard time keeping it sorted when sort
order varies from one platform to the next. You're already
calling bsearch(); why not call qsort() at the beginning too?


For the same reason that I didn't do All Kinds Of Other Stuff.

If the OP wishes to add a qsort call, however, he or she (I don't recall
which, I'm afraid) is entirely welcome. It'll be excellent practice.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5
L337Hax0r wrote:
for my operating systems design class i am supposed to write a shell
in c. i have no idea where to start and our teacher hasn't given us
much info. anyone know of any good tutorials?


Advanced Unix Programming
by Marc J. Rochkind

Jeremy.
Nov 14 '05 #6

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

Similar topics

1
by: Thomas W | last post by:
Well, is it possible to write Win32 shell extension in python? I want to create a virtual folder ( like gmailfs ) to serve files from a http-server ( and no, I cannot use webdav ) so that users can...
0
by: s99999999s2003 | last post by:
hi in my environment, besides shell scripts written for sys admin tasks, there are shell scripts that call java programs which in turn will do various processing like connecting to databases and...
5
by: Alan Searle | last post by:
I am exporting ms-access data to XML files. This works fine. However, I need to insert one line at the top of each exported file (i.e. a reference to the XSL file) and am having a problem with...
14
by: JHNielson | last post by:
I posted this in the Access thread, but seeing as how this is a VB problem, I thought this might be the better place I have a code to FTP files to a server, using a script FTP.scr. I need to...
2
by: Jeff Gaines | last post by:
I am in the process of upgrading to XP 64. VS2008 is running fine but one of my apps, which adds functionality to the Explorer context menu, just won't work under XP 64. Googling resulted in my...
19
by: ravipanand | last post by:
Hi; I m using following code for writing system IP in notepad from shell command shell("ipconfig > c:\ipfile.txt") but it do nothing( no errors comes also) please solve this problem
0
by: xahlee | last post by:
Here's a little tutorial that lets you write emacs commands for processing the current text selection in emacs in your favorite lang. Elisp Wrapper For Perl Scripts...
0
ashitpro
by: ashitpro | last post by:
Writing System Call Wrappers in User Space. Recently I saw a post in Linux/Unix section to know the user who deleted the file in linux. Currently there is nothing in linux which could achieve...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.