rdate.tcl

Go to the documentation of this file.
00001 /*  rdate.tcl - Copyright (C) 2003 Pat Thoyts <patthoyts@users.sourceforge.net>*/
00002 /* */
00003 /*  This is a sample implementation of the rdate(8) utility written using the*/
00004 /*  tcllib ntp time package.*/
00005 /* */
00006 /*  -------------------------------------------------------------------------*/
00007 /*  See the file "license.terms" for information on usage and redistribution*/
00008 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00009 /*  -------------------------------------------------------------------------*/
00010 /* */
00011 /*  $Id: rdate.tcl,v 1.3 2004/08/02 08:25:37 patthoyts Exp $*/
00012 
00013 package require time;                   /*  tcllib 1.4*/
00014 
00015 ret  usage () {
00016     set s {usage: rdate [-psa] [-utS] host
00017   -p    Do not set, just print the remote time.
00018   -s    Do not print the time. [NOT IMPLEMENTED]
00019   -a    Use the adjtime(2) call to gradually skew the local time to the
00020         remote time instead of just jumping. [NOT IMPLEMENTED]
00021   -u    Use UDP (default if available)
00022   -t    Use TCP
00023   -S    Use SNTP protocol (RFC 2030) (default is TIME, RFC 868)
00024 }
00025     return $s
00026 }
00027 
00028 ret  Error (type message) {
00029     puts stderr $message
00030     exit 1
00031 }
00032 
00033 ret  rdate (type args) {
00034     # process the command line options.
00035     array set opts {-p 0 -s 0 -a 0 -t 0 -u x -S 0}
00036     while {[string match -* [set option [lindex $args 0]]]} {
00037         switch -exact -- $option {
00038             -p { set opts(-p) 1 }
00039             -u { set opts(-t) 0 }
00040             -t { set opts(-t) 1 }
00041             -s { Error "not implemented: use rdate(8)" }
00042             -a { Error "not implemented: use rdate(8)" }
00043             -S { set opts(-S) 1 }
00044             -T { set opts(-S) 0 }
00045             -- { ::time::Pop args; break }
00046             default {
00047                 set err [join [lsort [array names opts -*]] ", "]
00048                 Error "bad option $option: must be $err"
00049             }
00050         }
00051         ::time::Pop args
00052     }
00053 
00054     # Check that we have a host to talk to.
00055     if {[llength $args] != 1} {
00056         Error [usage]
00057     }
00058     set host [lindex $args 0]
00059 
00060     # Construct the time command - optionally force the protocol to tcp
00061     set cmd ::time::gettime
00062     if {$opts(-S)} {
00063         set cmd ::time::getsntp
00064     }
00065     if {$opts(-t)} {
00066         lappend cmd -protocol tcp
00067     }
00068     lappend cmd $host
00069 
00070     # Perform the RFC 868 query (synchronously)
00071     set tok [eval $cmd]
00072 
00073     # Check for errors or extract the time in the unix epoch.
00074     set t 0
00075     if {[::time::status $tok] == "ok"} {
00076         set t [::time::unixtime $tok]
00077         ::time::cleanup $tok
00078     } else {
00079         set msg [::time::error $tok]
00080         ::time::cleanup $tok
00081         Error $msg 
00082     }
00083 
00084     # Display the time.
00085     if {$opts(-p)} {
00086         puts [clock format $t]
00087     }
00088 
00089     return 0
00090 }
00091 
00092 if {! $tcl_interactive} {
00093     eval rdate $argv
00094 }
00095 
00096 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1