• Non ci sono risultati.

Lezione 15 Completare il programma

N/A
N/A
Protected

Academic year: 2021

Condividi "Lezione 15 Completare il programma"

Copied!
5
0
0

Testo completo

(1)

Lezione 15

Completare il programma smallsh, definendo il codice delle funzioni gettok e runcommand.

int gettok(char **outptr) {

int type;

*outptr=tok;

while(*ptr==’ ’ || *ptr==’\t’) ptr++;

switch(*ptr) { case ’\n’:

type=EOL;

ptr++;

break;

case ’&’:

type=AMPERSAND;

ptr++;

break;

case ’;’:

type=SEMICOLON;

ptr++;

break;

default:

type=ARG;

while(inarg(*ptr))

*tok++=*ptr++;

}

*tok++=’\0’;

return type;

}

int runcommand(char **cline, int where) {

pid_t pid;

int status;

switch(pid=fork()) { case -1:

perror("Errore nella chiamata alla fork");

return -1;

case 0:

execvp(*cline,cline);

perror(*cline);

(2)

exit(1);

default:

if(where==BACKGROUND) {

printf("Process id: %d\n",pid);

return 0;

} else

{

if(waitpid(pid,&status,0)==-1) return -1;

else

return status;

} } }

Il programma completo `e riportato qui di seguito:

#include "smallsh.h"

static char inpbuf[MAXBUF], tokbuf[2*MAXBUF],

*ptr = inpbuf, *tok = tokbuf; /* buffer e puntatori globali */

static char special[]={’ ’,’\t’,’&’,’;’,’\n’,’\0’};

int userin(char *p) {

int c, count;

ptr=inpbuf;

tok=tokbuf;

printf("%s",p); /* stampa il prompt */

count=0;

while(1) {

if((c=getchar())==EOF) return(EOF);

if(count<MAXBUF) inpbuf[count++]=c;

if(c==’\n’ && count<MAXBUF) {

inpbuf[count]=’\0’;

return count;

(3)

}

if(c==’\n’) /* se linea troppo lunga, ricomincia */

{

printf("smallsh: input line too long\n");

count=0;

printf("%s",p);

} } }

int inarg(char c) {

char *p=special;

while(*p!=’\0’) {

if(c==*p++) return 0;

} return 1;

}

int gettok(char **outptr) {

int type;

*outptr=tok;

while(*ptr==’ ’ || *ptr==’\t’) ptr++;

switch(*ptr) { case ’\n’:

type=EOL;

ptr++;

break;

case ’&’:

type=AMPERSAND;

ptr++;

break;

case ’;’:

type=SEMICOLON;

ptr++;

break;

default:

(4)

type=ARG;

while(inarg(*ptr))

*tok++=*ptr++;

}

*tok++=’\0’;

return type;

}

int runcommand(char **cline, int where) {

pid_t pid;

int status;

switch(pid=fork()) { case -1:

perror("Errore nella chiamata alla fork");

return -1;

case 0:

execvp(*cline,cline);

perror(*cline);

exit(1);

default:

if(where==BACKGROUND) {

printf("Process id: %d\n",pid);

return 0;

} else

{

if(waitpid(pid,&status,0)==-1) return -1;

else

return status;

} } }

int procline(void) {

char *arg[MAXARG+1]; /* array di puntatori per runcommand */

int toktype; /* typo del token */

int narg; /* numero di argomenti correnti */

(5)

int type; /* FOREGROUND o BACKGROUND */

narg = 0;

for(;;) /* ciclo infinito */

{

switch(toktype = gettok(&arg[narg])){

case ARG: if(narg < MAXARG) narg++;

break;

case EOL:

case SEMICOLON:

case AMPERSAND:

if(toktype == AMPERSAND) type = BACKGROUND;

else

type = FOREGROUND;

if(narg != 0) {

arg[narg] = NULL;

runcommand(arg, type);

}

if(toktype == EOL) return;

narg = 0;

break;

} } }

char *prompt = "Command> "; /* prompt */

main() {

while(userin(prompt) != EOF) procline();

}

Riferimenti

Documenti correlati

savastanoi strains isolated from different host species and from diverse geographical regions was determined by fluorescent amplified fragment length polymorphism (f-AFLP)

To account for cerato- ulmin's role as a toxin (Richards, 1993), it is possible that in the case of the cu- and 'feltoid' isolates much higher levels of cerato-ulmin production

Occurrence of latent pathogens other than Botryosphaeriaceae in oak twig samples from the Maremma Regional Park at Alberese (Tuscany e Italy) based on their isolation

closed-loop optimization to a more complex dynamical pro- cess, that is, the loading of the BEC into a 3D lattice, effectively optimally driving the quantum phase transition from

The study led to the development of an empirical correlation for the prediction of the discharge coefficient: it expresses the C d of each hole as a function of the ratio between

Plant transcripts involved in the polypropanoid pathway, including phenylalanine ammonia-lyase (PAL), had greater expression in contaminated trees whereas most resistance genes

Results highlight how dry deposition significantly declines when soil moisture is used to regulate the stomatal opening, mainly in the semi-arid environments: in particular, over