]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - term-utils/mesg.c
misc: consolidate version printing and close_stdout()
[thirdparty/util-linux.git] / term-utils / mesg.c
index 1c96a412d913da5ad5bb9dd57ffdeaa21c1ffa91..57d53379f2d7ad785a92ff9090c9cd3823cb28db 100644 (file)
@@ -39,7 +39,7 @@
  * Modified Mon Jul  1 18:14:10 1996, janl@ifi.uio.no, writing to stdout
  *     as suggested by Michael Meskes <meskes@Informatik.RWTH-Aachen.DE>
  *
- * 1999-02-22 Arkadiusz Mikiewicz <misiek@pld.ORG.PL>
+ * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
  * - added Native Language Support
  *
  * 2010-12-01 Marek Polacek <mmpolacek@gmail.com>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <getopt.h>
+
+#include "closestream.h"
 #include "nls.h"
 #include "c.h"
+#include "rpmatch.h"
 
 /* exit codes */
 
 #define IS_NOT_ALLOWED    1  /* Receiving messages is not allowed.  */
 #define MESG_EXIT_FAILURE 2  /* An error occurred.  */
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, _("\nUsage:\n"
-                      " %s [options] [y | n]\n"), program_invocation_short_name);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       /* TRANSLATORS: this program uses for y and n rpmatch(3),
+        * which means they can be translated.  */
        fprintf(out,
-               _("\nOptions:\n"
-                 "  -v, --verbose      explain what is being done\n"
-                 "  -V, --version      output version information and exit\n"
-                 "  -h, --help         output help screen and exit\n\n"));
+             _(" %s [options] [y | n]\n"), program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Control write access of other users to your terminal.\n"), out);
 
-       exit(out == stderr ? MESG_EXIT_FAILURE : EXIT_SUCCESS);
+       fputs(USAGE_OPTIONS, out);
+       fputs(_(" -v, --verbose  explain what is being done\n"), out);
+       printf(USAGE_HELP_OPTIONS(16));
+       printf(USAGE_MAN_TAIL("mesg(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
 {
        struct stat sb;
        char *tty;
-       int ch, verbose = FALSE;
+       int ch, fd, verbose = FALSE, ret;
+
+       static const struct option longopts[] = {
+               { "verbose",    no_argument,       NULL, 'v' },
+               { "version",    no_argument,       NULL, 'V' },
+               { "help",       no_argument,       NULL, 'h' },
+               { NULL,         0, NULL, 0 }
+       };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-
-       static const struct option longopts[] = {
-               { "verbose",    no_argument,       0, 'v' },
-               { "version",    no_argument,       0, 'V' },
-               { "help",       no_argument,       0, 'h' },
-               { NULL,         0, 0, 0 }
-       };
+       close_stdout_atexit();
 
        while ((ch = getopt_long(argc, argv, "vVh", longopts, NULL)) != -1)
                switch (ch) {
                case 'v':
                        verbose = TRUE;
                        break;
+
                case 'V':
-                       printf(_("%s from %s\n"), program_invocation_short_name,
-                       PACKAGE_STRING);
-                       exit(EXIT_SUCCESS);
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
 
        argc -= optind;
        argv += optind;
 
+       if (!isatty(STDERR_FILENO)) {
+               if (verbose)
+                       warnx(_("no tty"));
+               exit(MESG_EXIT_FAILURE);
+       }
        if ((tty = ttyname(STDERR_FILENO)) == NULL)
                err(MESG_EXIT_FAILURE, _("ttyname failed"));
-
-       if (stat(tty, &sb) < 0)
-               err(MESG_EXIT_FAILURE, _("stat %s failed"), tty);
+       if ((fd = open(tty, O_RDONLY)) < 0)
+               err(MESG_EXIT_FAILURE, _("cannot open %s"), tty);
+       if (fstat(fd, &sb))
+               err(MESG_EXIT_FAILURE, _("stat of %s failed"), tty);
 
        if (!*argv) {
+               close(fd);
                if (sb.st_mode & (S_IWGRP | S_IWOTH)) {
                        puts(_("is y"));
                        return IS_ALLOWED;
@@ -126,25 +143,31 @@ int main(int argc, char *argv[])
                return IS_NOT_ALLOWED;
        }
 
-       switch (*argv[0]) {
-       case 'y':
+       switch (rpmatch(argv[0])) {
+       case RPMATCH_YES:
 #ifdef USE_TTY_GROUP
-               if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
+               if (fchmod(fd, sb.st_mode | S_IWGRP) < 0)
 #else
-               if (chmod(tty, sb.st_mode | S_IWGRP | S_IWOTH) < 0)
+               if (fchmod(fd, sb.st_mode | S_IWGRP | S_IWOTH) < 0)
 #endif
                        err(MESG_EXIT_FAILURE, _("change %s mode failed"), tty);
                if (verbose)
                        puts(_("write access to your terminal is allowed"));
-               return IS_ALLOWED;
-       case 'n':
-               if (chmod(tty, sb.st_mode & ~(S_IWGRP|S_IWOTH)) < 0)
+               ret = IS_ALLOWED;
+               break;
+       case RPMATCH_NO:
+               if (fchmod(fd, sb.st_mode & ~(S_IWGRP|S_IWOTH)) < 0)
                         err(MESG_EXIT_FAILURE, _("change %s mode failed"), tty);
                if (verbose)
                        puts(_("write access to your terminal is denied"));
-               return IS_NOT_ALLOWED;
-       default:
-               warnx(_("invalid argument: %c"), *argv[0]);
-               usage(stderr);
+               ret = IS_NOT_ALLOWED;
+               break;
+       case RPMATCH_INVALID:
+               warnx(_("invalid argument: %s"), argv[0]);
+               errtryhelp(EXIT_FAILURE);
+        default:
+                abort();
        }
+       close(fd);
+       return ret;
 }