|
| 1 | +/* date.c |
| 2 | +
|
| 3 | + Unix-like date for CP/M. |
| 4 | +
|
| 5 | + Get / set the system date & time. |
| 6 | +
|
| 7 | + Copyright (c) 2014-2016 Miguel I. Garcia Lopez. |
| 8 | +
|
| 9 | + This program is free software; you can redistribute it and/or modify it |
| 10 | + under the terms of the GNU General Public License as published by the |
| 11 | + Free Software Foundation; either version 2, or (at your option) any |
| 12 | + later version. |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + GNU General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU General Public License |
| 20 | + along with this program; if not, write to the Free Software |
| 21 | + Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | +
|
| 23 | + Usage: |
| 24 | +
|
| 25 | + date [[YYYY-MM-DD] hh:mm:ss] |
| 26 | + date [[YYYY/MM/DD] hh:mm:ss] |
| 27 | +
|
| 28 | + Examples: |
| 29 | +
|
| 30 | + date |
| 31 | + date 17:20:00 |
| 32 | + date 2015-04-08 17:20:00 |
| 33 | +
|
| 34 | + Changes: |
| 35 | +
|
| 36 | + 07 Apr 2014 : 1.00 : 1st version for SamaruX. |
| 37 | + 08 Apr 2014 : 1.01 : Add support to change the system date & time. |
| 38 | + 29 Nov 2015 : 1.01 : Added support for builtin / external. |
| 39 | + 01 Jun 2016 : 1.02 : Version for plain CP/M 3. |
| 40 | +*/ |
| 41 | + |
| 42 | +/* Standard MESCC library |
| 43 | + ---------------------- |
| 44 | +*/ |
| 45 | +#include <mescc.h> |
| 46 | + |
| 47 | +/* Standard MESCC libraries |
| 48 | + ------------------------ |
| 49 | +*/ |
| 50 | +#include <conio.h> |
| 51 | +#include <printf.h> |
| 52 | +#include <ctype.h> |
| 53 | +#include <clock.h> |
| 54 | +#include <cpm.h> |
| 55 | + |
| 56 | +/* Project defs. |
| 57 | + ------------- |
| 58 | +*/ |
| 59 | +#define APP_NAME "date" |
| 60 | +#define APP_VERSION "v1.02 / 01 Jun 2016" |
| 61 | +#define APP_COPYRGT "(c) 2014-2016 FloppySoftware" |
| 62 | +#define APP_USAGE "date [[YYYY-MM-DD] hh:mm:ss] or date [[YYYY/MM/DD] hh:mm:ss]" |
| 63 | + |
| 64 | +/* Project globals |
| 65 | + --------------- |
| 66 | +*/ |
| 67 | +unsigned char date_clk[CLOCK_LEN]; |
| 68 | +int date_dt[DATE_LEN]; |
| 69 | +char *date_days, *date_months; |
| 70 | + |
| 71 | +/* The program |
| 72 | + ----------- |
| 73 | +*/ |
| 74 | +main(argc, argv) |
| 75 | +int argc, argv[]; |
| 76 | +{ |
| 77 | + int cpmver; |
| 78 | + |
| 79 | + /* Check CP/M version */ |
| 80 | + |
| 81 | + cpmver = bdos_hl(BF_OSVER, NULL); |
| 82 | + |
| 83 | + if((cpmver & 0xF000) || ((cpmver & 0x00FF) < 0x30)) |
| 84 | + Error("Need CP/M 3"); |
| 85 | + |
| 86 | + /* Set up some values */ |
| 87 | + |
| 88 | + date_days = "SunMonTueWedThuFriSat"; |
| 89 | + date_months = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
| 90 | + |
| 91 | + /* Check arguments */ |
| 92 | + |
| 93 | + switch(argc) |
| 94 | + { |
| 95 | + case 1 : /* date */ |
| 96 | + DatePrint(); |
| 97 | + break; |
| 98 | + case 2 : /* date hh:mm:ss */ |
| 99 | + DateSet(NULL, argv[1]); |
| 100 | + break; |
| 101 | + case 3 : /* date YYYY-MM-DD hh:mm:ss */ |
| 102 | + DateSet(argv[1], argv[2]); |
| 103 | + break; |
| 104 | + default : |
| 105 | + Error("Bad arguments"); |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + /* Success */ |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +/* Print the system date & time |
| 115 | + ---------------------------- |
| 116 | +*/ |
| 117 | +DatePrint() |
| 118 | +{ |
| 119 | + char *p; |
| 120 | + |
| 121 | + /* Get system date & time */ |
| 122 | + |
| 123 | + getdate(getclock(date_clk), date_dt); |
| 124 | + |
| 125 | + /* Print system date & time as: |
| 126 | + Www Mmm dd hh:mm:ss yyyy |
| 127 | + Fri Jan 11 11:59:51 2008 */ |
| 128 | + |
| 129 | + /* Print dayweek name */ |
| 130 | + |
| 131 | + p = date_days + date_dt[6] * 3; putchar(*p++); putchar(*p++); putchar(*p); putchar(' '); |
| 132 | + |
| 133 | + /* Print month name */ |
| 134 | + |
| 135 | + p = date_months + date_dt[1] * 3; putchar(*p++); putchar(*p++); putchar(*p); |
| 136 | + |
| 137 | + /* Print the rest */ |
| 138 | + |
| 139 | + printf(" %02d %02d:%02d:%02d %04d\n", date_dt[2], date_dt[3], date_dt[4], date_dt[5], date_dt[0]); |
| 140 | +} |
| 141 | + |
| 142 | +/* Set the system date and time |
| 143 | + ---------------------------- |
| 144 | +*/ |
| 145 | +DateSet(dts, tms) |
| 146 | +char *dts, *tms; |
| 147 | +{ |
| 148 | + /* Get current system date & time */ |
| 149 | + |
| 150 | + getdate(getclock(date_clk), date_dt); |
| 151 | + |
| 152 | + /* Date as YYYY-MM-DD */ |
| 153 | + |
| 154 | + if(dts != NULL) |
| 155 | + { |
| 156 | + if(DateChk(dts, "####-##-##") || DateChk(dts, "####/##/##")) |
| 157 | + { |
| 158 | + date_dt[0] = DateVal(dts, 4); |
| 159 | + date_dt[1] = DateVal(dts + 5, 2) - 1; |
| 160 | + date_dt[2] = DateVal(dts + 8, 2); |
| 161 | + } |
| 162 | + else |
| 163 | + Error("Bad date"); |
| 164 | + } |
| 165 | + |
| 166 | + /* Time as HH:MM:SS */ |
| 167 | + |
| 168 | + if(DateChk(tms, "##:##:##")) |
| 169 | + { |
| 170 | + date_dt[3] = DateVal(tms, 2); |
| 171 | + date_dt[4] = DateVal(tms + 3, 2); |
| 172 | + date_dt[5] = DateVal(tms + 6, 2); |
| 173 | + } |
| 174 | + else |
| 175 | + Error("Bad time"); |
| 176 | + |
| 177 | + /* Change system date & time */ |
| 178 | + |
| 179 | + if(setdate(date_clk, date_dt) == NULL) |
| 180 | + Error("Bad date or time"); |
| 181 | + |
| 182 | + setclock(date_clk); |
| 183 | +} |
| 184 | + |
| 185 | +/* Check date field |
| 186 | + ---------------- |
| 187 | +*/ |
| 188 | +DateChk(str, mask) |
| 189 | +char *str, *mask; |
| 190 | +{ |
| 191 | + while(*str) |
| 192 | + { |
| 193 | + if(*mask == '#') |
| 194 | + { |
| 195 | + if(!isdigit(*str)) |
| 196 | + return 0; |
| 197 | + } |
| 198 | + else if(*mask != *str) |
| 199 | + return 0; |
| 200 | + |
| 201 | + ++str; ++mask; |
| 202 | + } |
| 203 | + |
| 204 | + if(*mask) |
| 205 | + return 0; |
| 206 | + |
| 207 | + return 1; |
| 208 | +} |
| 209 | + |
| 210 | +/* Get date field value |
| 211 | + -------------------- |
| 212 | +*/ |
| 213 | +DateVal(str, digits) |
| 214 | +char *str; int digits; |
| 215 | +{ |
| 216 | + int val; |
| 217 | + |
| 218 | + val = 0; |
| 219 | + |
| 220 | + while(digits--) |
| 221 | + val = val * 10 + (*str++ - '0'); |
| 222 | + |
| 223 | + return val; |
| 224 | +} |
| 225 | + |
| 226 | +/* Print error and exit |
| 227 | + -------------------- |
| 228 | +*/ |
| 229 | +Error(msg) |
| 230 | +char *msg; |
| 231 | +{ |
| 232 | + printf("%s: %s.\n", APP_NAME, msg); |
| 233 | + |
| 234 | + exit(-1); |
| 235 | +} |
| 236 | + |
| 237 | + |
0 commit comments