Buenos días, he tratado por mis propios medios tratar de solucionar este problema, pero no doy con una solución. Trataré de explicarlo. El problema es, tengo un bucle que recorre una estructura, hace un parseo con una función y agrega datos, funciona perfecto en una primera llamada que le hago, pero a la segunda, el bucle se queda parado.
La cabecera
Código C:
Ver original#define PORTS_DIR "/usr/ports/"
#define DB_DIR "/var/lib/pkg/db"
struct deppkg
{
char n_pkg[256];
char d_pkg[PATH_MAX];
int r_list; /* is read? */
int i_file; /* in other pkgfile of a pkg is installed? */
struct deppkg * pkgnext;
};
struct deppkg * d_firts = NULL;
struct deppkg * d_last = NULL;
int
add_dep (const char * n_pkg, const char * d_pkg)
{
struct deppkg * aux;
aux
= malloc(sizeof(struct deppkg
));
if (aux == NULL)
return -1;
aux->pkgnext = NULL;
aux->n_pkg[0] = '\0';
aux->d_pkg[0] = '\0';
aux->r_list = 0;
aux->i_file = 0;
if (d_firts == NULL)
{
d_firts = aux;
d_last = aux;
}
else
{
d_last->pkgnext = aux;
d_last = aux;
}
return 0;
}
void
clean_dep ()
{
struct deppkg * curr = d_firts;
struct deppkg * aux;
while (curr != NULL)
{
aux = curr->pkgnext;
curr = aux;
}
}
int
parse_dep (const char * d_pkg, char *r_pkg, size_t len)
{
if (d_pkg == 0 || r_pkg == 0 || len <= 0)
return -1;
char * buffer_next;
char buffer[3072];
int count = 0;
static int num_m = 1;
FILE * pkgfile;
pkgfile
= fopen(d_pkg
, "r");
if (pkgfile == NULL)
{
stderr,
"pkgrmd >> Error opening «%s»: %s\n",
d_pkg,
);
return -1;
}
while (fgets(buffer
, sizeof(buffer
), pkgfile
) != NULL
) {
if (buffer[0] != '#')
break;
if (strncmp(buffer
, "# Depends on:", 13) == 0) {
buffer_next
= strchr(buffer
, ':'); buffer_next += num_m;
if (*buffer_next == '\0')
break;
buffer_next++;
num_m++;
}
while (*buffer_next != '\0') {
|| *buffer_next == '-')
{
r_pkg[count] = *buffer_next;
count++;
}
if (*buffer_next == ' ' || *buffer_next == ','
|| *buffer_next == '\n')
{
return 0;
}
buffer_next++;
num_m++;
}
}
}
/* Test... */
num_m = 1;
return -1;
}
int
pkg_is_installed (const char * n_pkg)
{
return -1;
char pkg_fix[254];
char buffer[256];
int len;
len
= snprintf(pkg_fix
, sizeof(pkg_fix
), "%s\n", n_pkg
);
if (len <= 0)
return -1;
FILE * db;
if (db == NULL)
{
stderr,
"pkgrmd >> Error opening «%s»: %s\n",
DB_DIR,
);
return -1;
}
while (fgets(buffer
, sizeof(buffer
), db
) != NULL
) if (strcmp(buffer
, pkg_fix
) == 0) {
return 0;
}
return -1;
}
int
pkg_dir_inst (const char * n_pkg, char * d_pkg, size_t len)
{
if (n_pkg == 0 || len <= 0)
return -1;
char * verify = NULL;
char buffer[PATH_MAX];
int len_s;
DIR * ports_dir;
struct dirent *dir_s;
ports_dir = opendir(PORTS_DIR);
if (ports_dir == NULL)
{
stderr,
"pkgrmd >> Error opening «%s»: %s\n",
PORTS_DIR,
);
return -1;
}
while ((dir_s = readdir(ports_dir)) != NULL)
{
if (strcmp(dir_s
->d_name
, ".") == 0) continue; else if (strcmp(dir_s
->d_name
, "..") == 0) continue;
if (dir_s->d_type == DT_DIR) {
buffer,
sizeof(buffer),
"%s%s/%s/Pkgfile",
PORTS_DIR,
dir_s->d_name,
n_pkg
);
if (len_s <= 0)
return -1;
if (access(buffer, F_OK) != -1) {
verify
= strncpy(d_pkg
, buffer
, len
); break;
}
}
}
if (verify == NULL)
{
closedir(ports_dir);
return -1;
}
closedir(ports_dir);
return 0;
}
El cuerpo
Código C:
Ver original#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include "pkgrmd.h"
int main (int argc, char * argv[])
{
if (argc > 1)
{
char d_pkg[PATH_MAX] = {0};
char n_pkg[255] = {0};
char r_pkg[255] = {0};
char d_pkg_buff[PATH_MAX] = {0};
strncat(n_pkg
, argv
[1], sizeof(n_pkg
)-1);
if (pkg_is_installed(n_pkg) != 0)
{
fprintf(stderr
, "pkgrmd >> This package is not installed\n");
return EXIT_FAILURE;
}
if (pkg_dir_inst(n_pkg, d_pkg, sizeof(d_pkg)) != 0)
{
fprintf(stderr
,"pkgrmd >> An error was encountered\n");
return EXIT_FAILURE;
}
while (parse_dep(d_pkg, r_pkg, sizeof(r_pkg)) != -1)
{
if (pkg_dir_inst(r_pkg, d_pkg_buff, sizeof(d_pkg_buff)) != 0)
continue;
if (add_dep(r_pkg, d_pkg_buff) != 0)
continue;
}
/* Has two structures, the first is NULL if not have data */
if (d_firts == NULL)
{
"pkgrmd >> The package %s not have depends.\n", n_pkg);
return EXIT_FAILURE;
}
struct deppkg * aux = d_firts;
while (aux != NULL)
{
if (aux->r_list != 0)
continue;
if (pkg_dir_inst(aux->n_pkg,
d_pkg_buff,
sizeof(d_pkg_buff)) != 0)
continue;
while (parse_dep(d_pkg_buff, r_pkg, sizeof(r_pkg)) != -1)
{
// Aquí...
add_dep(r_pkg, "tes"t);
}
// Luego de arriba, aquí no se ejecuta.
aux = aux->pkgnext;
}
clean_dep();
}
else
{
stderr,
"pkgrmd >> syntax: pkgrmd <name_package>\n"
);
}
return 0;
}
Donde dice 'aquí' es la parte que donde se queda parado aún así el bucle haya terminado.
Muchas gracias, y espero sus respuestas. :)