-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmi_cat.c
More file actions
63 lines (55 loc) · 1.85 KB
/
mi_cat.c
File metadata and controls
63 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Antoni Font, Pedro Bustamante, Pavel Ernesto Garcia
#include "directorios.h"
const int TAM_BUFFER = BLOCKSIZE;
/* Permite escribir texto en una posición de un fichero (offset).
SINTAXIS: ./mi_cat <disco> </ruta_fichero> */
int main(int argc, char **argv)
{
// revisamos que la sintaxis sea correcta
if (argc > 2)
{
bmount(argv[1]);
unsigned int p_inodo_dir = 0;
unsigned int p_inodo = 0;
unsigned int p_entrada = 0;
int error = buscar_entrada(argv[2], &p_inodo_dir, &p_inodo, &p_entrada, 0, 6);
if (error < 0)
{
mostrar_error_buscar_entrada(error);
}
else
{
struct STAT estadisticasInodo;
mi_stat_f(p_inodo, &estadisticasInodo); // rellenamos la variable con las estadisticas
if (estadisticasInodo.tipo == 'f')
{
int numBytesLeidosTotal = 0;
int numBytesLeidos = 0;
char buffer[TAM_BUFFER];
int offset = 0;
int i = 0;
do // leemos el fichero
{
offset = TAM_BUFFER * i;
memset(buffer, 0, sizeof(buffer));
numBytesLeidos = mi_read(argv[2], buffer, offset, TAM_BUFFER);
write(1, buffer, numBytesLeidos);
numBytesLeidosTotal += numBytesLeidos;
i++;
} while (numBytesLeidos > 0);
char string[128];
sprintf(string, "\n\nTotal_leidos %i\n", numBytesLeidosTotal);
write(2, string, strlen(string));
}
else
{
// es un directorio
}
}
bumount();
}
else
{
fprintf(stderr, "Sintaxis: ./mi_cat <disco> </ruta_fichero>\n");
}
}