A task-oriented GnuPG frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

/*
* Yorkie - A task-oriented GnuPG frontend
* Copyright (C) 2020 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gpgutil.h"
#include <stdio.h>
#include <errno.h>
#include <locale.h>
gpgme_error_t
gpgutil_initialize(gpgme_ctx_t *ctx)
{
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
return gpgme_new(ctx);
}
gpgme_error_t
gpgutil_open_file(const char *name, const char *mode, gpgutil_file_t *file)
{
gpgme_error_t gerr;
gerr = 0;
file->buffer = NULL;
file->file = NULL;
if ( ! (file->file = fopen(name, mode)) )
gerr = gpgme_error_from_errno(errno);
if ( ! gerr )
gerr = gpgme_data_new_from_stream(&file->buffer, file->file);
if ( ! gerr )
gerr = gpgme_data_set_file_name(file->buffer, name);
if ( gerr ) {
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
}
if ( file->file ) {
fclose(file->file);
file->file = NULL;
}
}
return gerr ? gerr : gpgme_error(GPG_ERR_NO_ERROR);
}
gpgme_error_t
gpgutil_close_file(gpgutil_file_t *file)
{
int ret = GPG_ERR_NO_ERROR;
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
if ( fclose(file->file) )
return gpgme_error_from_errno(errno);
file->buffer = NULL;
}
return gpgme_error(GPG_ERR_NO_ERROR);
}