cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

path.c
Go to the documentation of this file.
1 #include <path.h>
2 #include <string_lib.h>
3 #include <file_lib.h>
4 
5 const char *Path_Basename(const char *path)
6 {
7  assert(path != NULL);
8 
9  const char *filename = strrchr(path, '/');
10 
11  if (filename != NULL)
12  {
13  filename += 1;
14  }
15  else
16  {
17  filename = path;
18  }
19 
20  if (filename[0] == '\0')
21  {
22  return NULL;
23  }
24 
25  return filename;
26 }
27 
28 char *Path_JoinAlloc(const char *dir, const char *leaf)
29 {
30  if (StringEndsWith(dir, "/"))
31  {
32  return StringConcatenate(2, dir, leaf);
33  }
34  return StringConcatenate(3, dir, "/", leaf);
35 }
36 
37 char *Path_GetQuoted(const char *path)
38 {
39  if (path == NULL)
40  {
41  return NULL;
42  }
43 
44  size_t path_len = strlen(path);
45  if ((path[0] == '"') && (path[path_len - 1] == '"'))
46  {
47  /* already quoted, just duplicate */
48  return SafeStringDuplicate(path);
49  }
50 
51  bool needs_quoting = false;
52  for (const char *cp = path; !needs_quoting && (*cp != '\0'); cp++)
53  {
54  /* let's quote everything that's not just alphanumerics, underscores and
55  * dashes */
56  needs_quoting = !(((*cp >= 'a') && (*cp <= 'z')) ||
57  ((*cp >= 'A') && (*cp <= 'Z')) ||
58  ((*cp >= '0') && (*cp <= '9')) ||
59  (*cp == '_') || (*cp == '-') ||
60  IsFileSep(*cp));
61  }
62  if (needs_quoting)
63  {
64  return StringConcatenate(3, "\"", path, "\"");
65  }
66  else
67  {
68  return SafeStringDuplicate(path);
69  }
70 }
#define IsFileSep(c)
Definition: file_lib.h:101
#define NULL
Definition: getopt1.c:56
const char * Path_Basename(const char *path)
Returns the filename part of a string/path, similar to basename.
Definition: path.c:5
char * Path_GetQuoted(const char *path)
Definition: path.c:37
char * Path_JoinAlloc(const char *dir, const char *leaf)
Definition: path.c:28
bool StringEndsWith(const char *str, const char *suffix)
Check if a string ends with the given suffix.
Definition: string_lib.c:1330
char * SafeStringDuplicate(const char *str)
Definition: string_lib.c:172
char * StringConcatenate(size_t count, const char *first,...)
Definition: string_lib.c:348