<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mazza</id>
	<title>Sistemi Operativi - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mazza"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php/Special:Contributions/Mazza"/>
	<updated>2026-05-01T16:07:18Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_pratica_17_07_14&amp;diff=1139</id>
		<title>Prova pratica 17 07 14</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_pratica_17_07_14&amp;diff=1139"/>
		<updated>2015-06-15T13:54:12Z</updated>

		<summary type="html">&lt;p&gt;Mazza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Esercizio 1 ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Scrivere un programma che per ogni file .c nella directory corrente chiami il compilatore gcc per generare il file oggetto (.o) a&lt;br /&gt;
meno che esista gia' nella directory un file oggetto relativo allo stesso sorgente che sia piu' nuovo (ultima modifica) del&lt;br /&gt;
sorgente.&lt;br /&gt;
Tutti I parametri devono essere passati al compilatore.&lt;br /&gt;
Es:&lt;br /&gt;
genobj -I . -ggdb&lt;br /&gt;
se nella directory corrente esistono I file uno.c e due.c e il file due.o deve richiamare&lt;br /&gt;
gcc -I. -ggdb -c uno.c&lt;br /&gt;
e, solo se due.o ha ultima modifica precedente a due.c, deve chiamare&lt;br /&gt;
gcc -I. -ggdb -c due.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Soluzione di Davide Quadrelli===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define MAX 256&lt;br /&gt;
#define DIM_MAX 1000&lt;br /&gt;
#define dim_last_data 4&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	int i,len,com_size,bol=0;	&lt;br /&gt;
	struct stat o_data,f_data;&lt;br /&gt;
	struct dirent *cur_file;&lt;br /&gt;
	char name[MAX];&lt;br /&gt;
	char prefix[DIM_MAX]=&amp;quot;gcc &amp;quot;;&lt;br /&gt;
	char *comm;&lt;br /&gt;
	FILE *o;&lt;br /&gt;
	FILE *obj=NULL;&lt;br /&gt;
	DIR *dir=opendir(&amp;quot;./&amp;quot;);&lt;br /&gt;
	/*store in prefix the common part of every command to launch*/&lt;br /&gt;
    for (i=1;i&amp;lt;argc;i++){&lt;br /&gt;
        strcat(prefix,argv[i]);&lt;br /&gt;
        strcat(prefix,&amp;quot; &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    com_size=strlen(prefix);&lt;br /&gt;
    /*search in the current directory C files*/&lt;br /&gt;
    while(cur_file=readdir(dir)){&lt;br /&gt;
        if(cur_file-&amp;gt;d_type==DT_REG){&lt;br /&gt;
            /*it's a file*/&lt;br /&gt;
            bol=0;&lt;br /&gt;
            strcpy(name,cur_file-&amp;gt;d_name);&lt;br /&gt;
            printf(&amp;quot;file found: %s &amp;quot;,name);&lt;br /&gt;
            len=strlen(name);&lt;br /&gt;
            if(strcmp(&amp;amp;(name[len-2]),&amp;quot;.c&amp;quot;)==0){&lt;br /&gt;
                /*C file found*/&lt;br /&gt;
                printf(&amp;quot;... source code found ...&amp;quot;);&lt;br /&gt;
                /*check if the file has already an uptaded object file*/&lt;br /&gt;
                name[len-1]='o';&lt;br /&gt;
                if ((o=fopen(name,&amp;quot;r&amp;quot;))==NULL){&lt;br /&gt;
                    /*the corrispondent object file doesn't exists*/&lt;br /&gt;
                    bol=1;&lt;br /&gt;
                    name[len-1]='c';&lt;br /&gt;
                }else{&lt;br /&gt;
                    fclose(o);&lt;br /&gt;
                    /*get the stat data of the object file*/&lt;br /&gt;
                    if(stat(name,&amp;amp;o_data)){&lt;br /&gt;
                        write(STDERR_FILENO,&amp;quot;ERRORE STAT&amp;quot;,11);&lt;br /&gt;
                        exit(-1);&lt;br /&gt;
                    }&lt;br /&gt;
                    /*get the stat data of the C file*/&lt;br /&gt;
                    name[len-1]='c';&lt;br /&gt;
                    if(stat(name,&amp;amp;f_data)){&lt;br /&gt;
                        write(STDERR_FILENO,&amp;quot;ERRORE STAT&amp;quot;,11);&lt;br /&gt;
                        exit(-1);&lt;br /&gt;
                    }&lt;br /&gt;
                    /*check if the object file must be re-created*/&lt;br /&gt;
                    if(o_data.st_mtime&amp;lt;f_data.st_mtime){&lt;br /&gt;
                        bol=1;&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            if(bol){&lt;br /&gt;
                /*prepare and exec the command*/&lt;br /&gt;
                comm=(char *)malloc(com_size+len+dim_last_data);&lt;br /&gt;
                strcpy(comm,prefix);&lt;br /&gt;
                strcat(comm,&amp;quot;-c &amp;quot;);&lt;br /&gt;
                strcat(comm,name);&lt;br /&gt;
                system(comm);&lt;br /&gt;
                printf(&amp;quot;object file created.\n%s&amp;quot;,comm);&lt;br /&gt;
            }&lt;br /&gt;
            printf(&amp;quot;\n\n&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nota del Prof: Nel testo dell'esercizio, in grassetto, c'e' scritto: '''Non usare system o popen o simili!'''. System e' una funzione non sicura. Se si vogliono comporre stringhe c'e' anche la asprintf (al posto di malloc+strcpy+strcat). Se si vuole usare la malloc si puo' usare la snprintf che' piu' comoda di tutte del strcpy/cat [[User:Renzo|Renzo]] ([[User talk:Renzo|talk]]) 16:09, 7 April 2015 (CEST)&lt;br /&gt;
&lt;br /&gt;
===Soluzione di Stefano Mazza===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;quot;s2argv/s2argv.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
/*funzione per sapere l'estensione del file: ritorna vero se quello considerato è un file .x (dove x è il carattere passato come input)*/&lt;br /&gt;
int formato(const char *name, const char x){&lt;br /&gt;
	char *point = strrchr(name, '.'); //cerco l'ultima occorrenza del punto della stringa data (nel nome del file)&lt;br /&gt;
	if (point==NULL || point==name) return 0;	&lt;br /&gt;
	else &lt;br /&gt;
	{&lt;br /&gt;
		point++;&lt;br /&gt;
		if (x==(*point)) return 1;&lt;br /&gt;
		else return 0; &lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*funzione per modificare l'estensione del file di nome name.c*/&lt;br /&gt;
char *modify_format(char *name, const char x){&lt;br /&gt;
	char *newname, *ext; 	&lt;br /&gt;
	strcpy(newname, name);	&lt;br /&gt;
	ext = strrchr(newname, 'c');&lt;br /&gt;
	*ext = x;	//cambio il formato con quello passato per parametro (nel nostro caso cambio .c con .o) &lt;br /&gt;
	return newname;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	if (argc!=4)&lt;br /&gt;
	{&lt;br /&gt;
		fprintf(stderr, &amp;quot;Usage: %s [-I] [directory] [-ggdb]&amp;quot;, argv[0]);&lt;br /&gt;
		exit(EXIT_FAILURE);&lt;br /&gt;
	}&lt;br /&gt;
	char *cmd = NULL;&lt;br /&gt;
	int n, i; &lt;br /&gt;
	struct stat info_c, info_o;&lt;br /&gt;
	struct dirent **entry_list; &lt;br /&gt;
	if( (n=scandir(argv[2], &amp;amp;entry_list, NULL, alphasort)) &amp;lt;0)	&lt;br /&gt;
	{&lt;br /&gt;
		perror(&amp;quot;Scandir: error while scanning directory\n&amp;quot;);&lt;br /&gt;
		exit(1);&lt;br /&gt;
	} &lt;br /&gt;
	chdir(argv[2]);&lt;br /&gt;
	for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
	{&lt;br /&gt;
		stat(entry_list[i]-&amp;gt;d_name, &amp;amp;info_c); &lt;br /&gt;
		if( S_ISREG(info_c.st_mode) &amp;amp;&amp;amp; formato(entry_list[i]-&amp;gt;d_name, 'c')) &lt;br /&gt;
		{	//controllo se per ogni file .c esiste il relativo file .o&lt;br /&gt;
			if ( access( modify_format(entry_list[i]-&amp;gt;d_name, 'o'), F_OK) &amp;lt; 0 ) 	//caso non esiste ==&amp;gt; lo creo&lt;br /&gt;
			{						&lt;br /&gt;
				printf(&amp;quot;Non esiste il .o di %s\n&amp;quot;, entry_list[i]-&amp;gt;d_name);&lt;br /&gt;
				asprintf(&amp;amp;cmd, &amp;quot;gcc %s %s %s -c %s&amp;quot;, argv[1], argv[2], argv[3], entry_list[i]-&amp;gt;d_name);&lt;br /&gt;
				printf(&amp;quot;%s\n&amp;quot;, cmd);&lt;br /&gt;
				system_noshell(cmd);				&lt;br /&gt;
				free(cmd);&lt;br /&gt;
				cmd = NULL;&lt;br /&gt;
			}&lt;br /&gt;
			else	//caso esiste&lt;br /&gt;
			{&lt;br /&gt;
				stat(modify_format(entry_list[i]-&amp;gt;d_name, 'o'), &amp;amp;info_o);&lt;br /&gt;
				if (info_c.st_mtime &amp;gt; info_o.st_mtime)		//non è ancora stato aggiornato&lt;br /&gt;
				{&lt;br /&gt;
					printf(&amp;quot;Esiste il .o di %s, ma da aggiornare\n&amp;quot;, entry_list[i]-&amp;gt;d_name);&lt;br /&gt;
					unlink(modify_format(entry_list[i]-&amp;gt;d_name, 'o')); //elimino il file per aggiornarlo(ne creo uno nuovo)&lt;br /&gt;
					asprintf(&amp;amp;cmd, &amp;quot;gcc %s %s %s -c %s&amp;quot;, argv[1], argv[2], argv[3], entry_list[i]-&amp;gt;d_name);&lt;br /&gt;
					printf(&amp;quot;%s\n&amp;quot;, cmd);&lt;br /&gt;
					system_noshell(cmd);				&lt;br /&gt;
					free(cmd);&lt;br /&gt;
					cmd = NULL;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}	&lt;br /&gt;
	return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Esercizio 3==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Il comando che dovrete implementare come script shell o programma python e' mytx.&lt;br /&gt;
Tale comando elenca tutti i file di una directory.&lt;br /&gt;
mytx ddd ddd.tx&lt;br /&gt;
Ogni riga del file di output (secondo parametro) deve contenere la lunghezza, uno spazio e il nume del file. Dopo l'ultima riga&lt;br /&gt;
deve inserire una riga bianca.&lt;br /&gt;
ddd.t2 deve contenere l'elenco dei file regolari. Il primo campo e' un numero intero seguito da uno spazio, tutto cio' che segue&lt;br /&gt;
fino alla fine riga e' il nome del file.&lt;br /&gt;
es.&lt;br /&gt;
12 file1&lt;br /&gt;
235 file di prova&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Soluzione di Davide Quadrelli===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#Interpreto la richiesta del testo come segue:&lt;br /&gt;
#implementare mytx ddd ddd.tx ove ddd è una directory, ddd.tx è un file&lt;br /&gt;
#mytx deve elencare i file della directory ddd scrivere nel file ddd.tx di output una riga per file contenente&lt;br /&gt;
#la lunghezza del file, uno spazio e il nome del file. ddd.tx deve terminare con una riga bianca.&lt;br /&gt;
&lt;br /&gt;
if [[ -d $1 ]] ; then&lt;br /&gt;
	ls $1&lt;br /&gt;
	old=`pwd`&lt;br /&gt;
	cd $1&lt;br /&gt;
	files=`ls`&lt;br /&gt;
	for var in $files ; do&lt;br /&gt;
		if [[ -f $var ]]; then&lt;br /&gt;
			wc -c ${var} 1&amp;gt;&amp;gt; ${old}/${2}&lt;br /&gt;
		fi&lt;br /&gt;
	done&lt;br /&gt;
	echo &amp;gt;&amp;gt; ${old}/${2}&lt;br /&gt;
&lt;br /&gt;
else&lt;br /&gt;
	echo &amp;quot;inserire come primo parametro la directory e come secondo il file da scrivere&amp;quot;&lt;br /&gt;
fi&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mazza</name></author>
	</entry>
</feed>