X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/a7a781b6b1a260c882ad779a9ecdaa517d1a6846..a73a4c8cb85f03d9863db77f85adb4db42a91286:/emufs/idx.c diff --git a/emufs/idx.c b/emufs/idx.c index 9979070..cfb798e 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -35,6 +35,26 @@ */ #include "idx.h" +#include +#include + +FILE* emufs_idx_abrir(EMUFS* efs, const char* mode) +{ + FILE* f; + char* filename; + + filename = (char*) malloc(sizeof(char) * (strlen(efs->nombre) + + strlen(EMUFS_IDX_EXT) + 1)); + if (filename == NULL) { + /* TODO Manejo de errores */ + return NULL; + } + strcpy(filename, efs->nombre); + strcat(filename, EMUFS_IDX_EXT); + f = fopen(filename, mode); + free(filename); + return f; +} int emufs_idx_buscar_mayor_id(EMUFS *emu) { @@ -70,16 +90,17 @@ int emufs_idx_buscar_registro(EMUFS *emu, int ID) strcat(name_f_idx, EMUFS_IDX_EXT); if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/ + reg.id_reg = -1; + reg.block = -1; while ( !feof(f_idx) ){ if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue; if ( reg.id_reg == ID ){ - fclose(f_idx); - return reg.block; + break; } } fclose(f_idx); - return -1; /*no existe el registro*/ + return reg.block; } /* agrega un registro al final del archivo */ @@ -92,7 +113,7 @@ int emufs_idx_agregar(EMUFS *emu, int num_bloque, int ID_aux) strcpy(name_f_idx,emu->nombre); strcat(name_f_idx, EMUFS_IDX_EXT); - if ( (f_idx = fopen(name_f_idx,"ab+"))==NULL ) return -1; + if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1; reg.block = num_bloque; reg.id_reg = ID_aux; @@ -100,3 +121,4 @@ int emufs_idx_agregar(EMUFS *emu, int num_bloque, int ID_aux) fclose(f_idx); return 0; } +