Consolidate #include <sys/time.h> so libbb.h does it.
[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 #include "busybox.h"
49
50 #if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
51 #define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
52 #endif
53 #if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
54 #define ADJ_FREQUENCY MOD_FREQUENCY
55 #endif
56 #if !defined ADJ_TIMECONST && defined MOD_TIMECONST
57 #define ADJ_TIMECONST MOD_TIMECONST
58 #endif
59 #if !defined ADJ_TICK && defined MOD_CLKB
60 #define ADJ_TICK MOD_CLKB
61 #endif
62
63 static const struct {int bit; const char *name;} statlist[] = {
64         { STA_PLL,       "PLL"       },
65         { STA_PPSFREQ,   "PPSFREQ"   },
66         { STA_PPSTIME,   "PPSTIME"   },
67         { STA_FLL,       "FFL"       },
68         { STA_INS,       "INS"       },
69         { STA_DEL,       "DEL"       },
70         { STA_UNSYNC,    "UNSYNC"    },
71         { STA_FREQHOLD,  "FREQHOLD"  },
72         { STA_PPSSIGNAL, "PPSSIGNAL" },
73         { STA_PPSJITTER, "PPSJITTER" },
74         { STA_PPSWANDER, "PPSWANDER" },
75         { STA_PPSERROR,  "PPSERROR"  },
76         { STA_CLOCKERR,  "CLOCKERR"  },
77         { 0, NULL } };
78
79 static const char * const ret_code_descript[] = {
80         "clock synchronized",
81         "insert leap second",
82         "delete leap second",
83         "leap second in progress",
84         "leap second has occurred",
85         "clock not synchronized" };
86
87 #ifdef BB_VER
88 #define main adjtimex_main
89 #else
90 void usage(char *prog)
91 {
92         fprintf(stderr,
93                 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
94                 prog);
95 }
96 #define bb_show_usage() usage(argv[0])
97 #endif
98
99 int main(int argc, char ** argv)
100 {
101         struct timex txc;
102         int quiet=0;
103         int c, i, ret, sep;
104         const char *descript;
105         txc.modes=0;
106         for (;;) {
107                 c = getopt( argc, argv, "qo:f:p:t:");
108                 if (c == EOF) break;
109                 switch (c) {
110                         case 'q':
111                                 quiet=1;
112                                 break;
113                         case 'o':
114                                 txc.offset = atoi(optarg);
115                                 txc.modes |= ADJ_OFFSET_SINGLESHOT;
116                                 break;
117                         case 'f':
118                                 txc.freq = atoi(optarg);
119                                 txc.modes |= ADJ_FREQUENCY;
120                                 break;
121                         case 'p':
122                                 txc.constant = atoi(optarg);
123                                 txc.modes |= ADJ_TIMECONST;
124                                 break;
125                         case 't':
126                                 txc.tick = atoi(optarg);
127                                 txc.modes |= ADJ_TICK;
128                                 break;
129                         default:
130                                 bb_show_usage();
131                                 exit(1);
132                 }
133         }
134         if (argc != optind) { /* no valid non-option parameters */
135                 bb_show_usage();
136                 exit(1);
137         }
138
139         ret = adjtimex(&txc);
140
141         if (ret < 0) perror("adjtimex");
142
143         if (!quiet && ret>=0) {
144                 printf(
145                         "    mode:         %d\n"
146                         "-o  offset:       %ld\n"
147                         "-f  frequency:    %ld\n"
148                         "    maxerror:     %ld\n"
149                         "    esterror:     %ld\n"
150                         "    status:       %d ( ",
151                 txc.modes, txc.offset, txc.freq, txc.maxerror,
152                 txc.esterror, txc.status);
153
154                 /* representative output of next code fragment:
155                    "PLL | PPSTIME" */
156                 sep=0;
157                 for (i=0; statlist[i].name; i++) {
158                         if (txc.status & statlist[i].bit) {
159                                 if (sep) fputs(" | ",stdout);
160                                 fputs(statlist[i].name,stdout);
161                                 sep=1;
162                         }
163                 }
164
165                 descript = "error";
166                 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
167                 printf(" )\n"
168                         "-p  timeconstant: %ld\n"
169                         "    precision:    %ld\n"
170                         "    tolerance:    %ld\n"
171                         "-t  tick:         %ld\n"
172                         "    time.tv_sec:  %ld\n"
173                         "    time.tv_usec: %ld\n"
174                         "    return value: %d (%s)\n",
175                 txc.constant,
176                 txc.precision, txc.tolerance, txc.tick,
177                 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
178         }
179         return (ret<0);
180 }