📄 pkg.c
字号:
return NULL; } buff[0] = '\0'; line = pkg_formatted_field(pkg, "Package"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Version"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Depends"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Recommends"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Suggests"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Provides"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Replaces"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Conflicts"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Status"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Section"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Essential"); /* @@@@ should be removed in future release. *//* I do not agree with this Pigi*/ strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Architecture"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Maintainer"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "MD5sum"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Size"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Filename"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Conffiles"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Source"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Description"); strncat(buff ,line, strlen(line)); free(line); line = pkg_formatted_field(pkg, "Installed-Time"); strncat(buff ,line, strlen(line)); free(line); return buff;}char * pkg_formatted_field(pkg_t *pkg, const char *field ){ static size_t LINE_LEN = 128; char * temp = (char *)malloc(1); int len = 0; int flag_provide_false = 0;/* Pigi: After some discussion with Florian we decided to modify the full procedure in dynamic memory allocation. This should avoid any other segv in this area ( except for bugs )*/ if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) { goto UNKNOWN_FMT_FIELD; } temp[0]='\0'; switch (field[0]) { case 'a': case 'A': if (strcasecmp(field, "Architecture") == 0) { /* Architecture */ if (pkg->architecture) { temp = (char *)realloc(temp,strlen(pkg->architecture)+17); if ( temp == NULL ){ fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; } temp[0]='\0'; snprintf(temp, (strlen(pkg->architecture)+17), "Architecture: %s\n", pkg->architecture); } } 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 ipkg_internal_use_only is used, and we discard it.*/ for ( i=0; i < pkg->provides_count; i++ ){ if (strstr(pkg->provides_str[i],"ipkg_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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -