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

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


  Mostrar Mensajes
Páginas: 1 2 [3]
21  Seguridad Informática / Hacking / AYUDA PORFAVOR CON Prueba de testing con Selenium en: 21 Marzo 2022, 10:48 am
Prueba de testing con Selenium

En kali linux tengo que hacer un testing con Selenium pero no se como utilizarla
para automatizar alguna busqueda de alguna pagina web con la que pueda testear el funcionamiento
de dicha web, el problema es que no se como hacerlo y que comandos debo de usar en la shell de kali
para testear un sitio web. Si alguien me puediese ayudar porfavor.

Este es el sitio web del Selenium: https://www.selenium.dev/
22  Seguridad Informática / Análisis y Diseño de Malware / NO ENTIENDO! en: 20 Marzo 2022, 18:28 pm
Tengo una duda, estoy haciendo una practica acerca de los keylogger y este es el que me tengo que descargar: https://github.com/GiacomoLaw/Keylogger. Tengo que utilizar Kali Linux.
Ya me le he descargado pero ahora no se que pasos debo seguir para instalarlo, para que capture y guarde en un archivo todas las letras tecleadas por el usuario.

Saludos
23  Programación / Programación C/C++ / Re: No se como empezar!! en: 27 Diciembre 2021, 16:57 pm
Sois tan brillantes como un agujero negro y el doble de denso
24  Programación / Programación C/C++ / No se como empezar!! en: 27 Diciembre 2021, 14:49 pm
Buenas tardes tengo un PROBLEMA!! y es que no sé como empezar el codigo del siguiente enunciado:
Crear en C un programa Padre que cree tantos procesos hijos como argumentos
restantes tenga en la linea de ordenes el proceso padre.

