Difference between revisions of "ProvaPratica 2011.09.12"

From Sistemi Operativi
Jump to navigation Jump to search
Line 114: Line 114:
  
 
stefano92
 
stefano92
 +
 +
 +
----
 +
 +
<syntaxhighlight lang="C">
 +
#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");
 +
}
 +
 +
</syntaxhighlight>
 +
GiuliaN.

Revision as of 14:58, 29 April 2014

http://www.cs.unibo.it/~renzo/so/compiti/2011-09-12.pdf


Esercizio 1

#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;
}

stefano92



#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");
}

GiuliaN.