settime.c

00001 #include "common.h"
00002 #include <string.h>
00003 
00004 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
00005 {
00006   char *cp, *bp;
00007   
00008   while (1) {
00009     fprintf(stdout, "%s> ", prompt);
00010     if ( fgets(buffer, bufsz, stdin) == NULL ) {
00011       if (ferror(stdin)) {
00012         perror("fgets");
00013       } else {
00014         fprintf(stderr, "EOF on stdin\n");
00015       }
00016       return NULL;
00017     }
00018     
00019     cp= strrchr(buffer, '\n');
00020     if ( cp != NULL ) *cp= '\0';
00021     
00022     bp= buffer;
00023     while ( bp != cp ) {
00024       if ( *bp != ' ' && *bp != '\t' ) return bp;
00025       bp++;
00026     }
00027     
00028     if (! required) return bp;
00029   }
00030 }
00031 
00032 static void dumptime(njb_time_t *time)
00033 {
00034   if (time != NULL) {
00035     switch(time->weekday) {
00036     case 0:
00037       printf("Sunday ");
00038       break;
00039     case 1:
00040       printf("Monday ");
00041       break;
00042     case 2:
00043       printf("Tuesday ");
00044       break;
00045     case 3:
00046       printf("Wednesday ");
00047       break;
00048     case 4:
00049       printf("Thursday ");
00050       break;
00051     case 5:
00052       printf("Friday ");
00053       break;
00054     case 6:
00055       printf("Saturday ");
00056     }
00057     /* OK this is the way we write dates
00058      * in Sweden, you may change it if you
00059      * don't like it!
00060      */
00061     printf("%u-%.2u-%.2u ", time->year, 
00062            time->month, time->day);
00063     printf("%.2u:%.2u:%.2u\n", time->hours, 
00064            time->minutes, time->seconds);
00065   }
00066 }
00067 
00068 
00069 int main(int argc, char **argv)
00070 {
00071   njb_t njbs[NJB_MAX_DEVICES], *njb;
00072   extern char *optarg;
00073   int opt;
00074   int n, debug;
00075   njb_time_t *time;
00076   char *pnum;
00077   char num[80];
00078   
00079   debug = 0;
00080   while ((opt = getopt(argc, argv, "D:")) != -1) {
00081     switch (opt) {
00082     case 'D':
00083       debug = atoi(optarg);
00084       break;
00085     default:
00086       fprintf(stderr, "usage: settime [ -D debuglvl ]\n");
00087       return 1;
00088     }
00089   }
00090   
00091   if (debug)
00092     NJB_Set_Debug(debug);
00093 
00094   if (NJB_Discover(njbs, 0, &n) == -1) {
00095     fprintf(stderr, "could not locate any jukeboxes\n");
00096     return 1;
00097   } 
00098   
00099   if (n == 0) {
00100     fprintf(stderr, "no NJB devices found\n");
00101     return 0;
00102   }
00103   
00104   njb = njbs;
00105   
00106   if (NJB_Open(njb) == -1) {
00107     NJB_Error_Dump(njb,stderr);
00108     return 1;
00109   }
00110 
00111   if (NJB_Capture(njb) == -1) {
00112     NJB_Error_Dump(njb,stderr);
00113     return 1;
00114   }
00115   
00116   time = NJB_Get_Time(njb);
00117   
00118   printf("The time on the jukebox is:\n");
00119   dumptime(time);
00120   
00121   printf("\nNew time (old values preserved if left blank):\n");
00122   
00123   if ( (pnum= prompt("Year:", num, 80, 0)) == NULL ) return 1;
00124   if ( strlen(pnum) ) {
00125     time->year= (u_int16_t) strtoul(pnum, 0, 10);
00126   }
00127   
00128   if ( (pnum= prompt("Month (1-12):", num, 80, 0)) == NULL ) return 1;
00129   if ( strlen(pnum) ) {
00130     time->month= (u_int16_t) strtoul(pnum, 0, 10);
00131   }
00132   
00133   if ( (pnum= prompt("Day:", num, 80, 0)) == NULL ) return 1;
00134   if ( strlen(pnum) ) {
00135     time->day= (u_int16_t) strtoul(pnum, 0, 10);
00136   }
00137   
00138   if ( (pnum= prompt("Weekday (0-6):", num, 80, 0)) == NULL ) return 1;
00139   if ( strlen(pnum) ) {
00140     time->weekday= (u_int16_t) strtoul(pnum, 0, 10);
00141   }
00142   
00143   if ( (pnum= prompt("Hours (0-23):", num, 80, 0)) == NULL ) return 1;
00144   if ( strlen(pnum) ) {
00145     time->hours= (u_int16_t) strtoul(pnum, 0, 10);
00146   }
00147   
00148   if ( (pnum= prompt("Minutes (0-59):", num, 80, 0)) == NULL ) return 1;
00149   if ( strlen(pnum) ) {
00150     time->minutes= (u_int16_t) strtoul(pnum, 0, 10);
00151   }
00152   
00153   if ( (pnum= prompt("Seconds (0-59):", num, 80, 0)) == NULL ) return 1;
00154   if ( strlen(pnum) ) {
00155     time->seconds= (u_int16_t) strtoul(pnum, 0, 10);
00156   }
00157   
00158   printf("The time on the jukebox is being set to:\n");
00159   dumptime(time);
00160   
00161   if (NJB_Set_Time(njb, time) == -1) {
00162     NJB_Error_Dump(njb,stderr);
00163   }
00164   
00165   NJB_Destroy_Time(time);
00166   
00167   NJB_Release(njb);
00168   
00169   NJB_Close(njb);
00170   return 0;
00171 }

Generated on Mon Sep 11 00:52:12 2006 for libnjb by  doxygen 1.4.7