From 585bea00605eeeb82a776f6e98534a57410e4e75 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 13 Mar 2019 22:02:43 -0400 Subject: migrate to gitlab --- write.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 write.c diff --git a/write.c b/write.c new file mode 100644 index 0000000..dafc104 --- /dev/null +++ b/write.c @@ -0,0 +1,78 @@ +/* + * UNG's Not GNU + * + * Copyright (c) 2011, Jakob Kaivo + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#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) +{ + char *user, *tty; + char *buf; + char line[PATH_MAX]; + size_t nread, len = 0; + struct utmpx *ut; + FILE *out; + + if (argc < 2 || argc > 3) { + return 1; + } else if (argc == 2) { + user = argv[1]; + tty = NULL; + } else { + user = argv[1]; + tty = argv[2]; + } + + 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(); + + if (ut == NULL) + return 1; + + 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); + + return 0; +} -- cgit v1.2.1