From 9bc5282e3021b239fc602887162f18ed421aeb91 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Wed, 24 Feb 2021 14:46:19 +0100 Subject: [PATCH] style: `make indent` for C --- install/dracut-install.c | 190 +++++++++++++++++++++------------------ install/hashmap.c | 122 ++++++++++++++++--------- install/hashmap.h | 64 ++++++------- install/log.c | 85 +++++++++--------- install/log.h | 32 ++----- install/macro.h | 14 +-- install/strv.c | 155 ++++++++++++++++++-------------- install/strv.h | 14 +-- install/util.c | 173 +++++++++++++++++++---------------- install/util.h | 144 ++++++++++++++++------------- logtee.c | 102 +++++++++++---------- 11 files changed, 597 insertions(+), 498 deletions(-) diff --git a/install/dracut-install.c b/install/dracut-install.c index 24c5287f..0f123e4a 100644 --- a/install/dracut-install.c +++ b/install/dracut-install.c @@ -88,45 +88,55 @@ static bool arg_mod_filter_noname = false; static int dracut_install(const char *src, const char *dst, bool isdir, bool resolvedeps, bool hashdst); - -static inline void kmod_module_unrefp(struct kmod_module **p) { +static inline void kmod_module_unrefp(struct kmod_module **p) +{ if (*p) kmod_module_unref(*p); } + #define _cleanup_kmod_module_unref_ _cleanup_(kmod_module_unrefp) -static inline void kmod_module_unref_listp(struct kmod_list **p) { +static inline void kmod_module_unref_listp(struct kmod_list **p) +{ if (*p) kmod_module_unref_list(*p); } + #define _cleanup_kmod_module_unref_list_ _cleanup_(kmod_module_unref_listp) -static inline void kmod_module_info_free_listp(struct kmod_list **p) { +static inline void kmod_module_info_free_listp(struct kmod_list **p) +{ if (*p) kmod_module_info_free_list(*p); } + #define _cleanup_kmod_module_info_free_list_ _cleanup_(kmod_module_info_free_listp) -static inline void kmod_unrefp(struct kmod_ctx **p) { +static inline void kmod_unrefp(struct kmod_ctx **p) +{ kmod_unref(*p); } + #define _cleanup_kmod_unref_ _cleanup_(kmod_unrefp) -static inline void kmod_module_dependency_symbols_free_listp(struct kmod_list **p) { +static inline void kmod_module_dependency_symbols_free_listp(struct kmod_list **p) +{ if (*p) kmod_module_dependency_symbols_free_list(*p); } + #define _cleanup_kmod_module_dependency_symbols_free_list_ _cleanup_(kmod_module_dependency_symbols_free_listp) -static inline void fts_closep(FTS **p) { +static inline void fts_closep(FTS ** p) +{ if (*p) fts_close(*p); } + #define _cleanup_fts_close_ _cleanup_(fts_closep) #define _cleanup_globfree_ _cleanup_(globfree) - static size_t dir_len(char const *file) { size_t length; @@ -288,7 +298,7 @@ static int cp(const char *src, const char *dst) if (ret == 0) { struct timeval tv[2]; if (fchown(dest_desc, sb.st_uid, sb.st_gid) != 0) - if(fchown(dest_desc, (uid_t) - 1, sb.st_gid) != 0) { + if (fchown(dest_desc, (uid_t) - 1, sb.st_gid) != 0) { if (geteuid() == 0) log_error("Failed to chown %s: %m", dst); else @@ -314,11 +324,11 @@ static int cp(const char *src, const char *dst) pid = fork(); if (pid == 0) { if (geteuid() == 0 && no_xattr == false) - execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,xattr,timestamps", "-fL", src, dst, - NULL); + execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,xattr,timestamps", "-fL", + src, dst, NULL); else - execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,timestamps", "-fL", src, dst, - NULL); + execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,timestamps", "-fL", src, + dst, NULL); _exit(EXIT_FAILURE); } @@ -326,11 +336,13 @@ static int cp(const char *src, const char *dst) if (errno != EINTR) { ret = -1; if (geteuid() == 0 && no_xattr == false) - log_error("Failed: cp --reflink=auto --sparse=auto --preserve=mode,xattr,timestamps -fL %s %s", src, - dst); + log_error + ("Failed: cp --reflink=auto --sparse=auto --preserve=mode,xattr,timestamps -fL %s %s", + src, dst); else - log_error("Failed: cp --reflink=auto --sparse=auto --preserve=mode,timestamps -fL %s %s", src, - dst); + log_error + ("Failed: cp --reflink=auto --sparse=auto --preserve=mode,timestamps -fL %s %s", + src, dst); break; } } @@ -415,24 +427,26 @@ static char *get_real_file(const char *src, bool fullyresolve) if (sysrootdirlen) { if (strncmp(src, sysrootdir, sysrootdirlen) == 0) fullsrcpath = strdup(src); - else if (asprintf(&fullsrcpath, "%s/%s", (sysrootdirlen ? sysrootdir : ""), (src[0] == '/' ? src+1 : src)) < 0) - _exit(EXIT_FAILURE); + else if (asprintf + (&fullsrcpath, "%s/%s", (sysrootdirlen ? sysrootdir : ""), + (src[0] == '/' ? src + 1 : src)) < 0) + _exit(EXIT_FAILURE); } else fullsrcpath = strdup(src); log_debug("get_real_file('%s')", fullsrcpath); if (lstat(fullsrcpath, &sb) < 0) - return NULL; + return NULL; switch (sb.st_mode & S_IFMT) { case S_IFDIR: case S_IFREG: - return strdup(fullsrcpath); + return strdup(fullsrcpath); case S_IFLNK: - break; + break; default: - return NULL; + return NULL; } linksz = readlink(fullsrcpath, linktarget, sizeof(linktarget)); @@ -678,7 +692,7 @@ void dracut_log_cp(const char *path) log_error("Could not append '%s' to logfile '%s': %m", path, logfile); } -static bool check_hashmap(Hashmap *hm, const char *item) +static bool check_hashmap(Hashmap * hm, const char *item) { char *existing; existing = hashmap_get(hm, item); @@ -690,7 +704,8 @@ static bool check_hashmap(Hashmap *hm, const char *item) return false; } -static int dracut_mkdir(const char *src) { +static int dracut_mkdir(const char *src) +{ _cleanup_free_ char *parent = NULL; char *path; struct stat sb; @@ -699,7 +714,7 @@ static int dracut_mkdir(const char *src) { if (!parent) return 1; - path = parent[0] == '/' ? parent+1 : parent; + path = parent[0] == '/' ? parent + 1 : parent; while (path) { path = strstr(path, "/"); if (path) @@ -788,7 +803,7 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir src_mode = sb.st_mode; } - ret = asprintf(&fulldstpath, "%s/%s", destrootdir, (dst[0]=='/' ? (dst+1) : dst)); + ret = asprintf(&fulldstpath, "%s/%s", destrootdir, (dst[0] == '/' ? (dst + 1) : dst)); if (ret < 0) { log_error("Out of memory!"); exit(EXIT_FAILURE); @@ -891,7 +906,9 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir if (lstat(fulldstpath, &sb) != 0) { _cleanup_free_ char *absdestpath = NULL; - ret = asprintf(&absdestpath, "%s/%s", destrootdir, (abspath[0]=='/' ? (abspath+1) : abspath) + sysrootdirlen); + ret = + asprintf(&absdestpath, "%s/%s", destrootdir, + (abspath[0] == '/' ? (abspath + 1) : abspath) + sysrootdirlen); if (ret < 0) { log_error("Out of memory!"); exit(EXIT_FAILURE); @@ -955,7 +972,7 @@ static void item_free(char *i) static void usage(int status) { - /* */ + /* */ printf("Usage: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -a SOURCE...\n" "or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... SOURCE DEST\n" "or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -m KERNELMODULE [KERNELMODULE …]\n" @@ -995,9 +1012,7 @@ static void usage(int status) " --debug Show debug output\n" " --version Show package version\n" " -h --help Show this help\n" - "\n", - program_invocation_short_name, program_invocation_short_name, - program_invocation_short_name); + "\n", program_invocation_short_name, program_invocation_short_name, program_invocation_short_name); exit(status); } @@ -1086,35 +1101,35 @@ static int parse_argv(int argc, char *argv[]) sysrootdirlen = strlen(sysrootdir); break; case 'p': - if (regcomp(&mod_filter_path, optarg, REG_NOSUB|REG_EXTENDED) != 0) { + if (regcomp(&mod_filter_path, optarg, REG_NOSUB | REG_EXTENDED) != 0) { log_error("Module path filter %s is not a regular expression", optarg); exit(EXIT_FAILURE); } arg_mod_filter_path = true; break; case 'P': - if (regcomp(&mod_filter_nopath, optarg, REG_NOSUB|REG_EXTENDED) != 0) { + if (regcomp(&mod_filter_nopath, optarg, REG_NOSUB | REG_EXTENDED) != 0) { log_error("Module path filter %s is not a regular expression", optarg); exit(EXIT_FAILURE); } arg_mod_filter_nopath = true; break; case 's': - if (regcomp(&mod_filter_symbol, optarg, REG_NOSUB|REG_EXTENDED) != 0) { + if (regcomp(&mod_filter_symbol, optarg, REG_NOSUB | REG_EXTENDED) != 0) { log_error("Module symbol filter %s is not a regular expression", optarg); exit(EXIT_FAILURE); } arg_mod_filter_symbol = true; break; case 'S': - if (regcomp(&mod_filter_nosymbol, optarg, REG_NOSUB|REG_EXTENDED) != 0) { + if (regcomp(&mod_filter_nosymbol, optarg, REG_NOSUB | REG_EXTENDED) != 0) { log_error("Module symbol filter %s is not a regular expression", optarg); exit(EXIT_FAILURE); } arg_mod_filter_nosymbol = true; break; case 'N': - if (regcomp(&mod_filter_noname, optarg, REG_NOSUB|REG_EXTENDED) != 0) { + if (regcomp(&mod_filter_noname, optarg, REG_NOSUB | REG_EXTENDED) != 0) { log_error("Module symbol filter %s is not a regular expression", optarg); exit(EXIT_FAILURE); } @@ -1337,7 +1352,9 @@ static int install_all(int argc, char **argv) for (j = 0; j < globbuf.gl_pathc; j++) { char *dest = strdup(globbuf.gl_pathv[j] + sysrootdirlen); - ret |= dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, dest, arg_createdir, arg_resolvedeps, true); + ret |= + dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, dest, + arg_createdir, arg_resolvedeps, true); free(dest); } } @@ -1378,7 +1395,8 @@ static int install_firmware_fullpath(const char *fwpath) return ret; } -static int install_firmware(struct kmod_module *mod) { +static int install_firmware(struct kmod_module *mod) +{ struct kmod_list *l; _cleanup_kmod_module_info_free_list_ struct kmod_list *list = NULL; int ret; @@ -1387,8 +1405,7 @@ static int install_firmware(struct kmod_module *mod) { ret = kmod_module_get_info(mod, &list); if (ret < 0) { - log_error("could not get modinfo from '%s': %s\n", - kmod_module_get_name(mod), strerror(-ret)); + log_error("could not get modinfo from '%s': %s\n", kmod_module_get_name(mod), strerror(-ret)); return ret; } kmod_list_foreach(l, list) { @@ -1412,20 +1429,23 @@ static int install_firmware(struct kmod_module *mod) { exit(EXIT_FAILURE); } - if ((strstr(value, "*") != 0 || strstr(value, "?") != 0 || strstr(value, "[") != 0) && stat(fwpath, &sb) != 0) { + if ((strstr(value, "*") != 0 || strstr(value, "?") != 0 || strstr(value, "[") != 0) + && stat(fwpath, &sb) != 0) { int i; _cleanup_globfree_ glob_t globbuf; glob(fwpath, 0, NULL, &globbuf); for (i = 0; i < globbuf.gl_pathc; i++) { install_firmware_fullpath(globbuf.gl_pathv[i]); if (ret != 0) { - log_info("Possible missing firmware %s for kernel module %s", value, kmod_module_get_name(mod)); + log_info("Possible missing firmware %s for kernel module %s", value, + kmod_module_get_name(mod)); } } } else { install_firmware_fullpath(fwpath); if (ret != 0) { - log_info("Possible missing firmware %s for kernel module %s", value, kmod_module_get_name(mod)); + log_info("Possible missing firmware %s for kernel module %s", value, + kmod_module_get_name(mod)); } } } @@ -1453,7 +1473,8 @@ static bool check_module_symbols(struct kmod_module *mod) const char *symbol = kmod_module_symbol_get_symbol(itr); // log_debug("Checking symbol %s", symbol); if (regexec(&mod_filter_nosymbol, symbol, 0, NULL, 0) == 0) { - log_debug("Module %s: symbol %s matched exclusion filter", kmod_module_get_name(mod), symbol); + log_debug("Module %s: symbol %s matched exclusion filter", kmod_module_get_name(mod), + symbol); return false; } } @@ -1464,7 +1485,8 @@ static bool check_module_symbols(struct kmod_module *mod) const char *symbol = kmod_module_dependency_symbol_get_symbol(itr); // log_debug("Checking symbol %s", symbol); if (regexec(&mod_filter_symbol, symbol, 0, NULL, 0) == 0) { - log_debug("Module %s: symbol %s matched inclusion filter", kmod_module_get_name(mod), symbol); + log_debug("Module %s: symbol %s matched inclusion filter", kmod_module_get_name(mod), + symbol); return true; } } @@ -1496,19 +1518,19 @@ static int install_dependent_modules(struct kmod_list *modlist) int ret = 0; kmod_list_foreach(itr, modlist) { - _cleanup_kmod_module_unref_ struct kmod_module *mod = NULL; + _cleanup_kmod_module_unref_ struct kmod_module *mod = NULL; mod = kmod_module_get_module(itr); path = kmod_module_get_path(mod); if (path == NULL) continue; - if (check_hashmap(items_failed, path)) - return -1; + if (check_hashmap(items_failed, path)) + return -1; - if (check_hashmap(items, path)) { - continue; - } + if (check_hashmap(items, path)) { + continue; + } name = kmod_module_get_name(mod); @@ -1518,18 +1540,18 @@ static int install_dependent_modules(struct kmod_list *modlist) ret = dracut_install(path, &path[kerneldirlen], false, false, true); if (ret == 0) { - _cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL; - _cleanup_kmod_module_unref_list_ struct kmod_list *modpre = NULL; - _cleanup_kmod_module_unref_list_ struct kmod_list *modpost = NULL; + _cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL; + _cleanup_kmod_module_unref_list_ struct kmod_list *modpre = NULL; + _cleanup_kmod_module_unref_list_ struct kmod_list *modpost = NULL; log_debug("dracut_install '%s' '%s' OK", path, &path[kerneldirlen]); install_firmware(mod); - modlist = kmod_module_get_dependencies(mod); - ret = install_dependent_modules(modlist); - if (ret == 0) { - ret = kmod_module_get_softdeps(mod, &modpre, &modpost); - if (ret == 0) { + modlist = kmod_module_get_dependencies(mod); + ret = install_dependent_modules(modlist); + if (ret == 0) { + ret = kmod_module_get_softdeps(mod, &modpre, &modpost); + if (ret == 0) { int r; - ret = install_dependent_modules(modpre); + ret = install_dependent_modules(modpre); r = install_dependent_modules(modpost); ret = ret ? : r; } @@ -1594,9 +1616,9 @@ static int install_module(struct kmod_module *mod) if (ret == 0) { ret = kmod_module_get_softdeps(mod, &modpre, &modpost); - if (ret == 0) { + if (ret == 0) { int r; - ret = install_dependent_modules(modpre); + ret = install_dependent_modules(modpre); r = install_dependent_modules(modpost); ret = ret ? : r; } @@ -1614,9 +1636,9 @@ static int modalias_list(struct kmod_ctx *ctx) { char *paths[] = { "/sys/devices", NULL }; - fts = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL); + fts = fts_open(paths, FTS_NOCHDIR | FTS_NOSTAT, NULL); } - for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { + for (FTSENT * ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { _cleanup_fclose_ FILE *f = NULL; _cleanup_kmod_module_unref_list_ struct kmod_list *list = NULL; struct kmod_list *l; @@ -1631,7 +1653,7 @@ static int modalias_list(struct kmod_ctx *ctx) if (!(f = fopen(ftsent->fts_accpath, "r"))) continue; - if(!fgets(alias, sizeof(alias), f)) + if (!fgets(alias, sizeof(alias), f)) continue; len = strlen(alias); @@ -1639,8 +1661,8 @@ static int modalias_list(struct kmod_ctx *ctx) if (len == 0) continue; - if (alias[len-1] == '\n') - alias[len-1] = 0; + if (alias[len - 1] == '\n') + alias[len - 1] = 0; err = kmod_module_new_from_lookup(ctx, alias, &list); if (err < 0) @@ -1723,15 +1745,15 @@ static int install_modules(int argc, char **argv) size_t len; char *dupname = NULL; - if(!(fgets(name, sizeof(name), f))) + if (!(fgets(name, sizeof(name), f))) continue; len = strlen(name); if (len == 0) continue; - if (name[len-1] == '\n') - name[len-1] = 0; + if (name[len - 1] == '\n') + name[len - 1] = 0; log_debug("Adding module '%s' to hostonly module list", name); dupname = strdup(name); @@ -1802,7 +1824,7 @@ static int install_modules(int argc, char **argv) log_error("ERROR: installing module '%s'", modname); return -ENOENT; }; - ret = ( ret == 0 ? 0 : r ); + ret = (ret == 0 ? 0 : r); modinst = 1; } } else if (argv[i][0] == '=') { @@ -1810,7 +1832,7 @@ static int install_modules(int argc, char **argv) _cleanup_fts_close_ FTS *fts = NULL; log_debug("Handling =%s", &argv[i][1]); - /* FIXME and add more paths*/ + /* FIXME and add more paths */ r = asprintf(&path2, "%s/kernel/%s", kerneldir, &argv[i][1]); if (r < 0) { log_error("Out of memory!"); @@ -1831,27 +1853,26 @@ static int install_modules(int argc, char **argv) { char *paths[] = { path1, path2, path3, NULL }; - fts = fts_open(paths, FTS_COMFOLLOW|FTS_NOCHDIR|FTS_NOSTAT|FTS_LOGICAL, NULL); + fts = fts_open(paths, FTS_COMFOLLOW | FTS_NOCHDIR | FTS_NOSTAT | FTS_LOGICAL, NULL); } - for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { + for (FTSENT * ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { _cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL; _cleanup_free_ const char *modname = NULL; - if((ftsent->fts_info == FTS_D) && !check_module_path(ftsent->fts_accpath)) { + if ((ftsent->fts_info == FTS_D) && !check_module_path(ftsent->fts_accpath)) { fts_set(fts, ftsent, FTS_SKIP); log_debug("Skipping %s", ftsent->fts_accpath); continue; } - if((ftsent->fts_info != FTS_F) && (ftsent->fts_info != FTS_SL)) { + if ((ftsent->fts_info != FTS_F) && (ftsent->fts_info != FTS_SL)) { log_debug("Ignoring %s", ftsent->fts_accpath); continue; } log_debug("Handling %s", ftsent->fts_accpath); r = kmod_module_new_from_path(ctx, ftsent->fts_accpath, &mod_o); if (r < 0) { - log_debug("Failed to lookup modules path '%s': %m", - ftsent->fts_accpath); + log_debug("Failed to lookup modules path '%s': %m", ftsent->fts_accpath); if (!arg_optional) { return -ENOENT; } @@ -1881,8 +1902,7 @@ static int install_modules(int argc, char **argv) } if (!modlist) { - log_error("Failed to find module '%s' %s", modname, - ftsent->fts_accpath); + log_error("Failed to find module '%s' %s", modname, ftsent->fts_accpath); if (!arg_optional) { return -ENOENT; } @@ -1897,7 +1917,7 @@ static int install_modules(int argc, char **argv) log_error("ERROR: installing module '%s'", modname); return -ENOENT; }; - ret = ( ret == 0 ? 0 : r ); + ret = (ret == 0 ? 0 : r); modinst = 1; } } @@ -1910,11 +1930,11 @@ static int install_modules(int argc, char **argv) if (endswith(modname, ".ko")) { int len = strlen(modname); - modname[len-3]=0; + modname[len - 3] = 0; } if (endswith(modname, ".ko.xz") || endswith(modname, ".ko.gz")) { int len = strlen(modname); - modname[len-6]=0; + modname[len - 6] = 0; } r = kmod_module_new_from_lookup(ctx, modname, &modlist); if (r < 0) { @@ -1944,7 +1964,7 @@ static int install_modules(int argc, char **argv) log_error("ERROR: installing '%s'", argv[i]); return -ENOENT; }; - ret = ( ret == 0 ? 0 : r ); + ret = (ret == 0 ? 0 : r); modinst = 1; } } diff --git a/install/hashmap.c b/install/hashmap.c index 7e77f9a6..bc119695 100644 --- a/install/hashmap.c +++ b/install/hashmap.c @@ -45,35 +45,40 @@ struct Hashmap { #define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + ALIGN(sizeof(Hashmap)))) -unsigned string_hash_func(const void *p) { +unsigned string_hash_func(const void *p) +{ unsigned hash = 5381; const signed char *c; /* DJB's hash function */ for (c = p; *c; c++) - hash = (hash << 5) + hash + (unsigned) *c; + hash = (hash << 5) + hash + (unsigned)*c; return hash; } -int string_compare_func(const void *a, const void *b) { +int string_compare_func(const void *a, const void *b) +{ return strcmp(a, b); } -unsigned trivial_hash_func(const void *p) { +unsigned trivial_hash_func(const void *p) +{ return PTR_TO_UINT(p); } -int trivial_compare_func(const void *a, const void *b) { +int trivial_compare_func(const void *a, const void *b) +{ return a < b ? -1 : (a > b ? 1 : 0); } -Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) { +Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) +{ Hashmap *h; size_t size; - size = ALIGN(sizeof(Hashmap)) + NBUCKETS * sizeof(struct hashmap_entry*); + size = ALIGN(sizeof(Hashmap)) + NBUCKETS * sizeof(struct hashmap_entry *); h = malloc0(size); @@ -89,7 +94,8 @@ Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) { return h; } -int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func) { +int hashmap_ensure_allocated(Hashmap ** h, hash_func_t hash_func, compare_func_t compare_func) +{ assert(h); if (*h) @@ -101,7 +107,8 @@ int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t return 0; } -static void link_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) { +static void link_entry(Hashmap * h, struct hashmap_entry *e, unsigned hash) +{ assert(h); assert(e); @@ -128,7 +135,8 @@ static void link_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) { assert(h->n_entries >= 1); } -static void unlink_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) { +static void unlink_entry(Hashmap * h, struct hashmap_entry *e, unsigned hash) +{ assert(h); assert(e); @@ -156,7 +164,8 @@ static void unlink_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) { h->n_entries--; } -static void remove_entry(Hashmap *h, struct hashmap_entry **ep) { +static void remove_entry(Hashmap * h, struct hashmap_entry **ep) +{ struct hashmap_entry *e = *ep; unsigned hash; @@ -171,7 +180,8 @@ static void remove_entry(Hashmap *h, struct hashmap_entry **ep) { *ep = NULL; } -void hashmap_free(Hashmap*h) { +void hashmap_free(Hashmap * h) +{ if (!h) return; @@ -181,7 +191,8 @@ void hashmap_free(Hashmap*h) { free(h); } -void hashmap_free_free(Hashmap *h) { +void hashmap_free_free(Hashmap * h) +{ void *p; while ((p = hashmap_steal_first(h))) @@ -190,7 +201,8 @@ void hashmap_free_free(Hashmap *h) { hashmap_free(h); } -void hashmap_clear(Hashmap *h) { +void hashmap_clear(Hashmap * h) +{ if (!h) return; @@ -200,7 +212,8 @@ void hashmap_clear(Hashmap *h) { } } -static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) { +static struct hashmap_entry *hash_scan(Hashmap * h, unsigned hash, const void *key) +{ struct hashmap_entry *e; assert(h); assert(hash < NBUCKETS); @@ -212,7 +225,8 @@ static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *ke return NULL; } -int hashmap_put(Hashmap *h, const void *key, void *value) { +int hashmap_put(Hashmap * h, const void *key, void *value) +{ struct hashmap_entry *e; unsigned hash; @@ -241,7 +255,8 @@ int hashmap_put(Hashmap *h, const void *key, void *value) { return 1; } -int hashmap_replace(Hashmap *h, const void *key, void *value) { +int hashmap_replace(Hashmap * h, const void *key, void *value) +{ struct hashmap_entry *e; unsigned hash; @@ -258,7 +273,8 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) { return hashmap_put(h, key, value); } -void* hashmap_get(Hashmap *h, const void *key) { +void *hashmap_get(Hashmap * h, const void *key) +{ unsigned hash; struct hashmap_entry *e; @@ -273,7 +289,8 @@ void* hashmap_get(Hashmap *h, const void *key) { return e->value; } -void* hashmap_remove(Hashmap *h, const void *key) { +void *hashmap_remove(Hashmap * h, const void *key) +{ struct hashmap_entry *e; unsigned hash; void *data; @@ -292,7 +309,8 @@ void* hashmap_remove(Hashmap *h, const void *key) { return data; } -int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value) { +int hashmap_remove_and_put(Hashmap * h, const void *old_key, const void *new_key, void *value) +{ struct hashmap_entry *e; unsigned old_hash, new_hash; @@ -317,7 +335,8 @@ int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, return 0; } -int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value) { +int hashmap_remove_and_replace(Hashmap * h, const void *old_key, const void *new_key, void *value) +{ struct hashmap_entry *e, *k; unsigned old_hash, new_hash; @@ -344,7 +363,8 @@ int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_ return 0; } -void* hashmap_remove_value(Hashmap *h, const void *key, void *value) { +void *hashmap_remove_value(Hashmap * h, const void *key, void *value) +{ struct hashmap_entry *e; unsigned hash; @@ -364,7 +384,8 @@ void* hashmap_remove_value(Hashmap *h, const void *key, void *value) { return value; } -void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) { +void *hashmap_iterate(Hashmap * h, Iterator * i, const void **key) +{ struct hashmap_entry *e; assert(i); @@ -378,7 +399,7 @@ void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) { if (*i == ITERATOR_FIRST && !h->iterate_list_head) goto at_end; - e = *i == ITERATOR_FIRST ? h->iterate_list_head : (struct hashmap_entry*) *i; + e = *i == ITERATOR_FIRST ? h->iterate_list_head : (struct hashmap_entry *)*i; if (e->iterate_next) *i = (Iterator) e->iterate_next; @@ -390,7 +411,7 @@ void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) { return e->value; -at_end: + at_end: *i = ITERATOR_LAST; if (key) @@ -399,7 +420,8 @@ at_end: return NULL; } -void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) { +void *hashmap_iterate_backwards(Hashmap * h, Iterator * i, const void **key) +{ struct hashmap_entry *e; assert(i); @@ -413,7 +435,7 @@ void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) { if (*i == ITERATOR_LAST && !h->iterate_list_tail) goto at_beginning; - e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry*) *i; + e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry *)*i; if (e->iterate_previous) *i = (Iterator) e->iterate_previous; @@ -425,7 +447,7 @@ void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) { return e->value; -at_beginning: + at_beginning: *i = ITERATOR_FIRST; if (key) @@ -434,7 +456,8 @@ at_beginning: return NULL; } -void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) { +void *hashmap_iterate_skip(Hashmap * h, const void *key, Iterator * i) +{ unsigned hash; struct hashmap_entry *e; @@ -451,7 +474,8 @@ void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) { return e->value; } -void* hashmap_first(Hashmap *h) { +void *hashmap_first(Hashmap * h) +{ if (!h) return NULL; @@ -462,7 +486,8 @@ void* hashmap_first(Hashmap *h) { return h->iterate_list_head->value; } -void* hashmap_first_key(Hashmap *h) { +void *hashmap_first_key(Hashmap * h) +{ if (!h) return NULL; @@ -470,10 +495,11 @@ void* hashmap_first_key(Hashmap *h) { if (!h->iterate_list_head) return NULL; - return (void*) h->iterate_list_head->key; + return (void *)h->iterate_list_head->key; } -void* hashmap_last(Hashmap *h) { +void *hashmap_last(Hashmap * h) +{ if (!h) return NULL; @@ -484,7 +510,8 @@ void* hashmap_last(Hashmap *h) { return h->iterate_list_tail->value; } -void* hashmap_steal_first(Hashmap *h) { +void *hashmap_steal_first(Hashmap * h) +{ struct hashmap_entry *e; void *data; @@ -501,7 +528,8 @@ void* hashmap_steal_first(Hashmap *h) { return data; } -void* hashmap_steal_first_key(Hashmap *h) { +void *hashmap_steal_first_key(Hashmap * h) +{ struct hashmap_entry *e; void *key; @@ -512,13 +540,14 @@ void* hashmap_steal_first_key(Hashmap *h) { return NULL; e = h->iterate_list_head; - key = (void*) e->key; + key = (void *)e->key; remove_entry(h, &e); return key; } -unsigned hashmap_size(Hashmap *h) { +unsigned hashmap_size(Hashmap * h) +{ if (!h) return 0; @@ -526,7 +555,8 @@ unsigned hashmap_size(Hashmap *h) { return h->n_entries; } -bool hashmap_isempty(Hashmap *h) { +bool hashmap_isempty(Hashmap * h) +{ if (!h) return true; @@ -534,7 +564,8 @@ bool hashmap_isempty(Hashmap *h) { return h->n_entries == 0; } -int hashmap_merge(Hashmap *h, Hashmap *other) { +int hashmap_merge(Hashmap * h, Hashmap * other) +{ struct hashmap_entry *e; assert(h); @@ -553,7 +584,8 @@ int hashmap_merge(Hashmap *h, Hashmap *other) { return 0; } -void hashmap_move(Hashmap *h, Hashmap *other) { +void hashmap_move(Hashmap * h, Hashmap * other) +{ struct hashmap_entry *e, *n; assert(h); @@ -581,7 +613,8 @@ void hashmap_move(Hashmap *h, Hashmap *other) { } } -int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) { +int hashmap_move_one(Hashmap * h, Hashmap * other, const void *key) +{ unsigned h_hash, other_hash; struct hashmap_entry *e; @@ -604,19 +637,20 @@ int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) { return 0; } -char **hashmap_get_strv(Hashmap *h) { +char **hashmap_get_strv(Hashmap * h) +{ char **sv; Iterator it; char *item; int n; - sv = new(char*, h->n_entries+1); + sv = new(char *, h->n_entries + 1); if (!sv) return NULL; n = 0; HASHMAP_FOREACH(item, h, it) - sv[n++] = item; + sv[n++] = item; sv[n] = NULL; return sv; diff --git a/install/hashmap.h b/install/hashmap.h index d5e45fd5..9a97b9b5 100644 --- a/install/hashmap.h +++ b/install/hashmap.h @@ -29,7 +29,7 @@ typedef struct Hashmap Hashmap; typedef struct _IteratorStruct _IteratorStruct; -typedef _IteratorStruct* Iterator; +typedef _IteratorStruct *Iterator; #define ITERATOR_FIRST ((Iterator) 0) #define ITERATOR_LAST ((Iterator) -1) @@ -44,37 +44,37 @@ unsigned trivial_hash_func(const void *p); int trivial_compare_func(const void *a, const void *b); Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func); -void hashmap_free(Hashmap *h); -void hashmap_free_free(Hashmap *h); -int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func); - -int hashmap_put(Hashmap *h, const void *key, void *value); -int hashmap_replace(Hashmap *h, const void *key, void *value); -void* hashmap_get(Hashmap *h, const void *key); -void* hashmap_remove(Hashmap *h, const void *key); -void* hashmap_remove_value(Hashmap *h, const void *key, void *value); -int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value); -int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value); - -int hashmap_merge(Hashmap *h, Hashmap *other); -void hashmap_move(Hashmap *h, Hashmap *other); -int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key); - -unsigned hashmap_size(Hashmap *h); -bool hashmap_isempty(Hashmap *h); - -void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key); -void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key); -void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i); - -void hashmap_clear(Hashmap *h); -void *hashmap_steal_first(Hashmap *h); -void *hashmap_steal_first_key(Hashmap *h); -void* hashmap_first(Hashmap *h); -void* hashmap_first_key(Hashmap *h); -void* hashmap_last(Hashmap *h); - -char **hashmap_get_strv(Hashmap *h); +void hashmap_free(Hashmap * h); +void hashmap_free_free(Hashmap * h); +int hashmap_ensure_allocated(Hashmap ** h, hash_func_t hash_func, compare_func_t compare_func); + +int hashmap_put(Hashmap * h, const void *key, void *value); +int hashmap_replace(Hashmap * h, const void *key, void *value); +void *hashmap_get(Hashmap * h, const void *key); +void *hashmap_remove(Hashmap * h, const void *key); +void *hashmap_remove_value(Hashmap * h, const void *key, void *value); +int hashmap_remove_and_put(Hashmap * h, const void *old_key, const void *new_key, void *value); +int hashmap_remove_and_replace(Hashmap * h, const void *old_key, const void *new_key, void *value); + +int hashmap_merge(Hashmap * h, Hashmap * other); +void hashmap_move(Hashmap * h, Hashmap * other); +int hashmap_move_one(Hashmap * h, Hashmap * other, const void *key); + +unsigned hashmap_size(Hashmap * h); +bool hashmap_isempty(Hashmap * h); + +void *hashmap_iterate(Hashmap * h, Iterator * i, const void **key); +void *hashmap_iterate_backwards(Hashmap * h, Iterator * i, const void **key); +void *hashmap_iterate_skip(Hashmap * h, const void *key, Iterator * i); + +void hashmap_clear(Hashmap * h); +void *hashmap_steal_first(Hashmap * h); +void *hashmap_steal_first_key(Hashmap * h); +void *hashmap_first(Hashmap * h); +void *hashmap_first_key(Hashmap * h); +void *hashmap_last(Hashmap * h); + +char **hashmap_get_strv(Hashmap * h); #define HASHMAP_FOREACH(e, h, i) \ for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL)) diff --git a/install/log.c b/install/log.c index 1afd5289..a3198f04 100644 --- a/install/log.c +++ b/install/log.c @@ -44,7 +44,8 @@ static bool show_location = false; * use here. */ static char *log_abort_msg = NULL; -void log_close_console(void) { +void log_close_console(void) +{ if (console_fd < 0) return; @@ -57,14 +58,15 @@ void log_close_console(void) { } } -static int log_open_console(void) { +static int log_open_console(void) +{ if (console_fd >= 0) return 0; if (getpid() == 1) { - console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); + console_fd = open_terminal("/dev/console", O_WRONLY | O_NOCTTY | O_CLOEXEC); if (console_fd < 0) { log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd)); return console_fd; @@ -77,33 +79,30 @@ static int log_open_console(void) { return 0; } - -int log_open(void) { +int log_open(void) +{ return log_open_console(); } - -void log_close(void) { +void log_close(void) +{ log_close_console(); } - -void log_set_max_level(int level) { +void log_set_max_level(int level) +{ assert((level & LOG_PRIMASK) == level); log_max_level = level; } -void log_set_facility(int facility) { +void log_set_facility(int facility) +{ log_facility = facility; } -static int write_to_console( - int level, - const char*file, - unsigned int line, - const char *func, - const char *buffer) { +static int write_to_console(int level, const char *file, unsigned int line, const char *func, const char *buffer) +{ struct iovec iovec[5]; unsigned n = 0; @@ -131,12 +130,8 @@ static int write_to_console( return 1; } -static int log_dispatch( - int level, - const char*file, - int line, - const char *func, - char *buffer) { +static int log_dispatch(int level, const char *file, int line, const char *func, char *buffer) +{ int r = 0; @@ -168,13 +163,8 @@ static int log_dispatch( return r; } -int log_metav( - int level, - const char*file, - int line, - const char *func, - const char *format, - va_list ap) { +int log_metav(int level, const char *file, int line, const char *func, const char *format, va_list ap) +{ char buffer[LINE_MAX]; int saved_errno, r; @@ -192,12 +182,8 @@ int log_metav( return r; } -int log_meta( - int level, - const char*file, - int line, - const char *func, - const char *format, ...) { +int log_meta(int level, const char *file, int line, const char *func, const char *format, ...) +{ int r; va_list ap; @@ -211,7 +197,8 @@ int log_meta( #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" -_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) { +_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) +{ static char buffer[LINE_MAX]; snprintf(buffer, sizeof(buffer), format, text, file, line, func); @@ -222,24 +209,29 @@ _noreturn_ static void log_assert(const char *text, const char *file, int line, log_dispatch(LOG_CRIT, file, line, func, buffer); abort(); } + #pragma GCC diagnostic pop -_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) { +_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) +{ log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting."); } -_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) { +_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) +{ log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); } -void log_set_target(LogTarget target) { +void log_set_target(LogTarget target) +{ assert(target >= 0); assert(target < _LOG_TARGET_MAX); log_target = target; } -int log_set_target_from_string(const char *e) { +int log_set_target_from_string(const char *e) +{ LogTarget t; t = log_target_from_string(e); @@ -250,7 +242,8 @@ int log_set_target_from_string(const char *e) { return 0; } -int log_set_max_level_from_string(const char *e) { +int log_set_max_level_from_string(const char *e) +{ int t; t = log_level_from_string(e); @@ -261,7 +254,8 @@ int log_set_max_level_from_string(const char *e) { return 0; } -void log_parse_environment(void) { +void log_parse_environment(void) +{ const char *e; if ((e = getenv("DRACUT_INSTALL_LOG_TARGET"))) { @@ -281,15 +275,16 @@ void log_parse_environment(void) { } } -LogTarget log_get_target(void) { +LogTarget log_get_target(void) +{ return log_target; } -int log_get_max_level(void) { +int log_get_max_level(void) +{ return log_max_level; } - static const char *const log_target_table[] = { [LOG_TARGET_CONSOLE] = "console", [LOG_TARGET_AUTO] = "auto", diff --git a/install/log.h b/install/log.h index a22330a6..6e8c4e6a 100644 --- a/install/log.h +++ b/install/log.h @@ -26,19 +26,19 @@ #include "macro.h" -typedef enum LogTarget{ +typedef enum LogTarget { LOG_TARGET_CONSOLE, LOG_TARGET_KMSG, LOG_TARGET_JOURNAL, LOG_TARGET_JOURNAL_OR_KMSG, LOG_TARGET_SYSLOG, LOG_TARGET_SYSLOG_OR_KMSG, - LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */ - LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */ + LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */ + LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */ LOG_TARGET_NULL, _LOG_TARGET_MAX, _LOG_TARGET_INVALID = -1 -} LogTarget; +} LogTarget; void log_set_target(LogTarget target); void log_set_max_level(int level); @@ -67,31 +67,15 @@ void log_close_console(void); void log_parse_environment(void); -int log_meta( - int level, - const char*file, - int line, - const char *func, - const char *format, ...) _printf_attr_(5,6); - -int log_metav( - int level, - const char*file, - int line, - const char *func, - const char *format, - va_list ap); +int log_meta(int level, const char *file, int line, const char *func, const char *format, ...) _printf_attr_(5, 6); + +int log_metav(int level, const char *file, int line, const char *func, const char *format, va_list ap); _noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func); _noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func); /* This modifies the buffer passed! */ -int log_dump_internal( - int level, - const char*file, - int line, - const char *func, - char *buffer); +int log_dump_internal(int level, const char *file, int line, const char *func, char *buffer); #define log_full(level, ...) log_meta(level, __FILE__, __LINE__, __func__, __VA_ARGS__) diff --git a/install/macro.h b/install/macro.h index a8fad1ce..3b6342e7 100644 --- a/install/macro.h +++ b/install/macro.h @@ -68,7 +68,8 @@ #define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) p)) #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) p)) -static inline size_t ALIGN_TO(size_t l, size_t ali) { +static inline size_t ALIGN_TO(size_t l, size_t ali) +{ return ((l + ali - 1) & ~(ali - 1)); } @@ -189,7 +190,8 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { _i->iov_len = strlen(_s); \ } while(false) -static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) { +static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) +{ unsigned j; size_t r = 0; @@ -199,7 +201,8 @@ static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) { return r; } -static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) { +static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) +{ unsigned j; for (j = 0; j < n; j++) { @@ -210,7 +213,7 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) { sub = MIN(i[j].iov_len, k); i[j].iov_len -= sub; - i[j].iov_base = (uint8_t*) i[j].iov_base + sub; + i[j].iov_base = (uint8_t *) i[j].iov_base + sub; k -= sub; } @@ -265,10 +268,9 @@ do { \ /* Because statfs.t_type can be int on some architecures, we have to cast * the const magic to the type, otherwise the compiler warns about * signed/unsigned comparison, because the magic can be 32 bit unsigned. - */ + */ #define F_TYPE_CMP(a, b) (a == (typeof(a)) b) - /* Returns the number of chars needed to format variables of the * specified type as a decimal string. Adds in extra space for a * negative '-' prefix. */ diff --git a/install/strv.c b/install/strv.c index 015dad13..484c5872 100644 --- a/install/strv.c +++ b/install/strv.c @@ -26,31 +26,34 @@ #include "util.h" #include "strv.h" -char *strv_find(char **l, const char *name) { +char *strv_find(char **l, const char *name) +{ char **i; assert(name); STRV_FOREACH(i, l) - if (streq(*i, name)) - return *i; + if (streq(*i, name)) + return *i; return NULL; } -char *strv_find_prefix(char **l, const char *name) { +char *strv_find_prefix(char **l, const char *name) +{ char **i; assert(name); STRV_FOREACH(i, l) - if (startswith(*i, name)) - return *i; + if (startswith(*i, name)) + return *i; return NULL; } -void strv_free(char **l) { +void strv_free(char **l) +{ char **k; if (!l) @@ -62,10 +65,11 @@ void strv_free(char **l) { free(l); } -char **strv_copy(char * const *l) { +char **strv_copy(char *const *l) +{ char **r, **k; - k = r = new(char*, strv_length(l) + 1); + k = r = new(char *, strv_length(l) + 1); if (!r) return NULL; @@ -82,7 +86,8 @@ char **strv_copy(char * const *l) { return r; } -unsigned int strv_length(char * const *l) { +unsigned int strv_length(char *const *l) +{ unsigned n = 0; if (!l) @@ -94,7 +99,8 @@ unsigned int strv_length(char * const *l) { return n; } -char **strv_new_ap(const char *x, va_list ap) { +char **strv_new_ap(const char *x, va_list ap) +{ const char *s; char **a; unsigned n = 0, i = 0; @@ -106,11 +112,11 @@ char **strv_new_ap(const char *x, va_list ap) { * the string list. */ if (x) { - n = x == (const char*) -1 ? 0 : 1; + n = x == (const char *)-1 ? 0 : 1; va_copy(aq, ap); - while ((s = va_arg(aq, const char*))) { - if (s == (const char*) -1) + while ((s = va_arg(aq, const char *))) { + if (s == (const char *)-1) continue; n++; @@ -119,21 +125,21 @@ char **strv_new_ap(const char *x, va_list ap) { va_end(aq); } - a = new(char*, n+1); + a = new(char *, n + 1); if (!a) return NULL; if (x) { - if (x != (const char*) -1) { + if (x != (const char *)-1) { a[i] = strdup(x); if (!a[i]) goto fail; i++; } - while ((s = va_arg(ap, const char*))) { + while ((s = va_arg(ap, const char *))) { - if (s == (const char*) -1) + if (s == (const char *)-1) continue; a[i] = strdup(s); @@ -148,12 +154,13 @@ char **strv_new_ap(const char *x, va_list ap) { return a; -fail: + fail: strv_free(a); return NULL; } -char **strv_new(const char *x, ...) { +char **strv_new(const char *x, ...) +{ char **r; va_list ap; @@ -164,7 +171,8 @@ char **strv_new(const char *x, ...) { return r; } -char **strv_merge(char **a, char **b) { +char **strv_merge(char **a, char **b) +{ char **r, **k; if (!a) @@ -173,7 +181,7 @@ char **strv_merge(char **a, char **b) { if (!b) return strv_copy(a); - r = new(char*, strv_length(a) + strv_length(b) + 1); + r = new(char *, strv_length(a) + strv_length(b) + 1); if (!r) return NULL; @@ -192,12 +200,13 @@ char **strv_merge(char **a, char **b) { *k = NULL; return r; -fail: + fail: strv_free(r); return NULL; } -char **strv_merge_concat(char **a, char **b, const char *suffix) { +char **strv_merge_concat(char **a, char **b, const char *suffix) +{ char **r, **k; /* Like strv_merge(), but appends suffix to all strings in b, before adding */ @@ -205,7 +214,7 @@ char **strv_merge_concat(char **a, char **b, const char *suffix) { if (!b) return strv_copy(a); - r = new(char*, strv_length(a) + strv_length(b) + 1); + r = new(char *, strv_length(a) + strv_length(b) + 1); if (!r) return NULL; @@ -226,13 +235,14 @@ char **strv_merge_concat(char **a, char **b, const char *suffix) { *k = NULL; return r; -fail: + fail: strv_free(r); return NULL; } -char **strv_split(const char *s, const char *separator) { +char **strv_split(const char *s, const char *separator) +{ char *state; char *w; size_t l; @@ -243,9 +253,9 @@ char **strv_split(const char *s, const char *separator) { n = 0; FOREACH_WORD_SEPARATOR(w, l, s, separator, state) - n++; + n++; - r = new(char*, n+1); + r = new(char *, n + 1); if (!r) return NULL; @@ -264,7 +274,8 @@ char **strv_split(const char *s, const char *separator) { return r; } -char **strv_split_quoted(const char *s) { +char **strv_split_quoted(const char *s) +{ char *state; char *w; size_t l; @@ -275,9 +286,9 @@ char **strv_split_quoted(const char *s) { n = 0; FOREACH_WORD_QUOTED(w, l, s, state) - n++; + n++; - r = new(char*, n+1); + r = new(char *, n + 1); if (!r) return NULL; @@ -295,7 +306,8 @@ char **strv_split_quoted(const char *s) { return r; } -char **strv_split_newlines(const char *s) { +char **strv_split_newlines(const char *s) +{ char **l; unsigned int n; @@ -312,15 +324,16 @@ char **strv_split_newlines(const char *s) { if (n == 0) return l; - if (isempty(l[n-1])) { - free(l[n-1]); - l[n-1] = NULL; + if (isempty(l[n - 1])) { + free(l[n - 1]); + l[n - 1] = NULL; } return l; } -char *strv_join(char **l, const char *separator) { +char *strv_join(char **l, const char *separator) +{ char *r, *e; char **s; size_t n, k; @@ -337,7 +350,7 @@ char *strv_join(char **l, const char *separator) { n += strlen(*s); } - r = new(char, n+1); + r = new(char, n + 1); if (!r) return NULL; @@ -354,7 +367,8 @@ char *strv_join(char **l, const char *separator) { return r; } -char **strv_append(char **l, const char *s) { +char **strv_append(char **l, const char *s) +{ char **r, **k; if (!l) @@ -363,7 +377,7 @@ char **strv_append(char **l, const char *s) { if (!s) return strv_copy(l); - r = new(char*, strv_length(l)+2); + r = new(char *, strv_length(l) + 2); if (!r) return NULL; @@ -380,12 +394,13 @@ char **strv_append(char **l, const char *s) { k[1] = NULL; return r; -fail: + fail: strv_free(r); return NULL; } -int strv_push(char ***l, char *value) { +int strv_push(char ***l, char *value) +{ char **c; unsigned n; @@ -393,18 +408,19 @@ int strv_push(char ***l, char *value) { return 0; n = strv_length(*l); - c = realloc(*l, sizeof(char*) * (n + 2)); + c = realloc(*l, sizeof(char *) * (n + 2)); if (!c) return -ENOMEM; c[n] = value; - c[n+1] = NULL; + c[n + 1] = NULL; *l = c; return 0; } -int strv_extend(char ***l, const char *value) { +int strv_extend(char ***l, const char *value) +{ char *v; int r; @@ -422,19 +438,21 @@ int strv_extend(char ***l, const char *value) { return r; } -char **strv_uniq(char **l) { +char **strv_uniq(char **l) +{ char **i; /* Drops duplicate entries. The first identical string will be * kept, the others dropped */ STRV_FOREACH(i, l) - strv_remove(i+1, *i); + strv_remove(i + 1, *i); return l; } -char **strv_remove(char **l, const char *s) { +char **strv_remove(char **l, const char *s) +{ char **f, **t; if (!l) @@ -459,7 +477,8 @@ char **strv_remove(char **l, const char *s) { return l; } -char **strv_remove_prefix(char **l, const char *s) { +char **strv_remove_prefix(char **l, const char *s) +{ char **f, **t; if (!l) @@ -484,7 +503,8 @@ char **strv_remove_prefix(char **l, const char *s) { return l; } -char **strv_parse_nulstr(const char *s, size_t l) { +char **strv_parse_nulstr(const char *s, size_t l) +{ const char *p; unsigned c = 0, i = 0; char **v; @@ -492,16 +512,16 @@ char **strv_parse_nulstr(const char *s, size_t l) { assert(s || l == 0); if (l == 0) - return new0(char*, 1); + return new0(char *, 1); for (p = s; p < s + l; p++) if (*p == 0) c++; - if (s[l-1] != 0) + if (s[l - 1] != 0) c++; - v = new0(char*, c+1); + v = new0(char *, c + 1); if (!v) return NULL; @@ -530,15 +550,16 @@ char **strv_parse_nulstr(const char *s, size_t l) { return v; } -char **strv_split_nulstr(const char *s) { +char **strv_split_nulstr(const char *s) +{ const char *i; char **r = NULL; NULSTR_FOREACH(i, s) - if (strv_extend(&r, i) < 0) { - strv_free(r); - return NULL; - } + if (strv_extend(&r, i) < 0) { + strv_free(r); + return NULL; + } if (!r) return strv_new(NULL, NULL); @@ -546,7 +567,8 @@ char **strv_split_nulstr(const char *s) { return r; } -bool strv_overlap(char **a, char **b) { +bool strv_overlap(char **a, char **b) +{ char **i, **j; STRV_FOREACH(i, a) { @@ -559,27 +581,30 @@ bool strv_overlap(char **a, char **b) { return false; } -static int str_compare(const void *_a, const void *_b) { - const char **a = (const char**) _a, **b = (const char**) _b; +static int str_compare(const void *_a, const void *_b) +{ + const char **a = (const char **)_a, **b = (const char **)_b; return strcmp(*a, *b); } -char **strv_sort(char **l) { +char **strv_sort(char **l) +{ if (strv_isempty(l)) return l; - qsort(l, strv_length(l), sizeof(char*), str_compare); + qsort(l, strv_length(l), sizeof(char *), str_compare); return l; } -void strv_print(char **l) { +void strv_print(char **l) +{ char **s; if (!l) return; STRV_FOREACH(s, l) - puts(*s); + puts(*s); } diff --git a/install/strv.h b/install/strv.h index 65fd28d2..7b94f682 100644 --- a/install/strv.h +++ b/install/strv.h @@ -28,11 +28,11 @@ char *strv_find(char **l, const char *name) _pure_; char *strv_find_prefix(char **l, const char *name) _pure_; void strv_free(char **l); -DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free); +DEFINE_TRIVIAL_CLEANUP_FUNC(char **, strv_free); #define _cleanup_strv_free_ _cleanup_(strv_freep) -char **strv_copy(char * const *l); -unsigned int strv_length(char * const *l) _pure_; +char **strv_copy(char *const *l); +unsigned int strv_length(char *const *l) _pure_; char **strv_merge(char **a, char **b); char **strv_merge_concat(char **a, char **b, const char *suffix); @@ -49,11 +49,13 @@ char **strv_uniq(char **l); char **strv_new(const char *x, ...) _sentinel_; char **strv_new_ap(const char *x, va_list ap); -static inline const char* STRV_IFNOTNULL(const char *x) { - return x ? x : (const char *) -1; +static inline const char *STRV_IFNOTNULL(const char *x) +{ + return x ? x : (const char *)-1; } -static inline bool strv_isempty(char * const *l) { +static inline bool strv_isempty(char *const *l) +{ return !l || !*l; } diff --git a/install/util.c b/install/util.c index 7ceb0054..937647ff 100644 --- a/install/util.c +++ b/install/util.c @@ -26,11 +26,13 @@ #include "util.h" -static inline pid_t gettid(void) { +static inline pid_t gettid(void) +{ return (pid_t) syscall(SYS_gettid); } -size_t page_size(void) { +size_t page_size(void) +{ static __thread size_t pgsz = 0; long r; @@ -39,12 +41,13 @@ size_t page_size(void) { assert_se((r = sysconf(_SC_PAGESIZE)) > 0); - pgsz = (size_t) r; + pgsz = (size_t)r; return pgsz; } -bool endswith(const char *s, const char *postfix) { +bool endswith(const char *s, const char *postfix) +{ size_t sl, pl; assert(s); @@ -61,7 +64,9 @@ bool endswith(const char *s, const char *postfix) { return memcmp(s + sl - pl, postfix, pl) == 0; } -int close_nointr(int fd) { + +int close_nointr(int fd) +{ assert(fd >= 0); for (;;) { @@ -76,7 +81,8 @@ int close_nointr(int fd) { } } -void close_nointr_nofail(int fd) { +void close_nointr_nofail(int fd) +{ int saved_errno = errno; /* like close_nointr() but cannot fail, and guarantees errno @@ -87,7 +93,8 @@ void close_nointr_nofail(int fd) { errno = saved_errno; } -int open_terminal(const char *name, int mode) { +int open_terminal(const char *name, int mode) +{ int fd, r; unsigned c = 0; @@ -130,7 +137,8 @@ int open_terminal(const char *name, int mode) { return fd; } -bool streq_ptr(const char *a, const char *b) { +bool streq_ptr(const char *a, const char *b) +{ /* Like streq(), but tries to make sense of NULL pointers */ @@ -142,16 +150,19 @@ bool streq_ptr(const char *a, const char *b) { return false; } -bool is_main_thread(void) { + +bool is_main_thread(void) +{ static __thread int cached = 0; if (_unlikely_(cached == 0)) - cached = getpid() == gettid() ? 1 : -1; + cached = getpid() == gettid()? 1 : -1; return cached > 0; } -int safe_atou(const char *s, unsigned *ret_u) { +int safe_atou(const char *s, unsigned *ret_u) +{ char *x = NULL; unsigned long l; @@ -164,10 +175,10 @@ int safe_atou(const char *s, unsigned *ret_u) { if (!x || *x || errno) return errno ? -errno : -EINVAL; - if ((unsigned long) (unsigned) l != l) + if ((unsigned long)(unsigned)l != l) return -ERANGE; - *ret_u = (unsigned) l; + *ret_u = (unsigned)l; return 0; } @@ -184,7 +195,8 @@ static const char *const log_level_table[] = { DEFINE_STRING_TABLE_LOOKUP(log_level, int); -char *strnappend(const char *s, const char *suffix, size_t b) { +char *strnappend(const char *s, const char *suffix, size_t b) +{ size_t a; char *r; @@ -201,25 +213,27 @@ char *strnappend(const char *s, const char *suffix, size_t b) { assert(suffix); a = strlen(s); - if (b > ((size_t) -1) - a) + if (b > ((size_t)-1) - a) return NULL; - r = new(char, a+b+1); + r = new(char, a + b + 1); if (!r) return NULL; memcpy(r, s, a); - memcpy(r+a, suffix, b); - r[a+b] = 0; + memcpy(r + a, suffix, b); + r[a + b] = 0; return r; } -char *strappend(const char *s, const char *suffix) { +char *strappend(const char *s, const char *suffix) +{ return strnappend(s, suffix, suffix ? strlen(suffix) : 0); } -char *strjoin(const char *x, ...) { +char *strjoin(const char *x, ...) +{ va_list ap; size_t l; char *r; @@ -238,7 +252,7 @@ char *strjoin(const char *x, ...) { break; n = strlen(t); - if (n > ((size_t) -1) - l) { + if (n > ((size_t)-1) - l) { va_end(ap); return NULL; } @@ -250,7 +264,7 @@ char *strjoin(const char *x, ...) { va_end(ap); - r = new(char, l+1); + r = new(char, l + 1); if (!r) return NULL; @@ -278,7 +292,8 @@ char *strjoin(const char *x, ...) { return r; } -char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) { +char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) +{ char *r, *t; const char *f; size_t pl; @@ -289,7 +304,7 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre pl = prefix ? strlen(prefix) : 0; - r = new(char, pl+length+1); + r = new(char, pl + length + 1); if (!r) return r; @@ -343,24 +358,24 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre *(t++) = ' '; break; - case 'x': { - /* hexadecimal encoding */ - int a, b; + case 'x':{ + /* hexadecimal encoding */ + int a, b; - a = unhexchar(f[1]); - b = unhexchar(f[2]); + a = unhexchar(f[1]); + b = unhexchar(f[2]); - if (a < 0 || b < 0) { - /* Invalid escape code, let's take it literal then */ - *(t++) = '\\'; - *(t++) = 'x'; - } else { - *(t++) = (char) ((a << 4) | b); - f += 2; - } + if (a < 0 || b < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '\\'; + *(t++) = 'x'; + } else { + *(t++) = (char)((a << 4) | b); + f += 2; + } - break; - } + break; + } case '0': case '1': @@ -369,28 +384,28 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre case '4': case '5': case '6': - case '7': { - /* octal encoding */ - int a, b, c; - - a = unoctchar(f[0]); - b = unoctchar(f[1]); - c = unoctchar(f[2]); - - if (a < 0 || b < 0 || c < 0) { - /* Invalid escape code, let's take it literal then */ - *(t++) = '\\'; - *(t++) = f[0]; - } else { - *(t++) = (char) ((a << 6) | (b << 3) | c); - f += 2; - } + case '7':{ + /* octal encoding */ + int a, b, c; + + a = unoctchar(f[0]); + b = unoctchar(f[1]); + c = unoctchar(f[2]); + + if (a < 0 || b < 0 || c < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '\\'; + *(t++) = f[0]; + } else { + *(t++) = (char)((a << 6) | (b << 3) | c); + f += 2; + } - break; - } + break; + } case 0: - /* premature end of string.*/ + /* premature end of string. */ *(t++) = '\\'; goto finish; @@ -402,19 +417,20 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre } } -finish: + finish: *t = 0; return r; } -char *cunescape_length(const char *s, size_t length) { +char *cunescape_length(const char *s, size_t length) +{ return cunescape_length_with_prefix(s, length, NULL); } - /* Split a string into words, but consider strings enclosed in '' and * "" as words even if they include spaces. */ -char *split_quoted(const char *c, size_t *l, char **state) { +char *split_quoted(const char *c, size_t *l, char **state) +{ const char *current, *e; bool escaped = false; @@ -430,7 +446,7 @@ char *split_quoted(const char *c, size_t *l, char **state) { return NULL; else if (*current == '\'') { - current ++; + current++; for (e = current; *e; e++) { if (escaped) @@ -441,11 +457,11 @@ char *split_quoted(const char *c, size_t *l, char **state) { break; } - *l = e-current; - *state = (char*) (*e == 0 ? e : e+1); + *l = e - current; + *state = (char *)(*e == 0 ? e : e + 1); } else if (*current == '\"') { - current ++; + current++; for (e = current; *e; e++) { if (escaped) @@ -456,8 +472,8 @@ char *split_quoted(const char *c, size_t *l, char **state) { break; } - *l = e-current; - *state = (char*) (*e == 0 ? e : e+1); + *l = e - current; + *state = (char *)(*e == 0 ? e : e + 1); } else { for (e = current; *e; e++) { @@ -468,30 +484,32 @@ char *split_quoted(const char *c, size_t *l, char **state) { else if (strchr(WHITESPACE, *e)) break; } - *l = e-current; - *state = (char*) e; + *l = e - current; + *state = (char *)e; } - return (char*) current; + return (char *)current; } /* Split a string into words. */ -char *split(const char *c, size_t *l, const char *separator, char **state) { +char *split(const char *c, size_t *l, const char *separator, char **state) +{ char *current; - current = *state ? *state : (char*) c; + current = *state ? *state : (char *)c; if (!*current || *c == 0) return NULL; current += strspn(current, separator); *l = strcspn(current, separator); - *state = current+*l; + *state = current + *l; - return (char*) current; + return (char *)current; } -int unhexchar(char c) { +int unhexchar(char c) +{ if (c >= '0' && c <= '9') return c - '0'; @@ -505,7 +523,8 @@ int unhexchar(char c) { return -1; } -int unoctchar(char c) { +int unoctchar(char c) +{ if (c >= '0' && c <= '7') return c - '0'; diff --git a/install/util.h b/install/util.h index c13b1a27..d212ec80 100644 --- a/install/util.h +++ b/install/util.h @@ -86,8 +86,8 @@ typedef struct dual_timestamp { usec_t now(clockid_t clock); -dual_timestamp* dual_timestamp_get(dual_timestamp *ts); -dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u); +dual_timestamp *dual_timestamp_get(dual_timestamp * ts); +dual_timestamp *dual_timestamp_from_realtime(dual_timestamp * ts, usec_t u); #define dual_timestamp_is_set(ts) ((ts)->realtime > 0) @@ -115,28 +115,33 @@ bool streq_ptr(const char *a, const char *b); #define malloc0(n) (calloc((n), 1)) -static inline const char* yes_no(bool b) { +static inline const char *yes_no(bool b) +{ return b ? "yes" : "no"; } -static inline const char* strempty(const char *s) { +static inline const char *strempty(const char *s) +{ return s ? s : ""; } -static inline const char* strnull(const char *s) { +static inline const char *strnull(const char *s) +{ return s ? s : "(null)"; } -static inline const char *strna(const char *s) { +static inline const char *strna(const char *s) +{ return s ? s : "n/a"; } -static inline bool isempty(const char *p) { +static inline bool isempty(const char *p) +{ return !p || !p[0]; } - -static inline const char *startswith(const char *s, const char *prefix) { +static inline const char *startswith(const char *s, const char *prefix) +{ if (strncmp(s, prefix, strlen(prefix)) == 0) return s + strlen(prefix); return NULL; @@ -144,7 +149,6 @@ static inline const char *startswith(const char *s, const char *prefix) { bool endswith(const char *s, const char *postfix); - bool startswith_no_case(const char *s, const char *prefix); bool first_word(const char *s, const char *word); @@ -154,11 +158,11 @@ void close_nointr_nofail(int fd); void close_many(const int fds[], unsigned n_fd); int parse_boolean(const char *v); -int parse_usec(const char *t, usec_t *usec); -int parse_nsec(const char *t, nsec_t *nsec); -int parse_bytes(const char *t, off_t *bytes); -int parse_pid(const char *s, pid_t* ret_pid); -int parse_uid(const char *s, uid_t* ret_uid); +int parse_usec(const char *t, usec_t * usec); +int parse_nsec(const char *t, nsec_t * nsec); +int parse_bytes(const char *t, off_t * bytes); +int parse_pid(const char *s, pid_t * ret_pid); +int parse_uid(const char *s, uid_t * ret_uid); #define parse_gid(s, ret_uid) parse_uid(s, ret_uid) int safe_atou(const char *s, unsigned *ret_u); @@ -168,43 +172,53 @@ int safe_atollu(const char *s, unsigned long long *ret_u); int safe_atolli(const char *s, long long int *ret_i); #if LONG_MAX == INT_MAX -static inline int safe_atolu(const char *s, unsigned long *ret_u) { +static inline int safe_atolu(const char *s, unsigned long *ret_u) +{ assert_cc(sizeof(unsigned long) == sizeof(unsigned)); - return safe_atou(s, (unsigned*) ret_u); + return safe_atou(s, (unsigned *)ret_u); } -static inline int safe_atoli(const char *s, long int *ret_u) { + +static inline int safe_atoli(const char *s, long int *ret_u) +{ assert_cc(sizeof(long int) == sizeof(int)); - return safe_atoi(s, (int*) ret_u); + return safe_atoi(s, (int *)ret_u); } #else -static inline int safe_atolu(const char *s, unsigned long *ret_u) { +static inline int safe_atolu(const char *s, unsigned long *ret_u) +{ assert_cc(sizeof(unsigned long) == sizeof(unsigned long long)); - return safe_atollu(s, (unsigned long long*) ret_u); + return safe_atollu(s, (unsigned long long *)ret_u); } -static inline int safe_atoli(const char *s, long int *ret_u) { + +static inline int safe_atoli(const char *s, long int *ret_u) +{ assert_cc(sizeof(long int) == sizeof(long long int)); - return safe_atolli(s, (long long int*) ret_u); + return safe_atolli(s, (long long int *)ret_u); } #endif -static inline int safe_atou32(const char *s, uint32_t *ret_u) { +static inline int safe_atou32(const char *s, uint32_t * ret_u) +{ assert_cc(sizeof(uint32_t) == sizeof(unsigned)); - return safe_atou(s, (unsigned*) ret_u); + return safe_atou(s, (unsigned *)ret_u); } -static inline int safe_atoi32(const char *s, int32_t *ret_i) { +static inline int safe_atoi32(const char *s, int32_t * ret_i) +{ assert_cc(sizeof(int32_t) == sizeof(int)); - return safe_atoi(s, (int*) ret_i); + return safe_atoi(s, (int *)ret_i); } -static inline int safe_atou64(const char *s, uint64_t *ret_u) { +static inline int safe_atou64(const char *s, uint64_t * ret_u) +{ assert_cc(sizeof(uint64_t) == sizeof(unsigned long long)); - return safe_atollu(s, (unsigned long long*) ret_u); + return safe_atollu(s, (unsigned long long *)ret_u); } -static inline int safe_atoi64(const char *s, int64_t *ret_i) { +static inline int safe_atoi64(const char *s, int64_t * ret_i) +{ assert_cc(sizeof(int64_t) == sizeof(long long int)); - return safe_atolli(s, (long long int*) ret_i); + return safe_atolli(s, (long long int *)ret_i); } char *split(const char *c, size_t *l, const char *separator, char **state); @@ -219,7 +233,7 @@ char *split_quoted(const char *c, size_t *l, char **state); #define FOREACH_WORD_QUOTED(word, length, s, state) \ for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state))) -pid_t get_parent_of_pid(pid_t pid, pid_t *ppid); +pid_t get_parent_of_pid(pid_t pid, pid_t * ppid); int get_starttime_of_pid(pid_t pid, unsigned long long *st); int write_one_line_file(const char *fn, const char *line); @@ -254,7 +268,7 @@ int rmdir_parents(const char *path, const char *stop); int get_process_comm(pid_t pid, char **name); int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line); int get_process_exe(pid_t pid, char **name); -int get_process_uid(pid_t pid, uid_t *uid); +int get_process_uid(pid_t pid, uid_t * uid); char hexchar(int x); int unhexchar(char c); @@ -323,7 +337,7 @@ bool fstype_is_network(const char *fstype); int chvt(int vt); -int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl); +int read_one_char(FILE * f, char *ret, usec_t timeout, bool *need_nl); int ask(char *ret, const char *replies, const char *text, ...); int reset_terminal_fd(int fd, bool switch_to_text); @@ -340,7 +354,7 @@ int default_signals(int sig, ...); int sigaction_many(const struct sigaction *sa, ...); int close_pipe(int p[]); -int fopen_temporary(const char *path, FILE **_f, char **_temp_path); +int fopen_temporary(const char *path, FILE ** _f, char **_temp_path); ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll); @@ -351,17 +365,17 @@ int dir_is_empty(const char *path); void rename_process(const char name[8]); -void sigset_add_many(sigset_t *ss, ...); +void sigset_add_many(sigset_t * ss, ...); -char* gethostname_malloc(void); +char *gethostname_malloc(void); bool hostname_is_set(void); -char* getlogname_malloc(void); +char *getlogname_malloc(void); int getttyname_malloc(int fd, char **r); int getttyname_harder(int fd, char **r); -int get_ctty_devnr(pid_t pid, dev_t *d); -int get_ctty(pid_t, dev_t *_devnr, char **r); +int get_ctty_devnr(pid_t pid, dev_t * d); +int get_ctty(pid_t, dev_t * _devnr, char **r); int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid); @@ -371,7 +385,7 @@ int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky int pipe_eof(int fd); -cpu_set_t* cpu_set_malloc(unsigned *ncpus); +cpu_set_t *cpu_set_malloc(unsigned *ncpus); void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap); void status_printf(const char *status, bool ellipse, const char *format, ...); @@ -393,7 +407,7 @@ int touch(const char *path); char *unquote(const char *s, const char *quotes); char *normalize_env_assignment(const char *s); -int wait_for_terminate(pid_t pid, siginfo_t *status); +int wait_for_terminate(pid_t pid, siginfo_t * status); int wait_for_terminate_and_warn(const char *name, pid_t pid); _noreturn_ void freeze(void); @@ -403,8 +417,8 @@ int null_or_empty_path(const char *fn); DIR *xopendirat(int dirfd, const char *name, int flags); -void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t); -void dual_timestamp_deserialize(const char *value, dual_timestamp *t); +void dual_timestamp_serialize(FILE * f, const char *name, dual_timestamp * t); +void dual_timestamp_deserialize(const char *value, dual_timestamp * t); char *fstab_node_to_udev_node(const char *p); @@ -414,11 +428,11 @@ bool tty_is_console(const char *tty); int vtnr_from_tty(const char *tty); const char *default_term_for_tty(const char *tty); -void execute_directory(const char *directory, DIR *_d, char *argv[]); +void execute_directory(const char *directory, DIR * _d, char *argv[]); int kill_and_sigcont(pid_t pid, int sig); -bool nulstr_contains(const char*nulstr, const char *needle); +bool nulstr_contains(const char *nulstr, const char *needle); bool plymouth_running(void); @@ -427,9 +441,9 @@ void skip_syslog_pid(char **buf); void skip_syslog_date(char **buf); bool hostname_is_valid(const char *s); -char* hostname_cleanup(char *s); +char *hostname_cleanup(char *s); -char* strshorten(char *s, size_t l); +char *strshorten(char *s, size_t l); int terminal_vhangup_fd(int fd); int terminal_vhangup(const char *name); @@ -445,14 +459,14 @@ int fchmod_umask(int fd, mode_t mode); bool display_is_local(const char *display); int socket_from_display(const char *display, char **path); -int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home); -int get_group_creds(const char **groupname, gid_t *gid); +int get_user_creds(const char **username, uid_t * uid, gid_t * gid, const char **home); +int get_group_creds(const char **groupname, gid_t * gid); int in_group(const char *name); int glob_exists(const char *path); -int dirent_ensure_type(DIR *d, struct dirent *de); +int dirent_ensure_type(DIR * d, struct dirent *de); int in_search_path(const char *path, char **search); int get_files_in_directory(const char *path, char ***list); @@ -461,9 +475,9 @@ char *join(const char *x, ...) _sentinel_; bool is_main_thread(void); -bool in_charset(const char *s, const char* charset); +bool in_charset(const char *s, const char *charset); -int block_get_whole_disk(dev_t d, dev_t *ret); +int block_get_whole_disk(dev_t d, dev_t * ret); int file_is_priv_sticky(const char *p); @@ -512,35 +526,41 @@ char *format_bytes(char *buf, size_t l, off_t t); int fd_wait_for_event(int fd, int event, usec_t timeout); -void* memdup(const void *p, size_t l); +void *memdup(const void *p, size_t l); int is_kernel_thread(pid_t pid); -static inline void freep(void *p) { - free(*(void**) p); +static inline void freep(void *p) +{ + free(*(void **)p); } -static inline void fclosep(FILE **f) { +static inline void fclosep(FILE ** f) +{ if (*f) fclose(*f); } -static inline void pclosep(FILE **f) { +static inline void pclosep(FILE ** f) +{ if (*f) pclose(*f); } -static inline void closep(int *fd) { +static inline void closep(int *fd) +{ if (*fd >= 0) close_nointr_nofail(*fd); } -static inline void closedirp(DIR **d) { +static inline void closedirp(DIR ** d) +{ if (*d) closedir(*d); } -static inline void umaskp(mode_t *u) { +static inline void umaskp(mode_t * u) +{ umask(*u); } @@ -555,7 +575,7 @@ static inline void umaskp(mode_t *u) { int fd_inc_sndbuf(int fd, size_t n); int fd_inc_rcvbuf(int fd, size_t n); -int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...); +int fork_agent(pid_t * pid, const int except[], unsigned n_except, const char *path, ...); int setrlimit_closest(int resource, const struct rlimit *rlim); diff --git a/logtee.c b/logtee.c index d32fa037..68de9c3a 100644 --- a/logtee.c +++ b/logtee.c @@ -9,65 +9,63 @@ #define BUFLEN 4096 -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { - int fd; - int len, slen; - int ret; - int timeout; - char *timeout_env; - struct pollfd fds[] = {{ - .fd = STDIN_FILENO, - .events = POLLIN | POLLERR, - }}; + int fd; + int len, slen; + int ret; + int timeout; + char *timeout_env; + struct pollfd fds[] = { { + .fd = STDIN_FILENO, + .events = POLLIN | POLLERR, + } + }; - timeout_env = getenv("LOGTEE_TIMEOUT_MS"); - if (timeout_env) - timeout = atoi(timeout_env); - else - timeout = -1; + timeout_env = getenv("LOGTEE_TIMEOUT_MS"); + if (timeout_env) + timeout = atoi(timeout_env); + else + timeout = -1; - if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - exit(EXIT_FAILURE); - } + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } - fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644); - if (fd == -1) { - perror("open"); - exit(EXIT_FAILURE); - } + fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644); + if (fd == -1) { + perror("open"); + exit(EXIT_FAILURE); + } - fprintf(stderr, "Logging to %s: ", argv[1]); + fprintf(stderr, "Logging to %s: ", argv[1]); - slen = 0; + slen = 0; - do { - ret = poll (fds, sizeof(fds) / sizeof(fds[0]), timeout); - if (ret == 0) { - fprintf (stderr, "Timed out after %d milliseconds of no output.\n", timeout); - exit(EXIT_FAILURE); - } - len = splice(STDIN_FILENO, NULL, fd, NULL, - BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK); + do { + ret = poll(fds, sizeof(fds) / sizeof(fds[0]), timeout); + if (ret == 0) { + fprintf(stderr, "Timed out after %d milliseconds of no output.\n", timeout); + exit(EXIT_FAILURE); + } + len = splice(STDIN_FILENO, NULL, fd, NULL, BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK); - if (len < 0) { - if (errno == EAGAIN) - continue; - perror("tee"); - exit(EXIT_FAILURE); - } else - if (len == 0) - break; - slen += len; - if ((slen/BUFLEN) > 0) { - fprintf(stderr, "."); - } - slen = slen % BUFLEN; + if (len < 0) { + if (errno == EAGAIN) + continue; + perror("tee"); + exit(EXIT_FAILURE); + } else if (len == 0) + break; + slen += len; + if ((slen / BUFLEN) > 0) { + fprintf(stderr, "."); + } + slen = slen % BUFLEN; - } while (1); - close(fd); - fprintf(stderr, "\n"); - exit(EXIT_SUCCESS); + } while (1); + close(fd); + fprintf(stderr, "\n"); + exit(EXIT_SUCCESS); }