libcfe  0.12.1
some useful C-functions
create_path.c
Go to the documentation of this file.
1 #include "config.h"
2 #include "create_path.h"
3 
4 #include <stdlib.h>
5 #include <string.h>
6 #include <libintl.h>
7 
8 #include "len.h"
9 #include "output.h"
10 
11 char *create_path(const char *dir, int dirlen, const char *file, int filelen)
12 {
13  char *path = NULL;
14  int pathlen = 0;
15 
16  if(dirlen == 0)
17  dirlen = str_len(dir);
18  if(filelen == 0)
19  filelen = str_len(file);
20 
21  if(dirlen == 0 && filelen > 0 && file[0] == '/')
22  {
23  path = strdup(file);
24  }
25  else if(dirlen > 0 && filelen > 0)
26  {
27  if(dir[dirlen - 1] == '/')
28  dirlen--;
29  if(file[0] == '/')
30  {
31  filelen--;
32  file++;
33  }
34  pathlen = dirlen + filelen + 1;
35  path = malloc(pathlen + 1);
36  strncpy(path, dir, dirlen);
37  path[dirlen] = '/';
38  strncpy(path + dirlen + 1, file, filelen);
39  path[pathlen] = '\0';
40  }
41  else
42  error(_("create_path: length of filename is zero."));
43 
44  return path;
45 }
#define str_len(s)
Shorthand for counting &#39;\0&#39; terminating strings. See _len for more info.
Definition: len.h:17
#define _(string)
Definition: output.h:43
#define error(...)
Definition: output.h:30
char * create_path(const char *dir, int dirlen, const char *file, int filelen)
Definition: create_path.c:11