No tengo linux para probar el código, pero debería ser una cosa así:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
// Sacado de https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux
static struct termios old, new;
/* Initialize new terminal i/o settings */
void initTermios(int echo) {
tcgetattr(0, &old); /* grab old terminal i/o settings */
new = old; /* make new settings same as old settings */
new.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
new.c_lflag |= ECHO; /* set echo mode */
} else {
new.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void) {
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo) {
char ch;
initTermios(echo);
resetTermios();
return ch;
}
/* Read 1 character without echo */
return getch_(0);
}
/* Read 1 character with echo */
char getche(void) {
return getch_(1);
}
void SetTermNoCanon( struct termios *SavedTM) {
struct termios tm;
tcgetattr(0, &tm);
tcgetattr(0, SavedTM);
tm.c_lflag &= ~(ICANON|ECHO);
tm.c_cc[VMIN]= 1;
tm.c_cc[VTIME]= 0;
tcsetattr(0, TCSANOW, &tm);
}
int main(int argc, const char * argv[]) {
char c;
int f;
long file_end;
long file_pos;
char buff[BUFFER_TAMAINA];
FILE *f1;
struct stat stat1;
// Voy a sustituir todos los write a stdout por printf o puts
// Número de argumentos diferente al esperado
if (argc != 3) {
printf("Uso: %s [fichero] [posicion]", argv
[0]); return EXIT_FAILURE;
}
file_pos
= atol(argv
[2]);
// No se pueden los metadatos del archivo
if(stat(argv[1], &statl) == -1) {
puts("error, no se pueden recuperar datos del archivo"); return EXIT_FAILURE;
}
// El argumento posición no es un número o es 0
if(file_pos < 1) {
puts("error, posicion debe ser un entero igual o mayor a 1"); return EXIT_FAILURE;
}
f1
= fopen(argv
[1], "r+");
// El archivo no existe o no se puede abrir
if(!f1) {
puts("error, el fichero no existe"); return EXIT_FAILURE;
}
//- Sustituyo todo esto
// while(fgetc(f1)!=EOF) {
// k++;
// }
// fclose(f1);
//- Por lo siguiente
// La posición entregada está fuera del archivo
if(file_pos>file_end) {
puts("error, la position es demasiado alta"); return EXIT_FAILURE;
}
fseek(f1
, file_pos
, SEEK_SET
);
SetTermNoCanon( &saved_tm );
while(c=getche()!=27) {
}
printf("Información: %s\n", argv
[1]); printf("---------------------------\n"); printf("Tamaño: \t \t %d bytes\n", stat1
->st_size
); printf("Links: \t%d\n", stat1
->st_nlink
); printf("inode: \t \t %d\n", stat1
->st_ino
);
return EXIT_SUCCESS;
}