Adjust more included headers to be compatible with the musl C
[sysvinit.git] / src / wall.c
CommitLineData
a74aeac6
PR
1/*
2 * wall.c Write to all users logged in.
3 *
4 * Usage: wall [text]
5 *
6 * Version: @(#)wall 2.79 12-Sep-2000 miquels@cistron.nl
7 *
8 * This file is part of the sysvinit suite,
9 * Copyright (C) 1991-2000 Miquel van Smoorenburg.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <pwd.h>
31#include <syslog.h>
a33c1f65 32#include <sys/types.h>
901fc4d2 33#include "init.h"
a74aeac6
PR
34
35
36char *Version = "@(#) wall 2.79 12-Sep-2000 miquels@cistron.nl";
37#define MAXLEN 4096
38#define MAXLINES 20
39
a74aeac6
PR
40int main(int argc, char **argv)
41{
42 char buf[MAXLEN];
43 char line[83];
44 int i, f, ch;
45 int len = 0;
46 int remote = 0;
47 char *p;
48 char *whoami;
49 struct passwd *pwd;
50
51 buf[0] = 0;
52 if ((pwd = getpwuid(getuid())) == NULL) {
53 if (getuid() == 0)
54 whoami = "root";
55 else {
56 fprintf(stderr, "You don't exist. Go away.\n");
57 exit(1);
58 }
59 } else
60 whoami = pwd->pw_name;
61
62 while((ch = getopt(argc, argv, "n")) != EOF)
63 switch(ch) {
64 case 'n':
65 /*
66 * Undocumented option for suppressing
67 * banner from rpc.rwalld. Only works if
68 * we are root or if we're NOT setgid.
69 */
70 if (geteuid() != 0 && getgid() != getegid()) {
71 fprintf(stderr, "wall -n: not priviliged\n");
72 exit(1);
73 }
74 remote = 1;
75 break;
76 default:
77 fprintf(stderr, "usage: wall [message]\n");
78 return 1;
79 break;
80 }
81
82 if ((argc - optind) > 0) {
83 for(f = optind; f < argc; f++) {
84 len += strlen(argv[f]) + 1;
85 if (len >= MAXLEN-2) break;
86 strcat(buf, argv[f]);
87 if (f < argc-1) strcat(buf, " ");
88 }
89 strcat(buf, "\r\n");
90 } else {
91 while(fgets(line, 80, stdin)) {
92 /*
93 * Make sure that line ends in \r\n
94 */
95 for(p = line; *p && *p != '\r' && *p != '\n'; p++)
96 ;
97 strcpy(p, "\r\n");
98 len += strlen(line);
99 if (len >= MAXLEN) break;
100 strcat(buf, line);
101 }
102 }
103
104 i = 0;
105 for (p = buf; *p; p++) {
c5ae561e 106 if (*p == '\n' && ++i >= MAXLINES) {
a74aeac6
PR
107 *++p = 0;
108 break;
109 }
110 }
111
112 openlog("wall", LOG_PID, LOG_USER);
c5ae561e 113 syslog(LOG_INFO, "wall: user %s broadcasted %d lines (%zu chars)",
a74aeac6
PR
114 whoami, i, strlen(buf));
115 closelog();
116
117 unsetenv("TZ");
901fc4d2 118 wall(buf, remote);
a74aeac6
PR
119
120 /*NOTREACHED*/
121 return 0;
122}
123