From a386133068e0d187c16fd5c077eb66a41245a622 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Mon, 21 Aug 2023 21:07:28 +0100 Subject: [PATCH] common/file-helpers.c: share file_exists() to reduce duplication --- include/common/file-helpers.h | 12 ++++++++++++ src/button/button-png.c | 10 +--------- src/common/file-helpers.c | 10 ++++++++++ src/common/meson.build | 1 + src/config/session.c | 10 ++-------- 5 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 include/common/file-helpers.h create mode 100644 src/common/file-helpers.c diff --git a/include/common/file-helpers.h b/include/common/file-helpers.h new file mode 100644 index 00000000..b9b1a228 --- /dev/null +++ b/include/common/file-helpers.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef LABWC_FILE_HELPERS_H +#define LABWC_FILE_HELPERS_H +#include + +/** + * file_exists() - Test if file exists. + * @filename: Name of file to test. + */ +bool file_exists(const char *filename); + +#endif /* LABWC_FILE_HELPERS_H */ diff --git a/src/button/button-png.c b/src/button/button-png.c index cc45594f..66cf5535 100644 --- a/src/button/button-png.c +++ b/src/button/button-png.c @@ -8,22 +8,14 @@ #include #include #include -#include #include #include "buffer.h" #include "button/button-png.h" #include "button/common.h" +#include "common/file-helpers.h" #include "labwc.h" #include "theme.h" -/* Share with session.c:isfile() */ -static bool -file_exists(const char *path) -{ - struct stat st; - return (!stat(path, &st)); -} - /* * cairo_image_surface_create_from_png() does not gracefully handle non-png * files, so we verify the header before trying to read the rest of the file. diff --git a/src/common/file-helpers.c b/src/common/file-helpers.c new file mode 100644 index 00000000..2361b546 --- /dev/null +++ b/src/common/file-helpers.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include "common/file-helpers.h" + +bool +file_exists(const char *filename) +{ + struct stat st; + return (!stat(filename, &st)); +} diff --git a/src/common/meson.build b/src/common/meson.build index 7007b061..72ad6f2b 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -2,6 +2,7 @@ labwc_sources += files( 'buf.c', 'dir.c', 'fd_util.c', + 'file-helpers.c', 'font.c', 'grab-file.c', 'graphic-helpers.c', diff --git a/src/config/session.c b/src/config/session.c index 06f8556a..1a528d18 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -8,18 +8,12 @@ #include #include #include "common/buf.h" +#include "common/file-helpers.h" #include "common/mem.h" #include "common/spawn.h" #include "common/string-helpers.h" #include "config/session.h" -static bool -isfile(const char *path) -{ - struct stat st; - return (!stat(path, &st)); -} - static bool string_empty(const char *s) { @@ -130,7 +124,7 @@ session_autostart_init(const char *dir) if (!autostart) { return; } - if (!isfile(autostart)) { + if (!file_exists(autostart)) { wlr_log(WLR_ERROR, "no autostart file"); goto out; }