1
/* miscutil.c - miscellaneous utilities
2
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG 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
* GnuPG 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 this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27
#ifdef HAVE_LANGINFO_H
35
* I know that the OpenPGP protocol has a Y2106 problem ;-)
44
* Scan a date string and return a timestamp.
45
* The only supported format is "yyyy-mm-dd"
46
* Returns 0 for an invalid date.
49
scan_isodatestr( const char *string )
56
if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
58
for( i=0; i < 4; i++ )
59
if( !isdigit(string[i]) )
61
if( !isdigit(string[5]) || !isdigit(string[6]) )
63
if( !isdigit(string[8]) || !isdigit(string[9]) )
66
month = atoi(string+5);
68
/* some basic checks */
69
if( year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 )
71
memset( &tmbuf, 0, sizeof tmbuf );
73
tmbuf.tm_mon = month-1;
74
tmbuf.tm_year = year - 1900;
76
stamp = mktime( &tmbuf );
77
if( stamp == (time_t)-1 )
84
add_days_to_timestamp( u32 stamp, u16 days )
86
return stamp + days*86400L;
91
* Return a string with a time value in the form: x Y, n D, n H
95
strtimevalue( u32 value )
97
static char buffer[30];
98
unsigned int years, days, hours, minutes;
101
minutes = value % 60;
109
sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
113
return strchr( buffer, 'y' ) + 1;
114
return strchr( buffer, 'd' ) + 1;
119
* Note: this function returns GMT
122
strtimestamp( u32 stamp )
124
static char buffer[11+5];
126
time_t atime = stamp;
129
strcpy (buffer, "????" "-??" "-??");
132
tp = gmtime( &atime );
133
sprintf(buffer,"%04d-%02d-%02d",
134
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
140
* Note: this function returns local time
143
asctimestamp( u32 stamp )
145
static char buffer[50];
146
#if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
150
time_t atime = stamp;
153
strcpy (buffer, "????" "-??" "-??");
157
tp = localtime( &atime );
159
#if defined(HAVE_NL_LANGINFO)
160
mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
161
if( strstr( fmt, "%Z" ) == NULL )
163
strftime( buffer, DIM(buffer)-1, fmt, tp );
165
/* fixme: we should check whether the locale appends a " %Z"
166
* These locales from glibc don't put the " %Z":
167
* fi_FI hr_HR ja_JP lt_LT lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN
169
strftime( buffer, DIM(buffer)-1, "%c %Z", tp );
171
buffer[DIM(buffer)-1] = 0;
173
mem2str( buffer, asctime(tp), DIM(buffer) );
179
* Print a string to FP, but filter all control characters out.
182
print_string( FILE *fp, const byte *p, size_t n, int delim )
185
if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
186
(delim && *p=='\\')) {
190
else if( *p == '\r' )
192
else if( *p == '\f' )
194
else if( *p == '\v' )
196
else if( *p == '\b' )
201
fprintf(fp, "x%02x", *p );
208
* Print an UTF8 string to FP and filter all control characters out.
211
print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim )
216
/* we can handle plain ascii simpler, so check for it first */
217
for(i=0; i < n; i++ ) {
222
buf = utf8_to_native ( p, n, delim );
223
/*(utf8 conversion already does the control character quoting)*/
228
print_string( fp, p, n, delim );
232
print_utf8_string( FILE *fp, const byte *p, size_t n )
234
print_utf8_string2 (fp, p, n, 0);
238
* This function returns a string which is suitable for printing
239
* Caller must release it with m_free()
242
make_printable_string( const byte *p, size_t n, int delim )
244
size_t save_n, buflen;
248
/* first count length */
249
for(save_n = n, save_p = p, buflen=1 ; n; n--, p++ ) {
250
if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
251
(delim && *p=='\\')) {
252
if( *p=='\n' || *p=='\r' || *p=='\f'
253
|| *p=='\v' || *p=='\b' || !*p )
263
/* and now make the string */
264
d = buffer = m_alloc( buflen );
265
for( ; n; n--, p++ ) {
266
if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
267
(delim && *p=='\\')) {
271
else if( *p == '\r' )
273
else if( *p == '\f' )
275
else if( *p == '\v' )
277
else if( *p == '\b' )
282
sprintf(d, "x%02x", *p );
294
answer_is_yes_no_default( const char *s, int def_answer )
296
const char *long_yes = _("yes");
297
const char *short_yes = _("yY");
298
const char *long_no = _("no");
299
const char *short_no = _("nN");
301
/* Note: we have to use the local dependent strcasecmp here */
302
if( !strcasecmp(s, long_yes ) )
304
if( *s && strchr( short_yes, *s ) && !s[1] )
306
/* test for no strings to catch ambiguities for the next test */
307
if( !strcasecmp(s, long_no ) )
309
if( *s && strchr( short_no, *s ) && !s[1] )
311
/* test for the english version (for those who are used to type yes) */
312
if( !ascii_strcasecmp(s, "yes" ) )
314
if( *s && strchr( "yY", *s ) && !s[1] )
320
answer_is_yes( const char *s )
322
return answer_is_yes_no_default(s,0);
326
* Return 1 for yes, -1 for quit, or 0 for no
329
answer_is_yes_no_quit( const char *s )
331
const char *long_yes = _("yes");
332
const char *long_no = _("no");
333
const char *long_quit = _("quit");
334
const char *short_yes = _("yY");
335
const char *short_no = _("nN");
336
const char *short_quit = _("qQ");
338
/* Note: We have to use the locale dependent strcasecmp */
339
if( !strcasecmp(s, long_no ) )
341
if( !strcasecmp(s, long_yes ) )
343
if( !strcasecmp(s, long_quit ) )
345
if( *s && strchr( short_no, *s ) && !s[1] )
347
if( *s && strchr( short_yes, *s ) && !s[1] )
349
if( *s && strchr( short_quit, *s ) && !s[1] )
352
if( !ascii_strcasecmp(s, "yes" ) )
354
if( !ascii_strcasecmp(s, "quit" ) )
356
if( *s && strchr( "yY", *s ) && !s[1] )
358
if( *s && strchr( "qQ", *s ) && !s[1] )