Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/12/2012, 02:17
Avatar de guzzano
guzzano
 
Fecha de Ingreso: julio-2010
Ubicación: Isla de Margarita
Mensajes: 162
Antigüedad: 13 años, 9 meses
Puntos: 13
Bucle inusual

Buenas, tengo un problema con un bucle que no termina y se repita dos veces aun así coloque un break, la linea es la 137.

Código C:
Ver original
  1. /*
  2.  
  3.     @author:        Alberto Jsé <guzzan0>
  4.     @package:       pkgrmd - This package remove all dependences alone of a applications.
  5.  
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13.  
  14. #define VERSION "Dannie27"
  15.  
  16. void search_package(char *package_name);
  17. void depends_parser(FILE *pkgfile_dir);
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     if (argc > 1)
  22.     {
  23.         if (strncmp(argv[1], "--info", 6) == 0)
  24.         {
  25.             printf("Written by Alberto Jse <guzzan0>\nVersion: %s\n\nPlease report bug to [email protected]\n", VERSION);
  26.         }
  27.         else if (strncmp(argv[1], "--help", 6) == 0)
  28.         {
  29.             printf("Usage: pkgrmd <commands> or <package to remove>\nExample: pkgrmd wine, pkgrmd xorg, etc.\n\n");
  30.             printf("Commands avaiable: \n\n--help (Show this help) \n--info (Show the current version and author)\n");
  31.         }
  32.         else
  33.         {
  34.             if ((strlen(argv[1])) > 20)
  35.             {
  36.                 printf("[pkgrmd] Sorry, max name package is 20 characteres. \n");
  37.             }
  38.             else
  39.             {
  40.                 search_package(argv[1]);
  41.             }
  42.         }
  43.     }
  44.     else
  45.     {
  46.         printf("[pkgrmd] Try \"pkgdmr --help\" for more information. \n");
  47.     }
  48. }
  49.  
  50. void search_package(char *package_name)
  51. {
  52.     FILE* package_installed = fopen("/var/lib/pkg/db", "r");
  53.    
  54.     char *package_n, package_file_lines[225];
  55.     int package_install = 0;
  56.  
  57.     package_n = (char *) malloc(strlen(package_name)+2);
  58.  
  59.     if (!package_n)
  60.     {
  61.         puts("Malloc can not create more space.");
  62.         exit(0);
  63.     }
  64.    
  65.     sprintf(package_n, "%s\n", package_name);
  66.    
  67.     if (package_installed != NULL)
  68.     {
  69.         while (fgets(package_file_lines, sizeof(package_file_lines), package_installed) != NULL)
  70.         {
  71.             if (strcmp(package_file_lines, package_n) == 0)
  72.             {
  73.                 package_install = 1;
  74.                 break;
  75.             }
  76.             else
  77.             {
  78.                 package_install = 0;
  79.             }
  80.         }
  81.         free(package_n);
  82.     }
  83.     else
  84.     {
  85.         puts("[pkgrmd] I can't open file: /var/lib/pkg/db - Sorry");
  86.         exit(0);
  87.     }
  88.  
  89.     fclose(package_installed);
  90.  
  91.     if (package_install == 1)
  92.     {
  93.         DIR *ports_directory, *package_directory;
  94.         struct dirent *structure_directory, *structure_package;
  95.  
  96.         char ports_base[12] = "/usr/ports/", *package_directories = NULL;
  97.  
  98.         if ((ports_directory = opendir(ports_base)) != NULL)
  99.         {
  100.             while ((structure_directory = readdir(ports_directory)) != NULL)
  101.             {
  102.                 if (*structure_directory->d_name == '.')
  103.                     continue;
  104.  
  105.                 package_directories = (char *) malloc(strlen(ports_base)+strlen(structure_directory->d_name)+1);
  106.                 if (!package_directories)
  107.                 {
  108.                     puts("Malloc can't create more space.");
  109.                     exit(0);
  110.                 }
  111.                
  112.                 sprintf(package_directories, "%s%s/", ports_base, structure_directory->d_name);
  113.  
  114.                 if ((package_directory = opendir(package_directories)) != NULL)
  115.                 {
  116.                     while ((structure_package = readdir(package_directory)) != NULL)
  117.                     {
  118.                         if (*structure_package->d_name == '.')
  119.                             continue;
  120.                            
  121.                         if (strcmp(package_name, structure_package->d_name) == 0)
  122.                         {  
  123.                             FILE *pkgbuild;
  124.                             char *depends;
  125.                            
  126.                             depends = (char *) malloc(strlen(package_directories)+strlen(structure_package->d_name)+9);
  127.                             if (!depends)
  128.                             {
  129.                                 puts("Malloc can't create more space.");
  130.                                 exit(0);
  131.                             }
  132.                            
  133.                             sprintf(depends, "%s%s/Pkgfile", package_directories, structure_package->d_name);
  134.  
  135.                             if ((pkgbuild = fopen(depends, "r")))
  136.                             {
  137.                                 puts("s");
  138.                                 fclose(pkgbuild);
  139.                             }
  140.                             else
  141.                             {
  142.                                 printf("[pkgrmd] The package %s not have file pkgfile. Renew pkgfile with commands \"ports -u\" and try again. \n", structure_package->d_name);
  143.                             }
  144.  
  145.                             break;
  146.                         }
  147.                     }
  148.                     puts("test");
  149.                 }
  150.                 else
  151.                 {
  152.                     printf("[pkgrmd] This error no is fatal. But I can't open dir: %s%s", package_directories, structure_package->d_name);
  153.                     exit(0);
  154.                 }
  155.             }
  156.             closedir(ports_directory);
  157.             closedir(package_directory);
  158.         }
  159.         else
  160.         {
  161.             printf("[pkgrmd] I cant read directory: %s, you have installed prt-get?", ports_base);
  162.             exit(0);
  163.         }
  164.     }
  165.     else
  166.     {
  167.         printf("[pkgrmd] The package %s not is installed. \n[pkgrmd] Please verify package with: prt-get search [name_package] \n", package_name);
  168.         exit(0);
  169.     }
  170. }

Como una duda adicional, ¿estará mal abusar de la función exit()? y he visto ejemplos donde con malloc se tiene que multiplicar lo que queremos abrir por sizeof(char*) ¿tengo que hacerlo?

Muchas gracias, saludos!

Última edición por guzzano; 14/12/2012 a las 02:58