#include #include enum { SECONDS = 1, MINUTES = 60, HOURS = 60 * 60 }; void print_usage(void) { printf("Usage: Alarm [units] [message]\n\n"); } int main(int argc, const char *argv[]) { long duration; long units = SECONDS; char const *message = NULL; CFStringRef alert_message = NULL; if (argc < 2) { printf("Too few arguments\n"); print_usage(); return EXIT_FAILURE; } duration = strtol(argv[1], NULL, 0); if (errno != 0) { printf("Duration must be a number\n"); print_usage(); return EXIT_FAILURE; } if (argc > 2) { if (strncasecmp("seconds", argv[2], 8) == 0) { units = SECONDS; } else if (strncasecmp("minutes", argv[2], 8) == 0) { units = MINUTES; } else if (strncasecmp("hours", argv[2], 6) == 0) { units = HOURS; } else { message = argv[2]; } if (argc == 4) { if (message != NULL) { printf("%s is not a valid unit", message); print_usage(); return EXIT_FAILURE; } message = argv[3]; } else { printf("Too many arguments\n"); print_usage(); return EXIT_FAILURE; } } sleep(duration * units); if (message == NULL) { alert_message = CFSTR("Time's Up"); } else { alert_message = CFStringCreateWithCString(kCFAllocatorDefault, message, kCFStringEncodingUTF8); } CFUserNotificationDisplayNotice(0.0, kCFUserNotificationPlainAlertLevel, NULL, NULL, NULL, CFSTR("Alarm"), alert_message, NULL); return EXIT_SUCCESS; }