Commit: b9be071dd763a8c3a073ad2ba1ac6564a2962f46
Parent: f3a2b10b4831b4016abb0435b73b91f3db1e0bb5
Author: Randy Palamar
Date: Tue, 16 Mar 2021 14:52:16 -0600
move utility functions to their own source file
Diffstat:
7 files changed, 57 insertions(+), 49 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
include config.mk
-SRC += status.c
+SRC += status.c util.c
OBJ = $(SRC:.c=.o)
all: status
diff --git a/linux.c b/linux.c
@@ -5,6 +5,7 @@
#include <alsa/mixer.h>
#include "status.h"
+#include "util.h"
const char *
getvol(const char *card, const char *output)
diff --git a/openbsd.c b/openbsd.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include "status.h"
+#include "util.h"
const char *
getvol(const char *card, const char *output)
diff --git a/status.c b/status.c
@@ -1,8 +1,6 @@
/* See LICENSE for license details. */
#include <signal.h>
-#include <stdarg.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@@ -10,62 +8,21 @@
#include <X11/Xlib.h>
#include "status.h"
+#include "util.h"
#include "config.h"
static int done = 0;
static int dflag = 0;
-static char buf[1024];
+char buf[BUFLEN];
static Display *dpy;
-void
-die(const char *errstr, ...)
-{
- va_list ap;
-
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
- va_end(ap);
- exit(1);
-}
-
static void
terminate(int signo)
{
done = 1;
}
-const char *
-bprintf(const char *fmt, ...)
-{
- int ret;
- va_list ap;
-
- va_start(ap, fmt);
- ret = vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
-
- return (ret < 0)? NULL : buf;
-}
-
-int
-pscanf(const char *path, const char *fmt, ...)
-{
- FILE *fp;
- va_list ap;
- int ret;
-
- if (!(fp = fopen(path, "r")))
- return -1;
-
- va_start(ap, fmt);
- ret = vfscanf(fp, fmt, ap);
- va_end(ap);
- fclose(fp);
-
- return (ret == EOF) ? -1 : ret;
-}
-
static void
setstatus(char *str)
{
diff --git a/status.h b/status.h
@@ -1,6 +1,5 @@
-void die(const char *errstr, ...);
-int pscanf(const char *path, const char *fmt, ...);
-const char *bprintf(const char *fmt, ...);
+#define BUFLEN 1024
+extern char buf[BUFLEN];
const char *getvol(const char *card, const char *output);
const char *batinfo(const char *bat);
diff --git a/util.c b/util.c
@@ -0,0 +1,47 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "status.h"
+
+void
+die(const char *errstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, errstr);
+ vfprintf(stderr, errstr, ap);
+ va_end(ap);
+ exit(1);
+}
+
+const char *
+bprintf(const char *fmt, ...)
+{
+ size_t ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+
+ return (ret < 0)? NULL : buf;
+}
+
+int
+pscanf(const char *path, const char *fmt, ...)
+{
+ FILE *fp;
+ va_list ap;
+ int ret;
+
+ if (!(fp = fopen(path, "r")))
+ return -1;
+
+ va_start(ap, fmt);
+ ret = vfscanf(fp, fmt, ap);
+ va_end(ap);
+ fclose(fp);
+
+ return (ret == EOF) ? -1 : ret;
+}
diff --git a/util.h b/util.h
@@ -0,0 +1,3 @@
+void die(const char *errstr, ...);
+char *bprintf(const char *fmt, ...);
+int pscanf(const char *path, const char *fmt, ...);