list.c

Vai alla documentazione di questo file.
00001 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #include "Include/fat.h"
00017 #include "Include/list.h"
00018 #include "Include/util.h"
00019 
00020 int fat_ls(FILE *fs, struct fat_ctrl *f_ctrl, char *path, char** list){
00021         struct dir_entry cart_p;
00022         int i, j, ret_val, numero_entry, MAX_ENTRY;
00023         unsigned int size, indice_fat;
00024         char ** lista;
00025         long fs_pos;
00026         char * list_tmp;
00027                 
00028         if ( !(fs) || !(f_ctrl) || !(path) ) return EBDP;
00029         /* La funzione parsing mi lascia il file pointer fs all'inzio del
00030          * primo blocco della DATA REGION che contiene la prima porzione 
00031          * della Directory Table relativa alla cartella puntata dalla path,
00032          * se esiste, altrimenti mi ritorna un codice di errore. */
00033         if ((ret_val = parsing(fs, f_ctrl, path, LS))!=0) return ret_val;
00034         MAX_ENTRY = f_ctrl->b_sector.block_size/sizeof(struct dir_entry);
00035         
00036         /* Leggo la dir_entry "." */
00037         if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))) return ERBD;
00038 
00039         numero_entry = cart_p.len/sizeof(struct dir_entry);
00040         indice_fat = cart_p.index;
00041         if (indice_fat > f_ctrl->b_sector.num_block) return ERFCD;
00042         size = 2;
00043 
00044         if (!(lista = calloc(numero_entry, sizeof(char *)))) return STDLIBERR;
00045         if (!(lista[0] = strdup(cart_p.name))) return STDLIBERR;
00046         
00047         for (i=1, j=1; i<numero_entry; i++, j++){
00048                 if (j==MAX_ENTRY){
00049                         if (f_ctrl->fat_ptr[indice_fat]==BLOCK_FREE) return ERFCD;
00050                         fs_pos = f_ctrl->blk_base + (f_ctrl->fat_ptr[indice_fat] * f_ctrl->b_sector.block_size);
00051                         fseek(fs, fs_pos, SEEK_SET);
00052                         indice_fat = f_ctrl->fat_ptr[indice_fat];
00053                         j=1;
00054                 }
00055                 if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))) return ERBD;
00056                 if (!strlen(cart_p.name)) return ERBD;
00057                 size += strlen(cart_p.name)+1;
00058                 if (!(lista[i] = strdup(cart_p.name))) return STDLIBERR;
00059         }
00060 
00061         if (!(list_tmp = calloc(size, sizeof(char *)))) return STDLIBERR;
00062         for (i=0; i<numero_entry; i++){
00063             if (i<numero_entry-1){
00064                         strcat(list_tmp, lista[i]);
00065                         strcat(list_tmp, "\t");
00066                         free(lista[i]);
00067             }else{
00068                         strcat(list_tmp, lista[i]);
00069                         strcat(list_tmp, "\0");
00070                         free(lista[i]);
00071             }
00072         }
00073         free(lista);
00074         
00075         /* SP ritorna il puntatore alla lista dei file */
00076         *list=list_tmp; 
00077 
00078         return ret_val;
00079 }
Generato il Fri Jan 28 22:16:31 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3