fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 145 lines 3.3 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: pcetime.c * 7 * Created: 2018-12-23 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> * 9 *****************************************************************************/ 10 11/***************************************************************************** 12 * This program is free software. You can redistribute it and / or modify it * 13 * under the terms of the GNU General Public License version 2 as published * 14 * by the Free Software Foundation. * 15 * * 16 * This program is distributed in the hope that it will be useful, but * 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 * Public License for more details. * 20 *****************************************************************************/ 21 22 23#include <stdio.h> 24#include <time.h> 25#include <sys/time.h> 26 27#include "config.h" 28#include "pce.h" 29 30 31const char *arg0 = NULL; 32 33static char par_print = 0; 34static char par_set = 0; 35 36 37static 38void print_help (void) 39{ 40 fputs ( 41 "pcetime: get the time from the host\n" 42 "\n" 43 "usage: pcetime [options]\n" 44 " -h Print help\n" 45 " -p Print the current host time\n" 46 " -s Set the system time to the current host time\n" 47 " -V Print version\n", 48 stdout 49 ); 50} 51 52static 53void print_version (void) 54{ 55 fputs ("pcetime version " PCEUTILS_VERSION_STR "\n", stdout); 56 fflush (stdout); 57} 58 59int main (int argc, char **argv) 60{ 61 int i; 62 const char *s; 63 time_t val; 64 65 arg0 = argv[0]; 66 67 if (pce_check()) { 68 fprintf (stderr, "%s: not running under pce\n", arg0); 69 return (1); 70 } 71 72 i = 1; 73 while (i < argc) { 74 if (argv[i][0] == '-') { 75 s = argv[i] + 1; 76 while (*s != 0) { 77 if (*s == 'h') { 78 print_help(); 79 return (0); 80 } 81 else if (*s == 'p') { 82 par_print = 1; 83 } 84 else if (*s == 's') { 85 par_set = 1; 86 } 87 else if (*s == 'V') { 88 print_version(); 89 return (0); 90 } 91 else { 92 fprintf (stderr, 93 "%s: unknown option (%s)\n", 94 arg0, argv[i] 95 ); 96 97 return (1); 98 } 99 100 s += 1; 101 } 102 } 103 else { 104 fprintf (stderr, "%s: unknown option (%s)\n", 105 arg0, argv[i] 106 ); 107 108 return (1); 109 } 110 111 i += 1; 112 } 113 114 if ((par_print == 0) && (par_set == 0)) { 115 print_help(); 116 return (0); 117 } 118 119 val = (time_t) pce_get_time_unix(); 120 121 if (par_print) { 122 struct tm *tm; 123 124 tm = gmtime (&val); 125 126 printf ("%04d-%02d-%02d %02d:%02d:%02d UTC\n", 127 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 128 tm->tm_hour, tm->tm_min, tm->tm_sec 129 ); 130 } 131 132 if (par_set) { 133 struct timeval tv; 134 135 tv.tv_sec = val; 136 tv.tv_usec = 0; 137 138 if (settimeofday (&tv, NULL)) { 139 fprintf (stderr, "%s: error setting time\n", arg0); 140 return (1); 141 } 142 } 143 144 return (0); 145}