Commit | Line | Data |
---|---|---|
051b1e55 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | static char sccsid[] = "@(#)util.c 5.1 (Berkeley) %G%"; | |
9 | #endif not lint | |
59fd1272 SL |
10 | |
11 | char *malloc(); | |
12 | #ifdef lint | |
13 | int xv_oid; | |
14 | #endif | |
15 | #include <stdio.h> | |
16 | #include <mp.h> | |
17 | move(a,b) MINT *a,*b; | |
18 | { int i,j; | |
19 | xfree(b); | |
20 | b->len=a->len; | |
21 | if((i=a->len)<0) i = -i; | |
22 | if(i==0) return; | |
23 | b->val=xalloc(i,"move"); | |
24 | for(j=0;j<i;j++) | |
25 | b->val[j]=a->val[j]; | |
26 | return; | |
27 | } | |
28 | dummy(){} | |
29 | short *xalloc(nint,s) char *s; | |
30 | { short *i; | |
31 | i=(short *)malloc(2*(unsigned)nint+4); | |
32 | #ifdef DBG | |
33 | if(dbg) fprintf(stderr, "%s: %o\n",s,i); | |
34 | #endif | |
35 | if(i!=NULL) return(i); | |
36 | fatal("mp: no free space"); | |
37 | return(0); | |
38 | } | |
39 | fatal(s) char *s; | |
40 | { | |
41 | fprintf(stderr,"%s\n",s); | |
42 | VOID fflush(stdout); | |
43 | sleep(2); | |
44 | abort(); | |
45 | } | |
46 | xfree(c) MINT *c; | |
47 | { | |
48 | #ifdef DBG | |
49 | if(dbg) fprintf(stderr, "xfree "); | |
50 | #endif | |
51 | if(c->len==0) return; | |
52 | shfree(c->val); | |
53 | c->len=0; | |
54 | return; | |
55 | } | |
56 | mcan(a) MINT *a; | |
57 | { int i,j; | |
58 | if((i=a->len)==0) return; | |
59 | else if(i<0) i= -i; | |
60 | for(j=i;j>0 && a->val[j-1]==0;j--); | |
61 | if(j==i) return; | |
62 | if(j==0) | |
63 | { xfree(a); | |
64 | return; | |
65 | } | |
66 | if(a->len > 0) a->len=j; | |
67 | else a->len = -j; | |
68 | } | |
69 | MINT *itom(n) | |
70 | { MINT *a; | |
71 | a=(MINT *)xalloc(2,"itom"); | |
72 | if(n>0) | |
73 | { a->len=1; | |
74 | a->val=xalloc(1,"itom1"); | |
75 | *a->val=n; | |
76 | return(a); | |
77 | } | |
78 | else if(n<0) | |
79 | { a->len = -1; | |
80 | a->val=xalloc(1,"itom2"); | |
81 | *a->val= -n; | |
82 | return(a); | |
83 | } | |
84 | else | |
85 | { a->len=0; | |
86 | return(a); | |
87 | } | |
88 | } | |
89 | mcmp(a,b) MINT *a,*b; | |
90 | { MINT c; | |
91 | int res; | |
92 | if(a->len!=b->len) return(a->len-b->len); | |
93 | c.len=0; | |
94 | msub(a,b,&c); | |
95 | res=c.len; | |
96 | xfree(&c); | |
97 | return(res); | |
98 | } |