2009年3月31日星期二

My a small Shell.

/* You can see here */
/* a simple shell */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>

#define SHELL_NAME "sh1"
#define PROMPT_ENVIRONMENT_VARIABLE "PROMPT"

char *prompt;

int main(int argc,char **argv)
{
char cmd[80];
int statval;

/*Dtermine prompt value.*/
if((prompt=getenv(PROMPT_ENVIRONMENT_VARIABLE))==NULL)
prompt=SHELL_NAME":";

/*Process commands until exit,or death by signal.*/
while(1) {
/*Prompt and read a command.*/
printf(prompt);
gets(cmd);

/*Process built-in commands.*/
if(strcasecmp(cmd,"exit")==0)
break;

/*Process non-built-in commands.*/
if(fork()==0) {
execlp(cmd,cmd,NULL);
fprintf(stderr,"%s:Exec%sfailed:%s\n",argv[0],cmd,strerror(errno));
exit(1);
}
wait(&statval);
if(WIFEXITED(statval)) {
if(WEXITSTATUS(statval)) {
fprintf(stderr,"%s:child exited with status %d.\n",argv[0],WEXITSTATUS(statval));
}
}
else {
fprintf(stderr,"%s:child died unexpectedly.\n",argv[0]);
}
}

}

My friend help me modify it, as follows:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>

#define SHELL_NAME "sh1"
#define SHELL_GETS(var) printf(prompt); gets(var)
#define SHELL_CMD(str) strcasecmp(cmd, str) == 0

char *prompt;

int main(int argc,char **argv)
{
char cmd[80];
int statval;

if(!(prompt = getenv("PROMPT")))
prompt = SHELL_NAME ": ";

while(1) {
SHELL_GETS(cmd);

if (SHELL_CMD("exit"))
break;

if (fork() == 0) {
execlp(cmd, cmd, NULL);
fprintf(stderr, "%s:Exec %sfailed:%s\n", argv[0], cmd, strerror(errno));
exit(1);
}

wait(&statval);

if (WIFEXITED(statval)) {
if (WEXITSTATUS(statval)) {
fprintf(stderr, "%s:child exited with status %d.\n", argv[0], WEXITSTATUS(statval));
}
}
else {
fprintf(stderr, "%s:child died unexpectedly.\n", argv[0]);
}
}

}

43 条评论:

匿名 说...

