Difference between revisions of "ProvaPratica 2012.09.19"
Jump to navigation
Jump to search
Line 39: | Line 39: | ||
nwritten = write(STDOUT_FILENO, buffer, nread); | nwritten = write(STDOUT_FILENO, buffer, nread); | ||
+ | if(nwritten < 0){ | ||
+ | perror("Write error: "); | ||
+ | exit(EXIT_FAILURE); | ||
+ | } | ||
n += nwritten | n += nwritten |
Revision as of 14:29, 30 April 2014
URL-> http://www.cs.unibo.it/~renzo/so/pratiche/2012.09.19.pdf
[C Esercizio 1]
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#define BUFSIZE 1024
long int n = 0;
void handler(int sig_num){
signal(SIGUSR1, handler);
fprintf(stderr, "\n%ld Bytes copied until now\n", n);
}
int main(){
int nread, nwritten,
i = 0;
char buffer[BUFSIZE],
cool_gui[] = "-\\|/";
signal(SIGUSR1, handler);
fprintf(stderr, "Copying ");
while( (nread = read(STDIN_FILENO, buffer, BUFSIZE)) != 0 ){
if(nread < 0){
perror("Read error: ");
exit(EXIT_FAILURE);
}
nwritten = write(STDOUT_FILENO, buffer, nread);
if(nwritten < 0){
perror("Write error: ");
exit(EXIT_FAILURE);
}
n += nwritten
fprintf(stderr, "\b\b%c ",cool_gui[i%4]);
(i == INT_MAX) ? (i=0) : (i++);
}
fprintf(stderr, "\nDone\n");
return EXIT_SUCCESS;
}
-Eduardo
[Bash Esercizio 3]
In due versioni:
1. one-liner, per amore di leggibilità
IFS=$(echo -ne "\n\b") && file `find $DIRECTORY` | sed -rn "s/(.*):\ +`file -b $FILENAME`/\1/p"
Purtroppo se esistono troppi file nell'albero della directory non funziona (Argument list too long)
2. script
#!/bin/bash
SUCCESS=0
FAILURE=1
#Check arguments
if [[ $# -ne 2 ]]
then
echo "Usage: `basename $0` file directory"
exit $FAILURE
fi
IFS=$(echo -e "\n\b") #set newline as separator
file_type=$(file -b $1)
file_list=$(find $2)
for item in $file_list
do
item_type=$(file -b $item)
if [[ $item_type == $file_type ]]
then
echo $item
fi
done
exit $SUCCESS
La riga di codice per settare il separatore l'ho trovata qui: http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html . Non mi è chiaro perchè il separatore debba essere "\n\b" e non solo "\n", ma ho provato in varie salse, e questo è l'unico modo in cui lo script funziona.
-Eduardo