]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - term-utils/mesg.c
su: change error message
[thirdparty/util-linux.git] / term-utils / mesg.c
index 12e4e0e8ca213ef8a5a1a955be7c38b5b60871fd..608f3cf61ace1133f93fab3c6daca5ce9cd62ec8 100644 (file)
 #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)
 {
+       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,
              _(" %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);
+
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -v, --verbose  explain what is being done\n"), out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
-       fprintf(out, USAGE_MAN_TAIL("mesg(1)"));
+       printf(USAGE_HELP_OPTIONS(16));
+       printf(USAGE_MAN_TAIL("mesg(1)"));
 
-       exit(out == stderr ? MESG_EXIT_FAILURE : EXIT_SUCCESS);
+       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,       0, 'v' },
-               { "version",    no_argument,       0, 'V' },
-               { "help",       no_argument,       0, 'h' },
-               { NULL,         0, 0, 0 }
+               { "verbose",    no_argument,       NULL, 'v' },
+               { "version",    no_argument,       NULL, 'V' },
+               { "help",       no_argument,       NULL, 'h' },
+               { NULL,         0, NULL, 0 }
        };
 
        setlocale(LC_ALL, "");
@@ -109,21 +113,28 @@ int main(int argc, char *argv[])
                        printf(UTIL_LINUX_VERSION);
                        exit(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 failed %s"), 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;
@@ -133,26 +144,30 @@ int main(int argc, char *argv[])
        }
 
        switch (rpmatch(argv[0])) {
-       case 1:
+       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 0:
-               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;
-        case -1:
+               ret = IS_NOT_ALLOWED;
+               break;
+       case RPMATCH_INVALID:
                warnx(_("invalid argument: %s"), argv[0]);
-               usage(stderr);
+               errtryhelp(EXIT_FAILURE);
         default:
                 abort();
        }
+       close(fd);
+       return ret;
 }