aboutsummaryrefslogtreecommitdiff
path: root/addts.c
blob: 07fae4febaa9b3555f60f243633e00109a700faf (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>

static char default_fmt[] = "%Y-%m-%d %H:%M:%S: ";

enum mode {
	PREFIX,
	SUFFIX,
	NMODE
};

enum state {
	SINI, /* initial */
	SEOL, /* end of line */
	SLIN, /* Midle of line */
	STIM, /* Time to insert timestamp */
	NSTATE
};

enum alpha {
	ACHR,
	AEOL,
	NALPHA
};

int prefix_trans[NSTATE][NALPHA] = {
	/* ACHR     AEOL */
	{  STIM,    SEOL }, /* SINI */
	{  STIM,    SEOL }, /* SEOL */
	{  SLIN,    SEOL }, /* SLIN */
	{  SLIN,    SEOL }  /* STIM */
};

int suffix_trans[NSTATE][NALPHA] = {
	/* ACHR     AEOL */
	{  SLIN,    SEOL }, /* SINI */
	{  SLIN,    SEOL }, /* SEOL */
	{  SLIN,    STIM }, /* SLIN */
	{  SLIN,    SEOL }  /* STIM */
};

static inline int
alpha(int c)
{
	return c == '\n' ? AEOL : ACHR;
}

int
main(int argc, char **argv)
{
	int c;
	char buf[512];
	int utc_opt = 0;
	int append_opt = 0;
	int ws_opt = 0;
	int end_opt = 0;
	char *p;
	char *fmt = NULL;
	FILE *fp;

	enum state state = SINI;
	int (*trans)[NALPHA] = prefix_trans;
	
	while ((c = getopt(argc, argv, "?af:suw:")) != EOF) {
		switch (c) {
		case 'a':
			append_opt = 1;
			break;
		case 'f':
			fmt = optarg;
			break;
		case 's':
			trans = suffix_trans;
			break;
		case 'u':
			utc_opt = 1;
			break;
		case 'w':
			ws_opt = optarg[0];
			break;
		default:
			if (optopt == 0) {
				printf("usage: %s [-asu] [-f FMT] [OUT-FILE]\n",
				       argv[0]);
				return 0;
			}
			return 1;
		}
	}

	if (fmt) {
		if (ws_opt) {
			for (p = fmt; *p; p++)
				if (*p == ws_opt)
					*p = ' ';
		}
	} else
		fmt = default_fmt;
	
	argc -= optind;
	argv += optind;
	
	switch (argc) {
	case 0:
		fp = stdout;
		break;
	case 1:
		fp = fopen(argv[0], append_opt ? "a" : "w");
		if (!fp) {
			perror(argv[0]);
			return 1;
		}
		break;
	default:
		fprintf(stderr, "%s: too many arguments\n", argv[0]);
		return 1;
	}
	setvbuf(fp, NULL, _IOLBF, 0);

	while ((c = getchar()) != EOF) {
		state = trans[state][alpha(c)];
		if (state == STIM) {
			struct timeval tv;
			struct tm *tm;
			size_t sz;
			char *start, *p;
			
			gettimeofday(&tv, NULL);
			tm = (utc_opt ? gmtime : localtime)(&tv.tv_sec);
			sz = strftime(buf, sizeof(buf), fmt, tm);
			if (sz == 0 || sz == sizeof(buf)) {
				strcpy(buf, "[OVERFLOW]: ");
				sz = strlen(buf);
			}
			start = buf;
			while ((p = strstr(start, "%@")) != NULL) {
				fwrite(start, 1, p - start, fp);
				fprintf(fp, "%06d", tv.tv_usec);
				sz -= p - start + 2;
				start = p + 2;
			}
			fwrite(start, 1, sz, fp);
		}
		fputc(c, fp);
	}
	fclose(fp);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.