#include #include #include #include #include #include #include #include #define CLOCK_TICK_RATE 1193180 #define NUMNOTES 12 unsigned long notes[NUMNOTES] = { 26163, 27718, 29366, 31113, 32963, 34923, 37000, 39200, 41530, 44000, 46616, 49388 }; int default_note = 10; // 1-based #define DEFAULT_PARAMS_LEN 3 static const char *default_params[DEFAULT_PARAMS_LEN] = { "beep", "-10", "10" }; static int fd = -1; void exiting(int signum) { int tone = 0; if (fd >= 0) ioctl(fd, KIOCSOUND, tone); } int check_sound_capabilities(const char *filename, int *fd) { int tone; int lfd; if (strcmp(filename, "-")) { lfd = open(filename, O_RDONLY); if (lfd < 0) { perror(filename); return 0; } } else { lfd = 0; } tone = 0; if (ioctl(lfd, KIOCSOUND, tone)) { close(lfd); perror(filename); return 0; } if (fd != NULL) { *fd = lfd; } return 1; } int main(int argc, char *argv[]) { int i; unsigned int tone; int hertz, length; int param_len; const char **params; if (!check_sound_capabilities("/dev/pcspeaker", &fd) && !check_sound_capabilities("-", &fd) && !check_sound_capabilities("/dev/tty0", &fd)) { exit(EXIT_FAILURE); } if (argc <= 1) { params = default_params; param_len = DEFAULT_PARAMS_LEN; } else { params = (const char **)argv; param_len = argc; } signal(SIGHUP, exiting); signal(SIGINT, exiting); signal(SIGQUIT, exiting); signal(SIGPIPE, exiting); signal(SIGTERM, exiting); signal(SIGTSTP, exiting); for (i = 1; i < param_len; i += 2) { hertz = strtol(params[i], NULL, 0); if (hertz < 0) { if (hertz < -NUMNOTES) hertz = -default_note; hertz = notes[-hertz - 1]; } if (i + 1 < param_len) length = strtol(params[i + 1], NULL, 0); else length = 10; // printf("hertz %d\n", hertz); fflush(stdout); tone = (CLOCK_TICK_RATE * 100) / hertz; ioctl(fd, KIOCSOUND, tone); usleep(((unsigned long)length) * 10000); tone = 0; ioctl(fd, KIOCSOUND, tone); } close(fd); fd = -1; exit(EXIT_SUCCESS); return 0; }