Skip to content

Commit 8957279

Browse files
committed
vlogger: use static buffer and add -f flag
1 parent 999b1ba commit 8957279

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

vlogger.8

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.Sh SYNOPSIS
88
.Nm vlogger
99
.Op Fl isS
10+
.Op Fl f Ar file
1011
.Op Fl p Ar pri
1112
.Op Fl t Ar tag
1213
.Sh DESCRIPTION
@@ -40,20 +41,10 @@ as default
4041
.Pp
4142
The options are as follows:
4243
.Bl -tag -width indent
43-
.It Fl S
44-
Force
45-
.Nm
46-
to use
47-
.Xr syslog 3
48-
even if
49-
.Pa /etc/vlogger
50-
exists.
51-
.It Fl s
52-
Output the message to standard error, as well as
53-
.Xr syslog 3 .
54-
Only supported if
55-
.Xr syslog 3
56-
is used.
44+
.It Fl f Ar file
45+
Read lines from the specified
46+
.Ar file .
47+
This
5748
.It Fl i
5849
Log the PID of the
5950
.Nm
@@ -75,6 +66,20 @@ or
7566
.Xr syslog 3 .
7667
The default is
7768
.Pa daemon.info .
69+
.It Fl S
70+
Force
71+
.Nm
72+
to use
73+
.Xr syslog 3
74+
even if
75+
.Pa /etc/vlogger
76+
exists.
77+
.It Fl s
78+
Output the message to standard error, as well as
79+
.Xr syslog 3 .
80+
Only supported if
81+
.Xr syslog 3
82+
is used.
7883
.It Fl t Ar tag
7984
Defines the
8085
.Xr openlog 3

vlogger.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ strpriority(char *s, int *facility, int *level)
7676
int
7777
main(int argc, char *argv[])
7878
{
79+
char buf[1024];
7980
char *p, *argv0;
8081
char *tag = "vlogger";
8182
int c;
@@ -98,16 +99,21 @@ main(int argc, char *argv[])
9899
}
99100
}
100101

101-
while ((c = getopt(argc, argv, "ip:Sst:")) != -1)
102+
while ((c = getopt(argc, argv, "f:ip:Sst:")) != -1)
102103
switch (c) {
104+
case 'f':
105+
if (freopen(optarg, "r", stdin) == NULL) {
106+
fprintf(stderr, "vlogger: %s: %s\n", optarg, strerror(errno));
107+
return 1;
108+
}
109+
break;
103110
case 'i': logflags |= LOG_PID; break;
104111
case 'p': strpriority(optarg, &facility, &level); break;
105112
case 'S': Sflag++; break;
106113
case 's': logflags |= LOG_PERROR; break;
107114
case 't': tag = optarg; break;
108115
default:
109-
usage:
110-
fprintf(stderr, "usage: vlogger [-isS] [-p pri] [-t tag]\n");
116+
fprintf(stderr, "usage: vlogger [-isS] [-f file] [-p pri] [-t tag]\n");
111117
exit(1);
112118
}
113119

@@ -129,14 +135,8 @@ main(int argc, char *argv[])
129135

130136
openlog(tag, logflags, facility);
131137

132-
char *line = NULL;
133-
size_t linelen = 0;
134-
ssize_t rd;
135-
while ((rd = getline(&line, &linelen, stdin)) != -1) {
136-
if (line[rd-1] == '\n')
137-
line[rd-1] = '\0';
138-
syslog(level|facility, "%s", line);
139-
}
138+
while (fgets(buf, sizeof buf, stdin) != NULL)
139+
syslog(level|facility, "%s", buf);
140140

141141
return 1;
142142
}

0 commit comments

Comments
 (0)