home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
Efork.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-11-07
|
1KB
|
62 lines
/* Efork.c
Viene richiesto un comando Unix
Il comando e gli argomenti vengono inseriti in un array (args)
Il processo figlio esegue il comando con execvp
*/
/* Nota:
provare ad eseguire come comando "ls *.c"
Perche' viene dato un messaggio d'errore
(del comando ls, non del programma Efork)?
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
char buf[1024];
char *args[64];
for (;;)
{
printf("Comando > ");
if (gets(buf) == NULL) {
printf("\n");
return;
}
parse(buf, args);
execute(args);
}
}
/* la procedura parse individua gli argomenti */
parse(char *buf, char **args)
{
while (*buf != NULL)
{
/* toglie blank e tab */
while ((*buf == ' ') || (*buf == '\t')) *buf++ = NULL;
*args++ = buf;
while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t')) buf++;
}
*args = NULL;
}
/* esegue il comando dato */
execute(char **args)
{
int pid, status;
if ((pid = fork()) < 0) {
perror("fork");
exit(1); }
if (pid == 0) {
execvp(*args, args);
perror(*args);
exit(1); }
wait(&status);
}