From 41aba1de283c51bc1c2bd178718887e640fe76fc Mon Sep 17 00:00:00 2001 From: Ellen Marie Dash Date: Wed, 18 Oct 2023 22:28:36 -0400 Subject: [PATCH] Add skeleton of "who" utilitiy. --- src/who.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/who.c diff --git a/src/who.c b/src/who.c new file mode 100644 index 0000000..1c07e77 --- /dev/null +++ b/src/who.c @@ -0,0 +1,91 @@ +/** + * NAME + * ==== + * who - display who is on the system + * + * SYNOPSIS + * ======== + * who [-mTu] + * + * DESCRIPTION + * =========== + * Print information about users that are on the system. + * + * -m Print information about only the current terminal. + * -T Show the state of each terminal. + * -u Include the "idle time" for each displayed user. + * + * --help Print help text and exit. + * --version Print version information and exit. + */ + + +#include +#include "boreutils.h" + +// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/who.html + +int main(int argc, char **argv) { + if (has_arg(argc, argv, "-h") || has_arg(argc, argv, "--help")) { + puts("Usage: who [-mTu]"); + puts("Print information about users that are on the system."); + return 1; + } + + int dash_m = 0; + int dash_T = 0; + int dash_u = 0; + + for (int i = 1; i < argc; i++) { + char *arg = argv[i]; + if (arg[0] != '-') { + bu_invalid_argument(argv[0], argv[i]); + return 1; + } + + arg++; + for (; *arg; arg++) { + char flag = *arg; + + if (flag == 'm') { dash_m = 1; } + else if (flag == 'T') { dash_T = 1; } + else if (flag == 'u') { dash_u = 1; } + else { + char tmp[3] = "-X"; + tmp[1] = *arg; + bu_invalid_argument(argv[0], tmp); + return 1; + } + } + } + + + char *user_name = "USERNAME"; + char terminal_state = '?'; + char *terminal_name = "TERMNAME"; + char *time_of_login = "TIME"; + char *idle_time; + + if (dash_T) { + // Actual terminal state. + // + allows write access to other users + // - denies write access to other users + // ? cannot be determined + // not associated with a terminal + // TODO: Actually set terminal_state. + } + + if (dash_u) { + idle_time = " IDLE"; + } else { + idle_time = ""; + } + + if (dash_T) { + printf("%s %c %s %s%s\n", user_name, terminal_state, terminal_name, time_of_login, idle_time); + } else { + printf("%s %s %s%s\n", user_name, terminal_name, time_of_login, idle_time); + } + + return 0; +}