elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Manipulación de archivos
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Manipulación de archivos  (Leído 1,978 veces)
Lotux5

Desconectado Desconectado

Mensajes: 24


Ver Perfil
Manipulación de archivos
« en: 23 Noviembre 2013, 17:01 pm »

¿Por qué no me funciona el stat?

Código
  1. #include<unistd.h>
  2. #include<sys/stat.h>
  3. #include<fcntl.h>
  4. #include<stdio.h>
  5. #include<errno.h>
  6. #include<sys/types.h>
  7. #include<dirent.h>
  8. #include<string.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. DIR *dir;
  13. struct dirent *ent;
  14. char **ptr;
  15. long octal;
  16.  
  17. if( strlen(argv[2]) != 4){
  18. printf("\nError al introducir el numero de digitos del parametro 2\n");
  19. exit(-1);
  20. }
  21.  
  22. if( (dir=opendir(argv[1]))==NULL){
  23. printf("\nError en el primer open\n");
  24. exit(-1);
  25. }
  26.  
  27. octal = strtol(argv[2], ptr, 8);
  28.  
  29. struct stat *atributos;
  30. int fd;
  31. while ((ent = readdir(dir)) != NULL){
  32. if( (strcmp(ent->d_name, ".")!=0) && (strcmp(ent->d_name, "..")!=0) ){
  33. if ( (fd = open(ent->d_name,O_CREAT|O_TRUNC|O_WRONLY,S_IRGRP|S_IWGRP|S_IXGRP))<0){
  34. printf("\nError en el segundo open\n");
  35. exit(-1);
  36. }
  37.  
  38. printf("%s", ent->d_name);
  39. printf("%s\n", " : ");
  40.  
  41. if ( (stat(ent->d_name, atributos)) <0){
  42. printf("\nError en stat\n");
  43. exit(-1);
  44. }
  45. chmod(ent->d_name, octal);
  46. close(fd);
  47. }
  48. }
  49.  
  50.  


En línea

Lotux5

Desconectado Desconectado

Mensajes: 24


Ver Perfil
Re: Manipulación de archivos
« Respuesta #1 en: 23 Noviembre 2013, 17:47 pm »

El codigo completo seria este (aunque no funciona):

