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