| 1 |
/* gen-help.c - Helper functions used by build time tools |
| 2 |
* Copyright (C) 2010 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Fountion; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* KSBA is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* No config.h - this file needs to build as plain ISO-C. */ |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include "gen-help.h" |
| 26 |
|
| 27 |
|
| 28 |
static void |
| 29 |
out_of_core(void) |
| 30 |
{ |
| 31 |
fputs ("\nfatal: out of memory\n", stderr); |
| 32 |
exit (2); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/* Implementation of the common xfoo() memory allocation functions */ |
| 38 |
void * |
| 39 |
xmalloc (size_t n ) |
| 40 |
{ |
| 41 |
void *p = malloc (n); |
| 42 |
if (!p) |
| 43 |
out_of_core (); |
| 44 |
return p; |
| 45 |
} |
| 46 |
|
| 47 |
void * |
| 48 |
xcalloc (size_t n, size_t m) |
| 49 |
{ |
| 50 |
void *p = calloc (n, m); |
| 51 |
if (!p) |
| 52 |
out_of_core (); |
| 53 |
return p; |
| 54 |
} |
| 55 |
|
| 56 |
void * |
| 57 |
xrealloc (void *mem, size_t n) |
| 58 |
{ |
| 59 |
void *p = realloc (mem, n); |
| 60 |
if (!p) |
| 61 |
out_of_core (); |
| 62 |
return p; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
char * |
| 67 |
xstrdup (const char *str) |
| 68 |
{ |
| 69 |
char *p = strdup (str); |
| 70 |
if (!p) |
| 71 |
out_of_core (); |
| 72 |
return p; |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
xfree (void *a) |
| 77 |
{ |
| 78 |
if (a) |
| 79 |
free (a); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/* Our version of stpcpy to avoid conflicts with already availabale |
| 84 |
implementations. */ |
| 85 |
char * |
| 86 |
gen_help_stpcpy (char *a, const char *b) |
| 87 |
{ |
| 88 |
while (*b) |
| 89 |
*a++ = *b++; |
| 90 |
*a = 0; |
| 91 |
|
| 92 |
return a; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/* Simple replacement function to avoid the need for a build libgpg-error */ |
| 97 |
const char * |
| 98 |
gpg_strerror (int err) |
| 99 |
{ |
| 100 |
switch (err) |
| 101 |
{ |
| 102 |
case 0: return "Success"; |
| 103 |
case GPG_ERR_GENERAL: return "General error"; |
| 104 |
case GPG_ERR_SYNTAX: return "Syntax error"; |
| 105 |
case GPG_ERR_INV_VALUE: return "Invalid value"; |
| 106 |
case GPG_ERR_BUG: return "Bug"; |
| 107 |
case GPG_ERR_ELEMENT_NOT_FOUND: return "Not found"; |
| 108 |
case GPG_ERR_IDENTIFIER_NOT_FOUND: return "Identifier not found"; |
| 109 |
default: return "Unknown error"; |
| 110 |
} |
| 111 |
} |