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