Initial public busybox upstream commit
[busybox4maemo] / miscutils / adjtimex.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables.
4  *
5  * Originally written: October 1997
6  * Last hack: March 2001
7  * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov>
8  *
9  * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
10  *
11  * Licensed under GPLv2 or later, see file License in this tarball for details.
12  */
13
14 #include "libbb.h"
15 #include <sys/timex.h>
16
17 static const uint16_t statlist_bit[] = {
18         STA_PLL,
19         STA_PPSFREQ,
20         STA_PPSTIME,
21         STA_FLL,
22         STA_INS,
23         STA_DEL,
24         STA_UNSYNC,
25         STA_FREQHOLD,
26         STA_PPSSIGNAL,
27         STA_PPSJITTER,
28         STA_PPSWANDER,
29         STA_PPSERROR,
30         STA_CLOCKERR,
31         0
32 };
33 static const char statlist_name[] =
34         "PLL"       "\0"
35         "PPSFREQ"   "\0"
36         "PPSTIME"   "\0"
37         "FFL"       "\0"
38         "INS"       "\0"
39         "DEL"       "\0"
40         "UNSYNC"    "\0"
41         "FREQHOLD"  "\0"
42         "PPSSIGNAL" "\0"
43         "PPSJITTER" "\0"
44         "PPSWANDER" "\0"
45         "PPSERROR"  "\0"
46         "CLOCKERR"
47 ;
48
49 static const char ret_code_descript[] =
50         "clock synchronized" "\0"
51         "insert leap second" "\0"
52         "delete leap second" "\0"
53         "leap second in progress" "\0"
54         "leap second has occurred" "\0"
55         "clock not synchronized"
56 ;
57
58 int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59 int adjtimex_main(int argc, char **argv)
60 {
61         enum {
62                 OPT_quiet = 0x1
63         };
64         unsigned opt;
65         char *opt_o, *opt_f, *opt_p, *opt_t;
66         struct timex txc;
67         int i, ret;
68         const char *descript;
69         txc.modes=0;
70
71         opt = getopt32(argv, "qo:f:p:t:",
72                         &opt_o, &opt_f, &opt_p, &opt_t);
73         //if (opt & 0x1) // -q
74         if (opt & 0x2) { // -o
75                 txc.offset = xatol(opt_o);
76                 txc.modes |= ADJ_OFFSET_SINGLESHOT;
77         }
78         if (opt & 0x4) { // -f
79                 txc.freq = xatol(opt_f);
80                 txc.modes |= ADJ_FREQUENCY;
81         }
82         if (opt & 0x8) { // -p
83                 txc.constant = xatol(opt_p);
84                 txc.modes |= ADJ_TIMECONST;
85         }
86         if (opt & 0x10) { // -t
87                 txc.tick = xatol(opt_t);
88                 txc.modes |= ADJ_TICK;
89         }
90         if (argc != optind) { /* no valid non-option parameters */
91                 bb_show_usage();
92         }
93
94         ret = adjtimex(&txc);
95
96         if (ret < 0) {
97                 bb_perror_nomsg_and_die();
98         }
99
100         if (!(opt & OPT_quiet)) {
101                 int sep;
102                 const char *name;
103
104                 printf(
105                         "    mode:         %d\n"
106                         "-o  offset:       %ld\n"
107                         "-f  frequency:    %ld\n"
108                         "    maxerror:     %ld\n"
109                         "    esterror:     %ld\n"
110                         "    status:       %d (",
111                 txc.modes, txc.offset, txc.freq, txc.maxerror,
112                 txc.esterror, txc.status);
113
114                 /* representative output of next code fragment:
115                    "PLL | PPSTIME" */
116                 name = statlist_name;
117                 sep = 0;
118                 for (i = 0; statlist_bit[i]; i++) {
119                         if (txc.status & statlist_bit[i]) {
120                                 if (sep)
121                                         fputs(" | ", stdout);
122                                 fputs(name, stdout);
123                                 sep = 1;
124                         }
125                         name += strlen(name) + 1;
126                 }
127
128                 descript = "error";
129                 if (ret <= 5)
130                         descript = nth_string(ret_code_descript, ret);
131                 printf(")\n"
132                         "-p  timeconstant: %ld\n"
133                         "    precision:    %ld\n"
134                         "    tolerance:    %ld\n"
135                         "-t  tick:         %ld\n"
136                         "    time.tv_sec:  %ld\n"
137                         "    time.tv_usec: %ld\n"
138                         "    return value: %d (%s)\n",
139                 txc.constant,
140                 txc.precision, txc.tolerance, txc.tick,
141                 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
142         }
143
144         return 0;
145 }