mesg.c - ubase - suckless linux base utils
 (HTM) git clone git://git.suckless.org/ubase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mesg.c (861B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <sys/stat.h>
            3 #include <sys/types.h>
            4 
            5 #include <stdio.h>
            6 #include <stdlib.h>
            7 #include <unistd.h>
            8 
            9 #include "util.h"
           10 
           11 static void
           12 usage(void)
           13 {
           14         eprintf("usage: %s [n|y]\n", argv0);
           15 }
           16 
           17 int
           18 main(int argc, char *argv[])
           19 {
           20         struct stat sb;
           21         mode_t mode;
           22 
           23         ARGBEGIN {
           24         default:
           25                 usage();
           26         } ARGEND;
           27 
           28         if (argc > 1)
           29                 usage();
           30 
           31         if (isatty(2) == 0)
           32                 eprintf("stderr: not a tty\n");
           33 
           34         if (fstat(2, &sb) < 0)
           35                 eprintf("fstat stderr:");
           36 
           37         if (argc == 0) {
           38                 puts(sb.st_mode & (S_IWGRP | S_IWOTH) ? "is y" : "is n");
           39                 return 0;
           40         }
           41 
           42         if (argv[0][0] == 'y' && argv[0][1] == '\0')
           43                 mode = sb.st_mode | S_IWGRP | S_IWOTH;
           44         else if (argv[0][0] == 'n' && argv[0][1] == '\0')
           45                 mode = sb.st_mode & ~(S_IWGRP | S_IWOTH);
           46         else
           47                 usage();
           48 
           49         if (fchmod(2, mode) < 0)
           50                 eprintf("fchmod stderr:");
           51 
           52         return 0;
           53 }