ProvaPratica 2009.06.23
Jump to navigation
Jump to search
http://www.cs.unibo.it/~renzo/so/pratiche/2009.06.23.pdf
Esercizio 1
Soluzione di Stefano92
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if (argc!=2)
{
printf("Usage: ./scriptexec COMMAND_FILE\n");
return(1);
}
FILE* fp;
char* buf= NULL;
int ris=0,ris_fork=0;
unsigned long size=0;
fp=fopen(argv[1],"r");
if(fp == NULL)
{
printf("FILE NON ESISTENTE\n");
return -1;
}
while ((ris=getline(&buf,&size,fp)!=-1))
{
if (buf[0]!='#')
{
ris_fork= fork();
if (ris_fork == 0)
execl("/bin/sh","/bin/sh","-c",buf,0);
else
wait(&ris_fork);
}
}
}
-stefano92
Soluzione di Dado e Pierg
#include <stdio.h>
#include <stdlib.h>
#include "s2argv/s2argv.h"
int main(int argc, char * argv[]){
if(argc!=2){
printf("Wrong parameters number\n");
}else{
char *comm=NULL;
FILE *f;
ssize_t dim=0;
f=fopen(argv[1],"r");
if(f==NULL){
printf("Error while opening file %s\n",argv[1]);
exit(1);
}
while(getline(&comm,&dim,f)>0){
system_noshell(comm);
/*preparo comandi successivi*/
free(comm);
comm=NULL;
}
}
}