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