2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley Software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
23 char *dfile
= "/usr/share/lib/unittab";
80 struct table
*hash(char *name
);
83 int equal(char *s1
, char *s2
);
84 int lookup(char *name
, struct unit
*up
, int den
, int c
);
85 int convr(struct unit
*up
);
86 int pu(int u
, int i
, int f
);
87 void units(struct unit
*up
);
90 main(int argc
, char *argv
[])
97 (void) setlocale(LC_ALL
, "");
98 #if !defined(TEXT_DOMAIN)
99 #define TEXT_DOMAIN "SYS_TEST"
101 (void) textdomain(TEXT_DOMAIN
);
103 if(argc
>1 && *argv
[1]=='-') {
111 if ((inp
= fopen(file
, "r")) == NULL
) {
112 printf(gettext("no table\n"));
120 printf(gettext("you have: "));
126 printf(gettext("you want: "));
129 for(i
=0; i
<NDIM
; i
++)
130 if(u1
.dim
[i
] != u2
.dim
[i
])
132 f
= u1
.factor
/u2
.factor
;
133 if(fperrc
|| f
== 0.0)
135 printf("\t* %e\n", f
);
136 printf("\t/ %e\n", 1./f
);
142 printf(gettext("conformability\n"));
148 printf(gettext("underflow or overflow\n"));
153 units(struct unit
*up
)
159 printf("\t%e ", p
->factor
);
161 for(i
=0; i
<NDIM
; i
++)
162 f
|= pu(p
->dim
[i
], i
, f
);
166 for(i
=0; i
<NDIM
; i
++)
167 f
|= pu(-p
->dim
[i
], i
, f
);
173 pu(int u
, int i
, int f
)
180 printf("%s", unames
[i
]); else
181 printf(gettext("*%c*"), i
+'a');
192 convr(struct unit
*up
)
201 for(c
=0; c
<NDIM
; c
++)
203 p
->factor
= getflt();
230 err
|= lookup(cp
, p
, den
, c
);
243 lookup(char *name
, struct unit
*up
, int den
, int c
)
259 p
->factor
/= q
->factor
*e
;
260 for(i
=0; i
<NDIM
; i
++)
261 p
->dim
[i
] -= q
->dim
[i
];
263 p
->factor
*= q
->factor
*e
;
264 for(i
=0; i
<NDIM
; i
++)
265 p
->dim
[i
] += q
->dim
[i
];
267 if(c
>= '2' && c
<= '9') {
273 for(i
=0; cp1
= prefix
[i
].pname
; i
++) {
275 while(*cp1
== *cp2
++)
281 e
*= prefix
[i
].factor
;
286 for(cp1
= name
; *cp1
; cp1
++);
287 if(cp1
> name
+1 && *--cp1
== 's') {
291 printf(gettext("cannot recognize %s\n"), name
);
296 equal(char *s1
, char *s2
)
312 struct table
*tp
, *lp
;
317 for(i
=0; i
<NDIM
; i
++) {
336 printf(gettext("%d units; %d bytes\n\n"), i
, cp
-names
);
337 for(tp
= &table
[0]; tp
< &table
[NTAB
]; tp
++) {
340 printf("%s", tp
->name
);
341 units((struct unit
*)tp
);
348 while(c
!= '\n' && c
!= 0)
355 while(c
!= ' ' && c
!= '\t') {
366 tp
->factor
= lp
->factor
;
367 for(c
=0; c
<NDIM
; c
++)
368 tp
->dim
[c
] = lp
->dim
[c
];
377 convr((struct unit
*)lp
);
381 if(lp
->factor
!= 1.0)
383 for(c
=0; c
<NDIM
; c
++) {
385 if(t
>1 || (f
>0 && t
!=0))
398 printf(gettext("redefinition %s\n"), np
);
413 while(c
== ' ' || c
== '\t');
416 if(c
>= '0' && c
<= '9') {
430 if(c
== '+' || c
== '-') {
436 while(c
>= '0' && c
<= '9') {
489 h
= h
*57 + *np
++ - '0';
490 if( ((int)h
)<0) h
= -(int)h
;
496 if(equal(name
, tp
->name
))
499 if(tp
>= &table
[NTAB
])