c
This is an old revision of the document!
getopt
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #define die(...) { fprintf(stderr, __VA_ARGS__); exit(1); } #define pdie(s) { perror(s); exit(1); } int main (int argc, char** argv) { int o; int opt_a; char* opt_c; while ((o = getopt (argc, argv, "abc:")) != -1) { switch (o) { case 'a': opt_a = 1; printf("a=yes\n"); break; case 'c': opt_c = optarg; printf("c='%s'\n",opt_c); break; case '?': exit(1); } } argc -= optind; argv += optind; // throw away option arguments } char* timestamp() { static char buf[64]; struct tm t; time_t now = time(NULL); localtime_r(&now,&t); sprintf(buf,"%04d%02d%02d-%02d%02d%02d",t.tm_year+1900,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec); return buf; } /** * Returns the number of seconds since the epoch as a double-precision * float with microsecond resolution. * * @return Time since first call to this function */ double getCurrentTime() { double t; struct timeval tstr; if (gettimeofday(&tstr, NULL) < 0) { fprintf(stderr,"gettimeofday failed\n"); exit(-1); } t=((tstr.tv_sec + tstr.tv_usec/(double)1000000)); return t; } long long get_usec() { struct timeval tv; gettimeofday(&tv,NULL); return tv.tv_sec*1000000+tv.tv_usec; }
c.1250181972.txt.gz · Last modified: 2009/08/13 09:46 by tkbletsc