saludos
25  Programación / Programación C/C++ / Re: Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento en: 3 Diciembre 2021, 20:24 pm
me pueden ayudar a hacerlo porfavor, esque tengo que entregarlo en un par de horas y no se como hacerlo   :-(
26  Programación / Programación C/C++ / Re: Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento en: 3 Diciembre 2021, 19:20 pm
@K-YreX He intentado hacer lo que me has dicho pero no se muy bien como pasarlo al codigo C, paso nuevamente el codigo modificado y la informacion que me devuelve la ejecucion del programa(Me devuelve unos cuantos errores).

Código
  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <dirent.h>
  10.  
  11. #define NORMAL  "\x1B[0m"
  12. #define GREEN  "\x1B[32m"
  13. #define BLUE  "\x1B[34m"
  14. #define RED  "\x1b[32m"
  15.  
  16. /* Función que procesa la lectura un archivo */
  17. void procesoArchivo(char *archivo);
  18.  
  19. void mostrarFiltrado(char * path);
  20.  
  21. int main(int argc, char *argv[]){
  22.  
  23. int opcion=0;
  24. char *path; //Es para la ruta
  25. char *type;
  26. //char *name;
  27. int size;
  28. int maxdepth;
  29. int niveles;
  30. unsigned num;
  31.  
  32. // Con ello evitamos fallos de violacion de segmentacion. Se le asigna un espacio en memoria.
  33. type = (char*)malloc(1024);
  34. path = (char*)malloc(1024);
  35. name = (char*)malloc(1024);
  36.  
  37. for(int i; i<argc; i++){
  38. if(strcmp(argv[i], "-maxdepth") == 0){ //Si la cadena que tengo en la posicion i es igual a "x"
  39. //Devolverá 0 en caso de ser iguales caracter a caracter
  40. if(i == argc-1){ //Si contador es igual al ultimo numero de parametros muestra error
  41. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  42. exit(EXIT_FAILURE);
  43. }
  44. opcion=1;
  45.  
  46. } else if(strcmp(argv[i], "-type") == 0){
  47. if(i == argc-1){
  48. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  49. exit(EXIT_FAILURE);
  50. }
  51. opcion=2;
  52.  
  53. } else if(strcmp(argv[i], "-name") == 0){
  54. if(i == argc-1){
  55. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  56. exit(EXIT_FAILURE);
  57. }
  58. opcion=3;
  59.  
  60. } else if(strcmp(argv[i], "-size") == 0){
  61. if(i == argc-1){
  62. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  63. exit(EXIT_FAILURE);
  64. }
  65. opcion=4;
  66. }else{
  67. switch(opcion){
  68. case 0: //Ruta
  69. if(i != 1){
  70. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  71. exit(EXIT_FAILURE);
  72. }
  73. strcpy(path, argv[i]);
  74. break;
  75.  
  76. case 1: //Maxdepth
  77. maxdepth <- atoi(argv[i]); //Convierte una cadena a un entero
  78. niveles = maxdepth;
  79.  
  80. if(! niveles){ //Si niveles es vacio if(niveles != NULL)
  81. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  82. exit(EXIT_FAILURE);
  83. }
  84. if(niveles < 0){ //Si niveles es menor que 0
  85. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  86. exit(EXIT_FAILURE);
  87. }
  88.  
  89. break;
  90.  
  91. case 2: //type
  92. //Si la cadena que haya en la posicion i es distinta a la mencionada da un error
  93. if(strcmp(argv[i], "d") != 0 && strcmp(argv[i], "f") != 0){
  94. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  95. exit(EXIT_FAILURE);
  96. }
  97.  
  98. //opcion = 3;
  99. strcpy(type, argv[i]);
  100. break;
  101.  
  102. case 3: //name
  103.  
  104.  
  105.  
  106. break;
  107.  
  108. case 4: //size
  109.  
  110.  
  111. break;
  112. }
  113. }
  114.  
  115.  
  116. }
  117.  
  118. /* Con un puntero a DIR abriremos el directorio */
  119. //Cuando solo tengo la ruta
  120. DIR *dir;
  121.  
  122. if (argv[1] != NULL){
  123.  
  124. dir = opendir (argv[1]);
  125.  
  126. if (dir == NULL){
  127. printf("\nDirectorio ""%s"" No valido!\n\n",argv[1] );
  128. closedir (dir);
  129. return 0;
  130. }
  131.  
  132. else {
  133. dir = opendir (argv[1]);
  134. path = argv[1];
  135. }
  136. }
  137. else {
  138.  
  139. dir = opendir (".");
  140. }
  141. /* en *ent almacena información sobre el archivo que se esta "obteniendo" a cada momento */
  142. struct dirent *ent;
  143.  
  144.  
  145. /* Leyendo uno a uno todos los archivos que hay */
  146. while ((ent = readdir (dir)) != NULL)
  147. {
  148. /* Nos devolvera el directorio actual (.) y el anterior (..) */
  149. if ( (strcmp(ent->d_name, ".")!=0 ) && (strcmp(ent->d_name, "..")!=0 ))
  150. {
  151. /* Una vez tenemos el archivo, lo pasamos a una funcion para procesarlo. */
  152. procesoArchivo(ent->d_name);
  153. }
  154. }
  155. closedir (dir);
  156.  
  157. return EXIT_SUCCESS;
  158.  
  159. mostrarFiltrado(path);
  160. }
  161.  
  162.  
  163. void procesoArchivo(char *archivo)
  164. {
  165. /* Para "procesar", o al menos, hacer algo con el archivo, vamos a decir su tamaño en bytes */
  166. /* */
  167. FILE *fich;
  168. long ftam;
  169.  
  170. fich=fopen(archivo, "r");
  171. if (fich)
  172. {
  173. fseek(fich, 0L, SEEK_END);
  174. ftam=ftell(fich);
  175. fclose(fich);
  176. /* Si todo va bien, decimos el tamaño */
  177. printf ("%30s (%ld bytes)\n", archivo, ftam);
  178. }
  179. else
  180. /* Si ha pasado algo, solo decimos el nombre */
  181. printf ("%30s\n", archivo);
  182. }
  183.  
  184.  
  185. /* Mostrar Contenido Recursivo */
  186. void mostrarFiltrado(char * path, char *type, int maxdepth)
  187. {
  188. DIR * d = opendir(path);
  189. if(d==NULL) return;
  190. struct dirent * dir;
  191.  
  192. while ((dir = readdir(d)) != NULL)
  193. {
  194. if(dir-> d_type != DT_DIR)
  195. printf("%s%s\n",BLUE, dir->d_name);
  196. else
  197. if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 )
  198. {
  199. printf("%s\n", dir->d_name);
  200. char d_path[255];
  201. printf(d_path, "%s/%s", path, dir->d_name);//imprime la ruta /mi_directorio
  202. mostrarFiltrado(d_path);
  203. }
  204.  
  205. }
  206.  
  207. current_path = type(dir);
  208. while (current_path != NULL){
  209. if(current_path.type == type){
  210. if(strcmp(argv[i], "f") == 0 || ! type ){
  211. printf(current_path);
  212. } else if(strcmp(argv[i], "d") == 0 || ! type){
  213. printf(current_path);
  214. }
  215. }
  216. if(current_path.type == dir && current_depth < maxdepth){
  217. mostrarFiltrado(current_path, type, maxdepth, current_depth + 1);
  218. }
  219.  
  220. current_path = type(dir);
  221. }
  222.  
  223. closedir(d);
  224. }
  225.  
  226.  

