Linux/Unix Shell Scripting - examen, hecharme un cable

 
Vista:

examen, hecharme un cable

Publicado por nero (1 intervención) el 07/03/2006 12:22:39
1.Crea un programa en C que muestre por pantalla el contenido de un directorio de forma recursiva.
El directorio se pasará como argumento al programa, en caso contrario mostrará el directorio actual.
Al final, mostrará un resumen indicando el número de ficheros listados y el tamaño total. Además mostrará una lista con el número de ficheros pertenecientes a cada usuario y el tamaño que ocupan sus ficheros.

Ej.:
-rwxrwxrwx 1 pepe pepe 10000 2005-01-29 19:53 catfish.dat
-rwxrwxrwx 1 pepe pepe 10000 1999-12-20 11:30 catfish.exe
-rwxrwxrwx 1 pepe pepe 20000 2005-01-18 21:55 FOTOS_1_cris.cf4
-rwxrwxrwx 1 luis luis 20000 2005-01-29 19:52 FOTOS_2_cris.cf4
-rwxrwxrwx 1 luis luis 30000 2005-01-29 19:53 FOTOS_2_luis.cf4
-rwxrwxrwx 1 root root 30000 2005-01-18 21:54 FOTOS_cris.cf4
-rwxrwxrwx 1 root root 40000 2005-01-18 21:53 FOTOS_root.cf4
..........
7 ficheros, 160000 bytes
luis ....... 2 ficheros, 50000 bytes
root ....... 2 ficheros, 70000 bytes
pepe ....... 3 ficheros, 40000 bytes


yo empece con esto pero tengo fallos::

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

int tamanyo[1000];
int situ[1000];
int ficheros[1000];


void leer(char v[] ){


char b[1000];
int init=0;
DIR *punt_dir;

struct dirent *reg_dir;
struct stat atributos;
struct tm *tiempo;

off_t pos;

punt_dir=opendir(v);
printf("\n\n- directorioke se esta abriendo: '%s' ",v);
getchar();



if(punt_dir!= NULL)
{ //entra solo una vez




while((reg_dir=readdir(punt_dir))!=NULL)
{



stat(reg_dir->d_name ,&atributos);
tiempo= gmtime(&atributos.st_mtime);


sumatoria (atributos.st_uid,atributos.st_size);


printf("usu:%d gid:%d tamanyo:%d bytes Fecha:%d %d %d archivo:%s \n",atributos.st_uid, atributos.st_gid, atributos.st_size, tiempo->tm_mday, tiempo->tm_mon,tiempo->tm_year, reg_dir->d_name);



//{
if(reg_dir->d_type == DT_DIR && (strcmp(reg_dir->d_name,".")!=0) && (strcmp(reg_dir->d_name,"..")!=0))
{ // si el tipo de registro es un directorio yy compara(si el nombre del registro no es un punto) yy compara(si el nombre del registro no son dos punto)

strcpy(b,v);
strcat(b,"/"); //concatena
strcat(b,reg_dir->d_name); //concatena subdirectorio

leer(b);

}

//}probando

init++;
}

printf("archivos: %d \n\n",init);
closedir(punt_dir);//cierra directorio
}
}




int sumatoria(int usuario,int big) {

int existe=0,j=0;

while(situ[j]>=0)
{
if(situ[j]==usuario)
{
existe=1;
ficheros[j]++; // acumula en numero de ficheros
tamanyo[j]=tamanyo[j]+big; // suma los tamaños en el vector tamanyo
}
j++;
}

if(existe==0)
{
situ[j]=usuario;
ficheros[j]=1; // numero de fichero es 1
tamanyo[j]=tamanyo[j]+big; // lo que ocupe el fichero
}
}


main (int argd, char *argz[])
{ int init=0;
int total=0,b=0,x=0;

for(x=0;x<1000;x++)
{
ficheros[x]=0;
situ[x]=-1;
tamanyo[x]=0;
}
x=0;





if (argd == 1)
{
printf("\errorrrr:");
printf("\nruta (ej:./ejecutable mas introducir la ruta para muestra de archivos)");
getchar(); //hace una parada
argz[1]=".";
leer(argz[1]);

}

else
{
leer(argz[1]);
}






while(situ[x]>=0)
{








printf("\nusu: %d %d archivos, tamanyo: %d bytes\n", situ[x], ficheros[x], tamanyo[x]);
total+=ficheros[x];
b=b+tamanyo[x];
x++;
}

printf("\nFIN SUMATORIA: %d archivos, tamanyo: %d bytes\n\n", total,b);
}

me lo piden para mañana , un saludo 7/03/06 gracias esta tarde estare conectado .... por si esta .... graciasss
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder