+ int package_num;
+ int dpkg_opt = 0;
+ int deb_count = 0;
+ int state_status;
+ int status_num;
+ int i;
+
+ while ((opt = getopt(argc, argv, "cF:ilPru")) != -1) {
+ switch (opt) {
+ case 'c':
+ dpkg_opt |= dpkg_opt_configure;
+ dpkg_opt |= dpkg_opt_package_name;
+ break;
+ case 'F': // equivalent to --force in official dpkg
+ if (strcmp(optarg, "depends") == 0) {
+ dpkg_opt |= dpkg_opt_force_ignore_depends;
+ }
+ case 'i':
+ dpkg_opt |= dpkg_opt_install;
+ dpkg_opt |= dpkg_opt_filename;
+ break;
+ case 'l':
+ dpkg_opt |= dpkg_opt_list_installed;
+ case 'P':
+ dpkg_opt |= dpkg_opt_purge;
+ dpkg_opt |= dpkg_opt_package_name;
+ break;
+ case 'r':
+ dpkg_opt |= dpkg_opt_remove;
+ dpkg_opt |= dpkg_opt_package_name;
+ break;
+ case 'u': /* Equivalent to --unpack in official dpkg */
+ dpkg_opt |= dpkg_opt_unpack;
+ dpkg_opt |= dpkg_opt_filename;
+ break;
+ default:
+ show_usage();
+ }
+ }
+
+ if ((argc == optind) || (dpkg_opt == 0)) {
+ show_usage();
+ }
+
+ puts("(Reading database ... xxxxx files and directories installed.)");
+ index_status_file("/var/lib/dpkg/status");
+
+ /* Read arguments and store relevant info in structs */
+ deb_file = xmalloc(sizeof(deb_file_t));
+ while (optind < argc) {
+ deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t));
+ if (dpkg_opt & dpkg_opt_filename) {
+ deb_file[deb_count]->filename = xstrdup(argv[optind]);
+ deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
+ if (deb_file[deb_count]->control_file == NULL) {
+ error_msg_and_die("Couldnt extract control file");
+ }
+ package_num = fill_package_struct(deb_file[deb_count]->control_file);
+
+ if (package_num == -1) {
+ error_msg("Invalid control file in %s", argv[optind]);
+ continue;
+ }
+ deb_file[deb_count]->package = (unsigned int) package_num;
+ /* Add the package to the status hashtable */
+ if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) {
+ status_node = (status_node_t *) xmalloc(sizeof(status_node_t));
+ status_node->package = deb_file[deb_count]->package;
+ /* use reinstreq isnt changed to "ok" until the package control info
+ * is written to the status file*/
+ status_node->status = search_name_hashtable("install reinstreq not-installed");
+
+ status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
+ status_hashtable[status_num] = status_node;
+ }
+ }
+ else if (dpkg_opt & dpkg_opt_package_name) {
+ deb_file[deb_count]->filename = NULL;
+ deb_file[deb_count]->control_file = NULL;
+ deb_file[deb_count]->package = search_package_hashtable(
+ search_name_hashtable(argv[optind]),
+ search_name_hashtable("ANY"), VER_ANY);
+ if (package_hashtable[deb_file[deb_count]->package] == NULL) {
+ error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
+ }
+ state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3);
+
+ /* check package status is "installed" */
+ if (dpkg_opt & dpkg_opt_remove) {
+ if ((strcmp(name_hashtable[state_status], "not-installed") == 0) ||
+ (strcmp(name_hashtable[state_status], "config-files") == 0)) {
+ error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
+ }
+ }
+ else if (dpkg_opt & dpkg_opt_purge) {
+ /* if package status is "conf-files" then its ok */
+ if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
+ error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
+ }