ProvaPratica 2014.01.23

From Sistemi Operativi
Revision as of 14:58, 23 March 2014 by Pirata (talk | contribs)
Jump to navigation Jump to search

[C esercizi 1 e 2]

/*
*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)
*Scrivere un programma in C “linean” che prenda come parametro il pathname di un file e un numero intero (che chiameremo n). 
*Il programma deve stampare come output il numero di caratteri presenti nella n-ma riga del file se il file e' un file regolare di testo, non deve stampare nulla negli altri casi.
*Un file viene considerato di testo se tutti i suoi byte hanno valori compresi nel range 1-127.
*Per controllare se il file e' “regolare” usare la system call lstat
*/

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



#define BUFFSIZE 200




int istextfile( FILE *fd ){                                   //check if the file is a regular file of text,it means that every byte has a value between 1-127
	int c;
	while ( (c = getc ( fd ) ) != EOF && c <= 127 );
	rewind(fd);                                           //sets the file's cursor at the beginning,otherwise the linean program cannot find the line
	return ( (c == EOF)? 1 : 0 );
}
	 
		
   





void linean( char *path , int line ){
	FILE *fd;
	struct stat buf;
	char bufline[BUFFSIZE];
	char *res;
	int n = line;
	res = NULL;
	lstat( path , &buf );
	if ( S_ISREG( buf.st_mode ) )
	{
		fd = fopen( path , "r" );
		if ( fd == NULL )
		{
			perror( "Error to open the file\n" );
			exit(1);
		}
			if ( istextfile( fd ) )
			{
				while ( ( line > 0 ) )                                        //iterates until at the nline and when is reached then checks if the buffer contains something
				{
					res = fgets(bufline, BUFFSIZE , fd );    //every time the nline is buffered,when exits from the cicle, the buffer contains the nline 
					line--;
				}
				if ( res != NULL )                
				{
					printf( "The file %s in line %d has : %d char\n" , path , n , ( strlen( bufline ) - 1 )  );  //strlen also consider the carriage return
				}
				else
				printf( "Error: isn't possible to find the line\n" );
			}
			else
				printf( "Error is not a regular file of text\n" );
				
		fclose(fd);
				
	}
	else
		printf( "The file %s isn't a regular file\n" , path );
	
	
}

	
	
int main( int argc , char *argv[] ){
	int n = atoi( argv[2] );
	linean( argv[1] , n );
	return(0);
}
/*
*Esercizio 2: completamento (10 punti)
*Si scriva un programma C chiamato “lineandir”. Il risultato del programma, stampato su standard output, deve essere un solo
*numero intero: la somma del numero di caratteri presenti nelle n-me righe di tutti i file regolari, di testo, non nascosti (il primo
*carattere deve essere diverso da punto) della directory corrente. 
*/

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


#define BUFFSIZE 200

int istextfile( FILE *fd );
int linean( char *path , int line );
void lineandir( char *dir , int line );





void lineandir( char *dir , int line ){
	int count , i;
	int sumline;
	struct dirent **de;
	sumline = 0;
	count = scandir( dir , &de , NULL , alphasort );
	for ( i = 0 ; i < count ; i++ )
	{
		if ( de[i]->d_name[0] != '.' )     //doesn't consider the hidden files
			sumline = sumline + linean( de[i]->d_name , line );
		free(de[i]);
	}
	printf("The sum of char for every regular text file's nline in the current directory is: %d\n" , sumline );
	free(de);
}






int istextfile( FILE *fd ){                                  
	int c;
	while ( (c = getc ( fd ) ) != EOF && c <= 127 );
	rewind(fd);                                           
	return ( (c == EOF)? 1 : 0 );
}
	 
		


/*linean is modified so it can return the value of the char in the nline */
int linean( char *path , int line ){
	FILE *fd;
	struct stat buf;
	char bufline[BUFFSIZE];
	char *res;
	int n = line;
	res = NULL;
	lstat( path , &buf );
	if ( S_ISREG( buf.st_mode ) )
	{
		fd = fopen( path , "r" );
		if ( fd == NULL )
		{
			perror( "Error to open the file\n" );
			exit(1);
		}
			if ( istextfile( fd ) )
			{
				while ( ( line > 0 ) )                                        
				{
					res = fgets(bufline, BUFFSIZE , fd );    
					line--;
				}
				if ( res != NULL )                
				{
					return( ( strlen( bufline ) - 1 )  );  
				}
				else
				printf( "Error: isn't possible to find the line\n" );
			}
			else
				printf( "Error is not a regular file of text\n" );
				
		fclose(fd);
				
	}
	else
		printf( "The file %s isn't a regular file\n" , path );
	
	
}

	
	
	

int main( int argc , char *argv[] ){
	int n = atoi( argv[1] );
	lineandir( "./" , n );
	return(0);
}

Pirata