char* smime_ask_for_key (char *prompt, char *mailbox, short public)
{
char *fname;
- smime_id *Table;
- long cert_num; /* Will contain the number of certificates.
- * To be able to get it, the .index file will be read twice... */
+ smime_id *table = 0;
+ int table_count;
char index_file[_POSIX_PATH_MAX];
FILE *index;
char buf[LONG_STRING];
snprintf(index_file, sizeof (index_file), "%s/.index",
public ? NONULL(SmimeCertificates) : NONULL(SmimeKeys));
- index = fopen(index_file, "r");
- if (index == NULL)
- {
- mutt_perror (index_file);
- return NULL;
- }
- /* Count Lines */
- cert_num = 0;
- while (!feof(index)) {
- if (fgets(buf, sizeof(buf), index)) cert_num++;
- }
- safe_fclose (&index);
-
FOREVER
{
*qry = 0;
}
/* Read Entries */
cur = 0;
- Table = safe_calloc(cert_num, sizeof (smime_id));
+ table_count = 0;
while (!feof(index)) {
numFields = fscanf (index, MUTT_FORMAT(STRING) " %x.%i " MUTT_FORMAT(STRING), fields[0], &hash,
&hash_suffix, fields[2]);
!mutt_stristr(fields[2], qry))
continue;
- Table[cur].hash = hash;
- Table[cur].suffix = hash_suffix;
- strncpy(Table[cur].email, fields[0], sizeof(Table[cur].email));
- strncpy(Table[cur].nick, fields[2], sizeof(Table[cur].nick));
- Table[cur].trust = *fields[4];
- Table[cur].public = public;
+ ++table_count;
+ safe_realloc(&table, sizeof(smime_id) * table_count);
+ table[cur].hash = hash;
+ table[cur].suffix = hash_suffix;
+ strncpy(table[cur].email, fields[0], sizeof(table[cur].email));
+ strncpy(table[cur].nick, fields[2], sizeof(table[cur].nick));
+ table[cur].trust = *fields[4];
+ table[cur].public = public;
cur++;
}
menu->max = cur;
menu->make_entry = smime_entry;
menu->help = helpstr;
- menu->data = Table;
+ menu->data = table;
menu->title = title;
/* sorting keys might be done later - TODO */
switch (mutt_menuLoop (menu)) {
case OP_GENERIC_SELECT_ENTRY:
cur = menu->current;
- hash = 1;
+ hash = 1;
done = 1;
break;
case OP_EXIT:
break;
}
}
- if (hash) {
- fname = safe_malloc(13); /* Hash + '.' + Suffix + \0 */
- sprintf(fname, "%.8x.%i", Table[cur].hash, Table[cur].suffix);
- }
+ if (table_count && hash)
+ safe_asprintf(&fname, "%.8x.%i", table[cur].hash, table[cur].suffix);
else fname = NULL;
mutt_menuDestroy (&menu);
- FREE (&Table);
+ FREE (&table);
set_option (OPTNEEDREDRAW);
if (fname) return fname;