#include <stdio.h>#include <getopt.h>#include <time.h>void printhelp(char *callname) { printf("bintime - show the current time of day in binary format\n"); printf("Usage: %s [-dsvh]\n\n", callname); printf(" -d, --decimal Show decimal numbers along with the binary ones\n"); printf(" -H, --hex Show hexadecimal numbers along with the binary ones\n"); printf(" -s, --seconds Show seconds along with hours and minutes\n"); printf(" -v, --version Print the version of bintime and exit\n"); printf(" -h, --help Print this help and exit\n");}void dec2binstr(int in, char *out, int outlen) {/* See http://en.wikipedia.org/wiki/Binary_numeral_system#Decimal for the * * principle of the conversion algorithm used here. */ char *outend = out + outlen; /* Initialize a pointer to do the */ *outend = '\0'; /* string from behind and insert */ /* the ASCII 0 at the end */ while (in) { /* Fill wih binary digits from LSB to MSB */ outend--; switch (in % 2) { case 0: *outend = '0'; break; case 1: *outend = '1'; break; } in = in / 2; } while (outend > out) { /* Fill with '0's until beginning of out */ outend--; *outend = '0'; }}void nibblestr2hexchar(char *nibble, char *hexchar) { switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': *hexchar = '0'; break; case '1': *hexchar = '1'; break; } break; case '1': nibble++; switch (*nibble) { case '0': *hexchar = '2'; break; case '1': *hexchar = '3'; break; } break; } break; case '1': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': *hexchar = '4'; break; case '1': *hexchar = '5'; break; } break; case '1': nibble++; switch (*nibble) { case '0': *hexchar = '6'; break; case '1': *hexchar = '7'; break; } break; } break; } break; case '1': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': *hexchar = '8'; break; case '1': *hexchar = '9'; break; } break; case '1': nibble++; switch (*nibble) { case '0': *hexchar = 'A'; break; case '1': *hexchar = 'B'; break; } break; } break; case '1': nibble++; switch (*nibble) { case '0': nibble++; switch (*nibble) { case '0': *hexchar = 'C'; break; case '1': *hexchar = 'D'; break; } break; case '1': nibble++; switch (*nibble) { case '0': *hexchar = 'E'; break; case '1': *hexchar = 'F'; break; } break; } break; } break; }}void binstr2hexstr(char *binstr, int inlen, char *out, int outlen) { char nibble[5]; char *nibbleend; char *binstrend = binstr + (inlen - 1); /* We don't wanna point to '\0' */ char *outend = out + outlen; *outend = '\0'; outend--; while (outend >= out) { strcpy(nibble, "0000"); nibbleend = nibble + 3; while (nibbleend >= nibble) { if (binstrend >= binstr) { *nibbleend = *binstrend; binstrend--; } nibbleend--; } nibblestr2hexchar(nibble, outend); /* Now we've got us a cute little nibble for the conversion function! */ outend--; }}int main(int argc, char *argv[]) {/* --- Declarations for getopt_long.h functionality ------------------- */ int opt_seconds = 0; int opt_decimal = 0; int opt_hex = 0; struct option longoptions[] = { {"decimal", no_argument, 0, 'd'}, {"hex", no_argument, 0, 'H'}, {"seconds", no_argument, 0, 's'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'} }; int longoptions_i = 0; signed char c;/* --- Declarations for time.h functionality ---------------------------*/ time_t curtime; struct tm *timedata;/* --- My own humble declarations --------------------------------------*/ int twelve_hour; char bin_hour[5]; /* only four binary digits for the hour */ char bin_min[7]; char bin_sec[7]; char hex_hour[2]; /* a simple char would do, but complicate things */ char hex_min[3]; char hex_sec[3];/* For a better idea of how getopt_long is used, you'd better check out * * http://www.gnu.org/software/libc/manual/html_node/Getopt.html along * * with my code here. They do it differently and more intelligible IMHO.*/ while(1) { if ((c = getopt_long(argc, argv, "dHsvh", longoptions, &longoptions_i)) == -1) { break; } switch (c) { case 0: break; case 'd': opt_decimal = 1; break; case 'H': opt_hex = 1; break; case 's': opt_seconds = 1; break; case 'v': printf("bintime 0.0.2beta\n"); return 0; break; case 'h': printhelp(argv[0]); return 0; break; case '?': default: printhelp(argv[0]); return 1; break; } } curtime = time(0); /* Getting system time and making it 12h*/ timedata = localtime(&curtime); twelve_hour = timedata->tm_hour; if (twelve_hour > 12) { twelve_hour -= 12; } dec2binstr(twelve_hour, bin_hour, 4); dec2binstr(timedata->tm_min, bin_min, 6); if (opt_seconds) { dec2binstr(timedata->tm_sec, bin_sec, 6); } /* I thought a lot about how to do this and I've decided to rather go * * with a bunch of printf(); lines than to do some kind of smartass * * printf("%s\n %s", bin_min, dec_min) black voodoo thing, because * * that would involve initializing an empty dec_min string and then * * filling it if needed. And since this is C and not Perl, that would * * be Shooting Myself In The Foot(tm) big time. */ if (opt_hex) { binstr2hexstr(bin_hour, 4, hex_hour, 1); printf("0%s ", hex_hour); } if (opt_decimal) { printf("%02i ", timedata->tm_hour); } printf(" %s\n", bin_hour); if (opt_hex) { binstr2hexstr(bin_min, 6, hex_min, 2); printf("%s ", hex_min); } if (opt_decimal) { printf("%02i ", timedata->tm_min); } printf("%s\n", bin_min); if (opt_seconds) { if (opt_hex) { binstr2hexstr(bin_sec, 6, hex_sec, 2); printf("%s ", hex_sec); } if (opt_decimal) { printf("%02i ", timedata->tm_sec); } printf("%s\n", bin_sec); } return 0;}