Título: Manipulación de archivos
Publicado por: Lotux5 en 23 Noviembre 2013, 17:01 pm
¿Por qué no me funciona el stat? #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<errno.h> #include<sys/types.h> #include<dirent.h> #include<string.h> int main(int argc, char *argv[]) { DIR *dir; struct dirent *ent; char **ptr; long octal; printf("\nError al introducir el numero de digitos del parametro 2\n"); } if( (dir=opendir(argv[1]))==NULL){ printf("\nError en el primer open\n"); } octal = strtol(argv [2], ptr , 8); struct stat *atributos; int fd; while ((ent = readdir(dir)) != NULL){ if( (strcmp(ent ->d_name , ".")!=0) && (strcmp(ent ->d_name , "..")!=0) ){ if ( (fd = open(ent->d_name,O_CREAT|O_TRUNC|O_WRONLY,S_IRGRP|S_IWGRP|S_IXGRP))<0){ printf("\nError en el segundo open\n"); } if ( (stat(ent->d_name, atributos)) <0){ } chmod(ent->d_name, octal); close(fd); } }
Título: Re: Manipulación de archivos
Publicado por: Lotux5 en 23 Noviembre 2013, 17:47 pm
El codigo completo seria este (aunque no funciona): #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<errno.h> #include<sys/types.h> #include<dirent.h> #include<string.h> int main(int argc, char *argv[]) { DIR *dir; struct dirent *ent; char **ptr; long octal; printf("\nError al introducir el numero de digitos del parametro 2\n"); } if( (dir=opendir(argv[1]))==NULL){ printf("\nError en el primer open\n"); } octal = strtol(argv [2], ptr , 8); struct stat atributos; int fd; while ((ent = readdir(dir)) != NULL){ if( (strcmp(ent ->d_name , ".")!=0) && (strcmp(ent ->d_name , "..")!=0) ){ if ( (fd = open(ent->d_name,O_RDONLY))<0){ printf("\nError en el segundo open\n"); } if ( (stat(ent->d_name, &atributos)) <0){ } if( (atributos.st_mode & S_IRUSR) != 0) else if( (atributos.st_mode & S_IWUSR) != 0) else if( (atributos.st_mode & S_IXUSR) != 0) else if( (atributos.st_mode & S_IRGRP) != 0) else if( (atributos.st_mode & S_IWGRP) != 0) else if( (atributos.st_mode & S_IXGRP) != 0) else if( (atributos.st_mode & S_IROTH) != 0) else if( (atributos.st_mode & S_IWOTH) != 0) else if( (atributos.st_mode & S_IXOTH) != 0) else if( (chmod(ent->d_name, octal)) <0){ } if( (atributos.st_mode & S_IRUSR) != 0) else if( (atributos.st_mode & S_IWUSR) != 0) else if( (atributos.st_mode & S_IXUSR) != 0) else if( (atributos.st_mode & S_IRGRP) != 0) else if( (atributos.st_mode & S_IWGRP) != 0) else if( (atributos.st_mode & S_IXGRP) != 0) else if( (atributos.st_mode & S_IROTH) != 0) else if( (atributos.st_mode & S_IWOTH) != 0) else if( (atributos.st_mode & S_IXOTH) != 0) else close(fd); } } }
Título: Re: Manipulación de archivos
Publicado por: Lotux5 en 23 Noviembre 2013, 17:59 pm
SOLUCIONADO. Por si alguien necesita el código, lo dejo como aporte. #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<errno.h> #include<sys/types.h> #include<dirent.h> #include<string.h> int main(int argc, char *argv[]) { DIR *dir; struct dirent *ent; char **ptr; long octal; printf("\nError al introducir el numero de digitos del parametro 2\n"); } if( (dir=opendir(argv[1]))==NULL){ printf("\nError en el primer open\n"); } octal = strtol(argv [2], ptr , 8); struct stat atributos; int fd; while ((ent = readdir(dir)) != NULL){ if( (strcmp(ent ->d_name , ".")!=0) && (strcmp(ent ->d_name , "..")!=0) ){ if ( (stat(ent->d_name, &atributos)) <0){ } if( (atributos.st_mode & S_IRUSR) != 0) else if( (atributos.st_mode & S_IWUSR) != 0) else if( (atributos.st_mode & S_IXUSR) != 0) else if( (atributos.st_mode & S_IRGRP) != 0) else if( (atributos.st_mode & S_IWGRP) != 0) else if( (atributos.st_mode & S_IXGRP) != 0) else if( (atributos.st_mode & S_IROTH) != 0) else if( (atributos.st_mode & S_IWOTH) != 0) else if( (atributos.st_mode & S_IXOTH) != 0) else if( (chmod(ent->d_name, octal)) <0){ } if ( (stat(ent->d_name, &atributos)) <0){ } if( (atributos.st_mode & S_IRUSR) != 0) else if( (atributos.st_mode & S_IWUSR) != 0) else if( (atributos.st_mode & S_IXUSR) != 0) else if( (atributos.st_mode & S_IRGRP) != 0) else if( (atributos.st_mode & S_IWGRP) != 0) else if( (atributos.st_mode & S_IXGRP) != 0) else if( (atributos.st_mode & S_IROTH) != 0) else if( (atributos.st_mode & S_IWOTH) != 0) else if( (atributos.st_mode & S_IXOTH) != 0) else } } }
|