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