xref: /freebsd/contrib/ntp/util/timetrim.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 #ifdef sgi
2 /*
3  * timetrim.c
4  *
5  * "timetrim" allows setting and adjustment of the system clock frequency
6  * trim parameter on Silicon Graphics machines.  The trim value native
7  * units are nanoseconds per second (10**-9), so a trim value of 1 makes
8  * the system clock step ahead 1 nanosecond more per second than a value
9  * of zero.  Xntpd currently uses units of 2**-20 secs for its frequency
10  * offset (drift) values; to convert to a timetrim value, multiply by
11  * 1E9 / 2**20 (about 954).
12  *
13  * "timetrim" with no arguments just prints out the current kernel value.
14  * With a numeric argument, the kernel value is set to the supplied value.
15  * The "-i" flag causes the supplied value to be added to the kernel value.
16  * The "-n" option causes all input and output to be in xntpd units rather
17  * than timetrim native units.
18  *
19  * Note that there is a limit of +-3000000 (0.3%) on the timetrim value
20  * which is (silently?) enforced by the kernel.
21  *
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #ifdef HAVE_SYS_SYSSGI_H
32 # include <sys/syssgi.h>
33 #endif
34 
35 #define abs(X) (((X) < 0) ? -(X) : (X))
36 #define USAGE "usage: timetrim [-n] [[-i] value]\n"
37 #define SGITONTP(X) ((double)(X) * 1048576.0/1.0e9)
38 #define NTPTOSGI(X) ((long)((X) * 1.0e9/1048576.0))
39 
40 int
41 main(
42 	int argc,
43 	char *argv[]
44 	)
45 {
46 	char *rem;
47 	int incremental = 0, ntpunits = 0;
48 	long timetrim;
49 	double value;
50 
51 	while (--argc && **++argv == '-' && isalpha((int)argv[0][1])) {
52 		switch (argv[0][1]) {
53 		    case 'i':
54 			incremental++;
55 			break;
56 		    case 'n':
57 			ntpunits++;
58 			break;
59 		    default:
60 			fprintf(stderr, USAGE);
61 			exit(1);
62 		}
63 	}
64 
65 	if (syssgi(SGI_GETTIMETRIM, &timetrim) < 0) {
66 		perror("syssgi");
67 		exit(2);
68 	}
69 
70 	if (argc == 0) {
71 		if (ntpunits)
72 		    fprintf(stdout, "%0.5f\n", SGITONTP(timetrim));
73 		else
74 		    fprintf(stdout, "%ld\n", timetrim);
75 	} else if (argc != 1) {
76 		fprintf(stderr, USAGE);
77 		exit(1);
78 	} else {
79 		value = strtod(argv[0], &rem);
80 		if (*rem != '\0') {
81 			fprintf(stderr, USAGE);
82 			exit(1);
83 		}
84 		if (ntpunits)
85 		    value = NTPTOSGI(value);
86 		if (incremental)
87 		    timetrim += value;
88 		else
89 		    timetrim = value;
90 		if (syssgi(SGI_SETTIMETRIM, timetrim) < 0) {
91 			perror("syssgi");
92 			exit(2);
93 		}
94 	}
95 	return 0;
96 }
97 #endif
98