This is the main of tty command:
main (int argc, char **argv)
{
char *tty;
int optc;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
close_stdout_set_status (3);
atexit (close_stdout);
silent = 0;
while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
{
switch (optc)
{
case 0:
break;
case 's':
silent = 1;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (2);
}
}
if (optind != argc)
error (0, 0, _("ignoring all arguments"));
tty = ttyname (0);
if (!silent)
{
if (tty)
puts (tty);
else
puts (_("not a tty"));
}
exit (isatty (0) ? 0 : 1);
}
Can someone briefly describe how it works?