"Fossies" - the Fresh Open Source Software Archive

Member "sysvinit-2.99/src/wall.c" (21 Feb 2021, 2826 Bytes) of package /linux/misc/sysvinit-2.99.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wall.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.98_vs_2.99.

    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>
   32 #include <sys/types.h>
   33 #include "init.h"
   34 
   35 
   36 #define MAXLEN 4096
   37 #define LINE_SIZE 80
   38 #define MAXLINES 20
   39 
   40 int main(int argc, char **argv)
   41 {
   42   char buf[MAXLEN];
   43   char line[LINE_SIZE + 3];   /* leave room for \r\n\0 */
   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 privileged\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-4) 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++) {
  106     if (*p == '\n' && ++i >= MAXLINES) {
  107         *++p = 0;
  108         break;
  109     }
  110   }
  111 
  112   openlog("wall", LOG_PID, LOG_USER);
  113   syslog(LOG_INFO, "wall: user %s broadcasted %d lines (%zu chars)",
  114     whoami, i, strlen(buf));
  115   closelog();
  116 
  117   unsetenv("TZ");
  118   wall(buf, remote);
  119 
  120   /*NOTREACHED*/
  121   return 0;
  122 }
  123