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=61d6680dddfb5c5dabbe9a45b38b7ebe658f9ee6;hb=a9169c1c589bf7c7a29e7905d17e350ce7c7c48e;hpb=d5099efc47d4e6ac60816b5381a5f607ab03f06e diff --git a/src/shared/util.c b/src/shared/util.c index 61d6680dd..4143f6d64 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1878,9 +1878,6 @@ int open_terminal(const char *name, int mode) { c++; } - if (fd < 0) - return -errno; - r = isatty(fd); if (r < 0) { safe_close(fd); @@ -2077,7 +2074,7 @@ int acquire_terminal( * ended our handle will be dead. It's important that * we do this after sleeping, so that we don't enter * an endless loop. */ - safe_close(fd); + fd = safe_close(fd); } safe_close(notify); @@ -3276,7 +3273,7 @@ unsigned columns(void) { c = 0; e = getenv("COLUMNS"); if (e) - safe_atoi(e, &c); + (void) safe_atoi(e, &c); if (c <= 0) c = fd_columns(STDOUT_FILENO); @@ -3310,7 +3307,7 @@ unsigned lines(void) { l = 0; e = getenv("LINES"); if (e) - safe_atou(e, &l); + (void) safe_atou(e, &l); if (l <= 0) l = fd_lines(STDOUT_FILENO); @@ -6936,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) { @@ -7167,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; +}