From d2a70fd8348d6c11d1960caf2afe0701833dad6a Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 19 Feb 2015 17:22:27 +0100 Subject: [PATCH] gpg: Replace remaining uses of stdio by estream. * g10/sign.c (sign_file): Use log_printf instead of stderr. * g10/tdbdump.c (export_ownertrust): Use estream fucntions. (import_ownertrust): Ditto. * g10/tdbio.c (tdbio_dump_record): Ditto. Change arg to estream_t. -- Reported-by: Guilhem Moulin Needed for unattended key edits with --status-fd, because since 2.1 status prompts are preceded by es_fflush (in cpr.c:do_get_from_fd) not fflush(3), so the standard output may not be flushed before each prompt. (Which breaks scripts using select(2) to multiplex between the standard and status outputs.) His patch only affected print_and_check_one_sig_colon() but there are many more places where stdio and estream are mixed. This patch now replaces most of them in g10/. At some places stdio is still used, but that is local to a function and should not have side effects. Signed-off-by: Werner Koch --- g10/getkey.c | 2 +- g10/gpg.c | 42 +++++++++++++++----------------- g10/keyedit.c | 20 +++++++-------- g10/keyserver.c | 4 +-- g10/mainproc.c | 8 +++--- g10/misc.c | 4 +-- g10/sig-check.c | 2 +- g10/sign.c | 4 +-- g10/tdbdump.c | 60 +++++++++++++++++++++++---------------------- g10/tdbio.c | 65 ++++++++++++++++++++++++++----------------------- g10/tdbio.h | 2 +- g10/trustdb.c | 20 +++++++-------- 12 files changed, 118 insertions(+), 115 deletions(-) diff --git a/g10/getkey.c b/g10/getkey.c index 30c454b21..76ee493e7 100644 --- a/g10/getkey.c +++ b/g10/getkey.c @@ -116,7 +116,7 @@ print_stats () for (i = 0; i < DIM (lkup_stats); i++) { if (lkup_stats[i].any) - fprintf (stderr, + es_fprintf (es_stderr, "lookup stats: mode=%-2d ok=%-6d nokey=%-6d err=%-6d\n", i, lkup_stats[i].okay_count, diff --git a/g10/gpg.c b/g10/gpg.c index 9a6b104c2..b8d621dce 100644 --- a/g10/gpg.c +++ b/g10/gpg.c @@ -1038,7 +1038,7 @@ build_list (const char *text, char letter, static void wrong_args( const char *text) { - fprintf (stderr, _("usage: %s [options] %s\n"), GPG_NAME, text); + es_fprintf (es_stderr, _("usage: %s [options] %s\n"), GPG_NAME, text); g10_exit(2); } @@ -4021,7 +4021,7 @@ main (int argc, char **argv) mpi_print (es_stdout, generate_elg_prime( 1, atoi(argv[1]), atoi(argv[2]), NULL,&factors ), 1); - putchar('\n'); + es_putc ('\n', es_stdout); mpi_print (es_stdout, factors[0], 1 ); /* print q */ } else if( mode == 4 && argc == 3 ) { @@ -4029,13 +4029,13 @@ main (int argc, char **argv) mpi_print (es_stdout, generate_elg_prime( 0, atoi(argv[1]), atoi(argv[2]), g, NULL ), 1); - putchar('\n'); + es_putc ('\n', es_stdout); mpi_print (es_stdout, g, 1 ); mpi_free (g); } else wrong_args("--gen-prime mode bits [qbits] "); - putchar('\n'); + es_putc ('\n', es_stdout); } #endif wrong_args("--gen-prime not yet supported "); @@ -4064,21 +4064,21 @@ main (int argc, char **argv) #endif if (opt.armor) { char *tmp = make_radix64_string (p, n); - fputs (tmp, stdout); + es_fputs (tmp, es_stdout); xfree (tmp); if (n%3 == 1) - putchar ('='); + es_putc ('=', es_stdout); if (n%3) - putchar ('='); + es_putc ('=', es_stdout); } else { - fwrite( p, n, 1, stdout ); + es_fwrite( p, n, 1, es_stdout ); } xfree(p); if( !endless ) count -= n; } if (opt.armor) - putchar ('\n'); + es_putc ('\n', es_stdout); } break; @@ -4298,7 +4298,7 @@ print_hex (gcry_md_hd_t md, int algo, const char *fname) if (indent>40) { - printf("\n"); + es_printf ("\n"); indent=0; } @@ -4396,24 +4396,22 @@ print_hashline( gcry_md_hd_t md, int algo, const char *fname ) static void print_mds( const char *fname, int algo ) { - FILE *fp; + estream_t fp; char buf[1024]; size_t n; gcry_md_hd_t md; if (!fname) { - fp = stdin; -#ifdef HAVE_DOSISH_SYSTEM - setmode ( fileno(fp) , O_BINARY ); -#endif + fp = es_stdin; + es_set_binary (fp); } else { - fp = fopen (fname, "rb" ); - if (fp && is_secured_file (fileno (fp))) + fp = es_fopen (fname, "rb" ); + if (fp && is_secured_file (es_fileno (fp))) { - fclose (fp); + es_fclose (fp); fp = NULL; gpg_err_set_errno (EPERM); } @@ -4444,10 +4442,10 @@ print_mds( const char *fname, int algo ) gcry_md_enable (md, GCRY_MD_SHA512); } - while ((n=fread (buf, 1, DIM(buf), fp))) + while ((n=es_fread (buf, 1, DIM(buf), fp))) gcry_md_write (md, buf, n); - if (ferror(fp)) + if (es_ferror(fp)) log_error ("%s: %s\n", fname?fname:"[stdin]", strerror(errno)); else { @@ -4497,8 +4495,8 @@ print_mds( const char *fname, int algo ) } gcry_md_close (md); - if (fp != stdin) - fclose (fp); + if (fp != es_stdin) + es_fclose (fp); } diff --git a/g10/keyedit.c b/g10/keyedit.c index f283e5538..91f5dae56 100644 --- a/g10/keyedit.c +++ b/g10/keyedit.c @@ -158,23 +158,23 @@ print_and_check_one_sig_colon (KBNODE keyblock, KBNODE node, if (sigrc != '?' || print_without_key) { - printf ("sig:%c::%d:%08lX%08lX:%lu:%lu:", - sigrc, sig->pubkey_algo, (ulong) sig->keyid[0], - (ulong) sig->keyid[1], (ulong) sig->timestamp, - (ulong) sig->expiredate); + es_printf ("sig:%c::%d:%08lX%08lX:%lu:%lu:", + sigrc, sig->pubkey_algo, (ulong) sig->keyid[0], + (ulong) sig->keyid[1], (ulong) sig->timestamp, + (ulong) sig->expiredate); if (sig->trust_depth || sig->trust_value) - printf ("%d %d", sig->trust_depth, sig->trust_value); + es_printf ("%d %d", sig->trust_depth, sig->trust_value); - printf (":"); + es_printf (":"); if (sig->trust_regexp) es_write_sanitized (es_stdout, sig->trust_regexp, strlen (sig->trust_regexp), ":", NULL); - printf ("::%02x%c\n", sig->sig_class, - sig->flags.exportable ? 'x' : 'l'); + es_printf ("::%02x%c\n", sig->sig_class, + sig->flags.exportable ? 'x' : 'l'); if (opt.show_subpackets) print_subpackets_colon (sig); @@ -4735,11 +4735,11 @@ ask_revoke_sig (KBNODE keyblock, KBNODE node) printf ("uat:::::::::%u %lu", uid->numattribs, uid->attrib_len); else { - printf ("uid:::::::::"); + es_printf ("uid:::::::::"); es_write_sanitized (es_stdout, uid->name, uid->len, ":", NULL); } - printf ("\n"); + es_printf ("\n"); print_and_check_one_sig_colon (keyblock, node, NULL, NULL, NULL, NULL, 1); diff --git a/g10/keyserver.c b/g10/keyserver.c index 035cd031f..8bcb82765 100644 --- a/g10/keyserver.c +++ b/g10/keyserver.c @@ -1488,12 +1488,12 @@ keyserver_search (ctrl_t ctrl, strlist_t tokens) /* Write global options */ /* for(temp=opt.keyserver_options.other;temp;temp=temp->next) */ - /* fprintf(spawn->tochild,"OPTION %s\n",temp->d); */ + /* es_fprintf(spawn->tochild,"OPTION %s\n",temp->d); */ /* Write per-keyserver options */ /* for(temp=keyserver->options;temp;temp=temp->next) */ - /* fprintf(spawn->tochild,"OPTION %s\n",temp->d); */ + /* es_fprintf(spawn->tochild,"OPTION %s\n",temp->d); */ { membuf_t mb; diff --git a/g10/mainproc.c b/g10/mainproc.c index 8ff84b738..e0dba13e0 100644 --- a/g10/mainproc.c +++ b/g10/mainproc.c @@ -930,15 +930,15 @@ print_userid (PACKET *pkt) if (pkt->pkttype != PKT_USER_ID) { - printf ("ERROR: unexpected packet type %d", pkt->pkttype ); + es_printf ("ERROR: unexpected packet type %d", pkt->pkttype ); return; } if (opt.with_colons) { if (pkt->pkt.user_id->attrib_data) - printf("%u %lu", - pkt->pkt.user_id->numattribs, - pkt->pkt.user_id->attrib_len); + es_printf("%u %lu", + pkt->pkt.user_id->numattribs, + pkt->pkt.user_id->attrib_len); else es_write_sanitized (es_stdout, pkt->pkt.user_id->name, pkt->pkt.user_id->len, ":", NULL); diff --git a/g10/misc.c b/g10/misc.c index 37582afa3..276ff0a1a 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -1394,8 +1394,8 @@ parse_options(char *str,unsigned int *options, for(i=0;opts[i].name;i++) if(opts[i].help) - printf("%s%*s%s\n",opts[i].name, - maxlen+2-(int)strlen(opts[i].name),"",_(opts[i].help)); + es_printf("%s%*s%s\n",opts[i].name, + maxlen+2-(int)strlen(opts[i].name),"",_(opts[i].help)); g10_exit(0); } diff --git a/g10/sig-check.c b/g10/sig-check.c index 27c51fdb5..f8ba98b83 100644 --- a/g10/sig-check.c +++ b/g10/sig-check.c @@ -427,7 +427,7 @@ check_revocation_keys(PKT_public_key *pk,PKT_signature *sig) busy=1; - /* printf("looking at %08lX with a sig from %08lX\n",(ulong)pk->keyid[1], + /* es_printf("looking at %08lX with a sig from %08lX\n",(ulong)pk->keyid[1], (ulong)sig->keyid[1]); */ /* is the issuer of the sig one of our revokers? */ diff --git a/g10/sign.c b/g10/sign.c index d2f4abf18..afc117e7d 100644 --- a/g10/sign.c +++ b/g10/sign.c @@ -1021,7 +1021,7 @@ sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr, } handle_progress (pfx, inp, sl->d); if( opt.verbose ) - fprintf(stderr, " '%s'", sl->d ); + log_printf (" '%s'", sl->d ); if(opt.textmode) { memset( &tfx, 0, sizeof tfx); @@ -1033,7 +1033,7 @@ sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr, iobuf_close(inp); inp = NULL; } if( opt.verbose ) - putc( '\n', stderr ); + log_printf ("\n"); } else { /* read, so that the filter can calculate the digest */ diff --git a/g10/tdbdump.c b/g10/tdbdump.c index c12e9b7a3..bf9f3875b 100644 --- a/g10/tdbdump.c +++ b/g10/tdbdump.c @@ -78,12 +78,12 @@ list_trustdb( const char *username ) ulong recnum; int i; - printf("TrustDB: %s\n", tdbio_get_dbname() ); + es_printf ("TrustDB: %s\n", tdbio_get_dbname() ); for(i=9+strlen(tdbio_get_dbname()); i > 0; i-- ) - putchar('-'); - putchar('\n'); + es_putc ('-', es_stdout); + es_putc ('\n', es_stdout); for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ ) - tdbio_dump_record( &rec, stdout ); + tdbio_dump_record (&rec, es_stdout); } } @@ -97,23 +97,25 @@ list_trustdb( const char *username ) void export_ownertrust() { - TRUSTREC rec; - ulong recnum; - int i; - byte *p; + TRUSTREC rec; + ulong recnum; + int i; + byte *p; - init_trustdb(); - printf(_("# List of assigned trustvalues, created %s\n" - "# (Use \"gpg --import-ownertrust\" to restore them)\n"), - asctimestamp( make_timestamp() ) ); - for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ ) { - if( rec.rectype == RECTYPE_TRUST ) { - if( !rec.r.trust.ownertrust ) - continue; - p = rec.r.trust.fingerprint; - for(i=0; i < 20; i++, p++ ) - printf("%02X", *p ); - printf(":%u:\n", (unsigned int)rec.r.trust.ownertrust ); + init_trustdb(); + es_printf (_("# List of assigned trustvalues, created %s\n" + "# (Use \"gpg --import-ownertrust\" to restore them)\n"), + asctimestamp( make_timestamp() ) ); + for (recnum=0; !tdbio_read_record (recnum, &rec, 0); recnum++ ) + { + if (rec.rectype == RECTYPE_TRUST) + { + if (!rec.r.trust.ownertrust) + continue; + p = rec.r.trust.fingerprint; + for (i=0; i < 20; i++, p++ ) + es_printf("%02X", *p ); + es_printf (":%u:\n", (unsigned int)rec.r.trust.ownertrust ); } } } @@ -122,7 +124,7 @@ export_ownertrust() void import_ownertrust( const char *fname ) { - FILE *fp; + estream_t fp; int is_stdin=0; char line[256]; char *p; @@ -134,24 +136,24 @@ import_ownertrust( const char *fname ) init_trustdb(); if( iobuf_is_pipe_filename (fname) ) { - fp = stdin; + fp = es_stdin; fname = "[stdin]"; is_stdin = 1; } - else if( !(fp = fopen( fname, "r" )) ) { + else if( !(fp = es_fopen( fname, "r" )) ) { log_error ( _("can't open '%s': %s\n"), fname, strerror(errno) ); return; } - if (is_secured_file (fileno (fp))) + if (is_secured_file (es_fileno (fp))) { - fclose (fp); + es_fclose (fp); gpg_err_set_errno (EPERM); log_error (_("can't open '%s': %s\n"), fname, strerror(errno) ); return; } - while( fgets( line, DIM(line)-1, fp ) ) { + while (es_fgets (line, DIM(line)-1, fp)) { TRUSTREC rec; if( !*line || *line == '#' ) @@ -216,10 +218,10 @@ import_ownertrust( const char *fname ) log_error (_("error finding trust record in '%s': %s\n"), fname, gpg_strerror (rc)); } - if( ferror(fp) ) + if (es_ferror (fp)) log_error ( _("read error in '%s': %s\n"), fname, strerror(errno) ); - if( !is_stdin ) - fclose(fp); + if (!is_stdin) + es_fclose (fp); if (any) { diff --git a/g10/tdbio.c b/g10/tdbio.c index 91ee3ab78..69438b423 100644 --- a/g10/tdbio.c +++ b/g10/tdbio.c @@ -1141,59 +1141,62 @@ update_trusthashtbl( TRUSTREC *tr ) void -tdbio_dump_record( TRUSTREC *rec, FILE *fp ) +tdbio_dump_record (TRUSTREC *rec, estream_t fp) { int i; ulong rnum = rec->recnum; - fprintf(fp, "rec %5lu, ", rnum ); + es_fprintf ( fp, "rec %5lu, ", rnum ); switch( rec->rectype ) { - case 0: fprintf(fp, "blank\n"); + case 0: + es_fprintf (fp, "blank\n"); break; - case RECTYPE_VER: fprintf(fp, - "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n", - rec->r.ver.trusthashtbl, - rec->r.ver.firstfree, - rec->r.ver.marginals, - rec->r.ver.completes, - rec->r.ver.cert_depth, - rec->r.ver.trust_model, - rec->r.ver.min_cert_level, - rec->r.ver.nextcheck, - strtimestamp(rec->r.ver.nextcheck) - ); + case RECTYPE_VER: + es_fprintf (fp, + "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n", + rec->r.ver.trusthashtbl, + rec->r.ver.firstfree, + rec->r.ver.marginals, + rec->r.ver.completes, + rec->r.ver.cert_depth, + rec->r.ver.trust_model, + rec->r.ver.min_cert_level, + rec->r.ver.nextcheck, + strtimestamp(rec->r.ver.nextcheck) + ); break; - case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next ); + case RECTYPE_FREE: + es_fprintf (fp, "free, next=%lu\n", rec->r.free.next ); break; case RECTYPE_HTBL: - fprintf(fp, "htbl,"); + es_fprintf (fp, "htbl,"); for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) - fprintf(fp, " %lu", rec->r.htbl.item[i] ); - putc('\n', fp); + es_fprintf (fp, " %lu", rec->r.htbl.item[i] ); + es_putc ('\n', fp); break; case RECTYPE_HLST: - fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next ); + es_fprintf (fp, "hlst, next=%lu,", rec->r.hlst.next ); for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) - fprintf(fp, " %lu", rec->r.hlst.rnum[i] ); - putc('\n', fp); + es_fprintf (fp, " %lu", rec->r.hlst.rnum[i] ); + es_putc ('\n', fp); break; case RECTYPE_TRUST: - fprintf(fp, "trust "); + es_fprintf (fp, "trust "); for(i=0; i < 20; i++ ) - fprintf(fp, "%02X", rec->r.trust.fingerprint[i] ); - fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust, - rec->r.trust.depth, rec->r.trust.validlist); + es_fprintf (fp, "%02X", rec->r.trust.fingerprint[i] ); + es_fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust, + rec->r.trust.depth, rec->r.trust.validlist); break; case RECTYPE_VALID: - fprintf(fp, "valid "); + es_fprintf (fp, "valid "); for(i=0; i < 20; i++ ) - fprintf(fp, "%02X", rec->r.valid.namehash[i] ); - fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity, - rec->r.valid.next); + es_fprintf(fp, "%02X", rec->r.valid.namehash[i] ); + es_fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity, + rec->r.valid.next); break; default: - fprintf(fp, "unknown type %d\n", rec->rectype ); + es_fprintf (fp, "unknown type %d\n", rec->rectype ); break; } } diff --git a/g10/tdbio.h b/g10/tdbio.h index 4f37de44e..d2595180c 100644 --- a/g10/tdbio.h +++ b/g10/tdbio.h @@ -95,7 +95,7 @@ typedef struct trust_record TRUSTREC; int tdbio_update_version_record(void); int tdbio_set_dbname( const char *new_dbname, int create, int *r_nofile); const char *tdbio_get_dbname(void); -void tdbio_dump_record( TRUSTREC *rec, FILE *fp ); +void tdbio_dump_record( TRUSTREC *rec, estream_t fp ); int tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected ); int tdbio_write_record( TRUSTREC *rec ); int tdbio_db_matches_options(void); diff --git a/g10/trustdb.c b/g10/trustdb.c index 08f6cf4a1..f0b550130 100644 --- a/g10/trustdb.c +++ b/g10/trustdb.c @@ -1068,7 +1068,7 @@ get_validity_counts (PKT_public_key *pk, PKT_user_id *uid) { uid->help_marginal_count=vrec.r.valid.marginal_count; uid->help_full_count=vrec.r.valid.full_count; - /* printf("Fetched marginal %d, full %d\n",uid->help_marginal_count,uid->help_full_count); */ + /* es_printf("Fetched marginal %d, full %d\n",uid->help_marginal_count,uid->help_full_count); */ break; } @@ -1197,8 +1197,8 @@ dump_key_array (int depth, struct key_array *keys) u32 kid[2]; keyid_from_pk(node->pkt->pkt.public_key, kid); - printf ("%d:%08lX%08lX:K::%c::::\n", - depth, (ulong)kid[0], (ulong)kid[1], '?'); + es_printf ("%d:%08lX%08lX:K::%c::::\n", + depth, (ulong)kid[0], (ulong)kid[1], '?'); for (; node; node = node->next) { @@ -1208,15 +1208,15 @@ dump_key_array (int depth, struct key_array *keys) if (len > 30) len = 30; - printf ("%d:%08lX%08lX:U:::%c:::", - depth, (ulong)kid[0], (ulong)kid[1], - (node->flag & 4)? 'f': - (node->flag & 2)? 'm': - (node->flag & 1)? 'q':'-'); + es_printf ("%d:%08lX%08lX:U:::%c:::", + depth, (ulong)kid[0], (ulong)kid[1], + (node->flag & 4)? 'f': + (node->flag & 2)? 'm': + (node->flag & 1)? 'q':'-'); es_write_sanitized (es_stdout, node->pkt->pkt.user_id->name, len, ":", NULL); - putchar (':'); - putchar ('\n'); + es_putc (':', es_stdout); + es_putc ('\n', es_stdout); } } }