Difference between revisions of "ProvaPratica 2011.09.12"

From Sistemi Operativi
Jump to navigation Jump to search
m (Aggiunta soluzione di un'altro studente e migliorata organizzazione pagina.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<h1>http://www.cs.unibo.it/~renzo/so/compiti/2011-09-12.pdf</h1>
+
[http://www.cs.unibo.it/~renzo/so/compiti/2011-09-12.pdf link al compito]
 
 
  
 
== Esercizio 1 ==
 
== Esercizio 1 ==
 
+
===Soluzione di stefano92===
 
<syntaxhighlight lang="C">
 
<syntaxhighlight lang="C">
 
#include <stdio.h>
 
#include <stdio.h>
Line 113: Line 112:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
stefano92
+
===Soluzione di GiuliaN.===
 
 
 
 
----
 
  
 
<syntaxhighlight lang="C">
 
<syntaxhighlight lang="C">
Line 200: Line 196:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
GiuliaN.
+
 
 +
===Soluzione di Mengo===
 +
 
 +
<syntaxhighlight lang="C">
 +
 
 +
 
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <dirent.h>
 +
#include <string.h>
 +
#include <stdbool.h>
 +
 
 +
#define FALSE 0
 +
#define TRUE !FALSE
 +
#define O_RDONLY "r"
 +
 
 +
int selector (const struct dirent *fileName);
 +
int diff (char* firstFile, char* secondFile);
 +
 
 +
int selector (const struct dirent *fileName)
 +
{
 +
int index;
 +
char* nameOfFile;
 +
 
 +
nameOfFile = strdup (fileName->d_name);
 +
if (strlen (nameOfFile)>2){
 +
for (index = 0; index <strlen(nameOfFile); index++)
 +
{
 +
if(nameOfFile[index] == '.')
 +
{
 +
if (nameOfFile[index+1] == 'c')
 +
{
 +
// ".c" is extension and not part of the name
 +
if (!nameOfFile[index+2])
 +
{
 +
return TRUE;
 +
}
 +
} else if (nameOfFile[index+1] == 'h')
 +
{
 +
// ".h" is extension and not part of the name
 +
if (!nameOfFile[index+2])
 +
{
 +
return TRUE;
 +
}
 +
}
 +
}
 +
}
 +
}
 +
return FALSE;
 +
}
 +
 
 +
int diff (char* firstFile, char* secondFile)
 +
{
 +
    int sameFile = 1;
 +
    FILE *fp;
 +
    FILE *fp2;
 +
    char charFirstFile;
 +
    char charSecondFile;
 +
 
 +
    fp = fopen (firstFile, O_RDONLY);
 +
    fp2 = fopen (secondFile, O_RDONLY);
 +
    while (((charFirstFile = getc (fp)) != EOF) && ((charSecondFile = getc (fp2)) != EOF))
 +
    {
 +
        if (charFirstFile != charSecondFile)
 +
        {
 +
            sameFile = 0;       
 +
        }
 +
    }
 +
    fclose (fp);
 +
    fclose (fp2);
 +
    return sameFile;
 +
}
 +
 
 +
 
 +
int main (int argc, char* argv[])
 +
{
 +
struct dirent **nameListFirstDir;
 +
struct dirent **nameListSecondDir;
 +
int numberOfElementFirstList;
 +
int numberOfElementSecondList;
 +
int indexOfFirstList;
 +
int indexOfSecondList;
 +
bool equal;
 +
 
 +
if (argc != 3)
 +
{
 +
printf("Wrong number of parameters\nUsage: ./cmpsource firstDirectory secondDirectory\n");
 +
return EXIT_FAILURE;
 +
}
 +
numberOfElementFirstList = scandir (argv[1], &nameListFirstDir, selector, alphasort);
 +
if (numberOfElementFirstList < 0)
 +
{
 +
perror ("Scandir first directory");
 +
}
 +
numberOfElementSecondList = scandir (argv[2], &nameListSecondDir, selector, alphasort);
 +
if (numberOfElementSecondList < 0)
 +
{
 +
perror ("Scandir first directory");
 +
}
 +
for (indexOfFirstList = 0; indexOfFirstList < numberOfElementFirstList; indexOfFirstList++)
 +
{
 +
for (indexOfSecondList = 0; indexOfSecondList < numberOfElementSecondList; indexOfSecondList++)
 +
{
 +
if (!(strcmp (nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name)))
 +
{
 +
equal = TRUE;
 +
if (diff (nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name))
 +
{
 +
printf("%s %s differ\n", nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name);
 +
}
 +
}
 +
}
 +
if (!equal)
 +
{
 +
printf("%s/%s not in %s\n",argv[1],nameListFirstDir[indexOfFirstList]->d_name,argv[2]);
 +
}
 +
}
 +
 
 +
equal = FALSE;
 +
 
 +
for (indexOfSecondList = 0; indexOfSecondList < numberOfElementSecondList; indexOfSecondList++)
 +
{
 +
for (indexOfFirstList = 0; indexOfFirstList < numberOfElementFirstList; indexOfFirstList++)
 +
{
 +
if (!(strcmp (nameListSecondDir[indexOfSecondList]->d_name, nameListFirstDir[indexOfFirstList]->d_name)))
 +
{
 +
equal = TRUE;
 +
}
 +
}
 +
if (!equal)
 +
{
 +
printf("%s/%s not in %s\n",argv[2],nameListSecondDir[indexOfSecondList]->d_name,argv[1]);
 +
}
 +
}
 +
return EXIT_SUCCESS;
 +
}
 +
</syntaxhighlight>
 +
===Soluzione di Pierg e Quadrelli===
 +
<source lang="c">
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <errno.h>
 +
#include <string.h>
 +
#include <sys/types.h>
 +
#include <dirent.h>
 +
 
 +
/*funzione per avere l'estensione del file*/
 +
const char *get_filename_ext(const char *filename) {
 +
    const char *dot = strrchr(filename, '.');
 +
    if(!dot || dot == filename) return "";
 +
    return dot + 1;
 +
}
 +
 
 +
/*funzione per comparare tutti i file tra una e l'altra directory*/
 +
void cmp(char *path1,char *path2){
 +
DIR *dir1,*dir2;
 +
struct dirent *f1,*f2;
 +
int bol;
 +
if((dir1=opendir(path1))==NULL){
 +
perror("ERRORE DIR1");
 +
exit(-1);
 +
}
 +
while((f1=readdir(dir1))!=NULL){
 +
bol=0;
 +
if(strcmp(get_filename_ext(f1->d_name),"c")==0 || strcmp(get_filename_ext(f1->d_name),"h")==0){
 +
if((dir2=opendir(path2))==NULL){
 +
perror("ERRORE DIR2");
 +
exit(-1);
 +
}
 +
while((f2=readdir(dir2))!=NULL){
 +
if(strcmp(f1->d_name,f2->d_name)==0){
 +
bol=1;
 +
break;
 +
}
 +
}
 +
closedir(dir2);
 +
if(!bol){
 +
printf("%s/%s not in %s\n",path1,f1->d_name,path2);
 +
}
 +
}
 +
}
 +
closedir(dir1);
 +
}
 +
 
 +
/*main del programma*/
 +
int main (int argc, char *argv[]){
 +
if(argc!=3){
 +
perror("PARAMETRI ERRATI");
 +
exit(-1);
 +
}
 +
cmp(argv[1],argv[2]);
 +
cmp(argv[2],argv[1]);
 +
return 0;
 +
}
 +
</source>

Latest revision as of 08:10, 9 May 2017

link al compito

Esercizio 1

Soluzione di stefano92

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

int N_DIR1=0,N_DIR2=0;

int source(char*s)
{
	int x=strlen(s);
	if (x>=3)
	{
		if ((s[x-1]=='c') || (s[x-1]=='h'))
		{
			if (s[x-2]=='.') return 1;
		}
	}
	return 0;
}
void print_diff(char**dir1,char**dir2)
{
	int i,j,found=0;
	for (i=0;i<N_DIR1;i++){
		for(j=0;j<N_DIR2;j++){
			if (!strcmp(dir1[i],dir2[j]))
				found=1;
			 }
			if (found!=1) 
				printf("%s presente solo nella directory 1\n",dir1[i]);
			found=0;
	}
	
	found=0;
	
	for (i=0;i<N_DIR2;i++)
	{
		for(j=0;j<N_DIR1;j++){
			if (!strcmp(dir2[i],dir1[j]))
				found=1;
			}
			if (found!=1)
				printf("%s presente solo nella directory 2\n",dir2[i]);
			found=0;
	}
	return;
}
int main (int argc,char*argv[])
{
	int ris,i=0;
	char c;
	char*tmp;
	struct stat prova;
	char**filedir1=NULL,**filedir2=NULL;
	DIR *dp,*dp2; 
	struct dirent *ep;    
	if (argc!=3)
	{
		printf("Usage: ./cmpsource.out DIR1 DIR2\n");
		return(1);
	} 
	/*APERTURA DIRECTORY 1*/
	dp = opendir (argv[1]);
	if (dp != NULL)
	{
		while ((ep = readdir (dp))) /*Leggo i file nella directory*/
    	{
    		ris=lstat(ep->d_name,&prova); /*Recupero le informazioni sul file*/
    		if (S_ISREG(prova.st_mode)) /*file regolare*/
    		{ 
    		/*IL FILE E' UN ESEGUIBILE REGOLARE*/
    			if (source(ep->d_name)){ /*IL FILE E' UN SORGENTE (.c/.h)*/
    			N_DIR1++;
    			filedir1=(char**)realloc(filedir1,N_DIR1*sizeof(char*));
    			filedir1[N_DIR1-1]=ep->d_name;
    			}
    		}
		}
		closedir (dp); /*ORA IN filedir1[] CI SONO TUTTI I FILE SORGENTE DELLA DIRECTORY 1*/
	}
	/*FINE LETTURA DIRECTORY 1*/
	/*APERTURA DIRECTORY 2*/
	dp2 = opendir (argv[2]);
	if (dp2 != NULL)
	{
		while ((ep = readdir (dp2))) /*Leggo i file nella directory*/
    	{
    		ris=lstat(ep->d_name,&prova); /*Recupero le informazioni sul file*/
    		if (S_ISREG(prova.st_mode)) /*file regolare*/
    		{ 
    		/*IL FILE E' UN ESEGUIBILE REGOLARE*/
    			if (source(ep->d_name)){ /*IL FILE E' UN SORGENTE (.c/.h)*/
    			N_DIR2++;
    			filedir2=(char**)realloc(filedir2,N_DIR2*sizeof(char*));
    			filedir2[N_DIR2-1]=ep->d_name;
    			}
    		}
		}
		closedir (dp); /*ORA IN filedir1[] CI SONO TUTTI I FILE SORGENTE DELLA DIRECTORY 1*/
	}
	/*FINE LETTURA DIRECTORY 2*/
	print_diff(filedir1,filedir2);
  	return 0;
}

Soluzione di GiuliaN.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int lastchar(char *name){
		int i=0;
		while(name[i]!='\0')
			i++;
		return(i-1);
	}//restituisce l'indice dell'ultimo carattere della stringa


int main(int argc, char *argv[]){
	char dir1[1024];
	char dir2[1024];
	int n1, n2;
	struct dirent **namelist1;
	struct dirent **namelist2;
	struct stat buf1;
	struct stat buf2;
	int lc;
	
	if(argc==3){ /*se vengono passate in input due directory va bene*/
		strcpy(dir1,argv[1]);
		strcpy(dir2,argv[2]);
		n1 = scandir(dir1, &namelist1, NULL, 0);
		n2 = scandir(dir2, &namelist2, NULL, 0);
		if(n1<0 || n2<0)
			perror("scandir");
		else {
			int index[n1];
			int i=0;
			int j=0; //delimitatore
			while(n1--){
				stat(namelist1[n1]->d_name,&buf1);
				lc = lastchar(namelist1[n1]->d_name);
				if((namelist1[n1]->d_name[lc]=='c' &&
					namelist1[n1]->d_name[lc-1]=='.') ||
					(namelist1[n1]->d_name[lc]=='h' &&
					namelist1[n1]->d_name[lc-1]=='.')){	
						index[i]=n1;
						i++;
				}
			}
			j=i;
			int bool=0;
			while(n2--){
				stat(namelist2[n2]->d_name,&buf2);
				lc = lastchar(namelist2[n2]->d_name);
				if((namelist2[n2]->d_name[lc]=='c' &&
					namelist2[n2]->d_name[lc-1]=='.') ||
					(namelist2[n2]->d_name[lc]=='h' &&
					namelist2[n2]->d_name[lc-1]=='.')){	
						for(i=0; i<j; i++){
							if(strcmp(namelist2[n2]->d_name, namelist1[index[i]]->d_name)==0){ //sono uguali
								bool=1;
								index[i]=-1;
							}
						}
						if(bool==0)
							printf("%s not in %s\n",namelist2[n2]->d_name,dir1);
				}
			bool=0;
			}
			printf("\n");
			for(i=0; i<j; i++){
				if(index[i]>=0)
					printf("%s not in %s\n",namelist1[index[i]]->d_name,dir2);
			}
		}
	}
	else /*altrimenti errore, vogliamo in input 2 directory*/
			printf("errore\n");
}

Soluzione di Mengo

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <stdbool.h>

#define FALSE 0
#define TRUE !FALSE
#define O_RDONLY "r"

int selector (const struct dirent *fileName);
int diff (char* firstFile, char* secondFile);

int selector (const struct dirent *fileName)
{
	int index;
	char* nameOfFile;

	nameOfFile = strdup (fileName->d_name);
	if (strlen (nameOfFile)>2){
		for (index = 0; index <strlen(nameOfFile); index++)
		{
			if(nameOfFile[index] == '.')
			{
				if (nameOfFile[index+1] == 'c')
				{
					// ".c" is extension and not part of the name
					if (!nameOfFile[index+2])
					{
						return TRUE;
					}
				} else if (nameOfFile[index+1] == 'h')
				{
					// ".h" is extension and not part of the name
					if (!nameOfFile[index+2])
					{
						return TRUE;
					}
				}
			}	
		}
	}
	return FALSE;
}

int diff (char* firstFile, char* secondFile)
{
    int sameFile = 1;
    FILE *fp;
    FILE *fp2;
    char charFirstFile; 
    char charSecondFile;

    fp = fopen (firstFile, O_RDONLY);
    fp2 = fopen (secondFile, O_RDONLY);
    while (((charFirstFile = getc (fp)) != EOF) && ((charSecondFile = getc (fp2)) != EOF))
    {
        if (charFirstFile != charSecondFile) 
        {
             sameFile = 0;        
        }
    }
    fclose (fp);
    fclose (fp2);
    return sameFile;
}


int main (int argc, char* argv[])
{
	struct dirent **nameListFirstDir;
	struct dirent **nameListSecondDir;
	int numberOfElementFirstList;
	int numberOfElementSecondList;
	int indexOfFirstList;
	int indexOfSecondList;
	bool equal;

	if (argc != 3)
	{
		printf("Wrong number of parameters\nUsage: ./cmpsource firstDirectory secondDirectory\n");
		return EXIT_FAILURE;
	}
	 numberOfElementFirstList = scandir (argv[1], &nameListFirstDir, selector, alphasort);
	 if (numberOfElementFirstList < 0)
	 {
	 	perror ("Scandir first directory");
	 }
	 numberOfElementSecondList = scandir (argv[2], &nameListSecondDir, selector, alphasort);
	 if (numberOfElementSecondList < 0)
	 {
	 	perror ("Scandir first directory");
	 }
	 for (indexOfFirstList = 0; indexOfFirstList < numberOfElementFirstList; indexOfFirstList++)
	 {
	 	for (indexOfSecondList = 0; indexOfSecondList < numberOfElementSecondList; indexOfSecondList++)
	 	{
	 		if (!(strcmp (nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name)))
	 		{
	 			equal = TRUE;
	 			if (diff (nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name))
	 			{
	 				printf("%s %s differ\n", nameListFirstDir[indexOfFirstList]->d_name, nameListSecondDir[indexOfSecondList]->d_name);
	 			}
	 		}
	 	}
	 	if (!equal)
	 	{
	 		printf("%s/%s not in %s\n",argv[1],nameListFirstDir[indexOfFirstList]->d_name,argv[2]);
	 	}
	 }

	 equal = FALSE;

	 for (indexOfSecondList = 0; indexOfSecondList < numberOfElementSecondList; indexOfSecondList++)
	 {
	 	for (indexOfFirstList = 0; indexOfFirstList < numberOfElementFirstList; indexOfFirstList++)
	 	{
	 		if (!(strcmp (nameListSecondDir[indexOfSecondList]->d_name, nameListFirstDir[indexOfFirstList]->d_name)))
	 		{
	 			equal = TRUE;
	 		}
	 	}
	 	if (!equal)
	 	{
	 		printf("%s/%s not in %s\n",argv[2],nameListSecondDir[indexOfSecondList]->d_name,argv[1]);
	 	}
	 }
	 return EXIT_SUCCESS;
}

Soluzione di Pierg e Quadrelli

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

/*funzione per avere l'estensione del file*/
const char *get_filename_ext(const char *filename) {
    const char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}

/*funzione per comparare tutti i file tra una e l'altra directory*/
void cmp(char *path1,char *path2){
	DIR *dir1,*dir2;
	struct dirent *f1,*f2;
	int bol;
	if((dir1=opendir(path1))==NULL){
		perror("ERRORE DIR1");
		exit(-1);
	}
	while((f1=readdir(dir1))!=NULL){
		bol=0;
		if(strcmp(get_filename_ext(f1->d_name),"c")==0 || strcmp(get_filename_ext(f1->d_name),"h")==0){
			if((dir2=opendir(path2))==NULL){
				perror("ERRORE DIR2");
				exit(-1);
			}
			while((f2=readdir(dir2))!=NULL){
				if(strcmp(f1->d_name,f2->d_name)==0){
					bol=1;
					break;
				} 
			}
			closedir(dir2);
			if(!bol){
				printf("%s/%s not in %s\n",path1,f1->d_name,path2);
			}
		}
	}
	closedir(dir1);
}

/*main del programma*/
int main (int argc, char *argv[]){
	if(argc!=3){
		perror("PARAMETRI ERRATI");
		exit(-1);
	}
	cmp(argv[1],argv[2]);
	cmp(argv[2],argv[1]);
return 0;
}