about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2018-06-03 19:46:21 +0200
committerDuncaen <mail@duncano.de>2018-06-20 18:03:28 +0200
commitde9ea4b80cf376e3ecc183e9ff5d17bb0b2b547e (patch)
treeb586e5c3dc3e427b9bd5f2b1f787dc045ac000ec
parenta73a09729b96311a12e47e6d9176e8e36998d45f (diff)
downloadrunit-void-de9ea4b80cf376e3ecc183e9ff5d17bb0b2b547e.tar.gz
runit-void-de9ea4b80cf376e3ecc183e9ff5d17bb0b2b547e.tar.xz
runit-void-de9ea4b80cf376e3ecc183e9ff5d17bb0b2b547e.zip
vlogger: style changes/cleanup
-rw-r--r--vlogger.c38
1 files changed, 15 insertions, 23 deletions
diff --git a/vlogger.c b/vlogger.c
index 605ff37..cd32143 100644
--- a/vlogger.c
+++ b/vlogger.c
@@ -6,7 +6,7 @@
 #include <unistd.h>
 #include <limits.h>
 
-static char pwd[PATH_MAX + 1];
+static char pwd[PATH_MAX];
 
 typedef struct {
   const char *const c_name;
@@ -77,32 +77,20 @@ int
 main(int argc, char *argv[])
 {
 	char c, *p, *argv0;
-	char *tag = "vlogger";
+	char *tag = NULL;
 	int facility = LOG_DAEMON;
 	int level = LOG_INFO;
-	size_t pwdlen = 0;
 
 	argv0 = *argv;
 
-	if (!strcmp(argv0, "./run")) {
-		p = getcwd(pwd, sizeof(pwd));
-		if (p == NULL) {
-/* FIXME: roll our own getcwd that does dynamic allocation? */
-			fprintf(stderr, "vlogger: getcwd: %s\n", strerror(errno));
-			exit(1);
-		}
-		if (pwd[0] != '/') {
-			fprintf(stderr, "Current working directory is not reachable or not absolute.\n");
-			exit(1);
-		}
-		pwdlen = strlen(pwd);
-		if (pwd[pwdlen - 1] == '/')
-			pwd[pwdlen - 1] = '\0';
-		if ((p = strrchr(pwd, '/')) && !strncmp(p+1, "log", 3) &&
-		    (*p = 0, (p = strrchr(pwd, '/'))) && (*(p + 1) != 0)) {
-			if (!(tag = strdup(p+1))) {
-				fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
-				exit(1);
+	if (strcmp(argv0, "./run") == 0) {
+		p = getcwd(pwd, sizeof (pwd));
+		if (p != NULL && *pwd == '/') {
+			if (*(p = pwd+(strlen(pwd)-1)) == '/')
+				*p = '\0';
+			if ((p = strrchr(pwd, '/')) && strncmp(p+1, "log", 3) == 0 &&
+			    (*p = '\0', (p = strrchr(pwd, '/'))) && (*(p+1) != '\0')) {
+				tag = strdup(p+1);
 			}
 		}
 	}
@@ -112,10 +100,14 @@ main(int argc, char *argv[])
 		case 'p': strpriority(optarg, &facility, &level); break;
 		case 't': tag = optarg; break;
 		default:
+usage:
 			fprintf(stderr, "usage: vlogger [-p priority] [-t tag]\n");
 			exit(1);
 		}
 
+	if (tag == NULL)
+		goto usage;
+
 	if (access("/etc/vlogger", X_OK) != -1) {
 		execl("/etc/vlogger", argv0, tag, (char *)0);
 		fprintf(stderr, "vlogger: exec: %s\n", strerror(errno));
@@ -129,7 +121,7 @@ main(int argc, char *argv[])
 	ssize_t rd;
 	while ((rd = getline(&line, &linelen, stdin)) != -1) {
 		if (line[rd-1] == '\n')
-			line[rd-1] = 0;
+			line[rd-1] = '\0';
 		syslog(level|facility, "%s", line);
 	}