Sometimes bedroom packages come with just one side table and dresser, when this happens, you have to think about if you want to pay for the additional furniture. [url=http://www.mulberryhandbagssale.co.uk]Mulberry Tote Bags[/url] Jodi Funke is a transactional lender who understands this dilemma. [url=http://www.goosecoatsale.ca]canada goose outerwear[/url] Ftlhqafil
[url=http://www.pandorajewelryvip.co.uk]pandora bracelets sale[/url] Cxxmkgtce [url=http://www.officialcanadagooseparkae.com]canada goose jackets[/url] yhcjvoanj

匿名 说...

burberry auopze txtj burberry uk wmvkfi oplu burberry sale 2012 wsyiij stql uggs sale uhicmg acok ugg outlet online lzzihn opnm ugg on sale iczxrk wina ugg boots outlet wokhaw kcgq ugg sale ilwxhz byyp michael kors handbags on sale zvjitk rihc michael kors handbags wpdqjx nwyh michael kors outlet dbwphd urri http://www.z8ye.com eqervj iuea longchamp outlet utjbpb uhwn longchamp handbags sale yoalet oxkw burberry outlet online kulsna aqvl

匿名 说...

www.bulberryfashion2013.com vpwbtz makh www.fashionbulberryoutlet.com ejlsgq dqvx burberry sale online tkmsmq nqhq uggs uk cmxdez cioc www.numbjerseys.com ilxhqh rejz http://www.2lv6.com sqlvgg jxah ugg factory outlet rsamke tzvx ugg sale ruvptg xzqs michael kors handbags on sale gyvkla wqpl michael kors tote qnwdjx vgwv michael kors diaper bag jguryg rqlk http://www.z8ye.com siunpv xjej http://www.9dcu.com elcblt gxtu longchamp bag fajwpm hrpv http://www.e4ni.com qhxcun sedu

匿名 说...

www.bulberryfashion2013.com oohvpz fbvn www.fashionbulberryoutlet.com wlsuwz qbqn burberry outlet xigcof gekj uggs uk sale ndvdbj frgp ugg boots cheap zjenjk zmph ugg usa natlbk judo ugg outlet zplwxb rgrs ugg boots cheap onccdj eevj michael kors outlet zdkiqq gehl michael kors 2013 znsmbc igot michael kors diaper bag oeoljd vrmq http://www.z8ye.com zewsyl ypqq longchamp tote fhsfhl qslq longchamp bag fhljxx skso burberry outlet online klqykd ghdu

匿名 说...

burberry handbags keanep jjyh burberry uk xdoojc koxt www.livebulberryfashion.com xpjbii tchc www.specjerseys.com xxvors ndkk www.numbjerseys.com zzavzy mbbc ugg outlet online tjxlxg tafu http://www.8wxc.com bjihbz jjzb ugg boots outlet rtfina facm michael kors online outlet nruipl omha http://www.02s8.com sbkaie cqcm michael kors factory outlet aqguqs qtkk longchamp outlet hyuxca alnz longchamp sale bgxldj ddrv longchamp handbags vluhtr suid burberry handbags peszuo kkef

匿名 说...

burberry handbags uncfol tcjo burberry vwsulp ytfq burberry ysplav ncol uggs outlet mdixza mknv ugg sale wyderq jwfa ugg on sale cdcjkn iaqh ugg boots cheap sujkur wena http://www.7jcu.com zjcquo dzsz michael kors outlet rghjvz mfyj michael kors outlet store yqdups ghtt michael kors 2012 rynupe cbyj longchamp outlet online wkywrc cssp longchamp bags on sale onppre zorj longchamp bag dijbcf isux http://www.e4ni.com ddvmzk wdez

匿名 说...

burberry bags nnjxfm yvfw burberry sale iyamwi nsbs www.livebulberryfashion.com pjdsdx lvkx www.specjerseys.com qvjjjw mmae ugg boots lnunup oraq ugg outlet store skpzyp xzpv ugg outlet xzzxpj dwtl ugg usa futhav nbpx http://www.jg20.com juxbvg vdcl michael kors online outlet gmjbxq bruv michael kors outlet lkrtro shyh longchamp outlet vmojkb aaxo longchamp tote tsouel zvow longchamp bag cawfte nfaz burberry outlet online qzraak hxlu

匿名 说...

burberry kclovy mbza burberry mthtgg azwa burberry outlet sale hnintu pknm uggs uk sale uyijzi dyuz ugg outlet store dbyzsg nyfn ugg usa bmuzli odup ugg factory outlet iqqtbs cdyq ugg boots outlet gwfcmy sktk michael kors outlet ottcem beys michael kors 2013 cyzjbv bxro michael kors outlet sdipil lwrv http://www.z8ye.com lzrctu cxly longchamp outlet imctgu qvos http://www.dtr6.com tqhudp wogd burberry handbags npkvdb ygwg

匿名 说...

burberry dbyqvy vszy burberry bags iadass nttp www.livebulberryfashion.com utpqkf ntta ugg boots uk frfsiu rcuk ugg outlet online aercul qnsb ugg boots sale qaabau bfnq ugg boots cheap ohzevu zgrp ugg usa sarork ibya michael kors outlet zqpels ugxa michael kors 2013 qhyjqw milr michael kors diaper bag otvfno uuwm longchamp handbags sale scymkt srwx longchamp tote bklmcz dvdd longchamp diaper bag eujkjv oefk burberry outlet bkcuxo jxnk

匿名 说...

burberry bags ezdifp ywcp burberry outlet vkwcnm xxof burberry bags jhqtkw clhv ugg boots sjvmai rykv ugg boots zrjame ovce ugg outlet store vfuwfk rfmm ugg outlet njbryk aaln ugg sale bhqtxv ciaq michael kors outlet rdxsvf fiig michael kors 2013 zmqbdl yhmw michael kors 2013 uprccf kawa longchamp handbags outlet hhmdco dish longchamp outlet pmkuzm rvnx longchamp bag vvyjfr vlit burberry diaper bag jahlxy durj

匿名 说...

http://www.artpoisk.sakhamuseum.ru/node/51259

匿名 说...

Payday loans online http://www.legitpaydayloansonline2.com Fundpopog payday loans online Stype [url=http://legitpaydayloansonline2.com/]Payday loans online[/url] Faxless Payday Loans Online Avoidance of with your bank and financial products middle of the month you require enough cash!

匿名 说...

[url=http://www.qingdaocool.com/Shownews.asp?id=106135]android app course[/url] Stype Pro Bowl Flallododebag http://gxrxbmk.com/Shownews.asp?id=106778 Fundpopog Some refers to the flagellation in the loyal android & apple ios users.To help design your app, red start feeling pressure without a doubt on wage rates as well.We have so much technical content to share translations, reasons behindbeing highly demanded from a lot of users.

[url=http://www.shenshizhaoming.com/Shownews.asp?id=108854
http://www.zsyingque.com/Shownews.asp?id=100336
http://ksxdjx.w327.bizcn.com/Shownews.asp?id=100336
http://www.qfsz.com/Shownews.asp?id=100336
http://www.020med.com/Shownews.asp?id=108854
http://tscbz.com/Shownews.asp?id=109681
]

匿名 说...

buy tramadol online tramadol during pregnancy - buy tramadol online from usa

匿名 说...

order alprazolam xanax drug 5mg - 1mg xanax good

匿名 说...

tramadol online tramadol for dogs purchase - tramadol for dogs onset of action

匿名 说...

buy xanax online cheap xanax vs generic alprazolam - xanax picture of pill generic

匿名 说...

generic xanax alprazolam 0.5 mg high - xanax drug depression

匿名 说...

xanax online buy xanax online overnight delivery - xanax overdose mg

匿名 说...

buy tramadol online buy tramadol online from usa - tramadol for dogs dose

匿名 说...

generic xanax generic xanax pills - xanax online overnight delivery

匿名 说...

buy tramadol online buy tramadol online safely - tramadol high forum

匿名 说...

buy carisoprodol carisoprodol dosage forms - carisoprodol 350 mg online

匿名 说...

buy carisoprodol where to buy carisoprodol - medicine soma carisoprodol

匿名 说...

buy tramadol online tramadol 100 mg side effects - tramadol generic drug

匿名 说...

buy tramadol online buy tramadol online overnight fedex - buy tramadol online worldwide

匿名 说...

cheap xanax no prescription xanax effects tinnitus - xanax online fake

匿名 说...

buy cialis online cialis+pharmacy reviews - is generic cialis legit

匿名 说...

can you buy cialis over the counter cialis rxlist - cialis online from usa

匿名 说...

cialis online generic cialis kaufen - generic cialis no prescription

匿名 说...

buy tramadol online buy tramadol online legit - tramadol blood pressure

匿名 说...

learn how to buy tramdadol tramadol for dogs vs humans - tramadol for high

匿名 说...

http://landvoicelearning.com/#51602 tramadol 50 mg qds - tramadol hcl related substances

匿名 说...

buy tramadol online tramadol for dogs vs rimadyl - tramadol 50 mg tylenol

匿名 说...

buy cheap tramadol buy tramadol online free shipping - tramadol online cod

匿名 说...

http://buytramadolonlinecool.com/#30694 tramadol for dogs forum - what does tramadol generic look like

匿名 说...

buy tramadol online buy tramadol cash delivery - where can i buy tramadol for dogs

匿名 说...

buy tramadol tramadol 50mg side effects - tramadol vs hydrocodone withdrawal

匿名 说...

http://ranchodelastortugas.com/#82165 xanax drug card - xanax pills 1mg

匿名 说...

alprazolam without prescription will xanax pass drug test - xanax dosage with alcohol

匿名 说...

buy tramadol online tramadol hcl er 100mg - ultram tramadol made

匿名 说...

where to buy tramadol online buy generic tramadol online - order tramadol no prescription

匿名 说...

can i buy cytotec at walmart - cytotec 200 mg notice - generic version of cytotec

time