diff options
author | Werner Koch <wk@gnupg.org> | 2020-10-20 11:52:16 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2020-10-20 12:15:56 +0200 |
commit | 390497ea115e1aca93feec297a5bd6ae7b1ba6dd (patch) | |
tree | 675448ff809e3f333bba4918e4787eacda7f8190 /tools | |
parent | Replace all calls to access by gnupg_access (diff) | |
download | gnupg2-390497ea115e1aca93feec297a5bd6ae7b1ba6dd.tar.xz gnupg2-390497ea115e1aca93feec297a5bd6ae7b1ba6dd.zip |
Replace most of the remaining stdio calls by estream calls.
--
We need to use es_fopen on Windows to cope with non-ascii file names.
This is quite a large but fortunately straightforward change. At a
very few places we keep using stdio (for example due to the use of
popen).
GnuPG-bug-id: 5098
Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gpg-check-pattern.c | 24 | ||||
-rw-r--r-- | tools/gpg-connect-agent.c | 41 | ||||
-rw-r--r-- | tools/wks-util.c | 2 |
3 files changed, 33 insertions, 34 deletions
diff --git a/tools/gpg-check-pattern.c b/tools/gpg-check-pattern.c index b3e1d5268..d798dbe2e 100644 --- a/tools/gpg-check-pattern.c +++ b/tools/gpg-check-pattern.c @@ -231,7 +231,7 @@ main (int argc, char **argv ) static char * read_file (const char *fname, size_t *r_length) { - FILE *fp; + estream_t fp; char *buf; size_t buflen; @@ -239,10 +239,8 @@ read_file (const char *fname, size_t *r_length) { size_t nread, bufsize = 0; - fp = stdin; -#ifdef HAVE_DOSISH_SYSTEM - setmode ( fileno(fp) , O_BINARY ); -#endif + fp = es_stdin; + es_set_binary (fp); buf = NULL; buflen = 0; #define NCHUNK 8192 @@ -254,8 +252,8 @@ read_file (const char *fname, size_t *r_length) else buf = xrealloc (buf, bufsize+1); - nread = fread (buf+buflen, 1, NCHUNK, fp); - if (nread < NCHUNK && ferror (fp)) + nread = es_fread (buf+buflen, 1, NCHUNK, fp); + if (nread < NCHUNK && es_ferror (fp)) { log_error ("error reading '[stdin]': %s\n", strerror (errno)); xfree (buf); @@ -271,30 +269,30 @@ read_file (const char *fname, size_t *r_length) { struct stat st; - fp = fopen (fname, "rb"); + fp = es_fopen (fname, "rb"); if (!fp) { log_error ("can't open '%s': %s\n", fname, strerror (errno)); return NULL; } - if (fstat (fileno(fp), &st)) + if (fstat (es_fileno (fp), &st)) { log_error ("can't stat '%s': %s\n", fname, strerror (errno)); - fclose (fp); + es_fclose (fp); return NULL; } buflen = st.st_size; buf = xmalloc (buflen+1); - if (fread (buf, buflen, 1, fp) != 1) + if (es_fread (buf, buflen, 1, fp) != 1) { log_error ("error reading '%s': %s\n", fname, strerror (errno)); - fclose (fp); + es_fclose (fp); xfree (buf); return NULL; } - fclose (fp); + es_fclose (fp); } buf[buflen] = 0; *r_length = buflen; diff --git a/tools/gpg-connect-agent.c b/tools/gpg-connect-agent.c index 6ec08c538..142f69143 100644 --- a/tools/gpg-connect-agent.c +++ b/tools/gpg-connect-agent.c @@ -182,7 +182,7 @@ typedef struct loopline_s *loopline_t; static pid_t server_pid = (pid_t)(-1); /* The current datasink file or NULL. */ -static FILE *current_datasink; +static estream_t current_datasink; /* A list of open file descriptors. */ static struct @@ -892,7 +892,7 @@ clear_definq (void) static void do_sendfd (assuan_context_t ctx, char *line) { - FILE *fp; + estream_t fp; char *name, *mode, *p; int rc, fd; @@ -918,14 +918,14 @@ do_sendfd (assuan_context_t ctx, char *line) } /* Open and send. */ - fp = fopen (name, mode); + fp = es_fopen (name, mode); if (!fp) { log_error ("can't open '%s' in \"%s\" mode: %s\n", name, mode, strerror (errno)); return; } - fd = fileno (fp); + fd = es_fileno (fp); if (opt.verbose) log_error ("file '%s' opened in \"%s\" mode, fd=%d\n", @@ -934,7 +934,7 @@ do_sendfd (assuan_context_t ctx, char *line) rc = assuan_sendfd (ctx, INT2FD (fd) ); if (rc) log_error ("sending descriptor %d failed: %s\n", fd, gpg_strerror (rc)); - fclose (fp); + es_fclose (fp); } @@ -950,7 +950,7 @@ do_recvfd (assuan_context_t ctx, char *line) static void do_open (char *line) { - FILE *fp; + estream_t fp; char *varname, *name, *mode, *p; int fd; @@ -994,14 +994,14 @@ do_open (char *line) } /* Open and send. */ - fp = fopen (name, mode); + fp = es_fopen (name, mode); if (!fp) { log_error ("can't open '%s' in \"%s\" mode: %s\n", name, mode, strerror (errno)); return; } - fd = dup (fileno (fp)); + fd = dup (es_fileno (fp)); if (fd >= 0 && fd < DIM (open_fd_table)) { open_fd_table[fd].inuse = 1; @@ -1051,7 +1051,7 @@ do_open (char *line) if (fd != -1) close (fd); /* Table was full. */ } - fclose (fp); + es_fclose (fp); } @@ -1605,17 +1605,17 @@ main (int argc, char **argv) if (current_datasink) { - if (current_datasink != stdout) - fclose (current_datasink); + if (current_datasink != es_stdout) + es_fclose (current_datasink); current_datasink = NULL; } tmpline = opt.enable_varsubst? substitute_line (p) : NULL; fname = tmpline? tmpline : p; if (fname && !strcmp (fname, "-")) - current_datasink = stdout; + current_datasink = es_stdout; else if (fname && *fname) { - current_datasink = fopen (fname, "wb"); + current_datasink = es_fopen (fname, "wb"); if (!current_datasink) log_error ("can't open '%s': %s\n", fname, strerror (errno)); @@ -1964,6 +1964,7 @@ handle_inquire (assuan_context_t ctx, char *line) { const char *name; definq_t d; + /* FIXME: Due to the use of popen we can't easily switch to estream. */ FILE *fp = NULL; char buffer[1024]; int rc, n; @@ -2115,7 +2116,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) } else c = *s; - putc (c, current_datasink); + es_putc (c, current_datasink); } } else if (opt.hex) @@ -2186,7 +2187,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) { if (need_lf) { - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) putchar ('\n'); need_lf = 0; } @@ -2195,7 +2196,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) && line[0] == 'S' && (line[1] == '\0' || line[1] == ' ')) { - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) { fwrite (line, linelen, 1, stdout); putchar ('\n'); @@ -2205,7 +2206,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) { fwrite (line, linelen, 1, stdout); putchar ('\n'); @@ -2223,7 +2224,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) if (!errval) errval = -1; set_int_var ("?", errval); - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) { fwrite (line, linelen, 1, stdout); putchar ('\n'); @@ -2237,7 +2238,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) { fwrite (line, linelen, 1, stdout); putchar ('\n'); @@ -2249,7 +2250,7 @@ read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr) && line[0] == 'E' && line[1] == 'N' && line[2] == 'D' && (line[3] == '\0' || line[3] == ' ')) { - if (!current_datasink || current_datasink != stdout) + if (!current_datasink || current_datasink != es_stdout) { fwrite (line, linelen, 1, stdout); putchar ('\n'); diff --git a/tools/wks-util.c b/tools/wks-util.c index 1706c694a..621be7920 100644 --- a/tools/wks-util.c +++ b/tools/wks-util.c @@ -912,7 +912,7 @@ ensure_policy_file (const char *addrspec) { err = gpg_error_from_syserror (); if (gpg_err_code (err) == GPG_ERR_EEXIST) - err = 0; /* Was created between the access() and fopen(). */ + err = 0; /* Was created between the gnupg_access() and es_fopen(). */ else log_error ("domain %s: error creating '%s': %s\n", domain, fname, gpg_strerror (err)); |