A CONTINUACION LA EJECUCION DEL PROGRAMA

allipson@allipson-Ubuntu:~/Escritorio/practicas_SOP$ gcc myfind2.c -o myfind.o
myfind2.c: In function ‘main’:
myfind2.c:34:2: error: ‘name’ undeclared (first use in this function)
   34 |  name = (char*)malloc(1024);
      |  ^~
myfind2.c:34:2: note: each undeclared identifier is reported only once for each function it appears in
myfind2.c: At top level:
myfind2.c:185:6: error: conflicting types for ‘mostrarFiltrado’
  185 | void mostrarFiltrado(char * path, char *type, int maxdepth)
      |      ^~~~~~~
myfind2.c:18:6: note: previous declaration of ‘mostrarFiltrado’ was here
   18 | void mostrarFiltrado(char * path);
      |      ^~~~~~~
myfind2.c: In function ‘mostrarFiltrado’:
myfind2.c:201:4: error: too few arguments to function ‘mostrarFiltrado’
  201 |    mostrarFiltrado(d_path);
      |    ^~~~~~~
myfind2.c:185:6: note: declared here
  185 | void mostrarFiltrado(char * path, char *type, int maxdepth)
      |      ^~~~~~~
myfind2.c:206:2: error: ‘current_path’ undeclared (first use in this function)
  206 |  current_path = type(dir);
      |  ^~~~~~
myfind2.c:206:17: error: called object ‘type’ is not a function or function pointer
  206 |  current_path = type(dir);
      |                 ^~
myfind2.c:185:41: note: declared here
  185 | void mostrarFiltrado(char * path, char *type, int maxdepth)
      |                                   ~~^~
