Sunday 8 October 2017

/usr/bin/cal with added hex output option. Generate Hex calendar ( unix src code )



Download
https://opensource.apple.com/source/misc_cmds/misc_cmds-9/cal/cal.c
apply this diff - compile and go. Use -x option to get hex output.


$ diff cal.c  cal_hex.c | sed 's/>/\>/;s/</\</'
36a37
>  * Hex version
138a140
> int xflag;
149,150c151,152
< yflag = year = 0;
< while ((ch = getopt(argc, argv, "jy")) != -1)
---
> yflag = year = xflag = 0;
> while ((ch = getopt(argc, argv, "jyx")) != -1)
157a160,162
> case 'x':
> xflag = 1;
> break;
210,211c215,219
< len = snprintf(lineout, sizeof(lineout), "%s %d",
<     month_names[month - 1], year);
---
> if (xflag) { 
> len = snprintf(lineout, sizeof(lineout), "%s 0x%x", month_names[month - 1], year);
> } else {
> len = snprintf(lineout, sizeof(lineout), "%s %d", month_names[month - 1], year);
> }
233c241,245
< (void)snprintf(lineout, sizeof(lineout), "%d", year);
---
> if (xflag) { 
> (void)snprintf(lineout, sizeof(lineout), "0x%x", year); 
> } else {
> (void)snprintf(lineout, sizeof(lineout), "%d", year);
> }
268c280,284
< (void)snprintf(lineout, sizeof(lineout), "%d", year);
---
> if (xflag) { 
> (void)snprintf(lineout, sizeof(lineout), "0x%x", year); 
> } else {
> (void)snprintf(lineout, sizeof(lineout), "%d", year);
> }
375a392,401
> static char *hday[] = {
>                 "",
>                 " 1", " 2", " 3", " 4", " 5", " 6", " 7",
>                 " 8", " 9", " A", " B", " C", " D", " E",
>                 " F" ,"10", "11", "12", "13", "14", "15", 
> "16", "17", "18", "19", "1A", "1B", "1C", 
> "1D", "1E", "1F",
>         };
395a422,425
> if ( xflag ) {
> *p++ = hday[day][0];
> *p++ = hday[day][1];
> } else {
397a428
> }
434c465
< (void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
---

> (void)fprintf(stderr, "usage: cal [-jyx] [[month] year]\n");

No comments: