1997-11-18 15:06:00 +01:00
|
|
|
/* util.h
|
2001-04-28 20:53:00 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-06-16 17:13:28 +02:00
|
|
|
* This file is part of GNUPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-06-16 17:13:28 +02:00
|
|
|
* GNUPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-06-16 17:13:28 +02:00
|
|
|
* GNUPG is distributed in the hope that it will be useful,
|
1997-11-18 15:06:00 +01:00
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
#ifndef G10_UTIL_H
|
|
|
|
#define G10_UTIL_H
|
|
|
|
|
2001-12-19 19:04:52 +01:00
|
|
|
#if defined (__MINGW32__) || defined (__CYGWIN32__)
|
2001-12-23 13:40:04 +01:00
|
|
|
# include <stdarg.h>
|
2001-12-19 19:04:52 +01:00
|
|
|
#endif
|
|
|
|
|
1997-12-23 18:30:18 +01:00
|
|
|
#include "types.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "errors.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "mpi.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int *argc; /* pointer to argc (value subject to change) */
|
|
|
|
char ***argv; /* pointer to argv (value subject to change) */
|
|
|
|
unsigned flags; /* Global flags (DO NOT CHANGE) */
|
|
|
|
int err; /* print error about last option */
|
|
|
|
/* 1 = warning, 2 = abort */
|
|
|
|
int r_opt; /* return option */
|
|
|
|
int r_type; /* type of return value (0 = no argument found)*/
|
|
|
|
union {
|
|
|
|
int ret_int;
|
|
|
|
long ret_long;
|
|
|
|
ulong ret_ulong;
|
|
|
|
char *ret_str;
|
|
|
|
} r; /* Return values */
|
|
|
|
struct {
|
1999-02-16 14:16:33 +01:00
|
|
|
int idx;
|
1997-11-18 15:06:00 +01:00
|
|
|
int inarg;
|
|
|
|
int stopped;
|
|
|
|
const char *last;
|
1998-11-13 20:41:41 +01:00
|
|
|
void *aliases;
|
1998-11-27 15:33:31 +01:00
|
|
|
const void *cur_alias;
|
1997-11-18 15:06:00 +01:00
|
|
|
} internal; /* DO NOT CHANGE */
|
|
|
|
} ARGPARSE_ARGS;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int short_opt;
|
|
|
|
const char *long_opt;
|
|
|
|
unsigned flags;
|
|
|
|
const char *description; /* optional option description */
|
|
|
|
} ARGPARSE_OPTS;
|
|
|
|
|
|
|
|
/*-- logger.c --*/
|
1999-05-22 22:54:54 +02:00
|
|
|
void log_set_logfile( const char *name, int fd );
|
|
|
|
FILE *log_stream(void);
|
1999-07-07 13:28:26 +02:00
|
|
|
void g10_log_print_prefix(const char *text);
|
1998-02-13 21:58:50 +01:00
|
|
|
void log_set_name( const char *name );
|
|
|
|
const char *log_get_name(void);
|
1998-01-02 21:40:10 +01:00
|
|
|
void log_set_pid( int pid );
|
|
|
|
int log_get_errorcount( int clear );
|
1999-12-31 14:06:59 +01:00
|
|
|
void log_inc_errorcount(void);
|
1999-05-17 22:03:24 +02:00
|
|
|
void g10_log_hexdump( const char *text, const char *buf, size_t len );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2001-08-20 13:53:05 +02:00
|
|
|
#if !defined (__riscos__) \
|
|
|
|
&& (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
|
1998-06-16 17:13:28 +02:00
|
|
|
void g10_log_bug( const char *fmt, ... )
|
1998-01-16 22:15:24 +01:00
|
|
|
__attribute__ ((noreturn, format (printf,1,2)));
|
1998-06-16 17:13:28 +02:00
|
|
|
void g10_log_bug0( const char *, int, const char * ) __attribute__ ((noreturn));
|
|
|
|
void g10_log_fatal( const char *fmt, ... )
|
1998-01-16 22:15:24 +01:00
|
|
|
__attribute__ ((noreturn, format (printf,1,2)));
|
1998-06-16 17:13:28 +02:00
|
|
|
void g10_log_error( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
|
|
|
|
void g10_log_info( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
|
|
|
|
void g10_log_debug( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
|
1998-07-08 11:29:43 +02:00
|
|
|
void g10_log_fatal_f( const char *fname, const char *fmt, ... )
|
|
|
|
__attribute__ ((noreturn, format (printf,2,3)));
|
|
|
|
void g10_log_error_f( const char *fname, const char *fmt, ... )
|
|
|
|
__attribute__ ((format (printf,2,3)));
|
|
|
|
void g10_log_info_f( const char *fname, const char *fmt, ... )
|
|
|
|
__attribute__ ((format (printf,2,3)));
|
|
|
|
void g10_log_debug_f( const char *fname, const char *fmt, ... )
|
|
|
|
__attribute__ ((format (printf,2,3)));
|
1998-06-16 17:13:28 +02:00
|
|
|
#define BUG() g10_log_bug0( __FILE__ , __LINE__, __FUNCTION__ )
|
1998-01-16 22:15:24 +01:00
|
|
|
#else
|
1998-06-16 17:13:28 +02:00
|
|
|
void g10_log_bug( const char *fmt, ... );
|
|
|
|
void g10_log_bug0( const char *, int );
|
|
|
|
void g10_log_fatal( const char *fmt, ... );
|
|
|
|
void g10_log_error( const char *fmt, ... );
|
|
|
|
void g10_log_info( const char *fmt, ... );
|
|
|
|
void g10_log_debug( const char *fmt, ... );
|
1998-07-08 11:29:43 +02:00
|
|
|
void g10_log_fatal_f( const char *fname, const char *fmt, ... );
|
|
|
|
void g10_log_error_f( const char *fname, const char *fmt, ... );
|
|
|
|
void g10_log_info_f( const char *fname, const char *fmt, ... );
|
|
|
|
void g10_log_debug_f( const char *fname, const char *fmt, ... );
|
1998-06-16 17:13:28 +02:00
|
|
|
#define BUG() g10_log_bug0( __FILE__ , __LINE__ )
|
1998-01-16 22:15:24 +01:00
|
|
|
#endif
|
|
|
|
|
1998-06-16 17:13:28 +02:00
|
|
|
#define log_hexdump g10_log_hexdump
|
|
|
|
#define log_bug g10_log_bug
|
|
|
|
#define log_bug0 g10_log_bug0
|
|
|
|
#define log_fatal g10_log_fatal
|
|
|
|
#define log_error g10_log_error
|
|
|
|
#define log_info g10_log_info
|
|
|
|
#define log_debug g10_log_debug
|
1998-07-08 11:29:43 +02:00
|
|
|
#define log_fatal_f g10_log_fatal_f
|
|
|
|
#define log_error_f g10_log_error_f
|
|
|
|
#define log_info_f g10_log_info_f
|
|
|
|
#define log_debug_f g10_log_debug_f
|
1998-06-16 17:13:28 +02:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/*-- errors.c --*/
|
|
|
|
const char * g10_errstr( int no );
|
|
|
|
|
|
|
|
/*-- argparse.c --*/
|
|
|
|
int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
|
1998-01-16 22:15:24 +01:00
|
|
|
int optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
|
|
|
|
ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
|
1997-11-18 15:06:00 +01:00
|
|
|
void usage( int level );
|
|
|
|
const char *default_strusage( int level );
|
|
|
|
|
|
|
|
|
|
|
|
/*-- (main program) --*/
|
|
|
|
const char *strusage( int level );
|
|
|
|
|
|
|
|
|
1998-11-27 21:40:56 +01:00
|
|
|
/*-- dotlock.c --*/
|
1999-02-10 17:22:40 +01:00
|
|
|
struct dotlock_handle;
|
|
|
|
typedef struct dotlock_handle *DOTLOCK;
|
1998-11-27 21:40:56 +01:00
|
|
|
|
2000-04-14 19:34:30 +02:00
|
|
|
void disable_dotlock(void);
|
1999-02-10 17:22:40 +01:00
|
|
|
DOTLOCK create_dotlock( const char *file_to_lock );
|
|
|
|
int make_dotlock( DOTLOCK h, long timeout );
|
|
|
|
int release_dotlock( DOTLOCK h );
|
2000-12-28 16:49:42 +01:00
|
|
|
void remove_lockfiles (void);
|
1998-11-27 21:40:56 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/*-- fileutil.c --*/
|
1998-10-25 20:00:01 +01:00
|
|
|
char * make_basename(const char *filepath);
|
|
|
|
char * make_dirname(const char *filepath);
|
1997-12-12 13:03:58 +01:00
|
|
|
char *make_filename( const char *first_part, ... );
|
1998-06-11 09:16:50 +02:00
|
|
|
int compare_filenames( const char *a, const char *b );
|
1998-03-09 22:44:06 +01:00
|
|
|
const char *print_fname_stdin( const char *s );
|
|
|
|
const char *print_fname_stdout( const char *s );
|
2001-12-22 13:46:47 +01:00
|
|
|
int is_file_compressed(const char *s, int *r_status);
|
1998-03-09 22:44:06 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/*-- miscutil.c --*/
|
|
|
|
u32 make_timestamp(void);
|
1999-07-07 13:28:26 +02:00
|
|
|
u32 scan_isodatestr( const char *string );
|
1998-04-30 16:06:01 +02:00
|
|
|
u32 add_days_to_timestamp( u32 stamp, u16 days );
|
1998-11-20 18:42:18 +01:00
|
|
|
const char *strtimevalue( u32 stamp );
|
1998-09-15 21:56:30 +02:00
|
|
|
const char *strtimestamp( u32 stamp ); /* GMT */
|
|
|
|
const char *asctimestamp( u32 stamp ); /* localized */
|
1999-05-25 19:56:15 +02:00
|
|
|
void print_string( FILE *fp, const byte *p, size_t n, int delim );
|
1999-08-31 17:30:12 +02:00
|
|
|
void print_utf8_string( FILE *fp, const byte *p, size_t n );
|
2001-09-19 16:25:05 +02:00
|
|
|
void print_utf8_string2( FILE *fp, const byte *p, size_t n, int delim);
|
1999-07-01 12:53:35 +02:00
|
|
|
char *make_printable_string( const byte *p, size_t n, int delim );
|
1997-12-20 18:23:29 +01:00
|
|
|
int answer_is_yes( const char *s );
|
1999-05-27 09:45:46 +02:00
|
|
|
int answer_is_yes_no_quit( const char *s );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/*-- strgutil.c --*/
|
|
|
|
void free_strlist( STRLIST sl );
|
|
|
|
#define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0)
|
1998-07-06 12:23:57 +02:00
|
|
|
STRLIST add_to_strlist( STRLIST *list, const char *string );
|
1999-07-01 12:53:35 +02:00
|
|
|
STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
|
1998-07-06 12:23:57 +02:00
|
|
|
STRLIST append_to_strlist( STRLIST *list, const char *string );
|
1999-07-01 12:53:35 +02:00
|
|
|
STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
|
1998-02-12 00:22:09 +01:00
|
|
|
STRLIST strlist_prev( STRLIST head, STRLIST node );
|
|
|
|
STRLIST strlist_last( STRLIST node );
|
1998-04-25 10:08:35 +02:00
|
|
|
const char *memistr( const char *buf, size_t buflen, const char *sub );
|
2001-06-12 20:42:40 +02:00
|
|
|
const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
|
1998-01-26 23:09:01 +01:00
|
|
|
char *mem2str( char *, const void *, size_t);
|
1998-01-07 21:47:46 +01:00
|
|
|
char *trim_spaces( char *string );
|
2001-03-12 21:21:50 +01:00
|
|
|
unsigned int trim_trailing_chars( byte *line, unsigned int len,
|
|
|
|
const char *trimchars);
|
|
|
|
unsigned int trim_trailing_ws( byte *line, unsigned len );
|
|
|
|
unsigned int check_trailing_chars( const byte *line, unsigned int len,
|
|
|
|
const char *trimchars );
|
|
|
|
unsigned int check_trailing_ws( const byte *line, unsigned int len );
|
1998-01-07 21:47:46 +01:00
|
|
|
int string_count_chr( const char *string, int c );
|
1998-11-13 20:41:41 +01:00
|
|
|
int set_native_charset( const char *newset );
|
1999-03-02 16:48:37 +01:00
|
|
|
const char* get_native_charset(void);
|
1998-11-03 20:38:58 +01:00
|
|
|
char *native_to_utf8( const char *string );
|
2001-09-19 16:25:05 +02:00
|
|
|
char *utf8_to_native( const char *string, size_t length, int delim);
|
1998-11-13 20:41:41 +01:00
|
|
|
int check_utf8_string( const char *string );
|
1998-01-07 21:47:46 +01:00
|
|
|
|
2001-06-12 20:42:40 +02:00
|
|
|
int ascii_isupper (int c);
|
|
|
|
int ascii_islower (int c);
|
|
|
|
int ascii_toupper (int c);
|
|
|
|
int ascii_tolower (int c);
|
|
|
|
int ascii_strcasecmp( const char *a, const char *b );
|
|
|
|
int ascii_memcasecmp( const char *a, const char *b, size_t n );
|
|
|
|
|
1998-10-06 14:10:02 +02:00
|
|
|
#ifndef HAVE_MEMICMP
|
|
|
|
int memicmp( const char *a, const char *b, size_t n );
|
|
|
|
#endif
|
1998-01-05 20:13:15 +01:00
|
|
|
#ifndef HAVE_STPCPY
|
|
|
|
char *stpcpy(char *a,const char *b);
|
|
|
|
#endif
|
1998-01-07 21:47:46 +01:00
|
|
|
#ifndef HAVE_STRLWR
|
|
|
|
char *strlwr(char *a);
|
|
|
|
#endif
|
2001-09-09 18:09:19 +02:00
|
|
|
#ifndef HAVE_STRSEP
|
|
|
|
char *strsep (char **stringp, const char *delim);
|
|
|
|
#endif
|
1999-11-22 11:10:27 +01:00
|
|
|
#ifndef HAVE_STRCASECMP
|
|
|
|
int strcasecmp( const char *, const char *b);
|
|
|
|
#endif
|
1998-01-24 17:32:27 +01:00
|
|
|
#ifndef HAVE_STRTOUL
|
|
|
|
#define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
|
|
|
|
#endif
|
1998-05-13 19:53:36 +02:00
|
|
|
#ifndef HAVE_MEMMOVE
|
|
|
|
#define memmove(d, s, n) bcopy((s), (d), (n))
|
|
|
|
#endif
|
1999-06-29 21:50:54 +02:00
|
|
|
#ifndef HAVE_STRICMP
|
|
|
|
#define stricmp(a,b) strcasecmp( (a), (b) )
|
|
|
|
#endif
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2001-12-19 19:04:52 +01:00
|
|
|
#if defined (__MINGW32__) || defined (__CYGWIN32__)
|
2001-06-06 20:58:05 +02:00
|
|
|
/*-- w32reg.c --*/
|
1999-10-08 20:34:56 +02:00
|
|
|
char *read_w32_registry_string( const char *root,
|
|
|
|
const char *dir, const char *name );
|
2001-09-20 09:12:52 +02:00
|
|
|
int write_w32_registry_string(const char *root, const char *dir,
|
|
|
|
const char *name, const char *value);
|
|
|
|
|
2001-06-06 20:58:05 +02:00
|
|
|
/*-- strgutil.c --*/
|
|
|
|
int vasprintf ( char **result, const char *format, va_list args);
|
1999-10-08 20:34:56 +02:00
|
|
|
#endif
|
|
|
|
|
1998-09-14 17:49:56 +02:00
|
|
|
/**** other missing stuff ****/
|
|
|
|
#ifndef HAVE_ATEXIT /* For SunOS */
|
|
|
|
#define atexit(a) (on_exit((a),0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_RAISE
|
|
|
|
#define raise(a) kill(getpid(), (a))
|
|
|
|
#endif
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/******** some macros ************/
|
|
|
|
#ifndef STR
|
|
|
|
#define STR(v) #v
|
|
|
|
#endif
|
|
|
|
#define STR2(v) STR(v)
|
|
|
|
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
|
|
|
|
#define DIMof(type,member) DIM(((type *)0)->member)
|
|
|
|
|
2001-08-20 13:53:05 +02:00
|
|
|
/******* RISC OS stuff ***********/
|
|
|
|
#ifdef __riscos__
|
2002-01-02 18:48:27 +01:00
|
|
|
pid_t riscos_getpid(void);
|
|
|
|
int riscos_kill(pid_t pid, int sig);
|
2001-08-20 13:53:05 +02:00
|
|
|
FILE *riscos_fopen(const char *filename, const char *mode);
|
|
|
|
int riscos_open(const char *filename, int oflag, ...);
|
|
|
|
int riscos_fstat(int fildes, struct stat *buf);
|
2002-01-02 18:48:27 +01:00
|
|
|
int riscos_access(const char *path, int amode);
|
2001-08-20 13:53:05 +02:00
|
|
|
int fdopenfile(const char *filename, const int allow_write);
|
|
|
|
void close_fds(void);
|
|
|
|
int renamefile(const char *old, const char *new);
|
|
|
|
char *gstrans(const char *old);
|
|
|
|
void not_implemented(const char *feature);
|
2002-01-02 18:48:27 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
void dump_fdlist(void);
|
|
|
|
void list_openfiles(void);
|
|
|
|
#endif
|
2001-08-20 13:53:05 +02:00
|
|
|
#ifndef __RISCOS__C__
|
2002-01-02 18:48:27 +01:00
|
|
|
#define getpid riscos_getpid
|
|
|
|
#define kill(a,b) riscos_kill((a),(b))
|
|
|
|
#define fopen(a,b) riscos_fopen((a),(b))
|
|
|
|
#define fstat(a,b) riscos_fstat((a),(b))
|
2001-08-20 13:53:05 +02:00
|
|
|
#define open riscos_open
|
2002-01-02 18:48:27 +01:00
|
|
|
#define access(a,b) riscos_access((a),(b))
|
2001-08-20 13:53:05 +02:00
|
|
|
#endif /* !__RISCOS__C__ */
|
|
|
|
#endif /* __riscos__ */
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
#endif /*G10_UTIL_H*/
|
2001-08-20 13:53:05 +02:00
|
|
|
|