Research V7 development
[unix-history] / usr / src / libmp / util.c
CommitLineData
168f834f
PW
1char *malloc();
2#ifdef lint
3int xv_oid;
4#endif
5#include <stdio.h>
6#include <mp.h>
7move(a,b) MINT *a,*b;
8{ int i,j;
9 xfree(b);
10 b->len=a->len;
11 if((i=a->len)<0) i = -i;
12 if(i==0) return;
13 b->val=xalloc(i,"move");
14 for(j=0;j<i;j++)
15 b->val[j]=a->val[j];
16 return;
17}
18dummy(){}
19short *xalloc(nint,s) char *s;
20{ short *i;
21 i=(short *)malloc(2*(unsigned)nint+4);
22#ifdef DBG
23 if(dbg) fprintf(stderr, "%s: %o\n",s,i);
24#endif
25 if(i!=NULL) return(i);
26 fatal("mp: no free space");
27 return(0);
28}
29fatal(s) char *s;
30{
31 fprintf(stderr,"%s\n",s);
32 VOID fflush(stdout);
33 sleep(2);
34 abort();
35}
36xfree(c) MINT *c;
37{
38#ifdef DBG
39 if(dbg) fprintf(stderr, "xfree ");
40#endif
41 if(c->len==0) return;
42 shfree(c->val);
43 c->len=0;
44 return;
45}
46mcan(a) MINT *a;
47{ int i,j;
48 if((i=a->len)==0) return;
49 else if(i<0) i= -i;
50 for(j=i;j>0 && a->val[j-1]==0;j--);
51 if(j==i) return;
52 if(j==0)
53 { xfree(a);
54 return;
55 }
56 if(a->len > 0) a->len=j;
57 else a->len = -j;
58}
59MINT *itom(n)
60{ MINT *a;
61 a=(MINT *)xalloc(2,"itom");
62 if(n>0)
63 { a->len=1;
64 a->val=xalloc(1,"itom1");
65 *a->val=n;
66 return(a);
67 }
68 else if(n<0)
69 { a->len = -1;
70 a->val=xalloc(1,"itom2");
71 *a->val= -n;
72 return(a);
73 }
74 else
75 { a->len=0;
76 return(a);
77 }
78}
79mcmp(a,b) MINT *a,*b;
80{ MINT c;
81 int res;
82 if(a->len!=b->len) return(a->len-b->len);
83 c.len=0;
84 msub(a,b,&c);
85 res=c.len;
86 xfree(&c);
87 return(res);
88}