02b6e89b0f47b7dfba78b8e77e34097c5889b480
[oweals/busybox.git] / miscutils / adjtimex.c
1 /*
2  * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables.
3  *
4  * Originally written: October 1997
5  * Last hack: March 2001
6  * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov>
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.c -o adjtimex
37  *
38  * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
39  * It will autosense if it is built in a busybox environment, based
40  * on the BB_VER preprocessor macro.
41  */
42
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <sys/timex.h>
48 #ifdef BB_VER
49 #include "busybox.h"
50 #endif
51
52 static struct {int bit; char *name;} statlist[] = {
53         { STA_PLL,       "PLL"       },
54         { STA_PPSFREQ,   "PPSFREQ"   },
55         { STA_PPSTIME,   "PPSTIME"   },
56         { STA_FLL,       "FFL"       },
57         { STA_INS,       "INS"       },
58         { STA_DEL,       "DEL"       },
59         { STA_UNSYNC,    "UNSYNC"    },
60         { STA_FREQHOLD,  "FREQHOLD"  },
61         { STA_PPSSIGNAL, "PPSSIGNAL" },
62         { STA_PPSJITTER, "PPSJITTER" },
63         { STA_PPSWANDER, "PPSWANDER" },
64         { STA_PPSERROR,  "PPSERROR"  },
65         { STA_CLOCKERR,  "CLOCKERR"  },
66         { 0, NULL } };
67
68 static char *ret_code_descript[] = {
69         "clock synchronized",
70         "insert leap second",
71         "delete leap second",
72         "leap second in progress",
73         "leap second has occurred",
74         "clock not synchronized" };
75
76 #ifdef BB_VER
77 #define main adjtimex_main
78 #else
79 void usage(char *prog)
80 {
81         fprintf(stderr, 
82                 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
83                 prog);
84 }
85 #define show_usage() usage(argv[0])
86 #endif
87
88 int main(int argc, char ** argv)
89 {
90         struct timex txc;
91         int quiet=0;
92         int c, i, ret, sep;
93         char *descript;
94         txc.modes=0;
95         for (;;) {
96                 c = getopt( argc, argv, "qo:f:p:t:");
97                 if (c == EOF) break;
98                 switch (c) {
99                         case 'q':
100                                 quiet=1;
101                                 break;
102                         case 'o':
103                                 txc.offset = atoi(optarg);
104                                 txc.modes |= ADJ_OFFSET_SINGLESHOT;
105                                 break;
106                         case 'f':
107                                 txc.freq = atoi(optarg);
108                                 txc.modes |= ADJ_FREQUENCY;
109                                 break;
110                         case 'p':
111                                 txc.constant = atoi(optarg);
112                                 txc.modes |= ADJ_TIMECONST;
113                                 break;
114                         case 't':
115                                 txc.tick = atoi(optarg);
116                                 txc.modes |= ADJ_TICK;
117                                 break;
118                         default:
119                                 show_usage();
120                                 exit(1);
121                 }
122         }
123         if (argc != optind) { /* no valid non-option parameters */
124                 show_usage();
125                 exit(1);
126         }
127
128         ret = adjtimex(&txc);
129
130         if (ret < 0) perror("adjtimex");
131         
132         if (!quiet && ret>=0) {
133                 printf(
134                         "    mode:         %d\n"
135                         "-o  offset:       %ld\n"
136                         "-f  frequency:    %ld\n"
137                         "    maxerror:     %ld\n"
138                         "    esterror:     %ld\n"
139                         "    status:       %d ( ",
140                 txc.modes, txc.offset, txc.freq, txc.maxerror,
141                 txc.esterror, txc.status);
142
143                 /* representative output of next code fragment:
144                    "PLL | PPSTIME" */
145                 sep=0;
146                 for (i=0; statlist[i].name; i++) {
147                         if (txc.status & statlist[i].bit) {
148                                 if (sep) fputs(" | ",stdout);
149                                 fputs(statlist[i].name,stdout);
150                                 sep=1;
151                         }
152                 }
153
154                 descript = "error";
155                 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
156                 printf(" )\n"
157                         "-p  timeconstant: %ld\n"
158                         "    precision:    %ld\n"
159                         "    tolerance:    %ld\n"
160                         "-t  tick:         %ld\n"
161                         "    time.tv_sec:  %ld\n"
162                         "    time.tv_usec: %ld\n"
163                         "    return value: %d (%s)\n",
164                 txc.constant,
165                 txc.precision, txc.tolerance, txc.tick,
166                 txc.time.tv_sec, txc.time.tv_usec, ret, descript);
167         }
168         return (ret<0);
169 }