mirror of
https://github.com/clearlinux/systemd-stable.git
synced 2026-09-27 16:40:23 +00:00
implement new utility functions strstrip() and file_in_same_dir()
This commit is contained in:
@@ -490,3 +490,51 @@ int reset_all_signal_handlers(void) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *strstrip(char *s) {
|
||||
char *e, *l = NULL;
|
||||
|
||||
/* Drops trailing whitespace. Modifies the string in
|
||||
* place. Returns pointer to first non-space character */
|
||||
|
||||
s += strspn(s, WHITESPACE);
|
||||
|
||||
for (e = s; *e; e++)
|
||||
if (!strchr(WHITESPACE, *e))
|
||||
l = e;
|
||||
|
||||
if (l)
|
||||
*(l+1) = 0;
|
||||
else
|
||||
*s = 0;
|
||||
|
||||
return s;
|
||||
|
||||
}
|
||||
|
||||
char *file_in_same_dir(const char *path, const char *filename) {
|
||||
char *e, *r;
|
||||
size_t k;
|
||||
|
||||
assert(path);
|
||||
assert(filename);
|
||||
|
||||
/* This removes the last component of path and appends
|
||||
* filename, unless the latter is absolute anyway or the
|
||||
* former isn't */
|
||||
|
||||
if (path_is_absolute(filename))
|
||||
return strdup(filename);
|
||||
|
||||
if (!(e = strrchr(path, '/')))
|
||||
return strdup(filename);
|
||||
|
||||
k = strlen(filename);
|
||||
if (!(r = new(char, e-path+1+k+1)))
|
||||
return NULL;
|
||||
|
||||
memcpy(r, path, e-path+1);
|
||||
memcpy(r+(e-path)+1, filename, k+1);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ typedef uint64_t usec_t;
|
||||
#define NSEC_PER_USEC 1000ULL
|
||||
|
||||
/* What is interpreted as whitespace? */
|
||||
#define WHITESPACE " \t\n"
|
||||
#define WHITESPACE " \t\n\r"
|
||||
|
||||
usec_t now(clockid_t clock);
|
||||
|
||||
@@ -101,4 +101,7 @@ char *path_make_absolute(const char *p, const char *prefix);
|
||||
|
||||
int reset_all_signal_handlers(void);
|
||||
|
||||
char *strstrip(char *s);
|
||||
char *file_in_same_dir(const char *path, const char *filename);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user