myfind2.c:209:14: error: ‘argv’ undeclared (first use in this function)
  209 |    if(strcmp(argv, "f") == 0 || ! type ){
      |              ^~
myfind2.c:209:19: error: ‘i’ undeclared (first use in this function)
  209 |    if(strcmp(argv, "f") == 0 || ! type ){
      |                   ^
myfind2.c:215:34: error: ‘current_depth’ undeclared (first use in this function)
  215 |   if(current_path.type == dir && current_depth < maxdepth){
      |                                  ^~~~~
myfind2.c:216:4: error: too many arguments to function ‘mostrarFiltrado’
  216 |    mostrarFiltrado(current_path, type, maxdepth, current_depth + 1);
      |    ^~~~~~~
myfind2.c:185:6: note: declared here
  185 | void mostrarFiltrado(char * path, char *type, int maxdepth)
      |      ^~~~~~~
myfind2.c:219:18: error: called object ‘type’ is not a function or function pointer
  219 |   current_path = type(dir);
      |                  ^~
myfind2.c:185:41: note: declared here
  185 | void mostrarFiltrado(char * path, char *type, int maxdepth)

27  Programación / Programación C/C++ / Re: Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento en: 3 Diciembre 2021, 17:50 pm
Creo que he corregido los errores que me habeis dicho tanto @BloodSharp como @K-YreX , ya no me aparece el error de violacion de segmentos. Pero sigo con una duda y es que no se como hacer la recursividad dentro de mostrarFiltrado de cada uno de los parametros (type, maxdepth)
Paso es codigo modificado y adjunto una foto de la ejecucion del programa:

Código
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9.  
  10. #define NORMAL  "\x1B[0m"
  11. #define GREEN  "\x1B[32m"
  12. #define BLUE  "\x1B[34m"
  13. #define RED  "\x1b[32m"
  14.  
  15. /* Función que procesa la lectura un archivo */
  16. void procesoArchivo(char *archivo);
  17.  
  18. void mostrarFiltrado(char * path);
  19.  
  20. int main(int argc, char *argv[]){
  21.  
  22. int opcion=0;
  23. char *path; //Es para la ruta
  24. char *type;
  25. char *name;
  26. int size;
  27. int maxdepth;
  28. int niveles;
  29. unsigned num;
  30.  
  31. // Con ello evitamos fallos de violacion de segmentacion. Se le asigna un espacio en memoria.
  32. type = (char*)malloc(1024);
  33. path = (char*)malloc(1024);
  34. name = (char*)malloc(1024);
  35.  
  36. for(int i; i<argc; i++){
  37. if(strcmp(argv[i], "-maxdepth") == 0){ //Si la cadena que tengo en la posicion i es igual a "x"
  38. //Devolverá 0 en caso de ser iguales caracter a caracter
  39. if(i == argc-1){ //Si contador es igual al ultimo numero de parametros muestra error
  40. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  41. exit(EXIT_FAILURE);
  42. }
  43. opcion=1;
  44.  
  45. } else if(strcmp(argv[i], "-type") == 0){
  46. if(i == argc-1){ //DUDA: utilizo == ?
  47. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  48. exit(EXIT_FAILURE);
  49. }
  50. opcion=2;
  51.  
  52. } else if(strcmp(argv[i], "-name") == 0){
  53. if(i == argc-1){
  54. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  55. exit(EXIT_FAILURE);
  56. }
  57. opcion=3;
  58.  
  59. } else if(strcmp(argv[i], "-size") == 0){
  60. if(i == argc-1){
  61. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  62. exit(EXIT_FAILURE);
  63. }
  64. opcion=4;
  65. }else{
  66. switch(opcion){
  67. case 0: //Ruta
  68. if(i != 1){
  69. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  70. exit(EXIT_FAILURE);
  71. }
  72. strcpy(path, argv[i]);
  73. break;
  74.  
  75. case 1: //Maxdepth
  76. maxdepth <- atoi(argv[i]); //Convierte una cadena a un entero
  77. niveles = maxdepth;
  78.  
  79. if(! niveles){ //Si niveles es vacio if(niveles != NULL)
  80. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. if(niveles < 0){ //Si niveles es menor que 0
  84. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  85. exit(EXIT_FAILURE);
  86. }
  87.  
  88. break;
  89.  
  90. case 2: //type
  91. //Si la cadena que haya en la posicion i es distinta a la mencionada da un error
  92. if(strcmp(argv[i], "d") != 0 && strcmp(argv[i], "f") != 0){
  93. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  94. exit(EXIT_FAILURE);
  95. }
  96.  
  97. //opcion = 3;
  98. strcpy(type, argv[i]);
  99. break;
  100.  
  101. case 3: //name
  102.  
  103.  
  104.  
  105. break;
  106.  
  107. case 4: //size
  108.  
  109.  
  110. break;
  111. }
  112. }
  113.  
  114.  
  115. }
  116.  
  117. /* Con un puntero a DIR abriremos el directorio */
  118. //Cuando solo tengo la ruta
  119. DIR *dir;
  120.  
  121. if (argv[1] != NULL){
  122.  
  123. dir = opendir (argv[1]);
  124.  
  125. if (dir == NULL){
  126. printf("\nDirectorio ""%s"" No valido!\n\n",argv[1] );
  127. closedir (dir);
  128. return 0;
  129. }
  130.  
  131. else {
  132. dir = opendir (argv[1]);
  133. path = argv[1];
  134. }
  135. }
  136. else {
  137.  
  138. dir = opendir (".");
  139. }
  140. /* en *ent almacena información sobre el archivo que se esta "obteniendo" a cada momento */
  141. struct dirent *ent;
  142.  
  143.  
  144. /* Leyendo uno a uno todos los archivos que hay */
  145. while ((ent = readdir (dir)) != NULL)
  146. {
  147. /* Nos devolvera el directorio actual (.) y el anterior (..) */
  148. if ( (strcmp(ent->d_name, ".")!=0 ) && (strcmp(ent->d_name, "..")!=0 ))
  149. {
  150. /* Una vez tenemos el archivo, lo pasamos a una funcion para procesarlo. */
  151. procesoArchivo(ent->d_name);
  152. }
  153. }
  154. closedir (dir);
  155.  
  156. return EXIT_SUCCESS;
  157.  
  158. mostrarFiltrado(path);
  159. }
  160.  
  161.  
  162. void procesoArchivo(char *archivo)
  163. {
  164. /* Para "procesar", o al menos, hacer algo con el archivo, vamos a decir su tamaño en bytes */
  165. /* */
  166. FILE *fich;
  167. long ftam;
  168.  
  169. fich=fopen(archivo, "r");
  170. if (fich)
  171. {
  172. fseek(fich, 0L, SEEK_END);
  173. ftam=ftell(fich);
  174. fclose(fich);
  175. /* Si todo va bien, decimos el tamaño */
  176. printf ("%30s (%ld bytes)\n", archivo, ftam);
  177. }
  178. else
  179. /* Si ha pasado algo, solo decimos el nombre */
  180. printf ("%30s\n", archivo);
  181. }
  182.  
  183.  
  184. /* Mostrar Contenido Recursivo */
  185. void mostrarFiltrado(char * path)
  186. {
  187. DIR * d = opendir(path);
  188. if(d==NULL) return;
  189. struct dirent * dir;
  190. while ((dir = readdir(d)) != NULL)
  191. {
  192. if(dir-> d_type != DT_DIR)
  193. printf("%s%s\n",BLUE, dir->d_name);
  194. else
  195. if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 )
  196. {
  197. printf("%s%s\n",GREEN, dir->d_name);
  198. char d_path[255];
  199. sprintf(d_path, "%s/%s", path, dir->d_name);//imprime la ruta /mi_directorio
  200. mostrarFiltrado(d_path);
  201. }
  202.  
  203. }
  204. closedir(d);
  205. }



No logro subir la foto, pero esto es lo que me devuelve la ejecucion:
Código:
allipson@allipson-Ubuntu:~/Escritorio/practicas_SOP$ ./myfind2.o . -type d
                      myfind.o (16816 bytes)
                      myfind.c (0 bytes)
               .ejemplo_41.swp (1024 bytes)
                     myfind2.c (4827 bytes)
                      fich_new (9223372036854775807 bytes)
ProyectoP1Allipson(9223372036854775807 bytes)
                       Nuevo.c (628 bytes)
                         Nuevo (634 bytes)
ProyectoP1Allipson.zip (29409 bytes)
                     Nuevo_dir (9223372036854775807 bytes)
                      funciona (9223372036854775807 bytes)
                     myfind2.o (16856 bytes)
allipson@allipson-Ubuntu:~/Escritorio/practicas_SOP$ ./myfind2.o . -maxdepth 2
                      myfind.o (16816 bytes)
                      myfind.c (0 bytes)
               .ejemplo_41.swp (1024 bytes)
                     myfind2.c (4827 bytes)
                      fich_new (9223372036854775807 bytes)
ProyectoP1Allipson.zip (9223372036854775807 bytes)
                       Nuevo.c (628 bytes)
                         Nuevo (634 bytes)
ProyectoP1Allipson.zip (29409 bytes)
                     Nuevo_dir (9223372036854775807 bytes)
                      funciona (9223372036854775807 bytes)
                     myfind2.o (16856 bytes)



MOD: Para publicar varios mensajes seguidos, modificar el primero y agregarlo a continuación, no hacer doble post.
MOD: Para código de un lenguaje específico, especificar el lenguaje en la etiqueta de code:
[code=c]
// El código C
[/code]

28  Programación / Programación C/C++ / Re: Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento en: 3 Diciembre 2021, 17:14 pm
He corregido los fallos, pero me sigue dando problemas con violacion de segmento. Paso el codigo modificado. El codigo consiste en simular la funcion del comando find.

Código
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9.  
  10. #define NORMAL  "\x1B[0m"
  11. #define GREEN  "\x1B[32m"
  12. #define BLUE  "\x1B[34m"
  13. #define RED  "\x1b[32m"
  14.  
  15. /* Función que procesa la lectura un archivo */
  16. void procesoArchivo(char *archivo);
  17.  
  18. void mostrarFiltrado(char * path);
  19.  
  20. int main(char *argv[], int argc){
  21.  
  22. int opcion=0;
  23. char *path; //Es para la ruta
  24. char *type;
  25. char *name;
  26. int size;
  27. int maxdepth;
  28. int niveles;
  29. unsigned num;
  30.  
  31. // Con ello evitamos fallos de violacion de segmentacion. Se le asigna un espacio en memoria.
  32. type = (char*)malloc(1024);
  33. path = (char*)malloc(1024);
  34. name = (char*)malloc(1024);
  35.  
  36. for(int i; i<argc; i++){
  37. if(strcmp(argv[i], "-maxdepth") == 0){ //Si la cadena que tengo en la posicion i es igual a " "
  38. //Devolverá 0 en caso de ser iguales caracter a caracter
  39. //if( ){ //Si contador es igual al ultimo numero de parametros muestra error
  40. // fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  41. // exit(EXIT_FAILURE);
  42. //}
  43. opcion=1;
  44.  
  45. } else if(strcmp(argv[i], "-type") == 0){
  46. if( i == argc){
  47. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  48. exit(EXIT_FAILURE);
  49. }
  50. opcion=2;
  51.  
  52. } else if(strcmp(argv[i], "-name") == 0){
  53. if( i == argc){
  54. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  55. exit(EXIT_FAILURE);
  56. }
  57. opcion=3;
  58.  
  59. } else if(strcmp(argv[i], "-size") == 0){
  60. if( i == argc){
  61. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  62. exit(EXIT_FAILURE);
  63. }
  64. opcion=4;
  65. }else{
  66. switch(opcion){
  67. case 0: //Ruta
  68. if(i != 1){  ///DUDA si va i o argv[i]
  69. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  70. exit(EXIT_FAILURE);
  71. }
  72. path=argv[i];
  73. break;
  74.  
  75. case 1: //Maxdepth
  76. maxdepth <- atoi(argv[i]); //Convierte una cadena a un entero
  77. niveles = maxdepth;
  78.  
  79. if(! niveles){ //Si niveles es vacio if(niveles != NULL)
  80. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. if(niveles < 0){ //Si niveles es menor que 0
  84. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  85. exit(EXIT_FAILURE);
  86. }
  87.  
  88. break;
  89.  
  90. case 2: //type
  91. //Si la cadena que haya en la posicion i es distinta a la mencionada da un error
  92. if(strcmp(argv[i], "d") != 0){
  93. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  94. exit(EXIT_FAILURE);
  95. }
  96. if(strcmp(argv[i], "f") != 0){
  97. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  98. exit(EXIT_FAILURE);
  99. }
  100. //opcion = 3;
  101. type=argv[i];
  102. break;
  103.  
  104. case 3: //name
  105.  
  106.  
  107. name=argv[i];
  108. break;
  109.  
  110. case 4: //size
  111.  
  112.  
  113. break;
  114. }
  115. }
  116.  
  117.  
  118. }
  119.  
  120. /* Con un puntero a DIR abriremos el directorio */
  121. //Cuando solo tengo la ruta
  122. DIR *dir;
  123.  
  124. if (argv[1] != NULL){
  125.  
  126. dir = opendir (argv[1]);
  127.  
  128. if (dir == NULL){
  129. printf("\nDirectorio ""%s"" No valido!\n\n",argv[1] );
  130. closedir (dir);
  131. return 0;
  132. }
  133.  
  134. else {
  135. dir = opendir (argv[1]);
  136. path = argv[1];
  137. }
  138. }
  139. else {
  140.  
  141. dir = opendir (".");
  142. }
  143. /* en *ent almacena información sobre el archivo que se esta "obteniendo" a cada momento */
  144. struct dirent *ent;
  145.  
  146.  
  147. /* Leyendo uno a uno todos los archivos que hay */
  148. while ((ent = readdir (dir)) != NULL)
  149. {
  150. /* Nos devolvera el directorio actual (.) y el anterior (..) */
  151. if ( (strcmp(ent->d_name, ".")!=0 ) && (strcmp(ent->d_name, "..")!=0 ))
  152. {
  153. /* Una vez tenemos el archivo, lo pasamos a una funcion para procesarlo. */
  154. procesoArchivo(ent->d_name);
  155. }
  156. }
  157. closedir (dir);
  158.  
  159. return EXIT_SUCCESS;
  160.  
  161. mostrarFiltrado(path);
  162. }
  163.  
  164.  
  165. void procesoArchivo(char *archivo)
  166. {
  167. /* Para "procesar", o al menos, hacer algo con el archivo, vamos a decir su tamaño en bytes */
  168. /* */
  169. FILE *fich;
  170. long ftam;
  171.  
  172. fich=fopen(archivo, "r");
  173. if (fich)
  174. {
  175. fseek(fich, 0L, SEEK_END);
  176. ftam=ftell(fich);
  177. fclose(fich);
  178. /* Si todo va bien, decimos el tamaño */
  179. printf ("%30s (%ld bytes)\n", archivo, ftam);
  180. }
  181. else
  182. /* Si ha pasado algo, solo decimos el nombre */
  183. printf ("%30s\n", archivo);
  184. }
  185.  
  186.  
  187. /* Mostrar Contenido Recursivo */
  188. void mostrarFiltrado(char * path)
  189. {
  190. DIR * d = opendir(path);
  191. if(d==NULL) return;
  192. struct dirent * dir;
  193. while ((dir = readdir(d)) != NULL)
  194. {
  195. if(dir-> d_type != DT_DIR)
  196. printf("%s%s\n",BLUE, dir->d_name);
  197. else
  198. if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 )
  199. {
  200. printf("%s%s\n",GREEN, dir->d_name);
  201. char d_path[255];
  202. sprintf(d_path, "%s/%s", path, dir->d_name);//imprime la ruta /mi_directorio
  203. mostrarFiltrado(d_path);
  204. }
  205.  
  206. }
  207. closedir(d);
  208. }
  209.  
  210.  
29  Programación / Programación C/C++ / Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento en: 3 Diciembre 2021, 16:19 pm
Buenas, necesito ayuda URGENTE!! en un ejercicio, y es que siempre me da Violacion de segmento y ya no se que mas hacer. Si alguien me pudiese ayudar se lo agradeceria de todo corazón.

Código
  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <dirent.h>
  10.  
  11. #define NORMAL  "\x1B[0m"
  12. #define GREEN  "\x1B[32m"
  13. #define BLUE  "\x1B[34m"
  14. #define RED  "\x1b[32m"
  15.  
  16. /* Función que procesa la lectura un archivo */
  17. void procesoArchivo(char *archivo);
  18.  
  19. void mostrarContenido(char * path);
  20.  
  21. int main(char **argv, int argc){
  22.  
  23. int opcion=0;
  24. char *path; //Es para la ruta
  25. char *type;
  26. char *name;
  27. int size;
  28. int maxdepth;
  29. int niveles;
  30. unsigned num;
  31.  
  32. // Con ello evitamos fallos de violacion de segmentacion. Se le asigna un espacio en memoria.
  33. type = malloc(1024);
  34. path = malloc(1024);
  35. name = malloc(1024);
  36.  
  37. for(int i; i<argc; i++){
  38. if(strcmp(argv[i], "-maxdepth") == 0){ //Si la cadena que tengo en la posicion i es igual a " "
  39. //Devolverá 0 en caso de ser iguales caracter a caracter
  40. if( i == argc){ //Si contador es igual al ultimo numero de parametros muestra error
  41. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  42. exit(EXIT_FAILURE);
  43. }
  44. opcion=1;
  45.  
  46. } else if(strcmp(argv[i], "-type") == 0){
  47. if( i == argc){
  48. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  49. exit(EXIT_FAILURE);
  50. }
  51. opcion=2;
  52.  
  53. } else if(strcmp(argv[i], "-name") == 0){
  54. if( i == argc){
  55. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  56. exit(EXIT_FAILURE);
  57. }
  58. opcion=3;
  59.  
  60. } else if(strcmp(argv[i], "-size") == 0){
  61. if( i == argc){
  62. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  63. exit(EXIT_FAILURE);
  64. }
  65. opcion=4;
  66. }else{
  67. switch(opcion){
  68. case 0: //Ruta
  69. if(i != 1){  ///DUDA si va i o argv[i]
  70. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  71. exit(EXIT_FAILURE);
  72. }
  73. path=argv[i];
  74. break;
  75.  
  76. case 1: //Maxdepth
  77. maxdepth <- atoi(argv[i]); //Convierte una cadena a un entero
  78. niveles = maxdepth;
  79.  
  80. if(! niveles){ //Si niveles es vacio if(niveles != NULL)
  81. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  82. exit(EXIT_FAILURE);
  83. }
  84. if(niveles < 0){ //Si niveles es menor que 0
  85. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  86. exit(EXIT_FAILURE);
  87. }
  88.  
  89. break;
  90.  
  91. case 2: //type
  92. //Si la cadena que haya en la posicion i es distinta a la mencionada da un error
  93. if(strcmp(argv[i], "d") != 0){
  94. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  95. exit(EXIT_FAILURE);
  96. }
  97. if(strcmp(argv[i], "f") != 0){
  98. fprintf(stderr, "Error. Modo de empleo: mifind [camino] [opciones]\n");
  99. exit(EXIT_FAILURE);
  100. }
  101. //opcion = 3;
  102. type=argv[i];
  103. break;
  104.  
  105. case 3: //name
  106.  
  107.  
  108. name=argv[i];
  109. break;
  110.  
  111. case 4: //size
  112.  
  113.  
  114. break;
  115. }
  116. }
  117.  
  118.  
  119. }
  120.  
  121. /* Con un puntero a DIR abriremos el directorio */
  122. //Cuando solo tengo la ruta
  123. DIR *dir;
  124.  
  125. if (argv[1] != NULL){
  126.  
  127. dir = opendir (argv[1]);
  128.  
  129. if (dir == NULL){
  130. printf("\nDirectorio ""%s"" No valido!\n\n",argv[1] );
  131. closedir (dir);
  132. return 0;
  133. }
  134.  
  135. else {
  136. dir = opendir (argv[1]);
  137. path = argv[1];
  138. }
  139. }
  140. else {
  141.  
  142. dir = opendir (".");
  143. }
  144. /* en *ent almacena información sobre el archivo que se esta "obteniendo" a cada momento */
  145. struct dirent *ent;
  146.  
  147.  
  148. /* Leyendo uno a uno todos los archivos que hay */
  149. while ((ent = readdir (dir)) != NULL)
  150. {
  151. /* Nos devolvera el directorio actual (.) y el anterior (..) */
  152. if ( (strcmp(ent->d_name, ".")!=0 ) && (strcmp(ent->d_name, "..")!=0 ))
  153. {
  154. /* Una vez tenemos el archivo, lo pasamos a una funcion para procesarlo. */
  155. procesoArchivo(ent->d_name);
  156. }
  157. }
  158. closedir (dir);
  159.  
  160. return EXIT_SUCCESS;
  161.  
  162. //mostrarFiltrado(path, name, type, size, maxdepth);
  163. mostrarFiltrado(path);
  164. }
  165.  
  166.  
  167. void procesoArchivo(char *archivo)
  168. {
  169. /* Para "procesar", o al menos, hacer algo con el archivo, vamos a decir su tamaño en bytes */
  170. /* */
  171. FILE *fich;
  172. long ftam;
  173.  
  174. fich=fopen(archivo, "r");
  175. if (fich)
  176. {
  177. fseek(fich, 0L, SEEK_END);
  178. ftam=ftell(fich);
  179. fclose(fich);
  180. /* Si todo va bien, decimos el tamaño */
  181. printf ("%30s (%ld bytes)\n", archivo, ftam);
  182. }
  183. else
  184. /* Si ha pasado algo, solo decimos el nombre */
  185. printf ("%30s\n", archivo);
  186. }
  187.  
  188. //void mostrarFiltrado(char *path, char *name, char *type, int size, int maxdepth){
  189. //}
  190.  
  191. /* Mostrar Contenido Recursivo */
  192. void mostrarFiltrado(char * path)
  193. {
  194. DIR * d = opendir(path);
  195. if(d==NULL) return;
  196. struct dirent * dir;
  197. while ((dir = readdir(d)) != NULL)
  198. {
  199. if(dir-> d_type != DT_DIR)
  200. printf("%s%s\n",BLUE, dir->d_name);
  201. else
  202. if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 )
  203. {
  204. printf("%s%s\n",GREEN, dir->d_name);
  205. char d_path[255];
  206. sprintf(d_path, "%s/%s", path, dir->d_name);
  207. mostrarFiltrado(d_path);
  208. }
  209. //if(strcmp(argv[2], "-type") == 0){
  210. // Funcion_type(dir, type);
  211. //}
  212. }
  213. closedir(d);
  214. }

MOD: Corregidas etiquetas de Código GeSHi
Páginas: 1 2 [3]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines