Welcome to little lamb

Code » anopa » master » tree

[master] / src / utils / aa-tty.c

/*
 * anopa - Copyright (C) 2015-2017 Olivier Brunel
 *
 * aa-tty.c
 * Copyright (C) 2015-2018 Olivier Brunel <jjk@jjacky.com>
 *
 * This file is part of anopa.
 *
 * anopa is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * anopa is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * anopa. If not, see http://www.gnu.org/licenses/
 */

#include <getopt.h>
#include <errno.h>
#include <skalibs/bytestr.h>
#include <skalibs/djbunix.h>
#include <anopa/output.h>
#include <anopa/common.h>

#define PREFIX  "/sys/class/tty/"
#define NAME    "/active"

static void
dieusage (int rc)
{
    aa_die_usage (rc, "",
            " -D, --double-output           Enable double-output mode\n"
            " -O, --log-file FILE|FD        Write log to FILE|FD\n"
            " -h, --help                    Show this help screen and exit\n"
            " -V, --version                 Show version information and exit\n"
            );
}

int
main (int argc, char * const argv[])
{
    PROG = "aa-tty";
    char file[256];
    size_t max = sizeof (file) - sizeof (PREFIX) - sizeof (NAME) + 1;
    char name[max];
    size_t skip;
    ssize_t r;

    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "log-file",           required_argument,  NULL,   'O' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "DhO:V", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'D':
                aa_set_double_output (1);
                break;

            case 'h':
                dieusage (RC_OK);

            case 'O':
                aa_set_log_file_or_die (optarg);
                break;

            case 'V':
                aa_die_version ();

            default:
                dieusage (RC_FATAL_USAGE);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc != 0)
        dieusage (RC_FATAL_USAGE);

    byte_copy (file, sizeof (PREFIX) - 1, PREFIX);
    byte_copy (file + sizeof (PREFIX) - 1, 7, "console");
    byte_copy (file + sizeof (PREFIX) + 6, sizeof (NAME), NAME);
    r = openreadnclose (file, name, max);
    if (r <= 0)
        aa_strerr_diefu2sys (RC_FATAL_IO, "read ", file);
    /* last entry is the active one */
    skip = byte_rchr (name, r, ' ') + 1;
    if (skip > (size_t) r)
        skip = 0;

    for (;;)
    {
        const char *s = name + skip;
        size_t l = r - skip;

        byte_copy (file + sizeof (PREFIX) - 1, l, s);
        byte_copy (file + sizeof (PREFIX) - 2 + l, sizeof (NAME), NAME);
        r = openreadnclose (file, name, max);
        if (r <= 0)
        {
            if (errno == ENOENT)
            {
                aa_bs (AA_OUT, "/dev/");
                aa_bb_flush (AA_OUT, s, l);
                return RC_OK;
            }
            else
                aa_strerr_diefu2sys (RC_FATAL_IO, "read ", file);
        }
        skip = 0;
    }
}