libnjb  2.2.7
settime.c
#include "common.h"
#include <string.h>
static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
{
char *cp, *bp;
while (1) {
fprintf(stdout, "%s> ", prompt);
if ( fgets(buffer, bufsz, stdin) == NULL ) {
if (ferror(stdin)) {
perror("fgets");
} else {
fprintf(stderr, "EOF on stdin\n");
}
return NULL;
}
cp= strrchr(buffer, '\n');
if ( cp != NULL ) *cp= '\0';
bp= buffer;
while ( bp != cp ) {
if ( *bp != ' ' && *bp != '\t' ) return bp;
bp++;
}
if (! required) return bp;
}
}
static void dumptime(njb_time_t *time)
{
if (time != NULL) {
switch(time->weekday) {
case 0:
printf("Sunday ");
break;
case 1:
printf("Monday ");
break;
case 2:
printf("Tuesday ");
break;
case 3:
printf("Wednesday ");
break;
case 4:
printf("Thursday ");
break;
case 5:
printf("Friday ");
break;
case 6:
printf("Saturday ");
}
/* OK this is the way we write dates
* in Sweden, you may change it if you
* don't like it!
*/
printf("%u-%.2u-%.2u ", time->year,
time->month, time->day);
printf("%.2u:%.2u:%.2u\n", time->hours,
time->minutes, time->seconds);
}
}
int main(int argc, char **argv)
{
njb_t njbs[NJB_MAX_DEVICES], *njb;
extern char *optarg;
int opt;
int n, debug;
njb_time_t *time;
char *pnum;
char num[80];
debug = 0;
while ((opt = getopt(argc, argv, "D:")) != -1) {
switch (opt) {
case 'D':
debug = atoi(optarg);
break;
default:
fprintf(stderr, "usage: settime [ -D debuglvl ]\n");
return 1;
}
}
if (debug)
NJB_Set_Debug(debug);
if (NJB_Discover(njbs, 0, &n) == -1) {
fprintf(stderr, "could not locate any jukeboxes\n");
return 1;
}
if (n == 0) {
fprintf(stderr, "no NJB devices found\n");
return 0;
}
njb = njbs;
if (NJB_Open(njb) == -1) {
NJB_Error_Dump(njb,stderr);
return 1;
}
if (NJB_Capture(njb) == -1) {
NJB_Error_Dump(njb,stderr);
return 1;
}
time = NJB_Get_Time(njb);
printf("The time on the jukebox is:\n");
dumptime(time);
printf("\nNew time (old values preserved if left blank):\n");
if ( (pnum= prompt("Year:", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->year= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Month (1-12):", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->month= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Day:", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->day= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Weekday (0-6):", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->weekday= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Hours (0-23):", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->hours= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Minutes (0-59):", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->minutes= (u_int16_t) strtoul(pnum, 0, 10);
}
if ( (pnum= prompt("Seconds (0-59):", num, 80, 0)) == NULL ) return 1;
if ( strlen(pnum) ) {
time->seconds= (u_int16_t) strtoul(pnum, 0, 10);
}
printf("The time on the jukebox is being set to:\n");
dumptime(time);
if (NJB_Set_Time(njb, time) == -1) {
NJB_Error_Dump(njb,stderr);
}
NJB_Close(njb);
return 0;
}
NJB_Discover
int NJB_Discover(njb_t *njbs, int limit, int *n)
Definition: procedure.c:108
njb_time_struct::month
int16_t month
Definition: libnjb.h:405
NJB_Get_Time
njb_time_t * NJB_Get_Time(njb_t *njb)
Definition: procedure.c:2079
NJB_Open
int NJB_Open(njb_t *njb)
Definition: procedure.c:130
njb_time_struct::weekday
int16_t weekday
Definition: libnjb.h:407
NJB_Close
void NJB_Close(njb_t *njb)
Definition: procedure.c:184
njb_struct
Definition: libnjb.h:182
njb_time_struct::seconds
int16_t seconds
Definition: libnjb.h:410
njb_time_struct::minutes
int16_t minutes
Definition: libnjb.h:409
njb_time_struct::day
int16_t day
Definition: libnjb.h:406
njb_time_struct::hours
int16_t hours
Definition: libnjb.h:408
NJB_Set_Time
int NJB_Set_Time(njb_t *njb, njb_time_t *time)
Definition: procedure.c:2113
njb_time_struct::year
int16_t year
Definition: libnjb.h:404
NJB_Capture
int NJB_Capture(njb_t *njb)
Definition: procedure.c:223
njb_time_struct
Definition: libnjb.h:403
NJB_MAX_DEVICES
#define NJB_MAX_DEVICES
Definition: libnjb.h:60
NJB_Destroy_Time
void NJB_Destroy_Time(njb_time_t *time)
Definition: procedure.c:2152
NJB_Set_Debug
void NJB_Set_Debug(int debug_flags)
Definition: procedure.c:2819
NJB_Error_Dump
void NJB_Error_Dump(njb_t *njb, FILE *fp)
Definition: njb_error.c:418
NJB_Release
int NJB_Release(njb_t *njb)
Definition: procedure.c:266