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>
This commit is contained in:
Werner Koch 2020-10-20 11:52:16 +02:00
parent c94ee1386e
commit 390497ea11
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
23 changed files with 220 additions and 216 deletions

View File

@ -215,7 +215,7 @@ struct ssh_key_type_spec
struct ssh_control_file_s
{
char *fname; /* Name of the file. */
FILE *fp; /* This is never NULL. */
estream_t fp; /* This is never NULL. */
int lnr; /* The current line number. */
struct {
int valid; /* True if the data of this structure is valid. */
@ -856,7 +856,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
}
/* FIXME: With "a+" we are not able to check whether this will
be created and thus the blurb needs to be written first. */
cf->fp = fopen (cf->fname, append? "a+":"r");
cf->fp = es_fopen (cf->fname, append? "a+":"r");
if (!cf->fp && errno == ENOENT)
{
estream_t stream = es_fopen (cf->fname, "wx,mode=-rw-r");
@ -869,7 +869,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
}
es_fputs (sshcontrolblurb, stream);
es_fclose (stream);
cf->fp = fopen (cf->fname, append? "a+":"r");
cf->fp = es_fopen (cf->fname, append? "a+":"r");
}
if (!cf->fp)
@ -886,7 +886,7 @@ open_control_file (ssh_control_file_t *r_cf, int append)
if (err && cf)
{
if (cf->fp)
fclose (cf->fp);
es_fclose (cf->fp);
xfree (cf->fname);
xfree (cf);
}
@ -900,9 +900,9 @@ open_control_file (ssh_control_file_t *r_cf, int append)
static void
rewind_control_file (ssh_control_file_t cf)
{
fseek (cf->fp, 0, SEEK_SET);
es_fseek (cf->fp, 0, SEEK_SET);
cf->lnr = 0;
clearerr (cf->fp);
es_clearerr (cf->fp);
}
@ -911,7 +911,7 @@ close_control_file (ssh_control_file_t cf)
{
if (!cf)
return;
fclose (cf->fp);
es_fclose (cf->fp);
xfree (cf->fname);
xfree (cf);
}
@ -928,13 +928,13 @@ read_control_file_item (ssh_control_file_t cf)
long ttl = 0;
cf->item.valid = 0;
clearerr (cf->fp);
es_clearerr (cf->fp);
do
{
if (!fgets (line, DIM(line)-1, cf->fp) )
if (!es_fgets (line, DIM(line)-1, cf->fp) )
{
if (feof (cf->fp))
if (es_feof (cf->fp))
return gpg_error (GPG_ERR_EOF);
return gpg_error_from_syserror ();
}
@ -943,7 +943,7 @@ read_control_file_item (ssh_control_file_t cf)
if (!*line || line[strlen(line)-1] != '\n')
{
/* Eat until end of line */
while ( (c=getc (cf->fp)) != EOF && c != '\n')
while ((c = es_getc (cf->fp)) != EOF && c != '\n')
;
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE);
@ -1099,7 +1099,7 @@ add_control_entry (ctrl_t ctrl, ssh_key_type_spec_t *spec,
/* Not yet in the file - add it. Because the file has been
opened in append mode, we simply need to write to it. */
tp = localtime (&atime);
fprintf (cf->fp,
es_fprintf (cf->fp,
("# %s key added on: %04d-%02d-%02d %02d:%02d:%02d\n"
"# Fingerprints: %s\n"
"# %s\n"

View File

@ -41,14 +41,14 @@ static char *
findkey_fname (const char *key, const char *fname)
{
gpg_error_t err = 0;
FILE *fp;
estream_t fp;
int lnr = 0;
int c;
char *p, line[256];
int in_item = 0;
membuf_t mb = MEMBUF_ZERO;
fp = fopen (fname, "r");
fp = es_fopen (fname, "r");
if (!fp)
{
if (errno != ENOENT)
@ -59,14 +59,14 @@ findkey_fname (const char *key, const char *fname)
return NULL;
}
while (fgets (line, DIM(line)-1, fp))
while (es_fgets (line, DIM(line)-1, fp))
{
lnr++;
if (!*line || line[strlen(line)-1] != '\n')
{
/* Eat until end of line. */
while ( (c=getc (fp)) != EOF && c != '\n')
while ((c = es_getc (fp)) != EOF && c != '\n')
;
err = gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE);
@ -130,14 +130,14 @@ findkey_fname (const char *key, const char *fname)
}
}
if ( !err && ferror (fp) )
if ( !err && es_ferror (fp) )
{
err = gpg_error_from_syserror ();
log_error (_("error reading '%s', line %d: %s\n"),
fname, lnr, gpg_strerror (err));
}
fclose (fp);
es_fclose (fp);
if (is_membuf_ready (&mb))
{
/* We have collected something. */

View File

@ -1068,6 +1068,7 @@ gnupg_access (const char *name, int mode)
/* Try to set an envvar. Print only a notice on error. */
#ifndef HAVE_W32_SYSTEM
static void
try_set_envvar (const char *name, const char *value, int silent)
{
@ -1076,6 +1077,7 @@ try_set_envvar (const char *name, const char *value, int silent)
log_info ("error setting envvar %s to '%s': %s\n", name, value,
gpg_strerror (my_error_from_syserror ()));
}
#endif /*!HAVE_W32_SYSTEM*/
/* Switch to USER which is either a name or an UID. This is a nop

View File

@ -362,13 +362,13 @@ maybe_create_keyring_or_box (char *filename, int is_box, int force_create)
that the detection magic will work the next time it is used. */
if (is_box)
{
FILE *fp = fopen (filename, "wb");
estream_t fp = es_fopen (filename, "wb");
if (!fp)
rc = gpg_error_from_syserror ();
else
{
rc = _keybox_write_header_blob (fp, NULL, 1);
fclose (fp);
rc = _keybox_write_header_blob (fp, 1);
es_fclose (fp);
}
if (rc)
{

View File

@ -99,8 +99,8 @@ migrate_secring (ctrl_t ctrl)
log_info ("porting secret keys from '%s' to gpg-agent\n", secring);
if (!import_old_secring (ctrl, secring))
{
FILE *fp = fopen (flagfile, "w");
if (!fp || fclose (fp))
estream_t fp = es_fopen (flagfile, "w");
if (!fp || es_fclose (fp))
log_error ("error creating flag file '%s': %s\n",
flagfile, gpg_strerror (gpg_error_from_syserror ()));
else

View File

@ -531,12 +531,12 @@ create_temp_file (struct spawn_info *info, const void *ptr, u32 len)
}
else
{
FILE *fp = fopen (info->tempfile, "wb");
estream_t fp = es_fopen (info->tempfile, "wb");
if (fp)
{
fwrite (ptr, len, 1, fp);
fclose (fp);
es_fwrite (ptr, len, 1, fp);
es_fclose (fp);
return 0;
}
else

View File

@ -725,7 +725,7 @@ tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
|| stat (fname, &statbuf)
|| statbuf.st_size == 0)
{
FILE *fp;
estream_t fp;
TRUSTREC rec;
int rc;
mode_t oldmask;
@ -747,11 +747,11 @@ tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
gpg_err_set_errno (EPERM);
}
else
fp = fopen (fname, "wb");
fp = es_fopen (fname, "wb");
umask(oldmask);
if (!fp)
log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno));
fclose (fp);
es_fclose (fp);
db_fd = open (db_name, O_RDWR | MY_O_BINARY);
if (db_fd == -1)

View File

@ -126,7 +126,7 @@ create_keybox (const char *filename)
filename, gpg_strerror (err));
goto leave;
}
err = _keybox_write_header_blob (NULL, fp, 1);
err = _keybox_write_header_blob (fp, 1);
es_fclose (fp);
if (err)
{

View File

@ -423,7 +423,7 @@ import_openpgp (const char *filename, int dryrun)
}
else
{
fflush (stdout);
es_fflush (es_stdout);
log_info ("%s: failed to parse OpenPGP keyblock: %s\n",
filename, gpg_strerror (err));
}
@ -437,17 +437,17 @@ import_openpgp (const char *filename, int dryrun)
err = _keybox_create_openpgp_blob (&blob, &info, p, nparsed, 0);
if (err)
{
fflush (stdout);
es_fflush (es_stdout);
log_error ("%s: failed to create OpenPGP keyblock: %s\n",
filename, gpg_strerror (err));
}
else
{
err = _keybox_write_blob (blob, stdout);
err = _keybox_write_blob (blob, es_stdout, NULL);
_keybox_release_blob (blob);
if (err)
{
fflush (stdout);
es_fflush (es_stdout);
log_error ("%s: failed to write OpenPGP keyblock: %s\n",
filename, gpg_strerror (err));
}

View File

@ -80,7 +80,7 @@ struct keybox_found_s
struct keybox_handle {
KB_NAME kb;
int secret; /* this is for a secret keybox */
FILE *fp;
estream_t fp;
int eof;
int error;
int ephemeral;
@ -172,8 +172,8 @@ void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
/*-- keybox-file.c --*/
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);
int _keybox_read_blob (KEYBOXBLOB *r_blob, estream_t fp, int *skipped_deleted);
int _keybox_write_blob (KEYBOXBLOB blob, estream_t fp, FILE *outfp);
/*-- keybox-search.c --*/
gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,

View File

@ -647,18 +647,18 @@ update_stats (KEYBOXBLOB blob, struct file_stats_s *s)
static FILE *
static estream_t
open_file (const char **filename, FILE *outfp)
{
FILE *fp;
estream_t fp;
if (!*filename)
{
*filename = "-";
fp = stdin;
fp = es_stdin;
}
else
fp = fopen (*filename, "rb");
fp = es_fopen (*filename, "rb");
if (!fp)
{
int save_errno = errno;
@ -673,7 +673,7 @@ open_file (const char **filename, FILE *outfp)
int
_keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
{
FILE *fp;
estream_t fp;
KEYBOXBLOB blob;
int rc;
unsigned long count = 0;
@ -726,8 +726,8 @@ _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
if (rc)
fprintf (outfp, "# error reading '%s': %s\n", filename, gpg_strerror (rc));
if (fp != stdin)
fclose (fp);
if (fp != es_stdin)
es_fclose (fp);
if (stats_only)
{
@ -787,7 +787,7 @@ cmp_dupitems (const void *arg_a, const void *arg_b)
int
_keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
{
FILE *fp;
estream_t fp;
KEYBOXBLOB blob;
int rc;
unsigned long recno = 0;
@ -849,8 +849,8 @@ _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
rc = 0;
if (rc)
fprintf (outfp, "error reading '%s': %s\n", filename, gpg_strerror (rc));
if (fp != stdin)
fclose (fp);
if (fp != es_stdin)
es_fclose (fp);
qsort (dupitems, dupitems_count, sizeof *dupitems, cmp_dupitems);
@ -880,7 +880,7 @@ int
_keybox_dump_cut_records (const char *filename, unsigned long from,
unsigned long to, FILE *outfp)
{
FILE *fp;
estream_t fp;
KEYBOXBLOB blob;
int rc;
unsigned long recno = 0;
@ -894,7 +894,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
break; /* Ready. */
if (recno >= from)
{
if ((rc = _keybox_write_blob (blob, outfp)))
if ((rc = _keybox_write_blob (blob, NULL, outfp)))
{
fprintf (stderr, "error writing output: %s\n",
gpg_strerror (rc));
@ -909,7 +909,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
if (rc)
fprintf (stderr, "error reading '%s': %s\n", filename, gpg_strerror (rc));
leave:
if (fp != stdin)
fclose (fp);
if (fp != es_stdin)
es_fclose (fp);
return rc;
}

View File

@ -48,7 +48,7 @@ ftello (FILE *stream)
/* Read a block at the current position and return it in R_BLOB.
R_BLOB may be NULL to simply skip the current block. */
int
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
_keybox_read_blob (KEYBOXBLOB *r_blob, estream_t fp, int *skipped_deleted)
{
unsigned char *image;
size_t imagelen = 0;
@ -61,19 +61,19 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
again:
if (r_blob)
*r_blob = NULL;
off = ftello (fp);
off = es_ftello (fp);
if (off == (off_t)-1)
return gpg_error_from_syserror ();
if ((c1 = getc (fp)) == EOF
|| (c2 = getc (fp)) == EOF
|| (c3 = getc (fp)) == EOF
|| (c4 = getc (fp)) == EOF
|| (type = getc (fp)) == EOF)
if ((c1 = es_getc (fp)) == EOF
|| (c2 = es_getc (fp)) == EOF
|| (c3 = es_getc (fp)) == EOF
|| (c4 = es_getc (fp)) == EOF
|| (type = es_getc (fp)) == EOF)
{
if ( c1 == EOF && !ferror (fp) )
if ( c1 == EOF && !es_ferror (fp) )
return -1; /* eof */
if (!ferror (fp))
if (!es_ferror (fp))
return gpg_error (GPG_ERR_TOO_SHORT);
return gpg_error_from_syserror ();
}
@ -85,7 +85,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
if (!type)
{
/* Special treatment for empty blobs. */
if (fseek (fp, imagelen-5, SEEK_CUR))
if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror ();
if (skipped_deleted)
*skipped_deleted = 1;
@ -96,7 +96,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
{
/* Seek forward so that the caller may choose to ignore this
record. */
if (fseek (fp, imagelen-5, SEEK_CUR))
if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror ();
return gpg_error (GPG_ERR_TOO_LARGE);
}
@ -104,7 +104,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
if (!r_blob)
{
/* This blob shall be skipped. */
if (fseek (fp, imagelen-5, SEEK_CUR))
if (es_fseek (fp, imagelen-5, SEEK_CUR))
return gpg_error_from_syserror ();
return 0;
}
@ -114,7 +114,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
return gpg_error_from_syserror ();
image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
if (fread (image+5, imagelen-5, 1, fp) != 1)
if (es_fread (image+5, imagelen-5, 1, fp) != 1)
{
gpg_error_t tmperr = gpg_error_from_syserror ();
xfree (image);
@ -130,7 +130,7 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
/* Write the block to the current file position */
int
_keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
_keybox_write_blob (KEYBOXBLOB blob, estream_t fp, FILE *outfp)
{
const unsigned char *image;
size_t length;
@ -140,15 +140,24 @@ _keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
if (length > IMAGELEN_LIMIT)
return gpg_error (GPG_ERR_TOO_LARGE);
if (fwrite (image, length, 1, fp) != 1)
return gpg_error_from_syserror ();
if (fp)
{
if (es_fwrite (image, length, 1, fp) != 1)
return gpg_error_from_syserror ();
}
else
{
if (fwrite (image, length, 1, outfp) != 1)
return gpg_error_from_syserror ();
}
return 0;
}
/* Write a fresh header type blob. Either FP or STREAM must be used. */
/* Write a fresh header type blob. */
gpg_error_t
_keybox_write_header_blob (FILE *fp, estream_t stream, int for_openpgp)
_keybox_write_header_blob (estream_t fp, int for_openpgp)
{
unsigned char image[32];
u32 val;
@ -174,15 +183,8 @@ _keybox_write_header_blob (FILE *fp, estream_t stream, int for_openpgp)
image[20+2] = (val >> 8);
image[20+3] = (val );
if (fp)
{
if (fwrite (image, 32, 1, fp) != 1)
return gpg_error_from_syserror ();
}
else
{
if (es_fwrite (image, 32, 1, stream) != 1)
return gpg_error_from_syserror ();
}
if (es_fwrite (image, 32, 1, fp) != 1)
return gpg_error_from_syserror ();
return 0;
}

View File

@ -180,7 +180,7 @@ keybox_release (KEYBOX_HANDLE hd)
_keybox_release_blob (hd->saved_found.blob);
if (hd->fp)
{
fclose (hd->fp);
es_fclose (hd->fp);
hd->fp = NULL;
}
xfree (hd->word_match.name);
@ -253,11 +253,11 @@ _keybox_close_file (KEYBOX_HANDLE hd)
{
if (roverhd->fp)
{
fclose (roverhd->fp);
es_fclose (roverhd->fp);
roverhd->fp = NULL;
}
}
assert (!hd->fp);
log_assert (!hd->fp);
}

View File

@ -880,7 +880,7 @@ static gpg_error_t
open_file (KEYBOX_HANDLE hd)
{
hd->fp = fopen (hd->kb->fname, "rb");
hd->fp = es_fopen (hd->kb->fname, "rb");
if (!hd->fp)
{
hd->error = gpg_error_from_syserror ();
@ -912,11 +912,11 @@ keybox_search_reset (KEYBOX_HANDLE hd)
if (hd->fp)
{
if (fseeko (hd->fp, 0, SEEK_SET))
if (es_fseeko (hd->fp, 0, SEEK_SET))
{
/* Ooops. Seek did not work. Close so that the search will
* open the file again. */
fclose (hd->fp);
es_fclose (hd->fp);
hd->fp = NULL;
}
}
@ -1007,7 +1007,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
* returned a blob which also was not the first one. We now
* need to skip over that blob and hope that the file has
* not changed. */
if (fseeko (hd->fp, lastfoundoff, SEEK_SET))
if (es_fseeko (hd->fp, lastfoundoff, SEEK_SET))
{
rc = gpg_error_from_syserror ();
log_debug ("%s: seeking to last found offset failed: %s\n",
@ -1462,7 +1462,7 @@ keybox_offset (KEYBOX_HANDLE hd)
{
if (!hd->fp)
return 0;
return ftello (hd->fp);
return es_ftello (hd->fp);
}
gpg_error_t
@ -1487,7 +1487,7 @@ keybox_seek (KEYBOX_HANDLE hd, off_t offset)
return err;
}
err = fseeko (hd->fp, offset, SEEK_SET);
err = es_fseeko (hd->fp, offset, SEEK_SET);
hd->error = gpg_error_from_errno (err);
return hd->error;

View File

@ -71,14 +71,14 @@ fseeko (FILE * stream, off_t newpos, int whence)
static int
create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, FILE **r_fp)
char **r_bakfname, char **r_tmpfname, estream_t *r_fp)
{
gpg_error_t err;
err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
if (!err)
{
*r_fp = fopen (*r_tmpfname, "wb");
*r_fp = es_fopen (*r_tmpfname, "wb");
if (!*r_fp)
{
err = gpg_error_from_syserror ();
@ -162,8 +162,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
int secret, int for_openpgp, off_t start_offset)
{
gpg_err_code_t ec;
FILE *fp, *newfp;
int rc=0;
estream_t fp, newfp;
int rc = 0;
char *bakfname = NULL;
char *tmpfname = NULL;
char buffer[4096]; /* (Must be at least 32 bytes) */
@ -174,30 +174,30 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec);
fp = fopen (fname, "rb");
fp = es_fopen (fname, "rb");
if (mode == FILECOPY_INSERT && !fp && errno == ENOENT)
{
/* Insert mode but file does not exist:
Create a new keybox file. */
newfp = fopen (fname, "wb");
newfp = es_fopen (fname, "wb");
if (!newfp )
return gpg_error_from_syserror ();
rc = _keybox_write_header_blob (newfp, NULL, for_openpgp);
rc = _keybox_write_header_blob (newfp, for_openpgp);
if (rc)
{
fclose (newfp);
es_fclose (newfp);
return rc;
}
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
{
fclose (newfp);
es_fclose (newfp);
return rc;
}
if ( fclose (newfp) )
if ( es_fclose (newfp) )
return gpg_error_from_syserror ();
/* if (chmod( fname, S_IRUSR | S_IWUSR )) */
@ -218,7 +218,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc)
{
fclose (fp);
es_fclose (fp);
goto leave;
}
@ -230,7 +230,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Copy everything to the new file. If this is for OpenPGP, we
make sure that the openpgp flag is set in the header. (We
failsafe the blob type.) */
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
{
if (first_record && for_openpgp
&& buffer[4] == KEYBOX_BLOBTYPE_HEADER)
@ -239,19 +239,19 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
buffer[7] |= 0x02; /* OpenPGP data may be available. */
}
if (fwrite (buffer, nread, 1, newfp) != 1)
if (es_fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
@ -267,24 +267,24 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
nbytes = DIM(buffer);
if (current + nbytes > start_offset)
nbytes = start_offset - current;
nread = fread (buffer, 1, nbytes, fp);
nread = es_fread (buffer, 1, nbytes, fp);
if (!nread)
break;
current += nread;
if (fwrite (buffer, nread, 1, newfp) != 1)
if (es_fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
@ -292,8 +292,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = _keybox_read_blob (NULL, fp, NULL);
if (rc)
{
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
return rc;
}
}
@ -301,11 +301,11 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Do an insert or update. */
if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE )
{
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
{
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
return rc;
}
}
@ -313,33 +313,33 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
/* Copy the rest of the packet for an delete or update. */
if (mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE)
{
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
{
if (fwrite (buffer, nread, 1, newfp) != 1)
if (es_fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
if (ferror (fp))
if (es_ferror (fp))
{
rc = gpg_error_from_syserror ();
fclose (fp);
fclose (newfp);
es_fclose (fp);
es_fclose (newfp);
goto leave;
}
}
/* Close both files. */
if (fclose(fp))
if (es_fclose(fp))
{
rc = gpg_error_from_syserror ();
fclose (newfp);
es_fclose (newfp);
goto leave;
}
if (fclose(newfp))
if (es_fclose(newfp))
{
rc = gpg_error_from_syserror ();
goto leave;
@ -504,7 +504,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
{
off_t off;
const char *fname;
FILE *fp;
estream_t fp;
gpg_err_code_t ec;
size_t flag_pos, flag_size;
const unsigned char *buffer;
@ -536,12 +536,12 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
off += flag_pos;
_keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b");
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
ec = 0;
if (fseeko (fp, off, SEEK_SET))
if (es_fseeko (fp, off, SEEK_SET))
ec = gpg_err_code_from_syserror ();
else
{
@ -557,7 +557,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
case 1:
case 2:
case 4:
if (fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
if (es_fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
ec = gpg_err_code_from_syserror ();
break;
default:
@ -566,7 +566,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
}
}
if (fclose (fp))
if (es_fclose (fp))
{
if (!ec)
ec = gpg_err_code_from_syserror ();
@ -582,7 +582,7 @@ keybox_delete (KEYBOX_HANDLE hd)
{
off_t off;
const char *fname;
FILE *fp;
estream_t fp;
int rc;
if (!hd)
@ -601,18 +601,18 @@ keybox_delete (KEYBOX_HANDLE hd)
off += 4;
_keybox_close_file (hd);
fp = fopen (hd->kb->fname, "r+b");
fp = es_fopen (hd->kb->fname, "r+b");
if (!fp)
return gpg_error_from_syserror ();
if (fseeko (fp, off, SEEK_SET))
if (es_fseeko (fp, off, SEEK_SET))
rc = gpg_error_from_syserror ();
else if (putc (0, fp) == EOF)
else if (es_fputc (0, fp) == EOF)
rc = gpg_error_from_syserror ();
else
rc = 0;
if (fclose (fp))
if (es_fclose (fp))
{
if (!rc)
rc = gpg_error_from_syserror ();
@ -630,7 +630,7 @@ keybox_compress (KEYBOX_HANDLE hd)
gpg_err_code_t ec;
int read_rc, rc;
const char *fname;
FILE *fp, *newfp;
estream_t fp, newfp;
char *bakfname = NULL;
char *tmpfname = NULL;
int first_blob;
@ -656,7 +656,7 @@ keybox_compress (KEYBOX_HANDLE hd)
if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec);
fp = fopen (fname, "rb");
fp = es_fopen (fname, "rb");
if (!fp && errno == ENOENT)
return 0; /* Ready. File has been deleted right after the access above. */
if (!fp)
@ -679,21 +679,21 @@ keybox_compress (KEYBOX_HANDLE hd)
if ( (last_maint + 3*3600) > make_timestamp () )
{
fclose (fp);
es_fclose (fp);
_keybox_release_blob (blob);
return 0; /* Compress run not yet needed. */
}
}
_keybox_release_blob (blob);
fseek (fp, 0, SEEK_SET);
clearerr (fp);
es_fseek (fp, 0, SEEK_SET);
es_clearerr (fp);
}
/* Create the new file. */
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc)
{
fclose (fp);
es_fclose (fp);
return rc;;
}
@ -725,14 +725,14 @@ keybox_compress (KEYBOX_HANDLE hd)
stamp and if needed (ie. used by gpg) set the openpgp
flag. */
_keybox_update_header_blob (blob, hd->for_openpgp);
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
break;
continue;
}
/* The header blob is missing. Insert it. */
rc = _keybox_write_header_blob (newfp, NULL, hd->for_openpgp);
rc = _keybox_write_header_blob (newfp, hd->for_openpgp);
if (rc)
break;
any_changes = 1;
@ -769,7 +769,7 @@ keybox_compress (KEYBOX_HANDLE hd)
}
}
rc = _keybox_write_blob (blob, newfp);
rc = _keybox_write_blob (blob, newfp, NULL);
if (rc)
break;
}
@ -782,9 +782,9 @@ keybox_compress (KEYBOX_HANDLE hd)
rc = read_rc;
/* Close both files. */
if (fclose(fp) && !rc)
if (es_fclose(fp) && !rc)
rc = gpg_error_from_syserror ();
if (fclose(newfp) && !rc)
if (es_fclose(newfp) && !rc)
rc = gpg_error_from_syserror ();
/* Rename or remove the temporary file. */

View File

@ -81,8 +81,7 @@ gpg_error_t keybox_lock (KEYBOX_HANDLE hd, int yes, long timeout);
/*-- keybox-file.c --*/
/* Fixme: This function does not belong here: Provide a better
interface to create a new keybox file. */
gpg_error_t _keybox_write_header_blob (FILE *fp, estream_t stream,
int openpgp_flag);
gpg_error_t _keybox_write_header_blob (estream_t fp, int openpgp_flag);
/*-- keybox-search.c --*/
gpg_error_t keybox_get_data (KEYBOX_HANDLE hd,

View File

@ -1935,18 +1935,18 @@ report_change (int slot, int old_status, int cur_status)
char *homestr, *envstr;
char *fname;
char templ[50];
FILE *fp;
estream_t fp;
snprintf (templ, sizeof templ, "reader_%d.status", slot);
fname = make_filename (gnupg_homedir (), templ, NULL );
fp = fopen (fname, "w");
fp = es_fopen (fname, "w");
if (fp)
{
fprintf (fp, "%s\n",
es_fprintf (fp, "%s\n",
(cur_status & 1)? "USABLE":
(cur_status & 4)? "ACTIVE":
(cur_status & 2)? "PRESENT": "NOCARD");
fclose (fp);
es_fclose (fp);
}
xfree (fname);

View File

@ -308,7 +308,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
{
gpg_error_t err;
char *policies;
FILE *fp;
estream_t fp;
int any_critical;
err = ksba_cert_get_cert_policies (cert, &policies);
@ -340,7 +340,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
return 0;
}
fp = fopen (opt.policy_file, "r");
fp = es_fopen (opt.policy_file, "r");
if (!fp)
{
if (opt.verbose || errno != ENOENT)
@ -369,14 +369,14 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
/* read line */
do
{
if (!fgets (line, DIM(line)-1, fp) )
if (!es_fgets (line, DIM(line)-1, fp) )
{
gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
gpg_error_t tmperr = gpg_error_from_syserror ();
xfree (policies);
if (feof (fp))
if (es_feof (fp))
{
fclose (fp);
es_fclose (fp);
/* With no critical policies this is only a warning */
if (!any_critical)
{
@ -388,16 +388,16 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
_("certificate policy not allowed"));
return gpg_error (GPG_ERR_NO_POLICY_MATCH);
}
fclose (fp);
es_fclose (fp);
return tmperr;
}
if (!*line || line[strlen(line)-1] != '\n')
{
/* eat until end of line */
while ( (c=getc (fp)) != EOF && c != '\n')
while ((c = es_getc (fp)) != EOF && c != '\n')
;
fclose (fp);
es_fclose (fp);
xfree (policies);
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE);
@ -417,7 +417,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
p = strpbrk (allowed, " :\n");
if (!*p || p == allowed)
{
fclose (fp);
es_fclose (fp);
xfree (policies);
return gpg_error (GPG_ERR_CONFIGURATION);
}
@ -430,7 +430,7 @@ check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
if (p[strlen (allowed)] != ':')
continue; /* The length does not match. */
/* Yep - it does match so return okay. */
fclose (fp);
es_fclose (fp);
xfree (policies);
return 0;
}

View File

@ -208,7 +208,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
{
gpg_err_code_t ec;
dotlock_t lockhd = NULL;
FILE *fp;
estream_t fp;
int rc;
mode_t oldmask;
char *last_slash_in_filename;
@ -299,7 +299,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
/* The file does not yet exist, create it now. */
oldmask = umask (077);
fp = fopen (filename, "wb");
fp = es_fopen (filename, "wb");
if (!fp)
{
rc = gpg_error_from_syserror ();
@ -313,10 +313,10 @@ maybe_create_keybox (char *filename, int force, int *r_created)
/* Make sure that at least one record is in a new keybox file, so
that the detection magic for OpenPGP keyboxes works the next time
it is used. */
rc = _keybox_write_header_blob (fp, NULL, 0);
rc = _keybox_write_header_blob (fp, 0);
if (rc)
{
fclose (fp);
es_fclose (fp);
log_error (_("error creating keybox '%s': %s\n"),
filename, gpg_strerror (rc));
goto leave;
@ -327,7 +327,7 @@ maybe_create_keybox (char *filename, int force, int *r_created)
if (r_created)
*r_created = 1;
fclose (fp);
es_fclose (fp);
rc = 0;
leave:
@ -394,14 +394,15 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
/* see whether we can determine the filetype */
if (rt == KEYDB_RESOURCE_TYPE_NONE)
{
FILE *fp = fopen( filename, "rb" );
estream_t fp;
fp = es_fopen( filename, "rb" );
if (fp)
{
u32 magic;
/* FIXME: check for the keybox magic */
if (fread (&magic, 4, 1, fp) == 1 )
if (es_fread (&magic, 4, 1, fp) == 1 )
{
if (magic == 0x13579ace || magic == 0xce9a5713)
; /* GDBM magic - no more support */
@ -410,7 +411,8 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
}
else /* maybe empty: assume keybox */
rt = KEYDB_RESOURCE_TYPE_KEYBOX;
fclose (fp);
es_fclose (fp);
}
else /* no file yet: create keybox */
rt = KEYDB_RESOURCE_TYPE_KEYBOX;

View File

@ -34,7 +34,7 @@
NULL indicates that this module has been initialized and if the
LISTFP is also NULL, no list of qualified signatures exists. */
static char *listname;
static FILE *listfp;
static estream_t listfp;
/* Read the trustlist and return entry by entry. KEY must point to a
@ -58,7 +58,7 @@ read_list (char *key, char *country, int *lnr)
if (!listname)
{
listname = make_filename (gnupg_sysconfdir (), "qualified.txt", NULL);
listfp = fopen (listname, "r");
listfp = es_fopen (listname, "r");
if (!listfp && errno != ENOENT)
{
err = gpg_error_from_syserror ();
@ -72,9 +72,9 @@ read_list (char *key, char *country, int *lnr)
do
{
if (!fgets (line, DIM(line)-1, listfp) )
if (!es_fgets (line, DIM(line)-1, listfp) )
{
if (feof (listfp))
if (es_feof (listfp))
return gpg_error (GPG_ERR_EOF);
return gpg_error_from_syserror ();
}
@ -82,7 +82,7 @@ read_list (char *key, char *country, int *lnr)
if (!*line || line[strlen(line)-1] != '\n')
{
/* Eat until end of line. */
while ( (c=getc (listfp)) != EOF && c != '\n')
while ((c = es_getc (listfp)) != EOF && c != '\n')
;
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
: GPG_ERR_INCOMPLETE_LINE);
@ -162,8 +162,8 @@ gpgsm_is_in_qualified_list (ctrl_t ctrl, ksba_cert_t cert, char *country)
if (listfp)
{
/* W32ce has no rewind, thus we use the equivalent code. */
fseek (listfp, 0, SEEK_SET);
clearerr (listfp);
es_fseek (listfp, 0, SEEK_SET);
es_clearerr (listfp);
}
while (!(err = read_list (key, mycountry, &lnr)))
{

View File

@ -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;

View File

@ -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');

View File

@ -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));