Add.
[shishi.git] / lib / utils.c
blob291f9fac020d8e0b0e3472b436ec5ffdd2d504b8
1 /* utils.c --- Auxilliary help functions.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * 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.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 #include "internal.h"
25 /* Get prototypes. */
26 #include "utils.h"
28 void
29 _shishi_escapeprint (const char *str, int len)
31 int i;
33 printf ("\t ;; `");
34 for (i = 0; i < len; i++)
35 if ((str[i] >= 'A' && str[i] <= 'Z') ||
36 (str[i] >= 'a' && str[i] <= 'z') ||
37 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
38 printf ("%c", str[i] & 0xFF);
39 else
40 printf ("\\x%02x", str[i] & 0xFF);
41 printf ("' (length %d bytes)\n", len);
44 void
45 _shishi_hexprint (const char *str, int len)
47 int i;
49 printf ("\t ;; ");
50 for (i = 0; i < len; i++)
52 printf ("%02x ", str[i] & 0xFF);
53 if ((i + 1) % 8 == 0)
54 printf (" ");
55 if ((i + 1) % 16 == 0 && i + 1 < len)
56 printf ("\n\t ;; ");
58 puts ("");
61 void
62 _shishi_binprint (const char *str, int len)
64 int i;
66 printf ("\t ;; ");
67 for (i = 0; i < len; i++)
69 printf ("%d%d%d%d%d%d%d%d ",
70 str[i] & 0x80 ? 1 : 0,
71 str[i] & 0x40 ? 1 : 0,
72 str[i] & 0x20 ? 1 : 0,
73 str[i] & 0x10 ? 1 : 0,
74 str[i] & 0x08 ? 1 : 0,
75 str[i] & 0x04 ? 1 : 0,
76 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
77 if ((i + 1) % 3 == 0)
78 printf (" ");
79 if ((i + 1) % 6 == 0 && i + 1 < len)
80 printf ("\n\t ;; ");
82 puts ("");
85 void
86 _shishi_bin7print (const char *str, int len)
88 int i;
90 printf ("\t ;; ");
91 for (i = 0; i < len; i++)
93 printf ("%d%d%d%d%d%d%d ",
94 str[i] & 0x40 ? 1 : 0,
95 str[i] & 0x20 ? 1 : 0,
96 str[i] & 0x10 ? 1 : 0,
97 str[i] & 0x08 ? 1 : 0,
98 str[i] & 0x04 ? 1 : 0,
99 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
100 if ((i + 1) % 3 == 0)
101 printf (" ");
102 if ((i + 1) % 6 == 0 && i + 1 < len)
103 printf ("\n\t ;; ");
105 puts ("");
108 time_t
109 xtime (time_t * t)
111 time_t now;
113 now = time (t);
114 if (now == (time_t) - 1)
116 perror ("time");
117 abort ();
120 return now;
123 time_t
124 shishi_get_date (const char *p, const time_t * now)
126 struct timespec nowspec = { 0, 0 };
127 struct timespec thenspec;
129 if (now)
130 nowspec.tv_sec = *now;
131 else
132 nowspec.tv_sec = time (NULL);
134 if (!get_date (&thenspec, p, &nowspec))
136 thenspec.tv_sec = (time_t) - 1;
137 thenspec.tv_nsec = 0;
140 return thenspec.tv_sec;
143 /* If non-NULL, call this function when memory is exhausted. */
144 void (*shishi_alloc_fail_function) (void) = 0;
146 void
147 shishi_xalloc_die (void)
149 if (shishi_alloc_fail_function)
150 (*shishi_alloc_fail_function) ();
151 fflush (stdout);
152 fprintf (stderr, _("%s: Memory allocation failed\n"), PACKAGE);
153 abort ();