1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/* utils.c --- Auxilliary help functions.
* Copyright (C) 2002-2022 Simon Josefsson
*
* This file is part of Shishi.
*
* Shishi 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.
*
* Shishi 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 Shishi; if not, see http://www.gnu.org/licenses or write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA
*
*/
#include "internal.h"
/* Get prototypes. */
#include "utils.h"
void
_shishi_escapeprint (const char *str, int len)
{
int i;
printf ("\t ;; `");
for (i = 0; i < len; i++)
if ((str[i] >= 'A' && str[i] <= 'Z') ||
(str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= '0' && str[i] <= '9') || str[i] == '.')
printf ("%c", str[i] & 0xFF);
else
printf ("\\x%02x", (unsigned) str[i] & 0xFF);
printf ("' (length %d bytes)\n", len);
}
void
_shishi_hexprint (const char *str, int len)
{
int i;
printf ("\t ;; ");
for (i = 0; i < len; i++)
{
printf ("%02x ", (unsigned) str[i] & 0xFF);
if ((i + 1) % 8 == 0)
printf (" ");
if ((i + 1) % 16 == 0 && i + 1 < len)
printf ("\n\t ;; ");
}
puts ("");
}
void
_shishi_binprint (const char *str, int len)
{
int i;
printf ("\t ;; ");
for (i = 0; i < len; i++)
{
printf ("%d%d%d%d%d%d%d%d ",
str[i] & 0x80 ? 1 : 0,
str[i] & 0x40 ? 1 : 0,
str[i] & 0x20 ? 1 : 0,
str[i] & 0x10 ? 1 : 0,
str[i] & 0x08 ? 1 : 0,
str[i] & 0x04 ? 1 : 0,
str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
if ((i + 1) % 3 == 0)
printf (" ");
if ((i + 1) % 6 == 0 && i + 1 < len)
printf ("\n\t ;; ");
}
puts ("");
}
void
_shishi_bin7print (const char *str, int len)
{
int i;
printf ("\t ;; ");
for (i = 0; i < len; i++)
{
printf ("%d%d%d%d%d%d%d ",
str[i] & 0x40 ? 1 : 0,
str[i] & 0x20 ? 1 : 0,
str[i] & 0x10 ? 1 : 0,
str[i] & 0x08 ? 1 : 0,
str[i] & 0x04 ? 1 : 0,
str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
if ((i + 1) % 3 == 0)
printf (" ");
if ((i + 1) % 6 == 0 && i + 1 < len)
printf ("\n\t ;; ");
}
puts ("");
}
time_t
xtime (time_t * t)
{
time_t now;
now = time (t);
if (now == (time_t) - 1)
{
perror ("time");
abort ();
}
return now;
}
time_t
shishi_get_date (const char *p, const time_t * now)
{
struct timespec nowspec = { 0, 0 };
struct timespec thenspec;
if (now)
nowspec.tv_sec = *now;
else
nowspec.tv_sec = time (NULL);
if (!parse_datetime (&thenspec, p, &nowspec))
{
thenspec.tv_sec = (time_t) - 1;
thenspec.tv_nsec = 0;
}
return thenspec.tv_sec;
}
/* If non-NULL, call this function when memory is exhausted. */
void (*shishi_alloc_fail_function) (void) = 0;
void
shishi_xalloc_die (void)
{
if (shishi_alloc_fail_function)
(*shishi_alloc_fail_function) ();
fflush (stdout);
fprintf (stderr, _("%s: Memory allocation failed\n"), PACKAGE);
abort ();
}
|