Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. /*
  2. * Project: binclock
  3. * Filename: main.c
  4. *
  5. * Description: simple text-only binary clock
  6. *
  7. * Version: 1.0
  8. * Created: 2012-05-23 16:51
  9. * Compiler: gcc
  10. *
  11. * Author: Ian D Brunton (ib), iandbrunton@gmail.com
  12. *
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <time.h>
  19.  
  20. char bc0;
  21. char bc1;
  22.  
  23. void byte_to_binary (char*, int);
  24. void print_version ();
  25. void print_usage ();
  26.  
  27. int
  28. main (int argc, char *argv[])
  29. {
  30. bc0 = 'v';
  31. bc1 = '^';
  32. char separator = ':';
  33.  
  34. int once = 0;
  35.  
  36. time_t time_raw;
  37. struct tm *time_struct;
  38.  
  39. //char seconds [7];
  40. char minutes [7];
  41. char hours [7];
  42.  
  43. /* get command-line options */
  44. int i;
  45. for (i = 1; i < argc; i++) {
  46. if (argv[i][0] == '-' && argv[i][1] == '0') {
  47. bc0 = argv[++i][0];
  48. } else if (argv[i][0] == '-' && argv[i][1] == '1') {
  49. bc1 = argv[++i][0];
  50. } else if (argv[i][0] == '-' && argv[i][1] == 's') {
  51. separator = argv[++i][0];
  52. } else if (argv[i][0] == '-' && argv[i][1] == 'o') {
  53. once = 1;
  54. } else if (argv[i][0] == '-' && argv[i][1] == 'v') {
  55. print_version ();
  56. return EXIT_SUCCESS;
  57. } else if (argv[i][0] == '-' && argv[i][1] == '?') {
  58. print_usage ();
  59. return EXIT_FAILURE;
  60. }
  61. }
  62.  
  63. int count = 0;
  64. while (count < 1) {
  65. time (&time_raw);
  66. time_struct = localtime (&time_raw);
  67.  
  68. // byte_to_binary (seconds, time_struct->tm_sec);
  69. byte_to_binary (minutes, time_struct->tm_min);
  70. byte_to_binary (hours, time_struct->tm_hour);
  71.  
  72. printf ("%s", hours);
  73. printf ("%c", separator);
  74. printf ("%s", minutes);
  75. // printf ("%c", separator);
  76. // printf ("%s", seconds);
  77. printf ("\n");
  78.  
  79. count += once;
  80. if (count == 0) {
  81. sleep (1);
  82. }
  83. }
  84.  
  85. return EXIT_SUCCESS;
  86. }
  87.  
  88. /* This function modified from
  89. http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
  90. */
  91. void
  92. byte_to_binary (char *b, int x)
  93. {
  94. b[0] = '\0';
  95.  
  96. int z;
  97. char *p = b;
  98.  
  99. for (z = 64; z > 0; z >>= 1) {
  100. *p++ = (x & z) ? bc1 : bc0;
  101. }
  102. }
  103.  
  104. void
  105. print_version ()
  106. {
  107. printf ("%s %s - a binary clock\n", APPNAME, VERSION);
  108. }
  109.  
  110. void
  111. print_usage ()
  112. {
  113. printf ("%s [-0 char] [-1 char] [-s char] [-o]\n", APPNAME);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement