Difference between revisions of "ProvaPratica 2011.09.12"

From Sistemi Operativi
Jump to navigation Jump to search
(Created page with "<h1>http://www.cs.unibo.it/~renzo/so/compiti/2011-09-12.pdf</h1> == Esercizio 1 == <syntaxhighlight lang="C"> #include <stdio.h> #include <sys/types.h> #include <dirent.h> ...")
 
Line 44: Line 44:
 
for (i=0;i<N_DIR2;i++)
 
for (i=0;i<N_DIR2;i++)
 
{
 
{
printf("SONO NEL FOR™\n");
 
 
for(j=0;j<N_DIR1;j++){
 
for(j=0;j<N_DIR1;j++){
 
if (!strcmp(dir2[i],dir1[j]))
 
if (!strcmp(dir2[i],dir1[j]))

Revision as of 14:54, 28 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