Utilización de FSTAT (programando C Linux)

Iniciado por joe2011, 21 Septiembre 2013, 21:34 PM

0 Miembros y 1 Visitante están viendo este tema.

joe2011

Buenas noches,

Tengo dudas con el siguiente código que muestra como usar fstat con descriptores de archivos en linux. El ejemplo está sacado del libro PROGRAMACIÓN EN LINUX (de Prentice Hall).  La salida es la siguiente cuando ejecuto

$./mstat mstat.c

ARCHIVO: mstat.c
        INODE: 1
     UNIDAD: 1738,751700
         NODO: 0
      ENLACES: 1
            UID: -1215903496
        GID: -1216286732
         TIPO: Tipo desconocido
     TAMAÑO: -1217516891
TAMAÑO DE BLOQUE: -1216286732
    BLOQUES: 134520036
      ACCESO: Tue Nov 12 06:31:52 1935
MODIFICACION: Sun Jun 21 08:42:40 1931
      CAMBIO: Tue Nov 12 06:32:40 1935

No entiendo porqué salen esos datos que creo incorrectos como el UID, el GID, el tamaño, el tamaño del bloque etc. Si fueran tan amables de orientarme. Muchas gracias.

El código fuente es el siguiente:


#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ( int argc, char *argv[] )
{
      struct stat buf;
      mode_t mode;
      char type[80];
      int fd;

      /* valida la linea de comandos */

      if(argc != 2) {
         puts("USAGE: mstat {file}");
         exit(EXIT_FAILURE);
      }

      /* abre el archivo  */

      if((fd = open(argv[1], O_RDONLY)) < 0) {
         perror("open");
         exit(EXIT_FAILURE);
      }

      /* obtiene las estadísticas del archivo */

      mode = buf.st_mode;
      printf("      ARCHIVO: %s\n", argv[1]);
      printf("        INODE: %ld\n", buf.st_ino);
      printf("     UNIDAD: %d,%d\n", major(buf.st_dev), minor(buf.st_dev));
      printf("         NODO: %#o\n", mode & ~(S_IFMT));
      printf("       ENLACES: %d\n", buf.st_nlink);
      printf("            UID: %d\n", buf.st_uid);
      printf("        GID: %d\n", buf.st_gid);
      
      if(S_ISLNK(mode))
         strcpy(type, "Enlace simbólico");
      else if(S_ISREG(mode))
         strcpy(type, "archivo normal");
      else if(S_ISDIR(mode))
         strcpy(type, "Directorio");
      else if(S_ISCHR(mode))
         strcpy(type, "Unidad de caracteres");
      else if(S_ISBLK(mode))
         strcpy(type, "Unidad de bloque");
      else if(S_ISFIFO(mode))
         strcpy(type, "FIFO");
      else if(S_ISSOCK(mode))
         strcpy(type, "Socket");
      else
         strcpy(type, "Tipo desconocido");
      printf("         TIPO: %s\n", type);
      printf("     TAMAÑO: %ld\n", buf.st_size);
      printf("TAMAÑO DE BLOQUE: %ld\n", buf.st_blksize);
      printf("    BLOQUES: %d\n", (int)buf.st_blocks);
      printf("      ACCESO: %s", ctime(&buf.st_atime));
      printf("MODIFICACION: %s", ctime(&buf.st_mtime));
      printf("      CAMBIO: %s", ctime(&buf.st_ctime));

      /* cierra el archivo */
      if(close(fd) <0) {
         perror("close");
         exit(EXIT_FAILURE);
      }
      return EXIT_SUCCESS;

rir3760

Falta la llamada a fstat.

Si los valores que se imprimen no son los esperados ello puede deberse a tipos incorrectos en las llamadas a printf, debes verificar cual es el tipo de los alias gid_t, uid_t, etc.

Un saludo
C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.
--
Kernighan & Ritchie, The C programming language

0xDani

Exacto, falta lo principal del código, que es llamar a fstat(). Antes de mostrar las estadísticas, pon esto:

fstat(fd, &buf);
I keep searching for something that I never seem to find, but maybe I won't, because I left it all behind!

I code for $$$
Hago trabajos en C/C++
Contactar por PM

joe2011

Muchísimas gracias por ayudarme en la resolución del problema. Está claro, no había incluido la llamada a fstat. Lo he incluido y el resultado es el esperado.

Un saludo.

if ((fstat(fd,&buf)) < 0){
    perror("fstat");
    exit(EXIT_FAILURE);
}