Prova Pratica 2012.05.30
Jump to navigation
Jump to search
Soluzione di Dado e Pierg
#include <stdio.h>
#include <stdlib.h>
#include "s2argv/s2argv.h"
#define MAKEFILE "minimakefile"
#define RUN "run"
int main(int argc, char * argv[]){
FILE *mf;
char *line=NULL;
char *target,*command;
int dim=0;
mf=fopen(MAKEFILE,"r");
if(mf==NULL){
fprintf(stderr,"minimakefile not found!\n");
exit(1);
}
while(getline(&line,&dim,mf)>0){
target=strtok(line,":");
command=strtok(NULL,"\n");
if(strcmp(target,RUN)==0){
system_noshell(command);
}
else if(fopen(target,"r")==NULL){
system_noshell(command);
}
else{
fprintf(stderr,"target %s già esistente: non eseguo\n",target);
}
}
free(line);
fclose(mf);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "s2argv/s2argv.h"
#define MAKEFILE "minimakefile"
#define RUN "run"
int main(int argc, char * argv[]){
FILE *mf;
char *line=NULL;
char *target, *tmp;
int dim = 0;
int a = b = 0;
struct stat st_target, st_tmp;
mf=fopen(MAKEFILE,"r");
if(fd==NULL){
fprintf(stderr,"minimakefile not found!\n");
exit(1);
}
while(getline(&line,&dim,mf)>0){
if (line[0] == "\t") {
if(a || b){
system_noshell(line + 1);
}
}
else{
a = b = 0;
target = strtok(line, ":")
if(fopen(target,"r")==NULL){
a = 1;
}
else if(strcmp(target,RUN)==0){
char *command = strtok(NULL, "\n");
system_noshell(command);
}
else {
stat(target, &st_target);
while ((tmp = (strtok(NULL, " "))!=NULL){
stat(tmp, &st_tmp);
if (st_tmp.st_mtime > st_target.st_mtime ) {
b = 1;
break;
}
}
}
}
}
free(line);
fclose(fd);
return 0;
}