Código
  1. #include<unistd.h>
  2. #include<sys/stat.h>
  3. #include<fcntl.h>
  4. #include<stdio.h>
  5. #include<errno.h>
  6. #include<sys/types.h>
  7. #include<dirent.h>
  8. #include<string.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. DIR *dir;
  13. struct dirent *ent;
  14. char **ptr;
  15. long octal;
  16.  
  17. if( strlen(argv[2]) != 4){
  18. printf("\nError al introducir el numero de digitos del parametro 2\n");
  19. exit(-1);
  20. }
  21.  
  22. if( (dir=opendir(argv[1]))==NULL){
  23. printf("\nError en el primer open\n");
  24. exit(-1);
  25. }
  26.  
  27. octal = strtol(argv[2], ptr, 8);
  28.  
  29. struct stat atributos;
  30. int fd;
  31. while ((ent = readdir(dir)) != NULL){
  32. if( (strcmp(ent->d_name, ".")!=0) && (strcmp(ent->d_name, "..")!=0) ){
  33. if ( (fd = open(ent->d_name,O_RDONLY))<0){
  34. printf("\nError en el segundo open\n");
  35. exit(-1);
  36. }
  37.  
  38. printf("%s", ent->d_name);
  39. printf("%s", " : ");
  40.  
  41. if ( (stat(ent->d_name, &atributos)) <0){
  42. printf("\nError en stat\n");
  43. exit(-1);
  44. }
  45.  
  46. if( (atributos.st_mode & S_IRUSR) != 0)
  47. printf("%s", "r");
  48. else
  49. printf("%s", "-");
  50.  
  51. if( (atributos.st_mode & S_IWUSR) != 0)
  52. printf("%s", "w");
  53. else
  54. printf("%s", "-");
  55.  
  56. if( (atributos.st_mode & S_IXUSR) != 0)
  57. printf("%s", "x");
  58. else
  59. printf("%s", "-");
  60.  
  61. if( (atributos.st_mode & S_IRGRP) != 0)
  62. printf("%s", "r");
  63. else
  64. printf("%s", "-");
  65.  
  66. if( (atributos.st_mode & S_IWGRP) != 0)
  67. printf("%s", "w");
  68. else
  69. printf("%s", "-");
  70.  
  71. if( (atributos.st_mode & S_IXGRP) != 0)
  72. printf("%s", "x");
  73. else
  74. printf("%s", "-");
  75.  
  76. if( (atributos.st_mode & S_IROTH) != 0)
  77. printf("%s", "r");
  78. else
  79. printf("%s", "-");
  80.  
  81. if( (atributos.st_mode & S_IWOTH) != 0)
  82. printf("%s", "w");
  83. else
  84. printf("%s", "-");
  85.  
  86. if( (atributos.st_mode & S_IXOTH) != 0)
  87. printf("%s", "x");
  88. else
  89. printf("%s", "-");
  90.  
  91.  
  92.  
  93.  
  94. printf("%s", " ");
  95.  
  96. if( (chmod(ent->d_name, octal)) <0){
  97. printf("\nError en chmod\n");
  98. exit(-1);
  99. }
  100.  
  101.  
  102.  
  103. if( (atributos.st_mode & S_IRUSR) != 0)
  104. printf("%s", "r");
  105. else
  106. printf("%s", "-");
  107.  
  108. if( (atributos.st_mode & S_IWUSR) != 0)
  109. printf("%s", "w");
  110. else
  111. printf("%s", "-");
  112.  
  113. if( (atributos.st_mode & S_IXUSR) != 0)
  114. printf("%s", "x");
  115. else
  116. printf("%s", "-");
  117.  
  118. if( (atributos.st_mode & S_IRGRP) != 0)
  119. printf("%s", "r");
  120. else
  121. printf("%s", "-");
  122.  
  123. if( (atributos.st_mode & S_IWGRP) != 0)
  124. printf("%s", "w");
  125. else
  126. printf("%s", "-");
  127.  
  128. if( (atributos.st_mode & S_IXGRP) != 0)
  129. printf("%s", "x");
  130. else
  131. printf("%s", "-");
  132.  
  133. if( (atributos.st_mode & S_IROTH) != 0)
  134. printf("%s", "r");
  135. else
  136. printf("%s", "-");
  137.  
  138. if( (atributos.st_mode & S_IWOTH) != 0)
  139. printf("%s", "w");
  140. else
  141. printf("%s", "-");
  142.  
  143. if( (atributos.st_mode & S_IXOTH) != 0)
  144. printf("%s\n", "x");
  145. else
  146. printf("%s\n", "-");
  147.  
  148.  
  149.  
  150. close(fd);
  151. }
  152. }
  153.  
  154.  
  155. }


En línea

Lotux5

Desconectado Desconectado

Mensajes: 24


Ver Perfil
Re: Manipulación de archivos
« Respuesta #2 en: 23 Noviembre 2013, 17:59 pm »

SOLUCIONADO. Por si alguien necesita el código, lo dejo como aporte.

