struct stat
Buenas.
Alguno de los presentes sabe cuál es el criterio de
ordenación de la estructura stat <sys/stat.h> ?
Estoy haciendo un programilla para escanear directorios
y no consigo establecer el criterio.Por fechas no es,
ni por orden alfabetico ,ni por nº de i-node.
Os atacho el código.
Gracias y un saludote.
César.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "cfg.h" //Estructura de datos relativos a cada objeto-fichero
#define MAXPATH 20
//#define DEBUG
uid_t UID;
gid_t GID;
OBJECTS objects;
int scan(const char *);
int main (int ,char **);
int process(struct dirent *,const char *);
int main(int argc,char **argv)
{
int rc;
//usuario
UID=getuid();
GID=getgid();
if(argc==2)
rc=scan(argv[1]);
else if(argc==1) {
if( (rc=scan(getenv("PWD"))!=0)
exit(-1);
}
else fprintf(stderr,"Uso %s [directorio ]\n",argv[0]);
#ifdef DEBUG
printf("Salida de scan : %d\n",rc);
#endif
return(0);
}
int scan(const char *actual)
{
struct stat statbuf;
struct dirent *dirbuf;
DIR *dp;
int i=0;
int rc=0;
/*
#ifdef DEBUG
#ifndef S_ISLINK
fprintf(stderr,"S_ISLINK no definido\n");
#endif
#ifndef S_ISSOCK
fprintf(stderr,"S_ISSOCK no definido\n");
#endif
#endif
#ifdef DEBUG
printf("Directorio actual = %s\n",actual);
#endif
*/
if(lstat(actual,&statbuf) < 0)
return(errno);
if(S_ISDIR(statbuf.st_mode) == 0)
return(errno);
if( (dp =opendir(actual)) == NULL)
return(errno);
#ifdef DEBUG
fprintf(stderr,"Error :%s",strerror(errno));
#endif
if( chdir(actual) <0 ){
fprintf(stderr,"Error al cambiar de directorio.\n");
exit(-1);
}
printf("%30s %10s %16s %12s %7s %7s %6s\n"," Nombre","I-Node","MODO","isDir","isRead","isExec","chDir");
while( (dirbuf=readdir(dp))!=NULL)
{
if(strcmp(dirbuf->d_name,".")==0 || strcmp(dirbuf->d_name,"..")==0 )
continue;
/*Falta Chequeo de error*/
rc=process(dirbuf,actual);
i++; //Paginador
if(i==20){
i=0;
puts("\n----------------------Pulsa RETURN para continuar---------------------");
getchar();
} //fin de paginador
#ifdef DEBUG
fprintf(stderr,"Error : %s\n",strerror(errno));
#endif
}
return 0;
}
int process(struct dirent *buf,const char *actual)
{
int rc;
struct stat statbuf;
char *tipo;
OBJECTS *new;
char *fullpath;
(char *)fullpath=(char *)actual;
if( (new=malloc(sizeof(OBJECTS)) )==NULL)
return(errno);
#ifdef DEBUG
fprintf(stderr,"Error : %s",strerror(errno));
#endif
fullpath=strcpy(fullpath,buf->d_name);
if( (rc=lstat(fullpath,&statbuf)) < 0){
#ifdef DEBUG
fprintf(stderr,"lstat :%d error : %s",rc,strerror(errno));
#endif
return(errno);
}
printf("%30s %10d %10d [%4d %4d %4d %4d]\n",
fullpath,statbuf.st_ino,statbuf.st_mode,isDir(&statbuf),isRead(&statbuf),
isExec(&statbuf),chDir(&statbuf));
//Aqui falta el codigo para encolar los archivos
// pero necesito saber que criterio sigue la estructura stat
free(new);
return 0;
}
int isDir(struct stat *buf)
{
return( S_ISDIR(buf->st_mode));
}
int chDir(struct stat *buf)
{
return (isDir(buf) && isExec(buf) && isRead(buf));
}
int isExec(struct stat *buf)
{
if(buf->st_uid==UID)
return(buf->st_mode & S_IXUSR);
else if(buf->st_gid==GID)
return(buf->st_mode & S_IXGRP);
else return (buf->st_mode & S_IXOTH);
}
int isRead(struct stat *buf)
{
if(buf->st_uid==UID)
return(buf->st_mode & S_IRUSR);
else if(buf->st_gid==GID)
return(buf->st_mode & S_IRGRP);
else return buf->st_mode & S_IROTH;
}
Reply to: