* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
* 2008, 2009, 2010 Free Software Foundation, Inc.
* Copyright (C) 2014 Werner Koch
+ * Copyright (C) 2015 g10 Code GmbH
*
- * This file is part of JNLIB, which is a subsystem of GnuPG.
+ * This file is part of GnuPG.
*
- * JNLIB is free software; you can redistribute it and/or modify it
+ * GnuPG is free software; you can redistribute it and/or modify it
* under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
*
* or both in parallel, as here.
*
- * JNLIB is distributed in the hope that it will be useful, but
+ * GnuPG is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
# endif
# include <windows.h>
#endif
+#include <assert.h>
-#include "libjnlib-config.h"
+#include "util.h"
+#include "common-defs.h"
#include "utf8conv.h"
#include "sysutils.h"
#include "stringhelp.h"
/* 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
- will be allocated using jnlib_xmalloc (i.e. if it runs out of core
+ will be allocated using xmalloc (i.e. if it runs out of core
the function terminates). Returns DES or a pointer to the
allocated memory.
*/
if( n ) {
if( !dest )
- dest = jnlib_xmalloc( n ) ;
+ dest = xmalloc( n ) ;
d = dest;
s = src ;
for(n--; n && *s; n-- )
if ( !(p=strrchr(filepath, ':')) )
#endif
{
- return jnlib_xstrdup(filepath);
+ return xstrdup(filepath);
}
- return jnlib_xstrdup(p+1);
+ return xstrdup(p+1);
#endif
}
if ( !(p=strrchr(filepath, ':')) )
#endif
{
- return jnlib_xstrdup(".");
+ return xstrdup(".");
}
dirname_length = p-filepath;
- dirname = jnlib_xmalloc(dirname_length+1);
+ dirname = xmalloc(dirname_length+1);
strncpy(dirname, filepath, dirname_length);
dirname[dirname_length] = 0;
if (pwd)
{
if (xmode)
- result = jnlib_xstrdup (pwd->pw_dir);
+ result = xstrdup (pwd->pw_dir);
else
- result = jnlib_strdup (pwd->pw_dir);
+ result = xtrystrdup (pwd->pw_dir);
}
#else /*!HAVE_PWD_H*/
/* No support at all. */
{
if (xmode)
BUG ();
- jnlib_set_errno (EINVAL);
+ gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
char *user;
if (xmode)
- user = jnlib_xstrdup (first_part+1);
+ user = xstrdup (first_part+1);
else
{
- user = jnlib_strdup (first_part+1);
+ user = xtrystrdup (first_part+1);
if (!user)
return NULL;
}
skip = 1 + strlen (user);
home = home_buffer = get_pwdir (xmode, user);
- jnlib_free (user);
+ xfree (user);
if (home)
n += strlen (home);
else
}
if (xmode)
- name = jnlib_xmalloc (n);
+ name = xmalloc (n);
else
{
- name = jnlib_malloc (n);
+ name = xtrymalloc (n);
if (!name)
{
- jnlib_free (home_buffer);
+ xfree (home_buffer);
return NULL;
}
}
else
p = stpcpy (name, first_part);
- jnlib_free (home_buffer);
+ xfree (home_buffer);
for (argc=0; argv[argc]; argc++)
- p = stpcpy (stpcpy (p, "/"), argv[argc]);
+ {
+ /* 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]);
+ }
if (want_abs)
{
strerror (errno));
exit(2);
}
- jnlib_free (name);
+ xfree (name);
return NULL;
}
n = strlen (home) + 1 + strlen (name) + 1;
if (xmode)
- home_buffer = jnlib_xmalloc (n);
+ home_buffer = xmalloc (n);
else
{
- home_buffer = jnlib_malloc (n);
+ home_buffer = xtrymalloc (n);
if (!home_buffer)
{
- jnlib_free (name);
+ xfree (name);
return NULL;
}
}
memcpy (home_buffer, p, p - name + 1);
p = home_buffer + (p - name + 1);
}
- strcpy (stpcpy (stpcpy (p, home), "/"), name);
- jnlib_free (name);
+
+ /* 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);
+
+ xfree (name);
name = home_buffer;
/* Let's do a simple compression to catch the most common
case of using "." for gpg's --homedir option. */
p = save_p;
n = save_n;
/* And now make the string */
- d = buffer = jnlib_xmalloc( buflen );
+ d = buffer = xmalloc( buflen );
for ( ; n; n--, p++ )
{
if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) {
return c;
}
+/* 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;
+}
int
ascii_strcasecmp( const char *a, const char *b )
if (str[i] == ':' || str[i] == '%' || (extra && strchr (extra, str[i])))
j++;
if (die)
- ptr = jnlib_xmalloc (i + 2 * j + 1);
+ ptr = xmalloc (i + 2 * j + 1);
else
{
- ptr = jnlib_malloc (i + 2 * j + 1);
+ ptr = xtrymalloc (i + 2 * j + 1);
if (!ptr)
return NULL;
}
needed += strlen (argv[argc]);
if (argc >= DIM (argv)-1)
{
- jnlib_set_errno (EINVAL);
+ gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
}
needed++;
- buffer = jnlib_malloc (needed);
+ buffer = xtrymalloc (needed);
if (buffer)
{
for (p = buffer, argc=0; argv[argc]; argc++)
char *result;
if (!s1)
- result = jnlib_strdup ("");
+ result = xtrystrdup ("");
else
{
va_start (arg_ptr, s1);
char *result;
if (!s1)
- result = jnlib_xstrdup ("");
+ result = xstrdup ("");
else
{
va_start (arg_ptr, s1);
}
return result;
}
+
+/* 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 ++;
+
+ result = xtrycalloc ((fields + 1), sizeof (*result));
+ 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;
+}
+
+
+/* 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;
+}