aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libmdsserver/macros.h37
-rw-r--r--src/mds-server.c19
2 files changed, 47 insertions, 9 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
new file mode 100644
index 0000000..9225c49
--- /dev/null
+++ b/src/libmdsserver/macros.h
@@ -0,0 +1,37 @@
+/**
+ * mds — A micro-display server
+ * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef MDS_LIBMDSSERVER_MACROS_H
+#define MDS_LIBMDSSERVER_MACROS_H
+
+
+#include <stdio.h>
+
+
+/**
+ * Wrapper for `snprintf` that allows you to forget about the buffer size
+ *
+ * @param buffer:char[] The buffer, must be of the type `char[]` and not `char*`
+ * @param format:char* The format
+ * @param ... The arguments
+ */
+#define xsnprintf(buffer, format, ...) \
+ snprintf(buffer, sizeof(buffer) / sizeof(char), format, __VA_ARGS__);
+
+
+#endif
+
diff --git a/src/mds-server.c b/src/mds-server.c
index 8a42961..b34f99b 100644
--- a/src/mds-server.c
+++ b/src/mds-server.c
@@ -22,6 +22,7 @@
#include <libmdsserver/hash-table.h>
#include <libmdsserver/fd-table.h>
#include <libmdsserver/mds-message.h>
+#include <libmdsserver/macros.h>
#include <alloca.h>
#include <signal.h>
@@ -273,7 +274,7 @@ int main(int argc_, char** argv_)
pid_t pid = getpid();
int reexec_fd, r;
char shm_path[NAME_MAX + 1];
- snprintf(shm_path, sizeof(shm_path) / sizeof(char), SHM_PATH_PATTERN, (unsigned long int)pid);
+ xsnprintf(shm_path, SHM_PATH_PATTERN, (unsigned long int)pid);
reexec_fd = shm_open(shm_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
if (reexec_fd < 0)
{
@@ -380,7 +381,7 @@ int main(int argc_, char** argv_)
pthread_mutex_unlock(&slave_mutex);
/* Marshal the state of the server. */
- snprintf(shm_path, sizeof(shm_path) / sizeof(char), SHM_PATH_PATTERN, (unsigned long int)pid);
+ xsnprintf(shm_path, SHM_PATH_PATTERN, (unsigned long int)pid);
reexec_fd = shm_open(shm_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
if (reexec_fd < 0)
{
@@ -611,17 +612,17 @@ void run_initrc(char** args)
/* Test $XDG_CONFIG_HOME. */
if ((env = getenv_nonempty("XDG_CONFIG_HOME")) != NULL)
{
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", env, INITRC_FILE);
+ xsnprintf(pathname, "%s/.%s", env, INITRC_FILE);
execv(args[0], args);
}
/* Test $HOME. */
if ((env = getenv_nonempty("HOME")) != NULL)
{
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.config/%s", env, INITRC_FILE);
+ xsnprintf(pathname, "%s/.config/%s", env, INITRC_FILE);
execv(args[0], args);
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", env, INITRC_FILE);
+ xsnprintf(pathname, "%s/.%s", env, INITRC_FILE);
execv(args[0], args);
}
@@ -632,10 +633,10 @@ void run_initrc(char** args)
home = pwd->pw_dir;
if ((home != NULL) && (*home != '\0'))
{
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.config/%s", home, INITRC_FILE);
+ xsnprintf(pathname, "%s/.config/%s", home, INITRC_FILE);
execv(args[0], args);
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", home, INITRC_FILE);
+ xsnprintf(pathname, "%s/.%s", home, INITRC_FILE);
execv(args[0], args);
}
}
@@ -652,7 +653,7 @@ void run_initrc(char** args)
len = (int)(end - begin);
if (len > 0)
{
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%.*s/%s", len, begin, INITRC_FILE);
+ xsnprintf(pathname, "%.*s/%s", len, begin, INITRC_FILE);
execv(args[0], args);
}
if (*end == '\0')
@@ -662,7 +663,7 @@ void run_initrc(char** args)
}
/* Test /etc. */
- snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/%s", SYSCONFDIR, INITRC_FILE);
+ xsnprintf(pathname, "%s/%s", SYSCONFDIR, INITRC_FILE);
execv(args[0], args);