X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=4143f6d643ebca2cb02c105ba0504d1a55d9f72d;hp=5f6249eb045d2fbb938af32e135a6811695f5c77;hb=a9169c1c589bf7c7a29e7905d17e350ce7c7c48e;hpb=6f53e671aa7539cab02c9f739d84d28a343ca5bc diff --git a/src/shared/util.c b/src/shared/util.c index 5f6249eb0..4143f6d64 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6933,10 +6933,21 @@ int is_symlink(const char *path) { if (lstat(path, &info) < 0) return -errno; - if (S_ISLNK(info.st_mode)) - return 1; + return !!S_ISLNK(info.st_mode); +} - return 0; +int is_dir(const char* path, bool follow) { + struct stat st; + + if (follow) { + if (stat(path, &st) < 0) + return -errno; + } else { + if (lstat(path, &st) < 0) + return -errno; + } + + return !!S_ISDIR(st.st_mode); } int unquote_first_word(const char **p, char **ret) { @@ -7164,3 +7175,23 @@ int free_and_strdup(char **p, const char *s) { return 0; } + +int sethostname_idempotent(const char *s) { + int r; + char buf[HOST_NAME_MAX + 1] = {}; + + assert(s); + + r = gethostname(buf, sizeof(buf)); + if (r < 0) + return -errno; + + if (streq(buf, s)) + return 0; + + r = sethostname(s, strlen(s)); + if (r < 0) + return -errno; + + return 1; +}