From cd4e520cc1b852b25f9164ae73c1f0522aba5df1 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 15 Mar 2019 11:16:53 -0400 Subject: update to current coding standards, fill in missing bits --- write.c | 107 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/write.c b/write.c index dafc104..e275805 100644 --- a/write.c +++ b/write.c @@ -17,62 +17,79 @@ * */ +#define _XOPEN_SOURCE 700 + +#include #include #include #include #include #include +#include #include -const char *write_desc = "write to another user"; -const char *write_inv = "write user_name [terminal]"; - -int -main(int argc, char **argv) +int main(int argc, char *argv[]) { - char *user, *tty; - char *buf; - char line[PATH_MAX]; - size_t nread, len = 0; - struct utmpx *ut; - FILE *out; + char *user = NULL; + char *terminal = NULL; + + setlocale(LC_ALL, ""); + + int c; + while ((c = getopt(argc, argv, "")) != -1) { + return 1; + } + + if (argc == optind + 1) { + user = argv[optind]; + } else if (argc == optind + 2) { + user = argv[optind]; + terminal = argv[optind + 1]; + } else { + return 1; + } + + struct utmpx *ut = NULL; + setutxent(); + while ((ut = getutxent()) != NULL) { + if ((ut->ut_type == USER_PROCESS || ut->ut_type == LOGIN_PROCESS) && !strcmp(user, ut->ut_user)) { + if (terminal == NULL || !strcmp(terminal, ut->ut_line)) { + break; + } + } + } + endutxent(); + + if (ut == NULL) { + return 1; + } + + char line[PATH_MAX]; + sprintf(line, "/dev/%s", ut->ut_line); + FILE *out = fopen(line, "w"); - if (argc < 2 || argc > 3) { - return 1; - } else if (argc == 2) { - user = argv[1]; - tty = NULL; - } else { - user = argv[1]; - tty = argv[2]; - } + char date[PATH_MAX]; + time_t now = time(NULL); + struct tm *tm = localtime(&now); + strftime(date, sizeof(date), "%c", tm); + terminal = ttyname(STDIN_FILENO); + if (!strncmp(terminal, "/dev/", 5)) { + terminal += 5; + } + fprintf(out, "Message from %s (%s) [%s]...\n", getlogin(), terminal, date); - setutxent(); - while ((ut = getutxent()) != NULL) { - if ((ut->ut_type == USER_PROCESS || ut->ut_type == LOGIN_PROCESS) && !strcmp (user, ut->ut_user)) { - if (tty == NULL) - break; - else if (!strcmp (tty, ut->ut_line)) - break; - } - } - endutxent(); + printf("\a\a"); + fflush(stdout); - if (ut == NULL) - return 1; + ssize_t nread = 0; + size_t len = 0; + char *buf = NULL; + while ((nread = getline(&buf, &len, stdin)) != -1) { + fwrite(buf, 1, nread, out); + } - strcat (line, "/dev/"); - strcat (line, ut->ut_line); - out = fopen (line, "w"); - fprintf (out, "Message from %s (%s) [%s]...\n", getlogin(), ttyname(fileno(stdin)), "FIXME"); - while (!feof(stdin)) { - if ((nread = getline (&buf, &len, stdin)) != -1) - fwrite (buf, sizeof(char), nread, out); - free (buf); - len = 0; - } - fprintf (out, "EOT\n"); - fclose (out); + fprintf(out, "EOT\n"); + fclose(out); - return 0; + return 0; } -- cgit v1.2.1