summaryrefslogtreecommitdiff
path: root/write.c
blob: e275805823763409afd5d2d88c8ec5a69b26dc18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * UNG's Not GNU
 * 
 * Copyright (c) 2011, Jakob Kaivo <jakob@kaivo.net>
 * 
 * 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.
 *
 */

#define _XOPEN_SOURCE 700

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <utmpx.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	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");

	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);

	printf("\a\a");
	fflush(stdout);

	ssize_t nread = 0;
	size_t len = 0;
	char *buf = NULL;
	while ((nread = getline(&buf, &len, stdin)) != -1) {
		fwrite(buf, 1, nread, out);
	}

	fprintf(out, "EOT\n");
	fclose(out);

	return 0;
}