- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- break;
- case 'c':
- case 'C':
- if (strcasecmp(field, "Conffiles") == 0) {
- /* Conffiles */
- conffile_list_elt_t *iter;
- char confstr[LINE_LEN];
-
- if (pkg->conffiles.head == NULL) {
- return temp;
- }
-
- len = 14 ;
- for (iter = pkg->conffiles.head; iter; iter = iter->next) {
- if (iter->data->name && iter->data->value) {
- len = len + (strlen(iter->data->name)+strlen(iter->data->value)+5);
- }
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Conffiles:\n", 12);
- for (iter = pkg->conffiles.head; iter; iter = iter->next) {
- if (iter->data->name && iter->data->value) {
- snprintf(confstr, LINE_LEN, "%s %s\n", iter->data->name, iter->data->value);
- strncat(temp, confstr, strlen(confstr));
- }
- }
- } else if (strcasecmp(field, "Conflicts") == 0) {
- int i;
-
- if (pkg->conflicts_count) {
- char conflictstr[LINE_LEN];
- len = 14 ;
- for(i = 0; i < pkg->conflicts_count; i++) {
- len = len + (strlen(pkg->conflicts_str[i])+5);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Conflicts:", 11);
- for(i = 0; i < pkg->conflicts_count; i++) {
- snprintf(conflictstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
- strncat(temp, conflictstr, strlen(conflictstr));
- }
- strncat(temp, "\n", strlen("\n"));
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- break;
- case 'd':
- case 'D':
- if (strcasecmp(field, "Depends") == 0) {
- /* Depends */
- int i;
-
- if (pkg->depends_count) {
- char depstr[LINE_LEN];
- len = 14 ;
- for(i = 0; i < pkg->depends_count; i++) {
- len = len + (strlen(pkg->depends_str[i])+4);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Depends:", 10);
- for(i = 0; i < pkg->depends_count; i++) {
- snprintf(depstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
- strncat(temp, depstr, strlen(depstr));
- }
- strncat(temp, "\n", strlen("\n"));
- }
- } else if (strcasecmp(field, "Description") == 0) {
- /* Description */
- if (pkg->description) {
- temp = (char *)realloc(temp,strlen(pkg->description)+16);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->description)+16), "Description: %s\n", pkg->description);
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- break;
- case 'e':
- case 'E': {
- /* Essential */
- if (pkg->essential) {
- temp = (char *)realloc(temp,16);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (16), "Essential: yes\n");
- }
- }
- break;
- case 'f':
- case 'F': {
- /* Filename */
- if (pkg->filename) {
- temp = (char *)realloc(temp,strlen(pkg->filename)+12);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->filename)+12), "Filename: %s\n", pkg->filename);
- }
- }
- break;
- case 'i':
- case 'I': {
- if (strcasecmp(field, "Installed-Size") == 0) {
- /* Installed-Size */
- temp = (char *)realloc(temp,strlen(pkg->installed_size)+17);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->installed_size)+17), "Installed-Size: %s\n", pkg->installed_size);
- } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
- temp = (char *)realloc(temp,29);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, 29, "Installed-Time: %lu\n", pkg->installed_time);
- }
- }
- break;
- case 'm':
- case 'M': {
- /* Maintainer | MD5sum */
- if (strcasecmp(field, "Maintainer") == 0) {
- /* Maintainer */
- if (pkg->maintainer) {
- temp = (char *)realloc(temp,strlen(pkg->maintainer)+14);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->maintainer)+14), "maintainer: %s\n", pkg->maintainer);
- }
- } else if (strcasecmp(field, "MD5sum") == 0) {
- /* MD5sum */
- if (pkg->md5sum) {
- temp = (char *)realloc(temp,strlen(pkg->md5sum)+11);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->md5sum)+11), "MD5Sum: %s\n", pkg->md5sum);
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- }
- break;
- case 'p':
- case 'P': {
- if (strcasecmp(field, "Package") == 0) {
- /* Package */
- temp = (char *)realloc(temp,strlen(pkg->name)+11);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->name)+11), "Package: %s\n", pkg->name);
- } else if (strcasecmp(field, "Priority") == 0) {
- /* Priority */
- temp = (char *)realloc(temp,strlen(pkg->priority)+12);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->priority)+12), "Priority: %s\n", pkg->priority);
- } else if (strcasecmp(field, "Provides") == 0) {
- /* Provides */
- int i;
-
- if (pkg->provides_count) {
- /* Here we check if the opkg_internal_use_only is used, and we discard it.*/
- for ( i=0; i < pkg->provides_count; i++ ){
- if (strstr(pkg->provides_str[i],"opkg_internal_use_only")!=NULL) {
- memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
- flag_provide_false = 1;
- }
- }
- if ( !flag_provide_false || /* Pigi there is not my trick flag */
- ((flag_provide_false) && (pkg->provides_count > 1))){ /* Pigi There is, but we also have others Provides */
- char provstr[LINE_LEN];
- len = 15;
- for(i = 0; i < pkg->provides_count; i++) {
- len = len + (strlen(pkg->provides_str[i])+5);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Provides:", 12);
- for(i = 0; i < pkg->provides_count; i++) {
- if (strlen(pkg->provides_str[i])>0){;
- snprintf(provstr, LINE_LEN, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
- strncat(temp, provstr, strlen(provstr));
- }
- }
- strncat(temp, "\n", strlen("\n"));
- }
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- }
- break;
- case 'r':
- case 'R': {
- int i;
- /* Replaces | Recommends*/
- if (strcasecmp (field, "Replaces") == 0) {
- if (pkg->replaces_count) {
- char replstr[LINE_LEN];
- len = 14;
- for (i = 0; i < pkg->replaces_count; i++) {
- len = len + (strlen(pkg->replaces_str[i])+5);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Replaces:", 12);
- for (i = 0; i < pkg->replaces_count; i++) {
- snprintf(replstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
- strncat(temp, replstr, strlen(replstr));
- }
- strncat(temp, "\n", strlen("\n"));
- }
- } else if (strcasecmp (field, "Recommends") == 0) {
- if (pkg->recommends_count) {
- char recstr[LINE_LEN];
- len = 15;
- for(i = 0; i < pkg->recommends_count; i++) {
- len = len + (strlen( pkg->recommends_str[i])+5);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Recommends:", 13);
- for(i = 0; i < pkg->recommends_count; i++) {
- snprintf(recstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
- strncat(temp, recstr, strlen(recstr));
- }
- strncat(temp, "\n", strlen("\n"));
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- }
- break;
- case 's':
- case 'S': {
- /* Section | Size | Source | Status | Suggests */
- if (strcasecmp(field, "Section") == 0) {
- /* Section */
- if (pkg->section) {
- temp = (char *)realloc(temp,strlen(pkg->section)+11);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
- }
- } else if (strcasecmp(field, "Size") == 0) {
- /* Size */
- if (pkg->size) {
- temp = (char *)realloc(temp,strlen(pkg->size)+8);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->size)+8), "Size: %s\n", pkg->size);
- }
- } else if (strcasecmp(field, "Source") == 0) {
- /* Source */
- if (pkg->source) {
- temp = (char *)realloc(temp,strlen(pkg->source)+10);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->source)+10), "Source: %s\n", pkg->source);
- }
- } else if (strcasecmp(field, "Status") == 0) {
- /* Status */
- /* Benjamin Pineau note: we should avoid direct usage of
- * strlen(arg) without keeping "arg" for later free()
- */
- char *pflag=pkg_state_flag_to_str(pkg->state_flag);
- char *pstat=pkg_state_status_to_str(pkg->state_status);
- char *pwant=pkg_state_want_to_str(pkg->state_want);
-
- size_t sum_of_sizes = (size_t) ( strlen(pwant)+ strlen(pflag)+ strlen(pstat) + 12 );
- temp = (char *)realloc(temp,sum_of_sizes);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, sum_of_sizes , "Status: %s %s %s\n", pwant, pflag, pstat);
- free(pflag);
- free(pwant);
- if(pstat) /* pfstat can be NULL if ENOMEM */
- free(pstat);
- } else if (strcasecmp(field, "Suggests") == 0) {
- if (pkg->suggests_count) {
- int i;
- char sugstr[LINE_LEN];
- len = 13;
- for(i = 0; i < pkg->suggests_count; i++) {
- len = len + (strlen(pkg->suggests_str[i])+5);
- }
- temp = (char *)realloc(temp,len);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- strncpy(temp, "Suggests:", 10);
- for(i = 0; i < pkg->suggests_count; i++) {
- snprintf(sugstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
- strncat(temp, sugstr, strlen(sugstr));
- }
- strncat(temp, "\n", strlen("\n"));
- }
- } else {
- goto UNKNOWN_FMT_FIELD;
- }
- }
- break;
- case 't':
- case 'T':
- if (strcasecmp(field, "Tags") == 0) {
- /* Tags */
- if (pkg->tags) {
- temp = (char *)realloc(temp,strlen(pkg->tags)+8);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(pkg->tags)+8), "Tags: %s\n", pkg->tags);
- }
- }
- break;
- case 'v':
- case 'V': {
- /* Version */
- char *version = pkg_version_str_alloc(pkg);
- temp = (char *)realloc(temp,strlen(version)+14);
- if ( temp == NULL ){
- fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
- return NULL;
- }
- temp[0]='\0';
- snprintf(temp, (strlen(version)+12), "Version: %s\n", version);
- free(version);
- }
- break;
- default:
- goto UNKNOWN_FMT_FIELD;
- }
-
- if ( strlen(temp)<2 ) {
- temp[0]='\0';
- }
- return temp;
-
- UNKNOWN_FMT_FIELD:
- fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
- if ( strlen(temp)<2 ) {
- temp[0]='\0';
- }
-
- return temp;