~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to jnlib/stringhelp.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
4
 *
5
5
 * This file is part of JNLIB.
6
6
 *
34
34
 
35
35
#define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
36
36
 
 
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. */
 
42
static inline char *
 
43
change_slashes (char *name)
 
44
{
 
45
#ifdef HAVE_DRIVE_LETTERS
 
46
  char *p;
 
47
 
 
48
  if (strchr (name, '\\'))
 
49
    {
 
50
      for (p=name; *p; p++)
 
51
        if (*p == '/')
 
52
          *p = '\\';
 
53
    }
 
54
#endif /*HAVE_DRIVE_LETTERS*/
 
55
  return name;
 
56
}
 
57
 
 
58
 
37
59
/*
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.
237
259
char *
238
260
make_basename(const char *filepath, const char *inputpath)
239
261
{
240
 
    char *p;
241
 
 
242
262
#ifdef __riscos__
243
263
    return riscos_make_basename(filepath, inputpath);
244
 
#endif
 
264
#else
 
265
    char *p;
 
266
 
 
267
    (void)inputpath; /* Only required for riscos.  */
245
268
 
246
269
    if ( !(p=strrchr(filepath, '/')) )
247
270
#ifdef HAVE_DRIVE_LETTERS
253
276
              }
254
277
 
255
278
    return jnlib_xstrdup(p+1);
 
279
#endif
256
280
}
257
281
 
258
282
 
288
312
}
289
313
 
290
314
 
291
 
 
292
 
/****************
293
 
 * Construct a filename from the NULL terminated list of parts.
294
 
 * Tilde expansion is done here.
295
 
 */
296
 
char *
297
 
make_filename( const char *first_part, ... )
298
 
{
299
 
  va_list arg_ptr ;
300
 
  size_t n;
301
 
  const char *s;
302
 
  char *name, *home, *p;
303
 
  
304
 
  va_start (arg_ptr, first_part);
305
 
  n = strlen (first_part) + 1;
306
 
  while ( (s = va_arg (arg_ptr, const char *)) )
307
 
    n += strlen(s) + 1;
308
 
  va_end(arg_ptr);
309
 
  
310
 
  home = NULL;
311
 
  if ( *first_part == '~' && first_part[1] == '/'
312
 
       && (home = getenv("HOME")) && *home )
313
 
    n += strlen (home);
314
 
  
315
 
  name = jnlib_xmalloc (n);
316
 
  p = (home 
317
 
       ? stpcpy (stpcpy (name,home), first_part + 1)
318
 
       : stpcpy(name, first_part));
319
 
 
320
 
  va_start (arg_ptr, first_part) ;
321
 
  while ( (s = va_arg(arg_ptr, const char *)) )
322
 
    p = stpcpy (stpcpy (p,"/"), s);
323
 
  va_end(arg_ptr);
324
 
 
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, '\\'))
331
 
    {
332
 
      for (p=name; *p; p++)
333
 
        if (*p == '/')
334
 
          *p = '\\';
335
 
    }
336
 
#endif /*HAVE_DRIVE_LETTERS*/
337
 
  return name;
338
 
}
339
 
 
340
 
 
 
315
 
 
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                        \
 
320
  va_list arg_ptr;                                 \
 
321
  size_t n;                                        \
 
322
  const char *s;                                   \
 
323
  char *name, *home, *p;                           \
 
324
                                                   \
 
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;                            \
 
329
  va_end(arg_ptr);                                 \
 
330
                                                   \
 
331
  home = NULL;                                     \
 
332
  if ( *first_part == '~' && first_part[1] == '/'  \
 
333
       && (home = getenv("HOME")) && *home )       \
 
334
    n += strlen (home);                            
 
335
  
 
336
#define MAKE_FILENAME_PART2                         \
 
337
  p = (home                                         \
 
338
       ? stpcpy (stpcpy (name,home), first_part + 1)\
 
339
       : stpcpy(name, first_part));                 \
 
340
                                                    \
 
341
  va_start (arg_ptr, first_part);                   \
 
342
  while ( (s = va_arg(arg_ptr, const char *)) )     \
 
343
    p = stpcpy (stpcpy (p,"/"), s);                 \
 
344
  va_end(arg_ptr);                                  \
 
345
  return change_slashes (name);
 
346
 
 
347
 
 
348
/* Construct a filename from the NULL terminated list of parts.  Tilde
 
349
   expansion is done here.  This function will never fail. */
 
350
char *
 
351
make_filename (const char *first_part, ... )
 
352
{
 
353
  MAKE_FILENAME_PART1
 
354
  name = jnlib_xmalloc (n);
 
355
  MAKE_FILENAME_PART2
 
356
}
 
357
 
 
358
/* Construct a filename from the NULL terminated list of parts.  Tilde
 
359
   expansion is done here.  This function may return NULL on error. */
 
360
char *
 
361
make_filename_try (const char *first_part, ... )
 
362
{
 
363
  MAKE_FILENAME_PART1
 
364
  name = jnlib_xmalloc (n);
 
365
  if (!name)
 
366
    return NULL;
 
367
  MAKE_FILENAME_PART2
 
368
}
 
369
#undef MAKE_FILENAME_PART1
 
370
#undef MAKE_FILENAME_PART2
 
371
 
 
372
 
 
373
 
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
406
439
 
407
440
  for (; length; length--, p++, count++)
408
441
    {
409
 
      /* Fixme: Check whether *p < 0xa0 is correct for utf8 encoding. */
410
442
      if (*p < 0x20 
411
 
          || (*p >= 0x7f && *p < 0xa0)
 
443
          || *p == 0x7f
412
444
          || *p == delim 
413
445
          || *p == delim2
414
446
          || ((delim || delim2) && *p=='\\'))