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. char character, last_character = '0', *string_depends = NULL, **depends_alone = NULL, *depends_p;
  56. int character_count = 0, memory_count = 1, position_check = 0;
  57.  
  58. while ((character = getc(pkgfile_dir)) != EOF)
  59. {
  60. if (character == '\n')
  61. {
  62. if ((strstr(string_depends, "Depends on:")))
  63. {
  64. string_depends[character_count] = '\0';
  65. depends_p = string_depends;
  66.  
  67. while (*depends_p != '\0')
  68. {
  69.  
  70. if (*depends_p == ':')
  71. {
  72. position_check = 1;
  73. depends_p++;
  74.  
  75. if (!isalpha(*depends_p)){}
  76. else
  77. {
  78. depends_p--;
  79. }
  80. }
  81. else if (position_check == 1)
  82. {
  83. printf("%c", *depends_p);
  84. }
  85.  
  86. depends_p++;
  87. }
  88. break;
  89. }
  90. else
  91. {
  92. character_count = 0;
  93. memory_count = 1;
  94. string_depends = malloc(0);
  95. }
  96. }
  97. else
  98. {
  99.  
  100. string_depends = (char *) realloc(string_depends, memory_count);
  101. string_depends[character_count] = character;
  102.  
  103. character_count++;
  104. memory_count++;
  105. }
  106. }
  107.  
  108. if (string_depends)
  109. free(string_depends);
  110. }
  111.  
  112.  
  113. void search_package(char *package_name)
  114. {
  115. FILE* package_installed = fopen("/var/lib/pkg/db", "r");
  116.  
  117. char *package_n, package_file_lines[225];
  118. int package_install = 0, package_found = 0;
  119.  
  120. package_n = (char *) malloc(strlen(package_name)+2);
  121.  
  122. if (!package_n)
  123. {
  124. puts("Malloc can not create more space.");
  125. exit(EXIT_FAILURE);
  126. }
  127.  
  128. sprintf(package_n, "%s\n", package_name);
  129.  
  130. if (package_installed != NULL)
  131. {
  132. while (fgets(package_file_lines, sizeof(package_file_lines), package_installed) != NULL)
  133. {
  134. if (strcmp(package_file_lines, package_n) == 0)
  135. {
  136. package_install = 1;
  137. break;
  138. }
  139. else
  140. {
  141. package_install = 0;
  142. }
  143. }
  144. free(package_n);
  145. }
  146. else
  147. {
  148. puts("[pkgrmd] I can't open file: /var/lib/pkg/db - Sorry");
  149. exit(EXIT_FAILURE);
  150. }
  151.  
  152. fclose(package_installed);
  153.  
  154. if (package_install == 1)
  155. {
  156. DIR *ports_directory, *package_directory;
  157. struct dirent *structure_directory, *structure_package;
  158.  
  159. char ports_base[12] = "/usr/ports/", *package_directories = NULL;
  160.  
  161. if ((ports_directory = opendir(ports_base)) != NULL)
  162. {
  163. while ((structure_directory = readdir(ports_directory)) != NULL)
  164. {
  165. if (*structure_directory->d_name == '.')
  166. continue;
  167.  
  168. if (package_found == 1)
  169. break;
  170.  
  171. package_directories = (char *) malloc(strlen(ports_base)+strlen(structure_directory->d_name)+1);
  172. if (!package_directories)
  173. {
  174. puts("Malloc can't create more space.");
  175. exit(EXIT_FAILURE);
  176. }
  177.  
  178. sprintf(package_directories, "%s%s/", ports_base, structure_directory->d_name);
  179.  
  180. if ((package_directory = opendir(package_directories)) != NULL)
  181. {
  182. while ((structure_package = readdir(package_directory)) != NULL)
  183. {
  184. if (*structure_package->d_name == '.')
  185. continue;
  186.  
  187. if (strcmp(package_name, structure_package->d_name) == 0)
  188. {
  189. FILE *pkgbuild;
  190. char *depends;
  191.  
  192. package_found = 1;
  193.  
  194. depends = (char *) malloc(strlen(package_directories)+strlen(structure_package->d_name)+9);
  195. if (!depends)
  196. {
  197. puts("Malloc can't create more space.");
  198. exit(EXIT_FAILURE);
  199. }
  200.  
  201. sprintf(depends, "%s%s/Pkgfile", package_directories, structure_package->d_name);
  202.  
  203. if ((pkgbuild = fopen(depends, "r")))
  204. {
  205. struct stat verify_file;
  206. stat(depends, &verify_file);
  207.  
  208. if (verify_file.st_size <= 0)
  209. {
  210. printf("[pkgrmd] The file: %s this in blank. Please verify and try again. \n", depends);
  211. exit(EXIT_FAILURE);
  212. }
  213. else if (verify_file.st_size > 0)
  214. {
  215. depends_parser(pkgbuild);
  216. }
  217.  
  218. fclose(pkgbuild);
  219. }
  220. else
  221. {
  222. printf("[pkgrmd] The package %s not have file pkgfile. Renew pkgfile with commands \"ports -u\" and try again. \n", structure_package->d_name);
  223. }
  224.  
  225. break;
  226. }
  227. }
  228. }
  229. else
  230. {
  231. printf("[pkgrmd] This error no is fatal. But I can't open dir: %s%s", package_directories, structure_package->d_name);
  232. }
  233. }
  234. closedir(ports_directory);
  235. closedir(package_directory);
  236. }
  237. else
  238. {
  239. printf("[pkgrmd] I cant read directory: %s, you have installed prt-get?", ports_base);
  240. }
  241. }
  242. else
  243. {
  244. printf("[pkgrmd] The package %s not is installed. \n[pkgrmd] Please verify package with: prt-get search [name_package] \n", package_name);
  245. }
  246. }
Success #stdin #stdout 0.01s 1724KB
stdin
Standard input is empty
stdout
[pkgrmd] Try "pkgdmr --help" for more information.