OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / ntpclient / adjtimex.c
1 /*
2  * adjtimex_1.c - read, and possibly modify, the Linux kernel `timex' variables.
3  *
4  * Originally written: October 1997
5  * Last hack: May 2003
6  * Copyright 1997, 2000, 2003 Larry Doolittle <larry@doolittle.boa.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License (Version 2,
10  *  June 1991) as published by the Free Software Foundation.  At the
11  *  time of writing, that license was published by the FSF with the URL
12  *  http://www.gnu.org/copyleft/gpl.html, and is incorporated herein by
13  *  reference.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  * This adjtimex(1) is very similar in intent to adjtimex(8) by Steven
21  * Dick <ssd@nevets.oau.org> and Jim Van Zandt <jrv@vanzandt.mv.com>
22  * (see http://metalab.unc.edu/pub/Linux/system/admin/time/adjtimex*).
23  * That version predates this one, and is _much_ bigger and more
24  * featureful.  My independently written version was very similar to
25  * Steven's from the start, because they both follow the kernel timex
26  * structure.  I further tweaked this version to be equivalent to Steven's
27  * where possible, but I don't like getopt_long, so the actual usage
28  * syntax is incompatible.
29  *
30  * Amazingly enough, my Red Hat 5.2 sys/timex (and sub-includes)
31  * don't actually give a prototype for adjtimex(2), so building
32  * this code (with -Wall) gives a warning.  Later versions of
33  * glibc fix this issue.
34  *
35  * This program is too simple for a Makefile, just build with:
36  *  gcc -Wall -O adjtimex_1.c -o adjtimex
37  */
38
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/timex.h>
44
45 static struct {int bit; const char *name;} statlist[] = {
46         { STA_PLL,       "PLL"       },
47         { STA_PPSFREQ,   "PPSFREQ"   },
48         { STA_PPSTIME,   "PPSTIME"   },
49         { STA_FLL,       "FFL"       },
50         { STA_INS,       "INS"       },
51         { STA_DEL,       "DEL"       },
52         { STA_UNSYNC,    "UNSYNC"    },
53         { STA_FREQHOLD,  "FREQHOLD"  },
54         { STA_PPSSIGNAL, "PPSSIGNAL" },
55         { STA_PPSJITTER, "PPSJITTER" },
56         { STA_PPSWANDER, "PPSWANDER" },
57         { STA_PPSERROR,  "PPSERROR"  },
58         { STA_CLOCKERR,  "CLOCKERR"  },
59         { 0, NULL } };
60
61 static const char *ret_code_descript[] = {
62         "clock synchronized",
63         "insert leap second",
64         "delete leap second",
65         "leap second in progress",
66         "leap second has occurred",
67         "clock not synchronized" };
68
69 static void usage(char *prog)
70 {
71         fprintf(stderr, 
72                 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
73                 prog);
74 }
75
76 int main(int argc, char ** argv)
77 {
78         struct timex txc;
79         int quiet=0;
80         int c, i, ret, sep;
81         txc.modes=0;
82         for (;;) {
83                 c = getopt( argc, argv, "qo:f:p:t:");
84                 if (c == EOF) break;
85                 switch (c) {
86                         case 'q':
87                                 quiet=1;
88                                 break;
89                         case 'o':
90                                 txc.offset = atoi(optarg);
91                                 txc.modes |= ADJ_OFFSET_SINGLESHOT;
92                                 break;
93                         case 'f':
94                                 txc.freq = atoi(optarg);
95                                 txc.modes |= ADJ_FREQUENCY;
96                                 break;
97                         case 'p':
98                                 txc.constant = atoi(optarg);
99                                 txc.modes |= ADJ_TIMECONST;
100                                 break;
101                         case 't':
102                                 txc.tick = atoi(optarg);
103                                 txc.modes |= ADJ_TICK;
104                                 break;
105                         default:
106                                 usage(argv[0]);
107                                 exit(1);
108                 }
109         }
110         if (argc != optind) { /* no valid non-option parameters */
111                 usage(argv[0]);
112                 exit(1);
113         }
114
115         ret = adjtimex(&txc);
116
117         if (ret < 0) perror("adjtimex");
118         
119         if (!quiet && ret>=0) {
120                 printf(
121                         "    mode:         %d\n"
122                         "-o  offset:       %ld\n"
123                         "-f  frequency:    %ld\n"
124                         "    maxerror:     %ld\n"
125                         "    esterror:     %ld\n"
126                         "    status:       %d ( ",
127                 txc.modes, txc.offset, txc.freq, txc.maxerror,
128                 txc.esterror, txc.status);
129
130                 /* representative output of next code fragment:
131                    "PLL | PPSTIME" */
132                 sep=0;
133                 for (i=0; statlist[i].name; i++) {
134                         if (txc.status & statlist[i].bit) {
135                                 if (sep) fputs(" | ",stdout);
136                                 fputs(statlist[i].name,stdout);
137                                 sep=1;
138                         }
139                 }
140
141                 printf(" )\n"
142                         "-p  timeconstant: %ld\n"
143                         "    precision:    %ld\n"
144                         "    tolerance:    %ld\n"
145                         "-t  tick:         %ld\n"
146                         "    time.tv_sec:  %ld\n"
147                         "    time.tv_usec: %ld\n"
148                         "    return value: %d (%s)\n",
149                 txc.constant,
150                 txc.precision, txc.tolerance, txc.tick,
151                 txc.time.tv_sec, txc.time.tv_usec, ret, 
152                 (ret >= 0 && ret <= 5) ? ret_code_descript[ret] : "error" );
153         }
154         return (ret<0);
155 }