2003-01-09 12:36:05 +00:00
|
|
|
|
/* stringhelp.c - standard string helper functions
|
2010-03-24 12:15:30 +00:00
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
|
|
|
|
|
* 2008, 2009, 2010 Free Software Foundation, Inc.
|
2014-04-15 16:40:48 +02:00
|
|
|
|
* Copyright (C) 2014 Werner Koch
|
2015-03-12 13:03:50 +01:00
|
|
|
|
* Copyright (C) 2015 g10 Code GmbH
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
|
* This file is part of GnuPG.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify it
|
2011-09-30 12:52:11 +02:00
|
|
|
|
* under the terms of either
|
|
|
|
|
*
|
|
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* or
|
|
|
|
|
*
|
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* or both in parallel, as here.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
|
* GnuPG is distributed in the hope that it will be useful, but
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2011-09-30 12:52:11 +02:00
|
|
|
|
* General Public License for more details.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2011-09-30 12:52:11 +02:00
|
|
|
|
* You should have received a copies of the GNU General Public License
|
|
|
|
|
* and the GNU Lesser General Public License along with this program;
|
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <ctype.h>
|
2009-08-25 20:19:37 +00:00
|
|
|
|
#include <errno.h>
|
2009-08-26 08:55:57 +00:00
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
|
# include <pwd.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
2004-12-13 15:49:56 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
|
# include <winsock2.h>
|
|
|
|
|
# endif
|
2009-08-26 08:55:57 +00:00
|
|
|
|
# include <windows.h>
|
2004-12-13 15:49:56 +00:00
|
|
|
|
#endif
|
2015-06-22 19:28:33 +02:00
|
|
|
|
#include <assert.h>
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2015-03-12 13:03:50 +01:00
|
|
|
|
#include "util.h"
|
2015-04-24 15:19:10 +02:00
|
|
|
|
#include "common-defs.h"
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#include "utf8conv.h"
|
2014-04-15 16:40:48 +02:00
|
|
|
|
#include "sysutils.h"
|
2003-01-09 12:36:05 +00:00
|
|
|
|
#include "stringhelp.h"
|
|
|
|
|
|
2007-06-19 09:11:11 +00:00
|
|
|
|
#define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
|
|
|
|
|
|
2008-10-29 17:24:27 +00:00
|
|
|
|
/* Sometimes we want to avoid mixing slashes and backslashes on W32
|
|
|
|
|
and prefer backslashes. There is usual no problem with mixing
|
|
|
|
|
them, however a very few W32 API calls can't grok plain slashes.
|
|
|
|
|
Printing filenames with mixed slashes also looks a bit strange.
|
|
|
|
|
This function has no effext on POSIX. */
|
|
|
|
|
static inline char *
|
|
|
|
|
change_slashes (char *name)
|
|
|
|
|
{
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2008-10-29 17:24:27 +00:00
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
if (strchr (name, '\\'))
|
|
|
|
|
{
|
|
|
|
|
for (p=name; *p; p++)
|
|
|
|
|
if (*p == '/')
|
|
|
|
|
*p = '\\';
|
|
|
|
|
}
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#endif /*HAVE_DOSISH_SYSTEM*/
|
2008-10-29 17:24:27 +00:00
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-02-21 20:25:12 +01:00
|
|
|
|
/*
|
2014-03-14 16:12:54 +01:00
|
|
|
|
* Check whether STRING starts with KEYWORD. The keyword is
|
2013-02-21 20:25:12 +01:00
|
|
|
|
* delimited by end of string, a space or a tab. Returns NULL if not
|
|
|
|
|
* found or a pointer into STRING to the next non-space character
|
|
|
|
|
* after the KEYWORD (which may be end of string).
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
has_leading_keyword (const char *string, const char *keyword)
|
|
|
|
|
{
|
|
|
|
|
size_t n = strlen (keyword);
|
|
|
|
|
|
|
|
|
|
if (!strncmp (string, keyword, n)
|
|
|
|
|
&& (!string[n] || string[n] == ' ' || string[n] == '\t'))
|
|
|
|
|
{
|
|
|
|
|
string += n;
|
|
|
|
|
while (*string == ' ' || *string == '\t')
|
|
|
|
|
string++;
|
|
|
|
|
return (char*)string;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Look for the substring SUB in buffer and return a pointer to that
|
2005-06-16 08:12:03 +00:00
|
|
|
|
* substring in BUFFER or NULL if not found.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
* Comparison is case-insensitive.
|
|
|
|
|
*/
|
|
|
|
|
const char *
|
2005-06-16 08:12:03 +00:00
|
|
|
|
memistr (const void *buffer, size_t buflen, const char *sub)
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const unsigned char *buf = buffer;
|
|
|
|
|
const unsigned char *t = (const unsigned char *)buffer;
|
|
|
|
|
const unsigned char *s = (const unsigned char *)sub;
|
|
|
|
|
size_t n = buflen;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
|
for ( ; n ; t++, n-- )
|
|
|
|
|
{
|
|
|
|
|
if ( toupper (*t) == toupper (*s) )
|
|
|
|
|
{
|
|
|
|
|
for ( buf=t++, buflen = n--, s++;
|
|
|
|
|
n && toupper (*t) == toupper (*s); t++, s++, n-- )
|
|
|
|
|
;
|
|
|
|
|
if (!*s)
|
|
|
|
|
return (const char*)buf;
|
|
|
|
|
t = buf;
|
|
|
|
|
s = (const unsigned char *)sub ;
|
|
|
|
|
n = buflen;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2005-06-16 08:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
const char *
|
2005-06-16 08:12:03 +00:00
|
|
|
|
ascii_memistr ( const void *buffer, size_t buflen, const char *sub )
|
2003-06-18 19:56:13 +00:00
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const unsigned char *buf = buffer;
|
|
|
|
|
const unsigned char *t = (const unsigned char *)buf;
|
|
|
|
|
const unsigned char *s = (const unsigned char *)sub;
|
|
|
|
|
size_t n = buflen;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
|
for ( ; n ; t++, n-- )
|
|
|
|
|
{
|
|
|
|
|
if (ascii_toupper (*t) == ascii_toupper (*s) )
|
|
|
|
|
{
|
|
|
|
|
for ( buf=t++, buflen = n--, s++;
|
|
|
|
|
n && ascii_toupper (*t) == ascii_toupper (*s); t++, s++, n-- )
|
|
|
|
|
;
|
|
|
|
|
if (!*s)
|
|
|
|
|
return (const char*)buf;
|
|
|
|
|
t = (const unsigned char *)buf;
|
|
|
|
|
s = (const unsigned char *)sub ;
|
|
|
|
|
n = buflen;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
2005-06-16 08:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
|
/* This function is similar to strncpy(). However it won't copy more
|
|
|
|
|
than N - 1 characters and makes sure that a '\0' is appended. With
|
|
|
|
|
N given as 0, nothing will happen. With DEST given as NULL, memory
|
2015-03-12 13:45:27 +01:00
|
|
|
|
will be allocated using xmalloc (i.e. if it runs out of core
|
2004-12-13 15:49:56 +00:00
|
|
|
|
the function terminates). Returns DES or a pointer to the
|
|
|
|
|
allocated memory.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
mem2str( char *dest , const void *src , size_t n )
|
|
|
|
|
{
|
|
|
|
|
char *d;
|
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
|
|
if( n ) {
|
|
|
|
|
if( !dest )
|
2015-03-12 13:45:27 +01:00
|
|
|
|
dest = xmalloc( n ) ;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
d = dest;
|
|
|
|
|
s = src ;
|
|
|
|
|
for(n--; n && *s; n-- )
|
|
|
|
|
*d++ = *s++;
|
|
|
|
|
*d = '\0' ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dest ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* remove leading and trailing white spaces
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
trim_spaces( char *str )
|
|
|
|
|
{
|
|
|
|
|
char *string, *p, *mark;
|
|
|
|
|
|
|
|
|
|
string = str;
|
|
|
|
|
/* find first non space character */
|
|
|
|
|
for( p=string; *p && isspace( *(byte*)p ) ; p++ )
|
|
|
|
|
;
|
|
|
|
|
/* move characters */
|
|
|
|
|
for( (mark = NULL); (*string = *p); string++, p++ )
|
|
|
|
|
if( isspace( *(byte*)p ) ) {
|
|
|
|
|
if( !mark )
|
|
|
|
|
mark = string ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mark = NULL ;
|
|
|
|
|
if( mark )
|
|
|
|
|
*mark = '\0' ; /* remove trailing spaces */
|
|
|
|
|
|
|
|
|
|
return str ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* remove trailing white spaces
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
trim_trailing_spaces( char *string )
|
|
|
|
|
{
|
|
|
|
|
char *p, *mark;
|
|
|
|
|
|
|
|
|
|
for( mark = NULL, p = string; *p; p++ ) {
|
|
|
|
|
if( isspace( *(byte*)p ) ) {
|
|
|
|
|
if( !mark )
|
|
|
|
|
mark = p;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mark = NULL;
|
|
|
|
|
}
|
|
|
|
|
if( mark )
|
|
|
|
|
*mark = '\0' ;
|
|
|
|
|
|
|
|
|
|
return string ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
|
|
|
|
|
{
|
|
|
|
|
byte *p, *mark;
|
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
|
|
for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
|
|
|
|
|
if( strchr(trimchars, *p ) ) {
|
|
|
|
|
if( !mark )
|
|
|
|
|
mark = p;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mark = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( mark ) {
|
|
|
|
|
*mark = 0;
|
|
|
|
|
return mark - line;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* remove trailing white spaces and return the length of the buffer
|
|
|
|
|
*/
|
|
|
|
|
unsigned
|
|
|
|
|
trim_trailing_ws( byte *line, unsigned len )
|
|
|
|
|
{
|
|
|
|
|
return trim_trailing_chars( line, len, " \t\r\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
size_t
|
|
|
|
|
length_sans_trailing_chars (const unsigned char *line, size_t len,
|
|
|
|
|
const char *trimchars )
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *p, *mark;
|
|
|
|
|
size_t n;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
for( mark=NULL, p=line, n=0; n < len; n++, p++ )
|
|
|
|
|
{
|
|
|
|
|
if (strchr (trimchars, *p ))
|
|
|
|
|
{
|
|
|
|
|
if( !mark )
|
|
|
|
|
mark = p;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mark = NULL;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
|
|
|
|
if (mark)
|
2003-06-18 19:56:13 +00:00
|
|
|
|
return mark - line;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return the length of line ignoring trailing white-space.
|
2003-06-18 19:56:13 +00:00
|
|
|
|
*/
|
|
|
|
|
size_t
|
|
|
|
|
length_sans_trailing_ws (const unsigned char *line, size_t len)
|
|
|
|
|
{
|
|
|
|
|
return length_sans_trailing_chars (line, len, " \t\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2009-07-07 16:51:33 +00:00
|
|
|
|
/*
|
|
|
|
|
* Extract from a given path the filename component. This function
|
|
|
|
|
* terminates the process on memory shortage.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
char *
|
2006-04-21 12:56:40 +00:00
|
|
|
|
make_basename(const char *filepath, const char *inputpath)
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2006-04-21 12:56:40 +00:00
|
|
|
|
#ifdef __riscos__
|
|
|
|
|
return riscos_make_basename(filepath, inputpath);
|
2008-10-20 13:53:23 +00:00
|
|
|
|
#else
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
(void)inputpath; /* Only required for riscos. */
|
2006-04-21 12:56:40 +00:00
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
if ( !(p=strrchr(filepath, '/')) )
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2003-01-09 12:36:05 +00:00
|
|
|
|
if ( !(p=strrchr(filepath, '\\')) )
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
2003-01-09 12:36:05 +00:00
|
|
|
|
if ( !(p=strrchr(filepath, ':')) )
|
2006-04-21 12:56:40 +00:00
|
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
return xstrdup(filepath);
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 13:45:27 +01:00
|
|
|
|
return xstrdup(p+1);
|
2008-10-20 13:53:23 +00:00
|
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-07 16:51:33 +00:00
|
|
|
|
/*
|
|
|
|
|
* Extract from a given filename the path prepended to it. If there
|
|
|
|
|
* isn't a path prepended to the filename, a dot is returned ('.').
|
|
|
|
|
* This function terminates the process on memory shortage.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
make_dirname(const char *filepath)
|
|
|
|
|
{
|
|
|
|
|
char *dirname;
|
|
|
|
|
int dirname_length;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
if ( !(p=strrchr(filepath, '/')) )
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2003-01-09 12:36:05 +00:00
|
|
|
|
if ( !(p=strrchr(filepath, '\\')) )
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
2003-01-09 12:36:05 +00:00
|
|
|
|
if ( !(p=strrchr(filepath, ':')) )
|
2007-08-02 18:12:43 +00:00
|
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
return xstrdup(".");
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dirname_length = p-filepath;
|
2015-03-12 13:45:27 +01:00
|
|
|
|
dirname = xmalloc(dirname_length+1);
|
2003-01-09 12:36:05 +00:00
|
|
|
|
strncpy(dirname, filepath, dirname_length);
|
|
|
|
|
dirname[dirname_length] = 0;
|
|
|
|
|
|
|
|
|
|
return dirname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-10-29 17:24:27 +00:00
|
|
|
|
|
2009-08-26 09:24:04 +00:00
|
|
|
|
static char *
|
|
|
|
|
get_pwdir (int xmode, const char *name)
|
|
|
|
|
{
|
|
|
|
|
char *result = NULL;
|
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
|
struct passwd *pwd = NULL;
|
|
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_GETPWNAM
|
|
|
|
|
/* Fixme: We should use getpwnam_r if available. */
|
|
|
|
|
pwd = getpwnam (name);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_GETPWUID
|
|
|
|
|
/* Fixme: We should use getpwuid_r if available. */
|
|
|
|
|
pwd = getpwuid (getuid());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
if (pwd)
|
|
|
|
|
{
|
|
|
|
|
if (xmode)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
result = xstrdup (pwd->pw_dir);
|
2009-08-26 09:24:04 +00:00
|
|
|
|
else
|
2015-03-12 13:45:27 +01:00
|
|
|
|
result = xtrystrdup (pwd->pw_dir);
|
2009-08-26 09:24:04 +00:00
|
|
|
|
}
|
2010-02-26 18:44:36 +00:00
|
|
|
|
#else /*!HAVE_PWD_H*/
|
|
|
|
|
/* No support at all. */
|
|
|
|
|
(void)xmode;
|
|
|
|
|
(void)name;
|
2009-08-26 09:24:04 +00:00
|
|
|
|
#endif /*HAVE_PWD_H*/
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 16:40:48 +02:00
|
|
|
|
|
|
|
|
|
/* xmode 0 := Return NULL on error
|
|
|
|
|
1 := Terminate on error
|
|
|
|
|
2 := Make sure that name is absolute; return NULL on error
|
|
|
|
|
3 := Make sure that name is absolute; terminate on error
|
|
|
|
|
*/
|
2009-08-26 08:55:57 +00:00
|
|
|
|
static char *
|
|
|
|
|
do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
|
|
|
|
|
{
|
|
|
|
|
const char *argv[32];
|
|
|
|
|
int argc;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
size_t n;
|
2009-08-26 08:55:57 +00:00
|
|
|
|
int skip = 1;
|
|
|
|
|
char *home_buffer = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
char *name, *home, *p;
|
2014-04-15 16:40:48 +02:00
|
|
|
|
int want_abs;
|
|
|
|
|
|
|
|
|
|
want_abs = !!(xmode & 2);
|
|
|
|
|
xmode &= 1;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
|
|
|
|
n = strlen (first_part) + 1;
|
2009-08-26 08:55:57 +00:00
|
|
|
|
argc = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
while ( (argv[argc] = va_arg (arg_ptr, const char *)) )
|
2009-08-26 08:55:57 +00:00
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
|
n += strlen (argv[argc]) + 1;
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (argc >= DIM (argv)-1)
|
|
|
|
|
{
|
|
|
|
|
if (xmode)
|
|
|
|
|
BUG ();
|
2015-04-24 15:19:10 +02:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
argc++;
|
2009-08-26 08:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
n++;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
|
|
|
|
home = NULL;
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (*first_part == '~')
|
|
|
|
|
{
|
|
|
|
|
if (first_part[1] == '/' || !first_part[1])
|
|
|
|
|
{
|
|
|
|
|
/* This is the "~/" or "~" case. */
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
|
if (!home)
|
2009-08-26 09:24:04 +00:00
|
|
|
|
home = home_buffer = get_pwdir (xmode, NULL);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (home && *home)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
n += strlen (home);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* This is the "~username/" or "~username" case. */
|
|
|
|
|
char *user;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (xmode)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
user = xstrdup (first_part+1);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
user = xtrystrdup (first_part+1);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (!user)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
p = strchr (user, '/');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = 0;
|
|
|
|
|
skip = 1 + strlen (user);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2009-08-26 09:24:04 +00:00
|
|
|
|
home = home_buffer = get_pwdir (xmode, user);
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (user);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (home)
|
|
|
|
|
n += strlen (home);
|
|
|
|
|
else
|
|
|
|
|
skip = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (xmode)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
name = xmalloc (n);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
name = xtrymalloc (n);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (!name)
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (home_buffer);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2009-08-26 08:55:57 +00:00
|
|
|
|
if (home)
|
|
|
|
|
p = stpcpy (stpcpy (name, home), first_part + skip);
|
|
|
|
|
else
|
|
|
|
|
p = stpcpy (name, first_part);
|
2008-10-29 17:24:27 +00:00
|
|
|
|
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (home_buffer);
|
2009-08-26 08:55:57 +00:00
|
|
|
|
for (argc=0; argv[argc]; argc++)
|
2015-07-22 11:05:32 +02:00
|
|
|
|
{
|
|
|
|
|
/* Avoid a leading double slash if the first part was "/". */
|
|
|
|
|
if (!argc && name[0] == '/' && !name[1])
|
|
|
|
|
p = stpcpy (p, argv[argc]);
|
|
|
|
|
else
|
|
|
|
|
p = stpcpy (stpcpy (p, "/"), argv[argc]);
|
|
|
|
|
}
|
2009-08-26 08:55:57 +00:00
|
|
|
|
|
2014-04-15 16:40:48 +02:00
|
|
|
|
if (want_abs)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
|
|
|
|
p = strchr (name, ':');
|
2014-05-14 10:26:30 +02:00
|
|
|
|
if (p)
|
|
|
|
|
p++;
|
|
|
|
|
else
|
2014-04-15 16:40:48 +02:00
|
|
|
|
p = name;
|
|
|
|
|
#else
|
|
|
|
|
p = name;
|
|
|
|
|
#endif
|
|
|
|
|
if (*p != '/'
|
|
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
|
|
|
|
&& *p != '\\'
|
|
|
|
|
#endif
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
home = gnupg_getcwd ();
|
|
|
|
|
if (!home)
|
|
|
|
|
{
|
|
|
|
|
if (xmode)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "\nfatal: getcwd failed: %s\n",
|
|
|
|
|
strerror (errno));
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (name);
|
2014-04-15 16:40:48 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
n = strlen (home) + 1 + strlen (name) + 1;
|
|
|
|
|
if (xmode)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
home_buffer = xmalloc (n);
|
2014-04-15 16:40:48 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
home_buffer = xtrymalloc (n);
|
2014-04-15 16:40:48 +02:00
|
|
|
|
if (!home_buffer)
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (name);
|
2014-04-15 16:40:48 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (p == name)
|
|
|
|
|
p = home_buffer;
|
|
|
|
|
else /* Windows case. */
|
|
|
|
|
{
|
|
|
|
|
memcpy (home_buffer, p, p - name + 1);
|
|
|
|
|
p = home_buffer + (p - name + 1);
|
|
|
|
|
}
|
2015-07-22 11:05:32 +02:00
|
|
|
|
|
|
|
|
|
/* Avoid a leading double slash if the cwd is "/". */
|
|
|
|
|
if (home[0] == '/' && !home[1])
|
|
|
|
|
strcpy (stpcpy (p, "/"), name);
|
|
|
|
|
else
|
|
|
|
|
strcpy (stpcpy (stpcpy (p, home), "/"), name);
|
|
|
|
|
|
2015-03-12 13:45:27 +01:00
|
|
|
|
xfree (name);
|
2014-04-15 16:40:48 +02:00
|
|
|
|
name = home_buffer;
|
|
|
|
|
/* Let's do a simple compression to catch the most common
|
|
|
|
|
case of using "." for gpg's --homedir option. */
|
|
|
|
|
n = strlen (name);
|
|
|
|
|
if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
|
|
|
|
|
name[n-2] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-26 08:55:57 +00:00
|
|
|
|
return change_slashes (name);
|
|
|
|
|
}
|
2008-10-29 17:24:27 +00:00
|
|
|
|
|
|
|
|
|
/* Construct a filename from the NULL terminated list of parts. Tilde
|
2009-08-26 08:55:57 +00:00
|
|
|
|
expansion is done for the first argument. This function terminates
|
|
|
|
|
the process on memory shortage. */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
char *
|
2008-10-29 17:24:27 +00:00
|
|
|
|
make_filename (const char *first_part, ... )
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2009-08-26 08:55:57 +00:00
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
va_start (arg_ptr, first_part);
|
|
|
|
|
result = do_make_filename (1, first_part, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
return result;
|
2008-10-29 17:24:27 +00:00
|
|
|
|
}
|
2007-08-02 18:12:43 +00:00
|
|
|
|
|
2008-10-29 17:24:27 +00:00
|
|
|
|
/* Construct a filename from the NULL terminated list of parts. Tilde
|
2009-08-26 08:55:57 +00:00
|
|
|
|
expansion is done for the first argument. This function may return
|
|
|
|
|
NULL on error. */
|
2008-10-29 17:24:27 +00:00
|
|
|
|
char *
|
|
|
|
|
make_filename_try (const char *first_part, ... )
|
|
|
|
|
{
|
2009-08-26 08:55:57 +00:00
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
va_start (arg_ptr, first_part);
|
|
|
|
|
result = do_make_filename (0, first_part, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
return result;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 16:40:48 +02:00
|
|
|
|
/* Construct an absolute filename from the NULL terminated list of
|
|
|
|
|
parts. Tilde expansion is done for the first argument. This
|
|
|
|
|
function terminates the process on memory shortage. */
|
|
|
|
|
char *
|
|
|
|
|
make_absfilename (const char *first_part, ... )
|
|
|
|
|
{
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
va_start (arg_ptr, first_part);
|
|
|
|
|
result = do_make_filename (3, first_part, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Construct an absolute filename from the NULL terminated list of
|
|
|
|
|
parts. Tilde expansion is done for the first argument. This
|
|
|
|
|
function may return NULL on error. */
|
|
|
|
|
char *
|
|
|
|
|
make_absfilename_try (const char *first_part, ... )
|
|
|
|
|
{
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
va_start (arg_ptr, first_part);
|
|
|
|
|
result = do_make_filename (2, first_part, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-10-29 17:24:27 +00:00
|
|
|
|
|
2007-08-24 09:34:39 +00:00
|
|
|
|
/* Compare whether the filenames are identical. This is a
|
2007-08-27 18:10:27 +00:00
|
|
|
|
special version of strcmp() taking the semantics of filenames in
|
2007-08-24 09:34:39 +00:00
|
|
|
|
account. Note that this function works only on the supplied names
|
2010-03-24 12:15:30 +00:00
|
|
|
|
without considering any context like the current directory. See
|
2007-08-24 09:34:39 +00:00
|
|
|
|
also same_file_p(). */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
int
|
2007-08-02 18:12:43 +00:00
|
|
|
|
compare_filenames (const char *a, const char *b)
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2010-03-24 12:15:30 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2011-02-04 12:57:53 +01:00
|
|
|
|
for ( ; *a && *b; a++, b++ )
|
2007-08-02 18:12:43 +00:00
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (*a != *b
|
2007-08-02 18:12:43 +00:00
|
|
|
|
&& (toupper (*(const unsigned char*)a)
|
|
|
|
|
!= toupper (*(const unsigned char*)b) )
|
|
|
|
|
&& !((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/')))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/'))
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
2011-02-04 12:57:53 +01:00
|
|
|
|
return (toupper (*(const unsigned char*)a)
|
2007-08-02 18:12:43 +00:00
|
|
|
|
- toupper (*(const unsigned char*)b));
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#else
|
2003-01-09 12:36:05 +00:00
|
|
|
|
return strcmp(a,b);
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
|
|
|
|
/* Convert 2 hex characters at S to a byte value. Return this value
|
|
|
|
|
or -1 if there is an error. */
|
|
|
|
|
int
|
|
|
|
|
hextobyte (const char *s)
|
|
|
|
|
{
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
if ( *s >= '0' && *s <= '9' )
|
|
|
|
|
c = 16 * (*s - '0');
|
|
|
|
|
else if ( *s >= 'A' && *s <= 'F' )
|
|
|
|
|
c = 16 * (10 + *s - 'A');
|
|
|
|
|
else if ( *s >= 'a' && *s <= 'f' )
|
|
|
|
|
c = 16 * (10 + *s - 'a');
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
s++;
|
|
|
|
|
if ( *s >= '0' && *s <= '9' )
|
|
|
|
|
c += *s - '0';
|
|
|
|
|
else if ( *s >= 'A' && *s <= 'F' )
|
|
|
|
|
c += 10 + *s - 'A';
|
|
|
|
|
else if ( *s >= 'a' && *s <= 'f' )
|
|
|
|
|
c += 10 + *s - 'a';
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-07-07 16:51:33 +00:00
|
|
|
|
/* Create a string from the buffer P_ARG of length N which is suitable
|
|
|
|
|
for printing. Caller must release the created string using xfree.
|
|
|
|
|
This function terminates the process on memory shortage. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
char *
|
2005-06-16 08:12:03 +00:00
|
|
|
|
sanitize_buffer (const void *p_arg, size_t n, int delim)
|
2003-06-18 19:56:13 +00:00
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const unsigned char *p = p_arg;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
size_t save_n, buflen;
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const unsigned char *save_p;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
char *buffer, *d;
|
|
|
|
|
|
2006-04-28 14:32:13 +00:00
|
|
|
|
/* First count length. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ )
|
2003-06-18 19:56:13 +00:00
|
|
|
|
{
|
2004-02-18 16:59:19 +00:00
|
|
|
|
if ( *p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\'))
|
2003-06-18 19:56:13 +00:00
|
|
|
|
{
|
|
|
|
|
if ( *p=='\n' || *p=='\r' || *p=='\f'
|
|
|
|
|
|| *p=='\v' || *p=='\b' || !*p )
|
|
|
|
|
buflen += 2;
|
|
|
|
|
else
|
2006-04-28 14:32:13 +00:00
|
|
|
|
buflen += 5;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
buflen++;
|
|
|
|
|
}
|
|
|
|
|
p = save_p;
|
|
|
|
|
n = save_n;
|
2006-04-28 14:32:13 +00:00
|
|
|
|
/* And now make the string */
|
2015-03-12 13:45:27 +01:00
|
|
|
|
d = buffer = xmalloc( buflen );
|
2003-06-18 19:56:13 +00:00
|
|
|
|
for ( ; n; n--, p++ )
|
|
|
|
|
{
|
2004-02-18 16:59:19 +00:00
|
|
|
|
if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) {
|
2003-06-18 19:56:13 +00:00
|
|
|
|
*d++ = '\\';
|
|
|
|
|
if( *p == '\n' )
|
|
|
|
|
*d++ = 'n';
|
|
|
|
|
else if( *p == '\r' )
|
|
|
|
|
*d++ = 'r';
|
|
|
|
|
else if( *p == '\f' )
|
|
|
|
|
*d++ = 'f';
|
|
|
|
|
else if( *p == '\v' )
|
|
|
|
|
*d++ = 'v';
|
|
|
|
|
else if( *p == '\b' )
|
|
|
|
|
*d++ = 'b';
|
|
|
|
|
else if( !*p )
|
|
|
|
|
*d++ = '0';
|
|
|
|
|
else {
|
|
|
|
|
sprintf(d, "x%02x", *p );
|
2006-04-28 14:32:13 +00:00
|
|
|
|
d += 3;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*d++ = *p;
|
|
|
|
|
}
|
|
|
|
|
*d = 0;
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
|
|
2007-01-25 10:26:55 +00:00
|
|
|
|
/* Given a string containing an UTF-8 encoded text, return the number
|
|
|
|
|
of characters in this string. It differs from strlen in that it
|
2015-11-23 22:13:56 +01:00
|
|
|
|
only counts complete UTF-8 characters. SIZE is the maximum length
|
|
|
|
|
of the string in bytes. If SIZE is -1, then a NUL character is
|
|
|
|
|
taken to be the end of the string. Note, that this function does
|
|
|
|
|
not take combined characters into account. */
|
2007-01-25 10:26:55 +00:00
|
|
|
|
size_t
|
2015-11-23 22:13:56 +01:00
|
|
|
|
utf8_charcount (const char *s, int len)
|
2007-01-25 10:26:55 +00:00
|
|
|
|
{
|
|
|
|
|
size_t n;
|
|
|
|
|
|
2015-11-23 22:13:56 +01:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2007-01-25 10:26:55 +00:00
|
|
|
|
for (n=0; *s; s++)
|
2015-11-23 22:13:56 +01:00
|
|
|
|
{
|
|
|
|
|
if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
if (len != -1)
|
|
|
|
|
{
|
|
|
|
|
len --;
|
|
|
|
|
if (len == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-25 10:26:55 +00:00
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
/****************************************************
|
2004-12-13 15:49:56 +00:00
|
|
|
|
********** W32 specific functions ****************
|
|
|
|
|
****************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
const char *
|
|
|
|
|
w32_strerror (int ec)
|
|
|
|
|
{
|
|
|
|
|
static char strerr[256];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
|
if (ec == -1)
|
|
|
|
|
ec = (int)GetLastError ();
|
2010-02-26 18:44:36 +00:00
|
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
|
/* There is only a wchar_t FormatMessage. It does not make much
|
|
|
|
|
sense to play the conversion game; we print only the code. */
|
|
|
|
|
snprintf (strerr, sizeof strerr, "ec=%d", (int)GetLastError ());
|
|
|
|
|
#else
|
2004-12-13 15:49:56 +00:00
|
|
|
|
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
|
|
|
|
|
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
|
strerr, DIM (strerr)-1, NULL);
|
2010-02-26 18:44:36 +00:00
|
|
|
|
#endif
|
2011-02-04 12:57:53 +01:00
|
|
|
|
return strerr;
|
2004-12-13 15:49:56 +00:00
|
|
|
|
}
|
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
******** Locale insensitive ctype functions ********
|
2003-01-09 12:36:05 +00:00
|
|
|
|
****************************************************/
|
|
|
|
|
/* FIXME: replace them by a table lookup and macros */
|
|
|
|
|
int
|
|
|
|
|
ascii_isupper (int c)
|
|
|
|
|
{
|
|
|
|
|
return c >= 'A' && c <= 'Z';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ascii_islower (int c)
|
|
|
|
|
{
|
|
|
|
|
return c >= 'a' && c <= 'z';
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
int
|
2003-01-09 12:36:05 +00:00
|
|
|
|
ascii_toupper (int c)
|
|
|
|
|
{
|
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
|
c &= ~0x20;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
int
|
2003-01-09 12:36:05 +00:00
|
|
|
|
ascii_tolower (int c)
|
|
|
|
|
{
|
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
|
c |= 0x20;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 18:16:45 +01:00
|
|
|
|
/* Lowercase all ASCII characters in S. */
|
|
|
|
|
char *
|
|
|
|
|
ascii_strlwr (char *s)
|
|
|
|
|
{
|
|
|
|
|
char *p = s;
|
|
|
|
|
|
|
|
|
|
for (p=s; *p; p++ )
|
|
|
|
|
if (isascii (*p) && *p >= 'A' && *p <= 'Z')
|
|
|
|
|
*p |= 0x20;
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ascii_strcasecmp( const char *a, const char *b )
|
|
|
|
|
{
|
|
|
|
|
if (a == b)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (; *a && *b; a++, b++) {
|
|
|
|
|
if (*a != *b && ascii_toupper(*a) != ascii_toupper(*b))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
int
|
2003-06-18 19:56:13 +00:00
|
|
|
|
ascii_strncasecmp (const char *a, const char *b, size_t n)
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *p1 = (const unsigned char *)a;
|
|
|
|
|
const unsigned char *p2 = (const unsigned char *)b;
|
|
|
|
|
unsigned char c1, c2;
|
|
|
|
|
|
|
|
|
|
if (p1 == p2 || !n )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
c1 = ascii_tolower (*p1);
|
|
|
|
|
c2 = ascii_tolower (*p2);
|
|
|
|
|
|
|
|
|
|
if ( !--n || c1 == '\0')
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
++p1;
|
|
|
|
|
++p2;
|
|
|
|
|
}
|
|
|
|
|
while (c1 == c2);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
return c1 - c2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
int
|
2005-06-16 08:12:03 +00:00
|
|
|
|
ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n )
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const char *a = a_arg;
|
|
|
|
|
const char *b = b_arg;
|
|
|
|
|
|
|
|
|
|
if (a == b)
|
2003-01-09 12:36:05 +00:00
|
|
|
|
return 0;
|
2005-06-16 08:12:03 +00:00
|
|
|
|
for ( ; n; n--, a++, b++ )
|
|
|
|
|
{
|
|
|
|
|
if( *a != *b && ascii_toupper (*a) != ascii_toupper (*b) )
|
|
|
|
|
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ascii_strcmp( const char *a, const char *b )
|
|
|
|
|
{
|
|
|
|
|
if (a == b)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (; *a && *b; a++, b++) {
|
|
|
|
|
if (*a != *b )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return *a == *b? 0 : (*(signed char *)a - *(signed char *)b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
ascii_memcasemem (const void *haystack, size_t nhaystack,
|
|
|
|
|
const void *needle, size_t nneedle)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!nneedle)
|
|
|
|
|
return (void*)haystack; /* finding an empty needle is really easy */
|
|
|
|
|
if (nneedle <= nhaystack)
|
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const char *a = haystack;
|
|
|
|
|
const char *b = a + nhaystack - nneedle;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
for (; a <= b; a++)
|
|
|
|
|
{
|
|
|
|
|
if ( !ascii_memcasecmp (a, needle, nneedle) )
|
|
|
|
|
return (void *)a;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*********************************************
|
|
|
|
|
********** missing string functions *********
|
|
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_STPCPY
|
|
|
|
|
char *
|
|
|
|
|
stpcpy(char *a,const char *b)
|
|
|
|
|
{
|
|
|
|
|
while( *b )
|
|
|
|
|
*a++ = *b++;
|
|
|
|
|
*a = 0;
|
|
|
|
|
|
|
|
|
|
return (char*)a;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-11-11 15:14:31 +01:00
|
|
|
|
#ifndef HAVE_STRPBRK
|
|
|
|
|
/* Find the first occurrence in S of any character in ACCEPT.
|
|
|
|
|
Code taken from glibc-2.6/string/strpbrk.c (LGPLv2.1+) and modified. */
|
|
|
|
|
char *
|
|
|
|
|
strpbrk (const char *s, const char *accept)
|
|
|
|
|
{
|
|
|
|
|
while (*s != '\0')
|
|
|
|
|
{
|
|
|
|
|
const char *a = accept;
|
|
|
|
|
while (*a != '\0')
|
|
|
|
|
if (*a++ == *s)
|
|
|
|
|
return (char *) s;
|
|
|
|
|
++s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif /*!HAVE_STRPBRK*/
|
|
|
|
|
|
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
|
#ifndef HAVE_STRSEP
|
|
|
|
|
/* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
|
|
|
|
|
char *
|
|
|
|
|
strsep (char **stringp, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
char *begin, *end;
|
|
|
|
|
|
|
|
|
|
begin = *stringp;
|
|
|
|
|
if (begin == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* A frequent case is when the delimiter string contains only one
|
2012-06-05 19:29:22 +02:00
|
|
|
|
character. Here we don't need to call the expensive 'strpbrk'
|
|
|
|
|
function and instead work using 'strchr'. */
|
2006-10-02 11:54:35 +00:00
|
|
|
|
if (delim[0] == '\0' || delim[1] == '\0')
|
|
|
|
|
{
|
|
|
|
|
char ch = delim[0];
|
|
|
|
|
|
|
|
|
|
if (ch == '\0')
|
|
|
|
|
end = NULL;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (*begin == ch)
|
|
|
|
|
end = begin;
|
|
|
|
|
else if (*begin == '\0')
|
|
|
|
|
end = NULL;
|
|
|
|
|
else
|
|
|
|
|
end = strchr (begin + 1, ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
/* Find the end of the token. */
|
|
|
|
|
end = strpbrk (begin, delim);
|
|
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
|
{
|
|
|
|
|
/* Terminate the token and set *STRINGP past NUL character. */
|
|
|
|
|
*end++ = '\0';
|
|
|
|
|
*stringp = end;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
/* No more delimiters; this is the last token. */
|
|
|
|
|
*stringp = NULL;
|
|
|
|
|
|
|
|
|
|
return begin;
|
|
|
|
|
}
|
|
|
|
|
#endif /*HAVE_STRSEP*/
|
|
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
#ifndef HAVE_STRLWR
|
|
|
|
|
char *
|
|
|
|
|
strlwr(char *s)
|
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
for(p=s; *p; p++ )
|
|
|
|
|
*p = tolower(*p);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_STRCASECMP
|
|
|
|
|
int
|
|
|
|
|
strcasecmp( const char *a, const char *b )
|
|
|
|
|
{
|
|
|
|
|
for( ; *a && *b; a++, b++ ) {
|
|
|
|
|
if( *a != *b && toupper(*a) != toupper(*b) )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return *(const byte*)a - *(const byte*)b;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* mingw32/cpd has a memicmp()
|
|
|
|
|
*/
|
|
|
|
|
#ifndef HAVE_MEMICMP
|
|
|
|
|
int
|
|
|
|
|
memicmp( const char *a, const char *b, size_t n )
|
|
|
|
|
{
|
|
|
|
|
for( ; n; n--, a++, b++ )
|
|
|
|
|
if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
|
|
|
|
|
return *(const byte *)a - *(const byte*)b;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2004-12-13 15:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
2006-10-19 14:22:06 +00:00
|
|
|
|
#ifndef HAVE_MEMRCHR
|
|
|
|
|
void *
|
|
|
|
|
memrchr (const void *buffer, int c, size_t n)
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *p = buffer;
|
|
|
|
|
|
|
|
|
|
for (p += n; n ; n--)
|
|
|
|
|
if (*--p == c)
|
2007-06-06 18:12:30 +00:00
|
|
|
|
return (void *)p;
|
2006-10-19 14:22:06 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif /*HAVE_MEMRCHR*/
|
2007-06-18 20:15:01 +00:00
|
|
|
|
|
|
|
|
|
|
2007-06-19 09:11:11 +00:00
|
|
|
|
/* Percent-escape the string STR by replacing colons with '%3a'. If
|
2007-11-19 16:03:50 +00:00
|
|
|
|
EXTRA is not NULL all characters in EXTRA are also escaped. */
|
|
|
|
|
static char *
|
|
|
|
|
do_percent_escape (const char *str, const char *extra, int die)
|
2007-06-18 20:15:01 +00:00
|
|
|
|
{
|
2007-06-19 09:11:11 +00:00
|
|
|
|
int i, j;
|
2007-06-18 20:15:01 +00:00
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2007-06-19 09:11:11 +00:00
|
|
|
|
for (i=j=0; str[i]; i++)
|
|
|
|
|
if (str[i] == ':' || str[i] == '%' || (extra && strchr (extra, str[i])))
|
2007-06-18 20:15:01 +00:00
|
|
|
|
j++;
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (die)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
ptr = xmalloc (i + 2 * j + 1);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-12 13:45:27 +01:00
|
|
|
|
ptr = xtrymalloc (i + 2 * j + 1);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (!ptr)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2007-06-18 20:15:01 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
while (*str)
|
|
|
|
|
{
|
|
|
|
|
if (*str == ':')
|
|
|
|
|
{
|
|
|
|
|
ptr[i++] = '%';
|
|
|
|
|
ptr[i++] = '3';
|
|
|
|
|
ptr[i++] = 'a';
|
|
|
|
|
}
|
2007-06-19 09:11:11 +00:00
|
|
|
|
else if (*str == '%')
|
|
|
|
|
{
|
|
|
|
|
ptr[i++] = '%';
|
|
|
|
|
ptr[i++] = '2';
|
|
|
|
|
ptr[i++] = '5';
|
|
|
|
|
}
|
|
|
|
|
else if (extra && strchr (extra, *str))
|
|
|
|
|
{
|
|
|
|
|
ptr[i++] = '%';
|
|
|
|
|
ptr[i++] = tohex_lower ((*str>>4)&15);
|
|
|
|
|
ptr[i++] = tohex_lower (*str&15);
|
|
|
|
|
}
|
2007-06-18 20:15:01 +00:00
|
|
|
|
else
|
|
|
|
|
ptr[i++] = *str;
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
ptr[i] = '\0';
|
|
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
|
|
|
|
|
|
/* Percent-escape the string STR by replacing colons with '%3a'. If
|
2009-07-07 16:51:33 +00:00
|
|
|
|
EXTRA is not NULL all characters in EXTRA are also escaped. This
|
|
|
|
|
function terminates the process on memory shortage. */
|
2007-11-19 16:03:50 +00:00
|
|
|
|
char *
|
|
|
|
|
percent_escape (const char *str, const char *extra)
|
|
|
|
|
{
|
|
|
|
|
return do_percent_escape (str, extra, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Same as percent_escape but return NULL instead of exiting on memory
|
|
|
|
|
error. */
|
|
|
|
|
char *
|
|
|
|
|
try_percent_escape (const char *str, const char *extra)
|
|
|
|
|
{
|
|
|
|
|
return do_percent_escape (str, extra, 0);
|
|
|
|
|
}
|
2009-08-25 20:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
do_strconcat (const char *s1, va_list arg_ptr)
|
|
|
|
|
{
|
|
|
|
|
const char *argv[48];
|
|
|
|
|
size_t argc;
|
|
|
|
|
size_t needed;
|
|
|
|
|
char *buffer, *p;
|
|
|
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
|
argv[argc++] = s1;
|
|
|
|
|
needed = strlen (s1);
|
|
|
|
|
while (((argv[argc] = va_arg (arg_ptr, const char *))))
|
|
|
|
|
{
|
|
|
|
|
needed += strlen (argv[argc]);
|
|
|
|
|
if (argc >= DIM (argv)-1)
|
|
|
|
|
{
|
2015-04-24 15:19:10 +02:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2009-08-25 20:19:37 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
argc++;
|
|
|
|
|
}
|
|
|
|
|
needed++;
|
2015-03-12 13:45:27 +01:00
|
|
|
|
buffer = xtrymalloc (needed);
|
2009-08-25 20:19:37 +00:00
|
|
|
|
if (buffer)
|
|
|
|
|
{
|
|
|
|
|
for (p = buffer, argc=0; argv[argc]; argc++)
|
|
|
|
|
p = stpcpy (p, argv[argc]);
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Concatenate the string S1 with all the following strings up to a
|
|
|
|
|
NULL. Returns a malloced buffer with the new string or NULL on a
|
|
|
|
|
malloc error or if too many arguments are given. */
|
|
|
|
|
char *
|
|
|
|
|
strconcat (const char *s1, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
if (!s1)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
result = xtrystrdup ("");
|
2009-08-25 20:19:37 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
va_start (arg_ptr, s1);
|
|
|
|
|
result = do_strconcat (s1, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Same as strconcat but terminate the process with an error message
|
|
|
|
|
if something goes wrong. */
|
|
|
|
|
char *
|
|
|
|
|
xstrconcat (const char *s1, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
if (!s1)
|
2015-03-12 13:45:27 +01:00
|
|
|
|
result = xstrdup ("");
|
2009-08-25 20:19:37 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
va_start (arg_ptr, s1);
|
|
|
|
|
result = do_strconcat (s1, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
}
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
if (errno == EINVAL)
|
|
|
|
|
fputs ("\nfatal: too many args for xstrconcat\n", stderr);
|
|
|
|
|
else
|
|
|
|
|
fputs ("\nfatal: out of memory\n", stderr);
|
|
|
|
|
exit (2);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-03-12 13:03:50 +01:00
|
|
|
|
|
|
|
|
|
/* Split a string into fields at DELIM. REPLACEMENT is the character
|
|
|
|
|
to replace the delimiter with (normally: '\0' so that each field is
|
|
|
|
|
NUL terminated). The caller is responsible for freeing the result.
|
|
|
|
|
Note: this function modifies STRING! If you need the original
|
|
|
|
|
value, then you should pass a copy to this function.
|
|
|
|
|
|
|
|
|
|
If malloc fails, this function returns NULL. */
|
|
|
|
|
char **
|
|
|
|
|
strsplit (char *string, char delim, char replacement, int *count)
|
|
|
|
|
{
|
|
|
|
|
int fields = 1;
|
|
|
|
|
char *t;
|
|
|
|
|
char **result;
|
|
|
|
|
|
|
|
|
|
/* First, count the number of fields. */
|
|
|
|
|
for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
|
|
|
|
|
fields ++;
|
|
|
|
|
|
2015-10-01 08:57:06 +09:00
|
|
|
|
result = xtrycalloc ((fields + 1), sizeof (*result));
|
2015-03-12 13:03:50 +01:00
|
|
|
|
if (! result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
result[0] = string;
|
|
|
|
|
fields = 1;
|
|
|
|
|
for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
|
|
|
|
|
{
|
|
|
|
|
result[fields ++] = t + 1;
|
|
|
|
|
*t = replacement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
|
*count = fields;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-06-22 19:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Tokenize STRING using the set of delimiters in DELIM. Leading
|
|
|
|
|
* spaces and tabs are removed from all tokens. The caller must xfree
|
|
|
|
|
* the result.
|
|
|
|
|
*
|
|
|
|
|
* Returns: A malloced and NULL delimited array with the tokens. On
|
|
|
|
|
* memory error NULL is returned and ERRNO is set.
|
|
|
|
|
*/
|
|
|
|
|
char **
|
|
|
|
|
strtokenize (const char *string, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
const char *s;
|
|
|
|
|
size_t fields;
|
|
|
|
|
size_t bytes, n;
|
|
|
|
|
char *buffer;
|
|
|
|
|
char *p, *px, *pend;
|
|
|
|
|
char **result;
|
|
|
|
|
|
|
|
|
|
/* Count the number of fields. */
|
|
|
|
|
for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
|
|
|
|
|
fields++;
|
|
|
|
|
fields++; /* Add one for the terminating NULL. */
|
|
|
|
|
|
|
|
|
|
/* Allocate an array for all fields, a terminating NULL, and space
|
|
|
|
|
for a copy of the string. */
|
|
|
|
|
bytes = fields * sizeof *result;
|
|
|
|
|
if (bytes / sizeof *result != fields)
|
|
|
|
|
{
|
|
|
|
|
gpg_err_set_errno (ENOMEM);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
n = strlen (string) + 1;
|
|
|
|
|
bytes += n;
|
|
|
|
|
if (bytes < n)
|
|
|
|
|
{
|
|
|
|
|
gpg_err_set_errno (ENOMEM);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
result = xtrymalloc (bytes);
|
|
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
buffer = (char*)(result + fields);
|
|
|
|
|
|
|
|
|
|
/* Copy and parse the string. */
|
|
|
|
|
strcpy (buffer, string);
|
|
|
|
|
for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
|
|
|
|
|
{
|
|
|
|
|
*pend = 0;
|
|
|
|
|
while (spacep (p))
|
|
|
|
|
p++;
|
|
|
|
|
for (px = pend - 1; px >= p && spacep (px); px--)
|
|
|
|
|
*px = 0;
|
|
|
|
|
result[n++] = p;
|
|
|
|
|
}
|
|
|
|
|
while (spacep (p))
|
|
|
|
|
p++;
|
|
|
|
|
for (px = p + strlen (p) - 1; px >= p && spacep (px); px--)
|
|
|
|
|
*px = 0;
|
|
|
|
|
result[n++] = p;
|
|
|
|
|
result[n] = NULL;
|
|
|
|
|
|
|
|
|
|
assert ((char*)(result + n + 1) == buffer);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-11-23 22:20:28 +01:00
|
|
|
|
|
2015-11-26 18:04:54 +01:00
|
|
|
|
|
|
|
|
|
/* Format a string so that it fits within about TARGET_COLS columns.
|
|
|
|
|
If IN_PLACE is 0, then TEXT is copied to a new buffer, which is
|
|
|
|
|
returned. Otherwise, TEXT is modified in place and returned.
|
|
|
|
|
Normally, target_cols will be 72 and max_cols is 80. */
|
2015-11-23 22:20:28 +01:00
|
|
|
|
char *
|
|
|
|
|
format_text (char *text, int in_place, int target_cols, int max_cols)
|
|
|
|
|
{
|
|
|
|
|
const int do_debug = 0;
|
|
|
|
|
|
|
|
|
|
/* The character under consideration. */
|
|
|
|
|
char *p;
|
|
|
|
|
/* The start of the current line. */
|
|
|
|
|
char *line;
|
|
|
|
|
/* The last space that we saw. */
|
|
|
|
|
char *last_space = NULL;
|
|
|
|
|
int last_space_cols = 0;
|
|
|
|
|
int copied_last_space = 0;
|
|
|
|
|
|
|
|
|
|
if (! in_place)
|
|
|
|
|
text = xstrdup (text);
|
|
|
|
|
|
|
|
|
|
p = line = text;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* The number of columns including any trailing space. */
|
|
|
|
|
int cols;
|
|
|
|
|
|
|
|
|
|
p = p + strcspn (p, "\n ");
|
|
|
|
|
if (! p)
|
|
|
|
|
/* P now points to the NUL character. */
|
|
|
|
|
p = &text[strlen (text)];
|
|
|
|
|
|
|
|
|
|
if (*p == '\n')
|
|
|
|
|
/* Pass through any newlines. */
|
|
|
|
|
{
|
|
|
|
|
p ++;
|
|
|
|
|
line = p;
|
|
|
|
|
last_space = NULL;
|
|
|
|
|
last_space_cols = 0;
|
|
|
|
|
copied_last_space = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Have a space or a NUL. Note: we don't count the trailing
|
|
|
|
|
space. */
|
|
|
|
|
cols = utf8_charcount (line, (uintptr_t) p - (uintptr_t) line);
|
|
|
|
|
if (cols < target_cols)
|
|
|
|
|
{
|
|
|
|
|
if (! *p)
|
|
|
|
|
/* Nothing left to break. */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
last_space = p;
|
|
|
|
|
last_space_cols = cols;
|
|
|
|
|
p ++;
|
|
|
|
|
/* Skip any immediately following spaces. If we break:
|
|
|
|
|
"... foo bar ..." between "foo" and "bar" then we want:
|
|
|
|
|
"... foo\nbar ...", which means that the left space has
|
|
|
|
|
to be the first space after foo, not the last space
|
|
|
|
|
before bar. */
|
|
|
|
|
while (*p == ' ')
|
|
|
|
|
p ++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int cols_with_left_space;
|
|
|
|
|
int cols_with_right_space;
|
|
|
|
|
int left_penalty;
|
|
|
|
|
int right_penalty;
|
|
|
|
|
|
|
|
|
|
cols_with_left_space = last_space_cols;
|
|
|
|
|
cols_with_right_space = cols;
|
|
|
|
|
|
|
|
|
|
if (do_debug)
|
|
|
|
|
log_debug ("Breaking: '%.*s'\n",
|
|
|
|
|
(int) ((uintptr_t) p - (uintptr_t) line), line);
|
|
|
|
|
|
|
|
|
|
/* The number of columns away from TARGET_COLS. We prefer
|
|
|
|
|
to underflow than to overflow. */
|
|
|
|
|
left_penalty = target_cols - cols_with_left_space;
|
|
|
|
|
right_penalty = 2 * (cols_with_right_space - target_cols);
|
|
|
|
|
|
|
|
|
|
if (cols_with_right_space > max_cols)
|
|
|
|
|
/* Add a large penalty for each column that exceeds
|
|
|
|
|
max_cols. */
|
|
|
|
|
right_penalty += 4 * (cols_with_right_space - max_cols);
|
|
|
|
|
|
|
|
|
|
if (do_debug)
|
|
|
|
|
log_debug ("Left space => %d cols (penalty: %d); right space => %d cols (penalty: %d)\n",
|
|
|
|
|
cols_with_left_space, left_penalty,
|
|
|
|
|
cols_with_right_space, right_penalty);
|
|
|
|
|
if (last_space_cols && left_penalty <= right_penalty)
|
|
|
|
|
/* Prefer the left space. */
|
|
|
|
|
{
|
|
|
|
|
if (do_debug)
|
|
|
|
|
log_debug ("Breaking at left space.\n");
|
|
|
|
|
p = last_space;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (do_debug)
|
|
|
|
|
log_debug ("Breaking at right space.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! *p)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
*p = '\n';
|
|
|
|
|
p ++;
|
|
|
|
|
if (*p == ' ')
|
|
|
|
|
{
|
|
|
|
|
int spaces;
|
|
|
|
|
for (spaces = 1; p[spaces] == ' '; spaces ++)
|
|
|
|
|
;
|
|
|
|
|
memmove (p, &p[spaces], strlen (&p[spaces]) + 1);
|
|
|
|
|
}
|
|
|
|
|
line = p;
|
|
|
|
|
last_space = NULL;
|
|
|
|
|
last_space_cols = 0;
|
|
|
|
|
copied_last_space = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Chop off any trailing space. */
|
2015-11-26 18:04:54 +01:00
|
|
|
|
trim_trailing_chars (text, strlen (text), " ");
|
2015-11-23 22:20:28 +01:00
|
|
|
|
/* If we inserted the trailing newline, then remove it. */
|
2015-11-26 18:04:54 +01:00
|
|
|
|
if (! copied_last_space && *text && text[strlen (text) - 1] == '\n')
|
2015-11-23 22:20:28 +01:00
|
|
|
|
text[strlen (text) - 1] = '\0';
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|