chiark / gitweb /
Get rid of our reimplementation of basename
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 7 Dec 2013 02:29:55 +0000 (21:29 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 7 Dec 2013 02:29:55 +0000 (21:29 -0500)
The only problem is that libgen.h #defines basename to point to it's
own broken implementation instead of the GNU one. This can be fixed
by #undefining basename.

22 files changed:
src/core/execute.c
src/core/load-fragment.c
src/core/manager.c
src/core/service.c
src/core/unit.c
src/delta/delta.c
src/journal/journalctl.c
src/locale/localectl.c
src/login/logind-inhibit.c
src/login/logind-seat.c
src/login/logind-session.c
src/nspawn/nspawn.c
src/shared/cgroup-show.c
src/shared/conf-files.c
src/shared/install.c
src/shared/path-util.c
src/shared/path-util.h
src/shared/util.c
src/shared/utmp-wtmp.c
src/systemctl/systemctl.c
src/test/test-install.c
src/test/test-path-util.c

index a0d63a4..55b8bb5 100644 (file)
@@ -41,6 +41,7 @@
 #include <linux/seccomp-bpf.h>
 #include <glob.h>
 #include <libgen.h>
+#undef basename
 
 #ifdef HAVE_PAM
 #include <security/pam_appl.h>
@@ -908,7 +909,7 @@ static void rename_process_from_path(const char *path) {
         /* This resulting string must fit in 10 chars (i.e. the length
          * of "/sbin/init") to look pretty in /bin/ps */
 
-        p = path_get_file_name(path);
+        p = basename(path);
         if (isempty(p)) {
                 rename_process("(...)");
                 return;
@@ -1197,13 +1198,13 @@ int exec_spawn(ExecCommand *command,
                         goto fail_child;
                 }
 
-                err = setup_output(context, STDOUT_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+                err = setup_output(context, STDOUT_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
                 if (err < 0) {
                         r = EXIT_STDOUT;
                         goto fail_child;
                 }
 
-                err = setup_output(context, STDERR_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+                err = setup_output(context, STDERR_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
                 if (err < 0) {
                         r = EXIT_STDERR;
                         goto fail_child;
index d43d1b4..99cf20d 100644 (file)
@@ -2431,7 +2431,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
                 /* Add the file name we are currently looking at to
                  * the names of this unit, but only if it is a valid
                  * unit name. */
-                name = path_get_file_name(*filename);
+                name = basename(*filename);
 
                 if (unit_name_is_valid(name, true)) {
 
index 0a3e840..f06df54 100644 (file)
@@ -1078,7 +1078,7 @@ int manager_load_unit_prepare(
                 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
 
         if (!name)
-                name = path_get_file_name(path);
+                name = basename(path);
 
         t = unit_name_to_type(name);
 
index 530a305..702443d 100644 (file)
@@ -790,7 +790,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                                 goto finish;
                                         }
 
-                                        r = sysv_translate_facility(n, path_get_file_name(path), &m);
+                                        r = sysv_translate_facility(n, basename(path), &m);
                                         free(n);
 
                                         if (r < 0)
@@ -842,7 +842,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                                 goto finish;
                                         }
 
-                                        r = sysv_translate_facility(n, path_get_file_name(path), &m);
+                                        r = sysv_translate_facility(n, basename(path), &m);
                                         if (r < 0) {
                                                 log_error_unit(u->id,
                                                                "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
index 81d2162..6458c7f 100644 (file)
@@ -1848,7 +1848,7 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
         assert(p);
 
         if (!name)
-                name = path_get_file_name(path);
+                name = basename(path);
 
         if (!unit_name_is_template(name)) {
                 *p = NULL;
@@ -2615,7 +2615,7 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
         if (u->unit_file_state < 0 && u->fragment_path)
                 u->unit_file_state = unit_file_get_state(
                                 u->manager->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
-                                NULL, path_get_file_name(u->fragment_path));
+                                NULL, basename(u->fragment_path));
 
         return u->unit_file_state;
 }
index 4aaf1df..0785e7b 100644 (file)
@@ -251,7 +251,7 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
                 if (!p)
                         return -ENOMEM;
 
-                k = hashmap_put(h, path_get_file_name(p), p);
+                k = hashmap_put(h, basename(p), p);
                 if (k < 0) {
                         free(p);
                         if (k != -EEXIST)
@@ -303,7 +303,7 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
 
                 path_kill_slashes(p);
 
-                k = hashmap_put(top, path_get_file_name(p), p);
+                k = hashmap_put(top, basename(p), p);
                 if (k >= 0) {
                         p = strdup(p);
                         if (!p)
@@ -313,8 +313,8 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
                         return k;
                 }
 
-                free(hashmap_remove(bottom, path_get_file_name(p)));
-                k = hashmap_put(bottom, path_get_file_name(p), p);
+                free(hashmap_remove(bottom, basename(p)));
+                k = hashmap_put(bottom, basename(p), p);
                 if (k < 0) {
                         free(p);
                         return k;
index c44be74..f74dde9 100644 (file)
@@ -658,7 +658,7 @@ static int add_matches(sd_journal *j, char **args) {
                                 if (executable_is_script(path, &interpreter) > 0) {
                                         _cleanup_free_ char *comm;
 
-                                        comm = strndup(path_get_file_name(path), 15);
+                                        comm = strndup(basename(path), 15);
                                         if (!comm)
                                                 return log_oom();
 
index 76a53f6..b0abe7b 100644 (file)
@@ -415,7 +415,7 @@ static int nftw_cb(
             !endswith(fpath, ".map.gz"))
                 return 0;
 
-        p = strdup(path_get_file_name(fpath));
+        p = strdup(basename(fpath));
         if (!p)
                 return log_oom();
 
index 35e1abd..ec6a722 100644 (file)
@@ -45,7 +45,7 @@ Inhibitor* inhibitor_new(Manager *m, const char* id) {
                 return NULL;
         }
 
-        i->id = path_get_file_name(i->state_file);
+        i->id = basename(i->state_file);
 
         if (hashmap_put(m->inhibitors, i->id, i) < 0) {
                 free(i->state_file);
index b1a5ec3..c738604 100644 (file)
@@ -51,7 +51,7 @@ Seat *seat_new(Manager *m, const char *id) {
                 return NULL;
         }
 
-        s->id = path_get_file_name(s->state_file);
+        s->id = basename(s->state_file);
         s->manager = m;
 
         if (hashmap_put(m->seats, s->id, s) < 0) {
index beaa601..10ea526 100644 (file)
@@ -79,7 +79,7 @@ Session* session_new(Manager *m, const char *id) {
                 return NULL;
         }
 
-        s->id = path_get_file_name(s->state_file);
+        s->id = basename(s->state_file);
 
         if (hashmap_put(m->sessions, s->id, s) < 0) {
                 hashmap_free(s->devices);
index eef0e9a..583912f 100644 (file)
@@ -1096,7 +1096,7 @@ int main(int argc, char *argv[]) {
         path_kill_slashes(arg_directory);
 
         if (!arg_machine) {
-                arg_machine = strdup(path_get_file_name(arg_directory));
+                arg_machine = strdup(basename(arg_directory));
                 if (!arg_machine) {
                         log_oom();
                         goto finish;
index ce02b76..ee14cee 100644 (file)
@@ -178,7 +178,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
 
                 if (last) {
                         printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_BRANCH),
-                                           path_get_file_name(last));
+                                           basename(last));
 
                         if (!p1) {
                                 p1 = strappend(prefix, draw_special_char(DRAW_TREE_VERT));
@@ -202,7 +202,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
 
         if (last) {
                 printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT),
-                                   path_get_file_name(last));
+                                   basename(last));
 
                 if (!p2) {
                         p2 = strappend(prefix, "  ");
index ed4070c..92204a6 100644 (file)
@@ -71,7 +71,7 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
                 if (!p)
                         return -ENOMEM;
 
-                r = hashmap_put(h, path_get_file_name(p), p);
+                r = hashmap_put(h, basename(p), p);
                 if (r == -EEXIST) {
                         log_debug("Skipping overridden file: %s.", p);
                         free(p);
@@ -92,7 +92,7 @@ static int base_cmp(const void *a, const void *b) {
 
         s1 = *(char * const *)a;
         s2 = *(char * const *)b;
-        return strcmp(path_get_file_name(s1), path_get_file_name(s2));
+        return strcmp(basename(s1), basename(s2));
 }
 
 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
index 100ed69..17e8a75 100644 (file)
@@ -276,10 +276,10 @@ static int remove_marked_symlinks_fd(
 
                         found =
                                 set_get(remove_symlinks_to, dest) ||
-                                set_get(remove_symlinks_to, path_get_file_name(dest));
+                                set_get(remove_symlinks_to, basename(dest));
 
                         if (unit_name_is_instance(p))
-                                found = found && strv_contains(files, path_get_file_name(p));
+                                found = found && strv_contains(files, basename(p));
 
                         if (found) {
 
@@ -451,7 +451,7 @@ static int find_symlinks_fd(
                         if (path_is_absolute(name))
                                 found_dest = path_equal(dest, name);
                         else
-                                found_dest = streq(path_get_file_name(dest), name);
+                                found_dest = streq(basename(dest), name);
 
                         if (found_path && found_dest) {
                                 _cleanup_free_ char *t = NULL;
@@ -718,7 +718,7 @@ int unit_file_link(
                 char *fn;
                 struct stat st;
 
-                fn = path_get_file_name(*i);
+                fn = basename(*i);
 
                 if (!path_is_absolute(*i) ||
                     !unit_name_is_valid(fn, true)) {
@@ -861,7 +861,7 @@ static int install_info_add(
         assert(name || path);
 
         if (!name)
-                name = path_get_file_name(path);
+                name = basename(path);
 
         if (!unit_name_is_valid(name, true))
                 return -EINVAL;
@@ -1429,7 +1429,7 @@ static int install_context_mark_for_removal(
                         char *unit_file;
 
                         if (i->path) {
-                                unit_file = path_get_file_name(i->path);
+                                unit_file = basename(i->path);
 
                                 if (unit_name_is_instance(unit_file))
                                         /* unit file named as instance exists, thus all symlinks
@@ -1647,7 +1647,7 @@ int unit_file_get_default(
                 else if (r < 0)
                         return r;
                 else
-                        n = strdup(path_get_file_name(tmp));
+                        n = strdup(basename(tmp));
 
                 if (!n)
                         return -ENOMEM;
@@ -2007,7 +2007,7 @@ int unit_file_get_list(
                                 f->state = UNIT_FILE_STATIC;
 
                 found:
-                        r = hashmap_put(h, path_get_file_name(f->path), f);
+                        r = hashmap_put(h, basename(f->path), f);
                         if (r < 0)
                                 return r;
                         f = NULL; /* prevent cleanup */
index 6c4efbf..3843861 100644 (file)
@@ -45,18 +45,6 @@ bool is_path(const char *p) {
         return !!strchr(p, '/');
 }
 
-char *path_get_file_name(const char *p) {
-        char *r;
-
-        assert(p);
-
-        r = strrchr(p, '/');
-        if (r)
-                return r + 1;
-
-        return (char*) p;
-}
-
 int path_get_parent(const char *path, char **_r) {
         const char *e, *a = NULL, *b = NULL, *p;
         char *r;
index 42b4189..1098f65 100644 (file)
@@ -34,7 +34,6 @@
 
 bool is_path(const char *p) _pure_;
 char** path_split_and_make_absolute(const char *p);
-char* path_get_file_name(const char *p) _pure_;
 int path_get_parent(const char *path, char **parent);
 bool path_is_absolute(const char *p) _pure_;
 char* path_make_absolute(const char *p, const char *prefix);
index c712931..9c07392 100644 (file)
@@ -59,6 +59,7 @@
 #include <langinfo.h>
 #include <locale.h>
 #include <libgen.h>
+#undef basename
 
 #include "macro.h"
 #include "util.h"
@@ -3961,8 +3962,8 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
         if (!t)
                 return -ENOMEM;
 
-        fn = path_get_file_name(path);
-        k = fn-path;
+        fn = basename(path);
+        k = fn - path;
         memcpy(t, path, k);
         t[k] = '.';
         stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
@@ -4147,7 +4148,7 @@ int symlink_atomic(const char *from, const char *to) {
         if (!t)
                 return -ENOMEM;
 
-        fn = path_get_file_name(to);
+        fn = basename(to);
         k = fn-to;
         memcpy(t, to, k);
         t[k] = '.';
index 5d88405..4a3a3f4 100644 (file)
@@ -216,7 +216,7 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
         strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
 
         if (line)
-                strncpy(store.ut_line, path_get_file_name(line), sizeof(store.ut_line));
+                strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
 
         return write_entry_both(&store);
 }
index 6b6cb3d..c6ebf9b 100644 (file)
@@ -980,7 +980,7 @@ static int compare_unit_file_list(const void *a, const void *b) {
                         return r;
         }
 
-        return strcasecmp(path_get_file_name(u->path), path_get_file_name(v->path));
+        return strcasecmp(basename(u->path), basename(v->path));
 }
 
 static bool output_show_unit_file(const UnitFileList *u) {
@@ -1000,7 +1000,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
                 if (!output_show_unit_file(u))
                         continue;
 
-                max_id_len = MAX(max_id_len, strlen(path_get_file_name(u->path)));
+                max_id_len = MAX(max_id_len, strlen(basename(u->path)));
                 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
         }
 
@@ -1041,7 +1041,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
                 } else
                         on = off = "";
 
-                id = path_get_file_name(u->path);
+                id = basename(u->path);
 
                 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
 
@@ -2688,7 +2688,7 @@ static void print_status_info(
 
                         last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
 
-                        printf("%s%s", path_get_file_name(*dropin), last ? "\n" : ", ");
+                        printf("%s%s", basename(*dropin), last ? "\n" : ", ");
                 }
         }
 
@@ -4404,7 +4404,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (!isempty(arg_root))
                         argv[c++] = q = strappend("--root=", arg_root);
 
-                argv[c++] = path_get_file_name(p);
+                argv[c++] = basename(p);
                 argv[c++] =
                         streq(verb, "enable") ? "on" :
                         streq(verb, "disable") ? "off" : "--level=5";
index 2c1b9ef..2087d52 100644 (file)
@@ -58,7 +58,7 @@ int main(int argc, char* argv[]) {
         HASHMAP_FOREACH(p, h, i) {
                 UnitFileState s;
 
-                s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(p->path));
+                s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(p->path));
 
                 assert_se(p->state == s);
 
@@ -176,7 +176,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
 
         log_error("disable files2");
         changes = NULL;
@@ -188,7 +188,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
 
         log_error("link files2");
         changes = NULL;
@@ -200,7 +200,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
 
         log_error("disable files2");
         changes = NULL;
@@ -212,7 +212,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
 
         log_error("link files2");
         changes = NULL;
@@ -224,7 +224,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
 
         log_error("reenable files2");
         changes = NULL;
@@ -236,7 +236,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
 
         log_error("disable files2");
         changes = NULL;
@@ -248,7 +248,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
         log_error("preset files");
         changes = NULL;
         n_changes = 0;
@@ -259,7 +259,7 @@ int main(int argc, char* argv[]) {
         dump_changes(changes, n_changes);
         unit_file_changes_free(changes, n_changes);
 
-        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files[0])) == UNIT_FILE_ENABLED);
+        assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files[0])) == UNIT_FILE_ENABLED);
 
         return 0;
 }
index ed3b315..bec2a83 100644 (file)
@@ -50,10 +50,10 @@ static void test_path(void) {
         assert_se(is_path("a/b"));
         assert_se(!is_path("."));
 
-        assert_se(streq(path_get_file_name("./aa/bb/../file.da."), "file.da."));
-        assert_se(streq(path_get_file_name("/aa///.file"), ".file"));
-        assert_se(streq(path_get_file_name("/aa///file..."), "file..."));
-        assert_se(streq(path_get_file_name("file.../"), ""));
+        assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
+        assert_se(streq(basename("/aa///.file"), ".file"));
+        assert_se(streq(basename("/aa///file..."), "file..."));
+        assert_se(streq(basename("file.../"), ""));
 
 #define test_parent(x, y) {                                \
                 char _cleanup_free_ *z = NULL;             \