Fix length field.
[shishi.git] / lib / utils.c
bloba8c74e043cc04f3b015b4ed1b18b46b778234f11
1 /* utils.c --- Auxilliary help functions.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi 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 Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi 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.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "internal.h"
24 /* Get prototypes. */
25 #include "utils.h"
27 void
28 _shishi_escapeprint (const char *str, int len)
30 int i;
32 printf ("\t ;; `");
33 for (i = 0; i < len; i++)
34 if ((str[i] >= 'A' && str[i] <= 'Z') ||
35 (str[i] >= 'a' && str[i] <= 'z') ||
36 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
37 printf ("%c", str[i] & 0xFF);
38 else
39 printf ("\\x%02x", str[i] & 0xFF);
40 printf ("' (length %d bytes)\n", len);
43 void
44 _shishi_hexprint (const char *str, int len)
46 int i;
48 printf ("\t ;; ");
49 for (i = 0; i < len; i++)
51 printf ("%02x ", str[i] & 0xFF);
52 if ((i + 1) % 8 == 0)
53 printf (" ");
54 if ((i + 1) % 16 == 0 && i + 1 < len)
55 printf ("\n\t ;; ");
57 puts ("");
60 void
61 _shishi_binprint (const char *str, int len)
63 int i;
65 printf ("\t ;; ");
66 for (i = 0; i < len; i++)
68 printf ("%d%d%d%d%d%d%d%d ",
69 str[i] & 0x80 ? 1 : 0,
70 str[i] & 0x40 ? 1 : 0,
71 str[i] & 0x20 ? 1 : 0,
72 str[i] & 0x10 ? 1 : 0,
73 str[i] & 0x08 ? 1 : 0,
74 str[i] & 0x04 ? 1 : 0,
75 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
76 if ((i + 1) % 3 == 0)
77 printf (" ");
78 if ((i + 1) % 6 == 0 && i + 1 < len)
79 printf ("\n\t ;; ");
81 puts ("");
84 void
85 _shishi_bin7print (const char *str, int len)
87 int i;
89 printf ("\t ;; ");
90 for (i = 0; i < len; i++)
92 printf ("%d%d%d%d%d%d%d ",
93 str[i] & 0x40 ? 1 : 0,
94 str[i] & 0x20 ? 1 : 0,
95 str[i] & 0x10 ? 1 : 0,
96 str[i] & 0x08 ? 1 : 0,
97 str[i] & 0x04 ? 1 : 0,
98 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
99 if ((i + 1) % 3 == 0)
100 printf (" ");
101 if ((i + 1) % 6 == 0 && i + 1 < len)
102 printf ("\n\t ;; ");
104 puts ("");
107 time_t
108 xtime (time_t * t)
110 time_t now;
112 now = time (t);
113 if (now == (time_t) - 1)
115 perror ("time");
116 abort ();
119 return now;
122 time_t
123 shishi_get_date (const char *p, const time_t * now)
125 struct timespec nowspec = { 0, 0 };
126 struct timespec thenspec;
128 if (now)
129 nowspec.tv_sec = *now;
130 else
131 nowspec.tv_sec = time (NULL);
133 if (!get_date (&thenspec, p, &nowspec))
135 thenspec.tv_sec = (time_t) - 1;
136 thenspec.tv_nsec = 0;
139 return thenspec.tv_sec;
142 /* If non-NULL, call this function when memory is exhausted. */
143 void (*shishi_alloc_fail_function) (void) = 0;
145 void
146 shishi_xalloc_die (void)
148 if (shishi_alloc_fail_function)
149 (*shishi_alloc_fail_function) ();
150 fflush (stdout);
151 fprintf (stderr, _("%s: Memory allocation failed\n"), PACKAGE);
152 abort ();