// File: /workspace/github_c_makefile_star2_O3/carlitosmarin[P]Sistema-Ficheros-SOII/./src/directorios.c
// Function: mi_dir
int mi_dir (const char *camino, char *buf) {
	int pid = 0, pi = 0, pe = 0;
	int BE = buscar_entrada(camino, &pid, &pi, &pe, 0, 0);
	if(BE < 0) { //Tratamiento de errores
		switch(BE){
		case -1: printf("\"%s\" Camino no valido\n", camino); break;
		case -2: printf("No tienes permisos de lectura\n"); break;
		case -3: printf("Error en la lectura de la entrada \"%s\"\n", camino); break;
		case -4: printf("No existe la entrada \"%s\"\n", camino); break;
		case -5: printf("Error a la hora de reservar un inodo"); break;
		case -6: printf("No existe directorio intermedio a \"%s\"", camino); break;
		case -7: printf("Error a la hora de liberar un inodo"); break;
		case -8: printf("Error en la escritura de la entrada \"%s\"\n", camino); break;
		case -9: printf("Ya existe la entrada \"%s\"\n", camino); break;
		}
		puts("Falla MI_DIR");
		return -1; //Error BE
	}
	struct inodo in;
	in = leer_inodo(pi);
	struct entrada entr;
	if(in.tipo != 'd' && in.permisos & 4) return -1;
	int numEntradas = in.tamBytesLogicos/sizeof(struct entrada);
	int nentrada = 0;
	while(nentrada < numEntradas){ //Mientras haya entradas
			if(mi_read_f(pi,&entr,nentrada*sizeof(struct entrada),sizeof(struct entrada)) < 0) return -2; //Leemos una entrada Error LECTURA
			in = leer_inodo(entr.inodo);

			if(in.tipo == 'd') strcat(buf,"D"); //Tipo de inodo
			else strcat(buf,"F");
			strcat(buf,"\t");
			//Para incorporar informacion acerca de los permisos
			if(in.permisos & 4) strcat(buf,"r");
			else strcat(buf,"-");

			if(in.permisos & 2) strcat(buf,"w");
			else strcat(buf,"-");

			if(in.permisos & 1) strcat(buf,"x");
			else strcat(buf,"-");

			strcat(buf, "\t");

			//Para incorporar informacion acerca del tiempo
			struct tm *tm;
			char tmp[100];
			tm = localtime(&in.mtime);
			sprintf(tmp, "%d-%02d-%02d %02d:%02d:%02d\t", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
			strcat(buf,tmp);

			strcat(buf,entr.nombre);
			strcat(buf,"\n");
			nentrada++;
		}
	return numEntradas;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %rbx
movq %rdi, %r14
xorl %eax, %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x14(%rsp), %rdx
movl %eax, (%rdx)
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2de3
testl %eax, %eax
js 0x33da
movl 0x14(%rsp), %esi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl %esi, 0x10(%rsp)
callq 0x1995
cmpb $0x64, (%r15)
je 0x324e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x4, 0x21(%rsp)
jne 0x346a
movl 0x44(%rsp), %ecx
movl %ecx, %eax
shrl $0x6, %eax
cmpl $0x40, %ecx
jb 0x346a
leaq 0xa0(%rsp), %r14
xorl %r15d, %r15d
movl %eax, 0xc(%rsp)
movl %eax, %ebp
movl $0x2d, %r13d
movl 0x10(%rsp), %edi
movq %r14, %rsi
movl %r15d, %edx
movl $0x40, %ecx
callq 0x2881
testl %eax, %eax
js 0x33f6
movl 0xdc(%rsp), %esi
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
callq 0x1995
movl $0x10, %ecx
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
xorl %r14d, %r14d
cmpb $0x64, 0x20(%rsp)
setne %r14b
movq %rbx, %rdi
callq 0x1080
leal 0x44(,%r14,2), %ecx
movw %cx, (%rbx,%rax)
movq %rbx, %rdi
callq 0x1080
movw $0x9, %cx
movw %cx, (%rbx,%rax)
movb 0x21(%rsp), %r14b
testb $0x4, %r14b
movl $0x72, %r12d
cmovel %r13d, %r12d
movq %rbx, %rdi
callq 0x1080
movw %r12w, (%rbx,%rax)
testb $0x2, %r14b
movl $0x77, %r12d
cmovel %r13d, %r12d
movq %rbx, %rdi
callq 0x1080
movw %r12w, (%rbx,%rax)
testb $0x1, %r14b
movl $0x78, %r14d
cmovel %r13d, %r14d
movq %rbx, %rdi
callq 0x1080
movw %r14w, (%rbx,%rax)
leaq 0xa0(%rsp), %r14
movq %rbx, %rdi
callq 0x1080
movw $0x9, %cx
movw %cx, (%rbx,%rax)
leaq 0x30(%rsp), %rdi
callq 0x1030
movl 0x14(%rax), %edx
movl $0x76c, %ecx            # imm = 0x76C
addl %ecx, %edx
movl 0x10(%rax), %ecx
incl %ecx
movl 0xc(%rax), %r8d
movl 0x8(%rax), %r9d
movl (%rax), %r10d
movl 0x4(%rax), %r11d
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
leaq 0x1115(%rip), %rsi      # 0x44a7
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x1160
addq $0x10, %rsp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1150
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1150
movq %rbx, %rdi
callq 0x1080
movw $0xa, (%rbx,%rax)
addl $0x40, %r15d
decl %ebp
jne 0x3277
movl 0xc(%rsp), %eax
jmp 0x346a
addl $0x9, %eax
leaq 0xe78(%rip), %rcx       # 0x425c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x107c(%rip), %rdi      # 0x4470
jmp 0x344f
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x346a
leaq 0xfd2(%rip), %rdi       # 0x43d6
jmp 0x340d
leaq 0x1015(%rip), %rdi      # 0x4422
xorl %eax, %eax
callq 0x1090
jmp 0x3459
leaq 0xfde(%rip), %rdi       # 0x43fb
jmp 0x344f
leaq 0x10db(%rip), %rdi      # 0x4501
callq 0x1060
jmp 0x3459
leaq 0x1012(%rip), %rdi      # 0x4446
jmp 0x344f
leaq 0xf7e(%rip), %rdi       # 0x43bb
jmp 0x344f
leaq 0xf4d(%rip), %rdi       # 0x4393
jmp 0x344f
leaq 0xf2d(%rip), %rdi       # 0x437c
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
leaq 0x103a(%rip), %rdi      # 0x449a
callq 0x1060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq