~ubuntu-branches/ubuntu/utopic/gettext/utopic

« back to all changes in this revision

Viewing changes to gettext-tools/libgettextpo/concat-filename.c

  • Committer: Colin Watson
  • Date: 2010-08-01 21:36:08 UTC
  • mfrom: (2.1.10 sid)
  • Revision ID: cjwatson@canonical.com-20100801213608-yy7vkm8lpatep3ci
merge from Debian 0.18.1.1-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Construct a full pathname from a directory and a filename.
2
 
   Copyright (C) 2001-2004, 2006-2007 Free Software Foundation, Inc.
 
1
/* Construct a full filename from a directory and a relative filename.
 
2
   Copyright (C) 2001-2004, 2006-2010 Free Software Foundation, Inc.
3
3
 
4
4
   This program is free software: you can redistribute it and/or modify it
5
5
   under the terms of the GNU General Public License as published by the
19
19
#include <config.h>
20
20
 
21
21
/* Specification.  */
 
22
#include "concat-filename.h"
 
23
 
 
24
#include <errno.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
 
22
28
#include "filename.h"
23
29
 
24
 
#include <string.h>
25
 
 
26
 
#include "xalloc.h"
27
 
 
28
30
/* Concatenate a directory filename, a relative filename and an optional
29
31
   suffix.  The directory may end with the directory separator.  The second
30
32
   argument may not start with the directory separator (it is relative).
31
 
   Return a freshly allocated filename.  */
 
33
   Return a freshly allocated filename.  Return NULL and set errno
 
34
   upon memory allocation failure.  */
32
35
char *
33
36
concatenated_filename (const char *directory, const char *filename,
34
 
                       const char *suffix)
 
37
                       const char *suffix)
35
38
{
36
39
  char *result;
37
40
  char *p;
39
42
  if (strcmp (directory, ".") == 0)
40
43
    {
41
44
      /* No need to prepend the directory.  */
42
 
      result = XNMALLOC (strlen (filename)
43
 
                         + (suffix != NULL ? strlen (suffix) : 0)
44
 
                         + 1,
45
 
                         char);
 
45
      result = (char *) malloc (strlen (filename)
 
46
                                + (suffix != NULL ? strlen (suffix) : 0)
 
47
                                + 1);
 
48
      if (result == NULL)
 
49
        return NULL; /* errno is set here */
46
50
      p = result;
47
51
    }
48
52
  else
49
53
    {
50
54
      size_t directory_len = strlen (directory);
51
55
      int need_slash =
52
 
        (directory_len > FILE_SYSTEM_PREFIX_LEN (directory)
53
 
         && !ISSLASH (directory[directory_len - 1]));
54
 
      result = XNMALLOC (directory_len + need_slash
55
 
                         + strlen (filename)
56
 
                         + (suffix != NULL ? strlen (suffix) : 0)
57
 
                         + 1,
58
 
                         char);
 
56
        (directory_len > FILE_SYSTEM_PREFIX_LEN (directory)
 
57
         && !ISSLASH (directory[directory_len - 1]));
 
58
      result = (char *) malloc (directory_len + need_slash
 
59
                                + strlen (filename)
 
60
                                + (suffix != NULL ? strlen (suffix) : 0)
 
61
                                + 1);
 
62
      if (result == NULL)
 
63
        return NULL; /* errno is set here */
59
64
      memcpy (result, directory, directory_len);
60
65
      p = result + directory_len;
61
66
      if (need_slash)
62
 
        *p++ = '/';
 
67
        *p++ = '/';
63
68
    }
64
69
  p = stpcpy (p, filename);
65
70
  if (suffix != NULL)