Código
  1. #include<unistd.h>
  2. #include<sys/stat.h>
  3. #include<fcntl.h>
  4. #include<stdio.h>
  5. #include<errno.h>
  6. #include<sys/types.h>
  7. #include<dirent.h>
  8. #include<string.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. DIR *dir;
  13. struct dirent *ent;
  14. char **ptr;
  15. long octal;
  16.  
  17. if( strlen(argv[2]) != 4){
  18. printf("\nError al introducir el numero de digitos del parametro 2\n");
  19. exit(-1);
  20. }
  21.  
  22. if( (dir=opendir(argv[1]))==NULL){
  23. printf("\nError en el primer open\n");
  24. exit(-1);
  25. }
  26.  
  27. octal = strtol(argv[2], ptr, 8);
  28.  
  29. struct stat atributos;
  30. int fd;
  31. while ((ent = readdir(dir)) != NULL){
  32. if( (strcmp(ent->d_name, ".")!=0) && (strcmp(ent->d_name, "..")!=0) ){
  33.  
  34. printf("%s", ent->d_name);
  35. printf("%s", " : ");
  36.  
  37. if ( (stat(ent->d_name, &atributos)) <0){
  38. printf("\nError en stat\n");
  39. exit(-1);
  40. }
  41.  
  42. if( (atributos.st_mode & S_IRUSR) != 0)
  43. printf("%s", "r");
  44. else
  45. printf("%s", "-");
  46.  
  47. if( (atributos.st_mode & S_IWUSR) != 0)
  48. printf("%s", "w");
  49. else
  50. printf("%s", "-");
  51.  
  52. if( (atributos.st_mode & S_IXUSR) != 0)
  53. printf("%s", "x");
  54. else
  55. printf("%s", "-");
  56.  
  57. if( (atributos.st_mode & S_IRGRP) != 0)
  58. printf("%s", "r");
  59. else
  60. printf("%s", "-");
  61.  
  62. if( (atributos.st_mode & S_IWGRP) != 0)
  63. printf("%s", "w");
  64. else
  65. printf("%s", "-");
  66.  
  67. if( (atributos.st_mode & S_IXGRP) != 0)
  68. printf("%s", "x");
  69. else
  70. printf("%s", "-");
  71.  
  72. if( (atributos.st_mode & S_IROTH) != 0)
  73. printf("%s", "r");
  74. else
  75. printf("%s", "-");
  76.  
  77. if( (atributos.st_mode & S_IWOTH) != 0)
  78. printf("%s", "w");
  79. else
  80. printf("%s", "-");
  81.  
  82. if( (atributos.st_mode & S_IXOTH) != 0)
  83. printf("%s", "x");
  84. else
  85. printf("%s", "-");
  86.  
  87.  
  88.  
  89.  
  90. printf("%s", " ");
  91.  
  92. if( (chmod(ent->d_name, octal)) <0){
  93. printf("\nError en chmod\n");
  94. exit(-1);
  95. }
  96.  
  97. if ( (stat(ent->d_name, &atributos)) <0){
  98. printf("\nError en stat\n");
  99. exit(-1);
  100. }
  101.  
  102.  
  103.  
  104. if( (atributos.st_mode & S_IRUSR) != 0)
  105. printf("%s", "r");
  106. else
  107. printf("%s", "-");
  108.  
  109. if( (atributos.st_mode & S_IWUSR) != 0)
  110. printf("%s", "w");
  111. else
  112. printf("%s", "-");
  113.  
  114. if( (atributos.st_mode & S_IXUSR) != 0)
  115. printf("%s", "x");
  116. else
  117. printf("%s", "-");
  118.  
  119. if( (atributos.st_mode & S_IRGRP) != 0)
  120. printf("%s", "r");
  121. else
  122. printf("%s", "-");
  123.  
  124. if( (atributos.st_mode & S_IWGRP) != 0)
  125. printf("%s", "w");
  126. else
  127. printf("%s", "-");
  128.  
  129. if( (atributos.st_mode & S_IXGRP) != 0)
  130. printf("%s", "x");
  131. else
  132. printf("%s", "-");
  133.  
  134. if( (atributos.st_mode & S_IROTH) != 0)
  135. printf("%s", "r");
  136. else
  137. printf("%s", "-");
  138.  
  139. if( (atributos.st_mode & S_IWOTH) != 0)
  140. printf("%s", "w");
  141. else
  142. printf("%s", "-");
  143.  
  144. if( (atributos.st_mode & S_IXOTH) != 0)
  145. printf("%s\n", "x");
  146. else
  147. printf("%s\n", "-");
  148.  
  149.  
  150. }
  151. }
  152.  
  153.  
  154. }
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Manipulacion masiva? « 1 2 3 »
Diseño Gráfico
Azielito 22 10,077 Último mensaje 18 Enero 2005, 05:22 am
por aNexos
manipulacion de Bitmaps
Programación Visual Basic
NeoXero 0 1,005 Último mensaje 16 Septiembre 2007, 03:59 am
por NeoXero
Manipulacion de BMP « 1 2 »
Programación Visual Basic
Gorky 13 6,895 Último mensaje 17 Octubre 2007, 07:54 am
por Gorky
[C] - Manipulación de archivos binarios
Programación C/C++
cbug 4 4,357 Último mensaje 14 Julio 2010, 03:01 am
por cbug
ayuda Manipulacion de lenguajes SQL
.NET (C#, VB.NET, ASP)
R3Z 2 1,730 Último mensaje 25 Noviembre 2015, 23:22 pm
por R3Z
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines