Skip to content

Commit de9ea4b

Browse files
committed
vlogger: style changes/cleanup
1 parent a73a097 commit de9ea4b

1 file changed

Lines changed: 15 additions & 23 deletions

File tree

vlogger.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <unistd.h>
77
#include <limits.h>
88

9-
static char pwd[PATH_MAX + 1];
9+
static char pwd[PATH_MAX];
1010

1111
typedef struct {
1212
const char *const c_name;
@@ -77,32 +77,20 @@ int
7777
main(int argc, char *argv[])
7878
{
7979
char c, *p, *argv0;
80-
char *tag = "vlogger";
80+
char *tag = NULL;
8181
int facility = LOG_DAEMON;
8282
int level = LOG_INFO;
83-
size_t pwdlen = 0;
8483

8584
argv0 = *argv;
8685

87-
if (!strcmp(argv0, "./run")) {
88-
p = getcwd(pwd, sizeof(pwd));
89-
if (p == NULL) {
90-
/* FIXME: roll our own getcwd that does dynamic allocation? */
91-
fprintf(stderr, "vlogger: getcwd: %s\n", strerror(errno));
92-
exit(1);
93-
}
94-
if (pwd[0] != '/') {
95-
fprintf(stderr, "Current working directory is not reachable or not absolute.\n");
96-
exit(1);
97-
}
98-
pwdlen = strlen(pwd);
99-
if (pwd[pwdlen - 1] == '/')
100-
pwd[pwdlen - 1] = '\0';
101-
if ((p = strrchr(pwd, '/')) && !strncmp(p+1, "log", 3) &&
102-
(*p = 0, (p = strrchr(pwd, '/'))) && (*(p + 1) != 0)) {
103-
if (!(tag = strdup(p+1))) {
104-
fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
105-
exit(1);
86+
if (strcmp(argv0, "./run") == 0) {
87+
p = getcwd(pwd, sizeof (pwd));
88+
if (p != NULL && *pwd == '/') {
89+
if (*(p = pwd+(strlen(pwd)-1)) == '/')
90+
*p = '\0';
91+
if ((p = strrchr(pwd, '/')) && strncmp(p+1, "log", 3) == 0 &&
92+
(*p = '\0', (p = strrchr(pwd, '/'))) && (*(p+1) != '\0')) {
93+
tag = strdup(p+1);
10694
}
10795
}
10896
}
@@ -112,10 +100,14 @@ main(int argc, char *argv[])
112100
case 'p': strpriority(optarg, &facility, &level); break;
113101
case 't': tag = optarg; break;
114102
default:
103+
usage:
115104
fprintf(stderr, "usage: vlogger [-p priority] [-t tag]\n");
116105
exit(1);
117106
}
118107

108+
if (tag == NULL)
109+
goto usage;
110+
119111
if (access("/etc/vlogger", X_OK) != -1) {
120112
execl("/etc/vlogger", argv0, tag, (char *)0);
121113
fprintf(stderr, "vlogger: exec: %s\n", strerror(errno));
@@ -129,7 +121,7 @@ main(int argc, char *argv[])
129121
ssize_t rd;
130122
while ((rd = getline(&line, &linelen, stdin)) != -1) {
131123
if (line[rd-1] == '\n')
132-
line[rd-1] = 0;
124+
line[rd-1] = '\0';
133125
syslog(level|facility, "%s", line);
134126
}
135127

0 commit comments

Comments
 (0)