xref: /freebsd/contrib/ntp/util/timetrim.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 #ifdef HAVE_SYS_SYSSGI_H
31 # include <sys/syssgi.h>
32 #endif
33 
34 #define abs(X) (((X) < 0) ? -(X) : (X))
35 #define USAGE "usage: timetrim [-n] [[-i] value]\n"
36 #define SGITONTP(X) ((double)(X) * 1048576.0/1.0e9)
37 #define NTPTOSGI(X) ((long)((X) * 1.0e9/1048576.0))
38 
39 int
40 main(
41 	int argc,
42 	char *argv[]
43 	)
44 {
45 	char *rem;
46 	int c, incremental = 0, ntpunits = 0;
47 	long timetrim;
48 	double value, strtod();
49 
50 	while (--argc && **++argv == '-' && isalpha(argv[0][1])) {
51 		switch (argv[0][1]) {
52 		    case 'i':
53 			incremental++;
54 			break;
55 		    case 'n':
56 			ntpunits++;
57 			break;
58 		    default:
59 			fprintf(stderr, USAGE);
60 			exit(1);
61 		}
62 	}
63 
64 	if (syssgi(SGI_GETTIMETRIM, &timetrim) < 0) {
65 		perror("syssgi");
66 		exit(2);
67 	}
68 
69 	if (argc == 0) {
70 		if (ntpunits)
71 		    fprintf(stdout, "%0.5lf\n", SGITONTP(timetrim));
72 		else
73 		    fprintf(stdout, "%ld\n", timetrim);
74 	} else if (argc != 1) {
75 		fprintf(stderr, USAGE);
76 		exit(1);
77 	} else {
78 		value = strtod(argv[0], &rem);
79 		if (*rem != '\0') {
80 			fprintf(stderr, USAGE);
81 			exit(1);
82 		}
83 		if (ntpunits)
84 		    value = NTPTOSGI(value);
85 		if (incremental)
86 		    timetrim += value;
87 		else
88 		    timetrim = value;
89 		if (syssgi(SGI_SETTIMETRIM, timetrim) < 0) {
90 			perror("syssgi");
91 			exit(2);
92 		}
93 	}
94 }
95 #endif
96