/* * Stopwatch * Copyright (C) 2013 Adam Williams * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include #include #include int main() { struct timeval current_time; struct timeval start_time; struct timeval select_time; while(1) { printf("Press Enter to stop.\n"); fd_set fds; gettimeofday(&start_time, 0); while(1) { usleep(100000); gettimeofday(¤t_time, 0); int64_t useconds = (current_time.tv_sec * 1000000 + current_time.tv_usec) - (start_time.tv_sec * 1000000 + start_time.tv_usec); int hours = useconds / ((int64_t)60 * 60 * 1000000); int minutes = (useconds / ((int64_t)60 * 1000000)) % 60; int seconds = (useconds / (int64_t)1000000) % 60; int hseconds = (useconds / 10000) % 100; printf("%02d:%02d:%02d:%02d\r", hours, minutes, seconds, hseconds); fflush(stdout); FD_ZERO(&fds); FD_SET(0, &fds); select_time.tv_sec = 0; select_time.tv_usec = 0; int got_stop = select(1, &fds, 0, 0, &select_time); if(got_stop) { printf("Got stop. \n"); getc(stdin); break; } } printf("Press Enter to start.\n"); getc(stdin); } }