Difference between revisions of "ProvaPratica 2014.01.23"
Jump to navigation
Jump to search
Line 243: | Line 243: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Pirata | Pirata | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="Bash"> | ||
+ | #!/bin/bash | ||
+ | |||
+ | function usage { | ||
+ | command=$(basename "$0") | ||
+ | echo "Usage: $command source destination" | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | if [[ ! -d $1 ]] | ||
+ | then | ||
+ | echo "Source is not a dir" | ||
+ | usage | ||
+ | fi | ||
+ | |||
+ | if [[ ! -d $2 ]] | ||
+ | then | ||
+ | echo "Destination is not a dir" | ||
+ | usage | ||
+ | fi | ||
+ | |||
+ | find $1 -type f -print0 | while IFS= read -r -d $'\0' file; do | ||
+ | filename=$(basename "$file") | ||
+ | if [[ -f "$2$filename" ]] | ||
+ | then | ||
+ | cp "$file" "$2$filename" | ||
+ | else | ||
+ | if cmp -s "$file" "$2$filename" | ||
+ | then | ||
+ | /* Do nothing are the same */ | ||
+ | else | ||
+ | cp "$file" "$2$filename" | ||
+ | fi | ||
+ | fi | ||
+ | done | ||
+ | </syntaxhighlight> | ||
+ | Mrta |
Revision as of 15:53, 30 April 2014
[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);
}
[Bash esercizio 3]
#Esercizio 3: Script bash o Python: (10 punti):
#Il comando che dovrete implementare come script shell o programma python e' updatedir. Updatedir prende due directorycome parametri.
#updatedir dira dirb deve copiare in dirb tutti i file regolari che sono in dira e non in dirb. Se un file regolare e' presente con lo stesso nome sia in
#dira sia in dirb, il file deve essere copiato dalla dira alla dirb solo se i contenuti differiscono.
#! /bin/bash
for file in "$1"/*; do
if [[ -f $file ]] ; then #checks if the file is regular
thereisnotfile=1 #variable that identify if the file is also in dir2
for file2 in "$2"/*; do
if [[ -f $file2 ]] ; then
if [[ $file2 != $file ]] ; then #checks if the file is only in dir1 and not in dir2
continue
else
thereisnotfile=0 #the file is in both dir
fi
if [[ $thereisnotfile -eq 0 ]]; then
if [[ $(md5sum "$file") != $(md5sum "$file2") ]] ; then #checks if the file in dir1 is different of file in dir2 by md5sum
bn=$(basename "$file")
cp $file "$2"/"$bn"
thereisnotfile=2 #is set in this way for not continue to iterate
fi
fi
fi
done
if [[ $thereisnotfile -eq 1 ]]; then
bn=$(basename "$file")
cp $file "$2"/"$bn"
fi
fi
done
Pirata
#!/bin/bash
function usage {
command=$(basename "$0")
echo "Usage: $command source destination"
exit 1
}
if [[ ! -d $1 ]]
then
echo "Source is not a dir"
usage
fi
if [[ ! -d $2 ]]
then
echo "Destination is not a dir"
usage
fi
find $1 -type f -print0 | while IFS= read -r -d $'\0' file; do
filename=$(basename "$file")
if [[ -f "$2$filename" ]]
then
cp "$file" "$2$filename"
else
if cmp -s "$file" "$2$filename"
then
/* Do nothing are the same */
else
cp "$file" "$2$filename"
fi
fi
done
Mrta