Difference between revisions of "ProvaPratica 2009.06.23"
Jump to navigation
Jump to search
Stefano 92 (talk | contribs) (Created page with "<h1>http://www.cs.unibo.it/~renzo/so/pratiche/2009.06.23.pdf</h1> == Esercizio 1 == <syntaxhighlight lang="C"> /* Esercizio1 (obbligatorio): (15 punti) Scrivere un programm...") |
m (Aggiunte soluzioni di un altro studente) |
||
Line 2: | Line 2: | ||
== Esercizio 1 == | == Esercizio 1 == | ||
− | + | ===Soluzione di Stefano92=== | |
<syntaxhighlight lang="C"> | <syntaxhighlight lang="C"> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
Line 50: | Line 41: | ||
-stefano92 | -stefano92 | ||
+ | ===Soluzione di Dado e Pierg=== | ||
+ | <source lang="c"> | ||
+ | #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; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> |
Latest revision as of 16:27, 6 May 2017
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;
}
}
}