diff options
author | Werner Koch <[email protected]> | 1999-12-08 21:03:03 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 1999-12-08 21:03:03 +0000 |
commit | 4555c0be941cef55486b7a1644d9b70f4ea50c77 (patch) | |
tree | 1a10627bf31163f9aef5040724c53f75e8daf3e6 /util | |
parent | See ChangeLog: Fri Nov 19 17:15:20 CET 1999 Werner Koch (diff) | |
download | gnupg-4555c0be941cef55486b7a1644d9b70f4ea50c77.tar.gz gnupg-4555c0be941cef55486b7a1644d9b70f4ea50c77.zip |
See ChangeLog: Wed Dec 8 21:58:32 CET 1999 Werner Koch
Diffstat (limited to 'util')
-rw-r--r-- | util/ChangeLog | 18 | ||||
-rw-r--r-- | util/Makefile.am | 13 | ||||
-rw-r--r-- | util/argparse.c | 28 | ||||
-rw-r--r-- | util/argparse.h | 64 | ||||
-rw-r--r-- | util/libutil-config.h | 56 | ||||
-rw-r--r-- | util/logging.c | 170 | ||||
-rw-r--r-- | util/logging.h | 41 | ||||
-rw-r--r-- | util/mischelp.h | 42 | ||||
-rw-r--r-- | util/strgutil.c | 154 | ||||
-rw-r--r-- | util/stringhelp.c | 197 | ||||
-rw-r--r-- | util/stringhelp.h | 59 | ||||
-rw-r--r-- | util/xmalloc.c | 70 | ||||
-rw-r--r-- | util/xmalloc.h | 30 |
13 files changed, 773 insertions, 169 deletions
diff --git a/util/ChangeLog b/util/ChangeLog index ea9c7f3fe..aadb8b4d0 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,21 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <[email protected]> + + * strgutil.c (strcasecmp): New. + + * argparse.h: New. + * libutil.h: New. + * argparse.c: Use these new files. + (optfile_parse): s/m_alloc/libutil_xalloc/ + + * strgutil.c: Moved a lot of function to ... + * stringhelp.c: ... this new file + * stringhelp.h: ... and the definitions to here + + * mischelp.h: New. + + * logging.h: New, but not yet used in GnuPG. + * logging.c: Ditto. + Fri Nov 19 17:15:20 CET 1999 Werner Koch <[email protected]> * argparse.c (default_strusage): Renamed to strusage. Fall back diff --git a/util/Makefile.am b/util/Makefile.am index c695db586..acb8f05cb 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -1,5 +1,8 @@ ## Process this file with automake to produce Makefile.in +# Those 2 files are in the CVS but currently not used. +EXTRA_DIST = xmalloc.c xmalloc.h logging.c logging.c + INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl noinst_LTLIBRARIES = libutil.la @@ -7,8 +10,12 @@ noinst_LTLIBRARIES = libutil.la libutil_la_LDFLAGS = libutil_la_SOURCES = logger.c fileutil.c miscutil.c strgutil.c \ - ttyio.c argparse.c memory.c secmem.c errors.c iobuf.c \ - dotlock.c http.c simple-gettext.c + ttyio.c memory.c secmem.c errors.c iobuf.c \ + dotlock.c http.c simple-gettext.c \ + libutil-config.h \ + mischelp.h \ + stringhelp.h stringhelp.c \ + argparse.h argparse.c http-test: http.c @@ -17,3 +24,5 @@ http-test: http.c + + diff --git a/util/argparse.c b/util/argparse.c index 12b0ebd7f..ba51980a9 100644 --- a/util/argparse.c +++ b/util/argparse.c @@ -15,9 +15,6 @@ * 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 - * - * - * Note: This is an independent version of the one in WkLib */ #include <config.h> @@ -26,8 +23,13 @@ #include <ctype.h> #include <string.h> -#include "util.h" -#include "i18n.h" +#include "libutil-config.h" +#include "mischelp.h" +#include "stringhelp.h" +#ifndef LIBUTIL_CONFIG_OF_GNUPG + #include "logging.h" /* currently not used in GnUPG */ +#endif +#include "argparse.h" /********************************* @@ -155,7 +157,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) arg->err = 0; arg->flags |= 1<<15; /* mark initialized */ if( *arg->argc < 0 ) - log_bug("Invalid argument for ArgParse\n"); + libutil_log_bug("Invalid argument for ArgParse\n"); } @@ -177,7 +179,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) s = "%s:%u: invalid alias definition\n"; else s = "%s:%u: invalid option\n"; - log_error(s, filename, *lineno ); + libutil_log_error(s, filename, *lineno ); } else { if( arg->r_opt == -3 ) @@ -192,7 +194,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) s = "Command \"%.50s\" is ambiguous\n"; else s = "Invalid option \"%.50s\"\n"; - log_error(s, arg->internal.last? arg->internal.last:"[??]" ); + libutil_log_error(s, arg->internal.last? arg->internal.last:"[??]" ); } if( arg->err != 1 ) exit(2); @@ -306,7 +308,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, trim_spaces( p ); } if( !p || !*p ) { - m_free( buffer ); + libutil_free( buffer ); arg->r_opt = -10; } else { @@ -320,7 +322,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, char *p; if( !buffer ) { keyword[i] = 0; - buffer = m_strdup(keyword); + buffer = libutil_strdup(keyword); } else buffer[i] = 0; @@ -333,7 +335,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, p[strlen(p)-1] = 0; } if( !set_opt_arg(arg, opts[idx].flags, p) ) - m_free(buffer); + libutil_free(buffer); } break; } @@ -388,7 +390,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, buffer[i++] = c; else { buflen += 50; - buffer = m_realloc(buffer, buflen); + buffer = libutil_realloc(buffer, buflen); buffer[i++] = c; } } @@ -396,7 +398,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, keyword[i++] = c; else { buflen = DIM(keyword)+50; - buffer = m_alloc(buflen); + buffer = libutil_xmalloc(buflen); memcpy(buffer, keyword, i); buffer[i++] = c; } diff --git a/util/argparse.h b/util/argparse.h new file mode 100644 index 000000000..12132edd3 --- /dev/null +++ b/util/argparse.h @@ -0,0 +1,64 @@ +/* argparse.h + * Copyright (C) 1998,1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBUTIL_ARGPARSE_H +#define LIBUTIL_ARGPARSE_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 { + int idx; + int inarg; + int stopped; + const char *last; + void *aliases; + const void *cur_alias; + } 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; + + + +int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); +int optfile_parse( FILE *fp, const char *filename, unsigned *lineno, + ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); +void usage( int level ); +const char *strusage( int level ); +void set_strusage( const char *(*f)( int ) ); + +#endif /*LIBUTIL_ARGPARSE_H*/ diff --git a/util/libutil-config.h b/util/libutil-config.h new file mode 100644 index 000000000..a4c80115b --- /dev/null +++ b/util/libutil-config.h @@ -0,0 +1,56 @@ +/* libutil-config.h - configuration of the libutil functions + * Copyright (C) 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +/**************** + * This header is to be included only by the files in this directory + * it should not be used by other modules. + */ + +#ifndef LIBUTIL_CONFIG_H +#define LIBUTIL_CONFIG_H + +#define LIBUTIL_CONFIG_OF_GNUPG 1 /* currently we need this kludge */ + +#include <sys/types.h> + +#ifndef HAVE_BYTE_TYPEDEF + #undef byte /* (this matches the test used by configure) */ + typedef unsigned char byte; + #define HAVE_BYTE_TYPEDEF +#endif + +#include "types.h" +#include "memory.h" +#include "util.h" +#include "i18n.h" + +#define libutil_xmalloc(a) m_alloc( (a) ) +#define libutil_realloc(a,n) m_realloc( (a), (n) ) +#define libutil_strdup(a) m_strdup( (a) ) +#define libutil_free(a) m_free( (a) ) + +#define libutil_log_debug log_debug +#define libutil_log_info log_info +#define libutil_log_error log_error +#define libutil_log_fatal log_fatal +#define libutil_log_bug log_bug + + +#endif /*LIBUTIL_CONFIGH*/ diff --git a/util/logging.c b/util/logging.c new file mode 100644 index 000000000..76931f9be --- /dev/null +++ b/util/logging.c @@ -0,0 +1,170 @@ +/* logging.c - useful logging functions + * Copyright (C) 1998, 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + + +/* This file should replace logger.c in the future - for now it is not + * used GnuPG. + * It is a quite simple implemenation but sufficient for most purposes. + */ + +#include <config.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "libutil-config.h" +#include "logging.h" + +enum my_log_levels { + MY_LOG_CONT, + MY_LOG_INFO, + MY_LOG_WARN, + MY_LOG_ERROR, + MY_LOG_FATAL, + MY_LOG_BUG, + MY_LOG_DEBUG +}; + + +#if 0 +static void +write2stderr( const char *s ) +{ + write( 2, s, strlen(s) ); +} + + +static void +do_die(int rc, const char *text ) +{ + write2stderr("\nFatal error: "); + write2stderr(text); + write2stderr("\n"); + abort(); +} +#endif + + +static void +do_logv( int level, const char *fmt, va_list arg_ptr ) +{ + switch ( level ) { + case MY_LOG_CONT: break; + case MY_LOG_INFO: break; + case MY_LOG_WARN: break; + case MY_LOG_ERROR: break; + case MY_LOG_FATAL: fputs("Fatal: ",stderr ); break; + case MY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; + case MY_LOG_DEBUG: fputs("DBG: ", stderr ); break; + default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; + } + vfprintf(stderr,fmt,arg_ptr) ; + + if( level == MY_LOG_FATAL ) + exit(2); + if( level == MY_LOG_BUG ) + abort(); +} + +static void +do_log( int level, const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( level, fmt, arg_ptr ); + va_end(arg_ptr); +} + + + +void +log_info( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_INFO, fmt, arg_ptr ); + va_end(arg_ptr); +} + +void +log_error( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_ERROR, fmt, arg_ptr ); + va_end(arg_ptr); +} + + +void +log_fatal( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_FATAL, fmt, arg_ptr ); + va_end(arg_ptr); + abort(); /* never called, bugs it makes the compiler happy */ +} + +void +log_bug( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_BUG, fmt, arg_ptr ); + va_end(arg_ptr); + abort(); /* never called, bugs it makes the compiler happy */ +} + +void +log_debug( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_DEBUG, fmt, arg_ptr ); + va_end(arg_ptr); +} + + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) +void +bug_at( const char *file, int line, const char *func ) +{ + do_log( MY_LOG_BUG, + ("... this is a bug (%s:%d:%s)\n"), file, line, func ); + abort(); /* never called, but it makes the compiler happy */ +} +#else +void +bug_at( const char *file, int line ) +{ + do_log( MY_LOG_BUG, + _("you found a bug ... (%s:%d)\n"), file, line); + abort(); /* never called, but it makes the compiler happy */ +} +#endif + diff --git a/util/logging.h b/util/logging.h new file mode 100644 index 000000000..961751b52 --- /dev/null +++ b/util/logging.h @@ -0,0 +1,41 @@ +/* logging.h + * Copyright (C) 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBUTIL_LOGGING_H +#define LIBUTIL_LOGGING_H + +#include "mischelp.h" + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) + void bug_at( const char *file, int line, const char *func ) LIBUTIL_GCC_A_NR; +# define BUG() bug_at( __FILE__ , __LINE__, __FUNCTION__ ) +#else + void bug_at( const char *file, int line ); +# define BUG() bug_at( __FILE__ , __LINE__ ) +#endif + +void log_bug( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); +void log_fatal( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); +void log_error( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); +void log_info( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); +void log_debug( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); + + +#endif /*LIBUTIL_LOGGING_H*/ diff --git a/util/mischelp.h b/util/mischelp.h new file mode 100644 index 000000000..1c83da1be --- /dev/null +++ b/util/mischelp.h @@ -0,0 +1,42 @@ +/* mischelp.h + * Copyright (C) 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBUTIL_MISCHELP_H +#define LIBUTIL_MISCHHELP_H + + +#define DIM(v) (sizeof(v)/sizeof((v)[0])) +#define DIMof(type,member) DIM(((type *)0)->member) + + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) +# define LIBUTIL_GCC_A_NR __attribute__ ((noreturn)) +# define LIBUTIL_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) +# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) \ + __attribute__ ((noreturn, format (printf,f,a))) +#else +# define LIBUTIL_GCC_A_NR +# define LIBUTIL_GCC_A_PRINTF( f, a ) +# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) +#endif + + + +#endif /*LIBUTIL_MISCHELP_H*/ diff --git a/util/strgutil.c b/util/strgutil.c index ea9527bd8..a20ba2c75 100644 --- a/util/strgutil.c +++ b/util/strgutil.c @@ -189,117 +189,6 @@ strlist_last( STRLIST node ) -/**************** - * look for the substring SUB in buffer and return a pointer to that - * substring in BUF or NULL if not found. - * Comparison is case-insensitive. - */ -const char * -memistr( const char *buf, size_t buflen, const char *sub ) -{ - const byte *t, *s ; - size_t n; - - for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) - if( toupper(*t) == toupper(*s) ) { - for( buf=t++, buflen = n--, s++; - n && toupper(*t) == toupper(*s); t++, s++, n-- ) - ; - if( !*s ) - return buf; - t = buf; n = buflen; s = sub ; - } - - return NULL ; -} - -/**************** - * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein - * '\0' angeh�ngt. Ist n = 0, so geschieht nichts, ist Destination - * gleich NULL, so wird via m_alloc Speicher besorgt, ist dann nicht - * gen�gend Speicher vorhanden, so bricht die funktion ab. - */ -char * -mem2str( char *dest , const void *src , size_t n ) -{ - char *d; - const char *s; - - if( n ) { - if( !dest ) - dest = m_alloc( n ) ; - d = dest; - s = src ; - for(n--; n && *s; n-- ) - *d++ = *s++; - *d = '\0' ; - } - - return dest ; -} - - -/**************** - * remove leading and trailing white spaces - */ -char * -trim_spaces( char *str ) -{ - char *string, *p, *mark; - - string = str; - /* find first non space character */ - for( p=string; *p && isspace( *(byte*)p ) ; p++ ) - ; - /* move characters */ - for( (mark = NULL); (*string = *p); string++, p++ ) - if( isspace( *(byte*)p ) ) { - if( !mark ) - mark = string ; - } - else - mark = NULL ; - if( mark ) - *mark = '\0' ; /* remove trailing spaces */ - - return str ; -} - - - -unsigned -trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) -{ - byte *p, *mark; - unsigned n; - - for(mark=NULL, p=line, n=0; n < len; n++, p++ ) { - if( strchr(trimchars, *p ) ) { - if( !mark ) - mark = p; - } - else - mark = NULL; - } - - if( mark ) { - *mark = 0; - return mark - line; - } - return len; -} - -/**************** - * remove trailing white spaces and return the length of the buffer - */ -unsigned -trim_trailing_ws( byte *line, unsigned len ) -{ - return trim_trailing_chars( line, len, " \t\r\n" ); -} - - - int string_count_chr( const char *string, int c ) { @@ -606,46 +495,3 @@ utf8_to_native( const char *string, size_t length ) } - -/********************************************* - ********** missing string functions ********* - *********************************************/ - -#ifndef HAVE_STPCPY -char * -stpcpy(char *a,const char *b) -{ - while( *b ) - *a++ = *b++; - *a = 0; - - return (char*)a; -} -#endif - -#ifndef HAVE_STRLWR -char * -strlwr(char *s) -{ - char *p; - for(p=s; *p; p++ ) - *p = tolower(*p); - return s; -} -#endif - -/**************** - * mingw32/cpd has a memicmp() - */ -#ifndef HAVE_MEMICMP -int -memicmp( const char *a, const char *b, size_t n ) -{ - for( ; n; n--, a++, b++ ) - if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) ) - return *(const byte *)a - *(const byte*)b; - return 0; -} -#endif - - diff --git a/util/stringhelp.c b/util/stringhelp.c new file mode 100644 index 000000000..16908f9a3 --- /dev/null +++ b/util/stringhelp.c @@ -0,0 +1,197 @@ +/* stringhelp.c - standard string helper functions + * Copyright (C) 1998, 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "libutil-config.h" +#include "stringhelp.h" + + +/**************** + * look for the substring SUB in buffer and return a pointer to that + * substring in BUF or NULL if not found. + * Comparison is case-insensitive. + */ +const char * +memistr( const char *buf, size_t buflen, const char *sub ) +{ + const byte *t, *s ; + size_t n; + + for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) + if( toupper(*t) == toupper(*s) ) { + for( buf=t++, buflen = n--, s++; + n && toupper(*t) == toupper(*s); t++, s++, n-- ) + ; + if( !*s ) + return buf; + t = buf; n = buflen; s = sub ; + } + + return NULL ; +} + +/**************** + * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein + * '\0' angeh�ngt. Ist n = 0, so geschieht nichts, ist Destination + * gleich NULL, so wird via libutil_malloc Speicher besorgt, ist dann nicht + * gen�gend Speicher vorhanden, so bricht die funktion ab. + */ +char * +mem2str( char *dest , const void *src , size_t n ) +{ + char *d; + const char *s; + + if( n ) { + if( !dest ) + dest = libutil_xmalloc( n ) ; + d = dest; + s = src ; + for(n--; n && *s; n-- ) + *d++ = *s++; + *d = '\0' ; + } + + return dest ; +} + + +/**************** + * remove leading and trailing white spaces + */ +char * +trim_spaces( char *str ) +{ + char *string, *p, *mark; + + string = str; + /* find first non space character */ + for( p=string; *p && isspace( *(byte*)p ) ; p++ ) + ; + /* move characters */ + for( (mark = NULL); (*string = *p); string++, p++ ) + if( isspace( *(byte*)p ) ) { + if( !mark ) + mark = string ; + } + else + mark = NULL ; + if( mark ) + *mark = '\0' ; /* remove trailing spaces */ + + return str ; +} + + + +unsigned +trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) +{ + byte *p, *mark; + unsigned n; + + for(mark=NULL, p=line, n=0; n < len; n++, p++ ) { + if( strchr(trimchars, *p ) ) { + if( !mark ) + mark = p; + } + else + mark = NULL; + } + + if( mark ) { + *mark = 0; + return mark - line; + } + return len; +} + +/**************** + * remove trailing white spaces and return the length of the buffer + */ +unsigned +trim_trailing_ws( byte *line, unsigned len ) +{ + return trim_trailing_chars( line, len, " \t\r\n" ); +} + + + +/********************************************* + ********** missing string functions ********* + *********************************************/ + +#ifndef HAVE_STPCPY +char * +stpcpy(char *a,const char *b) +{ + while( *b ) + *a++ = *b++; + *a = 0; + + return (char*)a; +} +#endif + +#ifndef HAVE_STRLWR +char * +strlwr(char *s) +{ + char *p; + for(p=s; *p; p++ ) + *p = tolower(*p); + return s; +} +#endif + + +#ifndef HAVE_STRCASECMP +int +strcasecmp( const char *a, const char *b ) +{ + for( ; *a && *b; a++, b++ ) { + if( *a != *b && toupper(*a) != toupper(*b) ) + break; + } + return *(const byte*)a - *(const byte*)b; +} +#endif + + +/**************** + * mingw32/cpd has a memicmp() + */ +#ifndef HAVE_MEMICMP +int +memicmp( const char *a, const char *b, size_t n ) +{ + for( ; n; n--, a++, b++ ) + if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) ) + return *(const byte *)a - *(const byte*)b; + return 0; +} +#endif + + + diff --git a/util/stringhelp.h b/util/stringhelp.h new file mode 100644 index 000000000..98b3ad551 --- /dev/null +++ b/util/stringhelp.h @@ -0,0 +1,59 @@ +/* stringhelp.h + * Copyright (C) 1998,1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBUTIL_STRINGHELP_H +#define LIBUTIL_STRINGHELP_H + + +const char *memistr( const char *buf, size_t buflen, const char *sub ); +char *mem2str( char *, const void *, size_t); +char *trim_spaces( char *string ); +unsigned int trim_trailing_chars( byte *line, unsigned len, + const char *trimchars); +unsigned int trim_trailing_ws( byte *line, unsigned len ); + + +#ifndef HAVE_MEMICMP +int memicmp( const char *a, const char *b, size_t n ); +#endif +#ifndef HAVE_STPCPY +char *stpcpy(char *a,const char *b); +#endif +#ifndef HAVE_STRLWR +char *strlwr(char *a); +#endif +#ifndef HAVE_STRTOUL + #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) +#endif +#ifndef HAVE_MEMMOVE + #define memmove(d, s, n) bcopy((s), (d), (n)) +#endif +#ifndef HAVE_STRICMP + #define stricmp(a,b) strcasecmp( (a), (b) ) +#endif + + +#ifndef STR + #define STR(v) #v +#endif +#define STR2(v) STR(v) + + +#endif /*LIBUTIL_STRINGHELP_H*/ diff --git a/util/xmalloc.c b/util/xmalloc.c new file mode 100644 index 000000000..21e23d0dc --- /dev/null +++ b/util/xmalloc.c @@ -0,0 +1,70 @@ +/* xmalloc.c - standard malloc wrappers + * Copyright (C) 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <stdio.h> + +#include "libutil-config.h" +#include "xmalloc.h" + +static void +out_of_core(void) +{ + fputs("\nfatal: out of memory\n", stderr ); + exit(2); +} + + +void * +xmalloc( size_t n ) +{ + void *p = malloc( n ); + if( !p ) + out_of_core(); + return p; +} + +void * +xrealloc( void *a, size_t n ) +{ + void *p = realloc( a, n ); + if( !p ) + out_of_core(); + return p; +} + +void * +xcalloc( size_t n, size_t m ) +{ + void *p = calloc( n, m ); + if( !p ) + out_of_core(); + return p; +} + +char * +xstrdup( const char *string ) +{ + void *p = xmalloc( strlen(string)+1 ); + strcpy( p, string ); + return p; +} + diff --git a/util/xmalloc.h b/util/xmalloc.h new file mode 100644 index 000000000..4a33d36a5 --- /dev/null +++ b/util/xmalloc.h @@ -0,0 +1,30 @@ +/* xmalloc.h + * Copyright (C) 1999 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG 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 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBUTIL_XMALLOC_H +#define LIBUTIL_XMALLOC_H + +void *xmalloc( size_t n ); +void *xrealloc( void *a, size_t n ); +void *xcalloc( size_t n, size_t m ); +char *xstrdup( const char *string ); + + +#endif /*LIBUTIL_XMALLOC_H*/ |