1
1
/* stringhelp.c - standard string helper functions
2
2
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005,
3
* 2006, 2007 Free Software Foundation, Inc.
3
* 2006, 2007, 2008 Free Software Foundation, Inc.
5
5
* This file is part of JNLIB.
35
35
#define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
37
/* Sometimes we want to avoid mixing slashes and backslashes on W32
38
and prefer backslashes. There is usual no problem with mixing
39
them, however a very few W32 API calls can't grok plain slashes.
40
Printing filenames with mixed slashes also looks a bit strange.
41
This function has no effext on POSIX. */
43
change_slashes (char *name)
45
#ifdef HAVE_DRIVE_LETTERS
48
if (strchr (name, '\\'))
54
#endif /*HAVE_DRIVE_LETTERS*/
38
60
* Look for the substring SUB in buffer and return a pointer to that
39
61
* substring in BUFFER or NULL if not found.
293
* Construct a filename from the NULL terminated list of parts.
294
* Tilde expansion is done here.
297
make_filename( const char *first_part, ... )
302
char *name, *home, *p;
304
va_start (arg_ptr, first_part);
305
n = strlen (first_part) + 1;
306
while ( (s = va_arg (arg_ptr, const char *)) )
311
if ( *first_part == '~' && first_part[1] == '/'
312
&& (home = getenv("HOME")) && *home )
315
name = jnlib_xmalloc (n);
317
? stpcpy (stpcpy (name,home), first_part + 1)
318
: stpcpy(name, first_part));
320
va_start (arg_ptr, first_part) ;
321
while ( (s = va_arg(arg_ptr, const char *)) )
322
p = stpcpy (stpcpy (p,"/"), s);
325
#ifdef HAVE_DRIVE_LETTERS
326
/* We better avoid mixing slashes and backslashes and prefer
327
backslashes. There is usual no problem with mixing them, however
328
a very few W32 API calls can't grok plain slashes. Printing
329
filenames with mixed slashes also looks a bit strange. */
330
if (strchr (name, '\\'))
332
for (p=name; *p; p++)
336
#endif /*HAVE_DRIVE_LETTERS*/
316
/* Implementation of make_filename and make_filename_try. We need to
317
use macros here to avoid the use of the sometimes problematic
318
va_copy function which is not available on all systems. */
319
#define MAKE_FILENAME_PART1 \
323
char *name, *home, *p; \
325
va_start (arg_ptr, first_part); \
326
n = strlen (first_part) + 1; \
327
while ( (s = va_arg (arg_ptr, const char *)) ) \
328
n += strlen(s) + 1; \
332
if ( *first_part == '~' && first_part[1] == '/' \
333
&& (home = getenv("HOME")) && *home ) \
336
#define MAKE_FILENAME_PART2 \
338
? stpcpy (stpcpy (name,home), first_part + 1)\
339
: stpcpy(name, first_part)); \
341
va_start (arg_ptr, first_part); \
342
while ( (s = va_arg(arg_ptr, const char *)) ) \
343
p = stpcpy (stpcpy (p,"/"), s); \
345
return change_slashes (name);
348
/* Construct a filename from the NULL terminated list of parts. Tilde
349
expansion is done here. This function will never fail. */
351
make_filename (const char *first_part, ... )
354
name = jnlib_xmalloc (n);
358
/* Construct a filename from the NULL terminated list of parts. Tilde
359
expansion is done here. This function may return NULL on error. */
361
make_filename_try (const char *first_part, ... )
364
name = jnlib_xmalloc (n);
369
#undef MAKE_FILENAME_PART1
370
#undef MAKE_FILENAME_PART2
341
374
/* Compare whether the filenames are identical. This is a
342
375
special version of strcmp() taking the semantics of filenames in
343
376
account. Note that this function works only on the supplied names