mout.c revision 2
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A
2N/A/*
2N/A * Copyright (c) 1980 Regents of the University of California.
2N/A * All rights reserved. The Berkeley software License Agreement
2N/A * specifies the terms and conditions for redistribution.
2N/A */
2N/A/* Portions Copyright(c) 1988, Sun Microsystems Inc. */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Copyright (c) 1997, by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */
2N/A
2N/A/* LINTLIBRARY */
2N/A
2N/A#include <stdio.h>
2N/A#include <mp.h>
2N/A#include <sys/types.h>
2N/A#include "libmp.h"
2N/A#include <stdlib.h>
2N/A
2N/Astatic int
2N/Am_in(MINT *a, short b, FILE *f)
2N/A{
2N/A MINT x, y, ten;
2N/A int sign, c;
2N/A short qten, qy;
2N/A
2N/A _mp_xfree(a);
2N/A sign = 1;
2N/A ten.len = 1;
2N/A ten.val = &qten;
2N/A qten = b;
2N/A x.len = 0;
2N/A y.len = 1;
2N/A y.val = &qy;
2N/A while ((c = getc(f)) != EOF)
2N/A switch (c) {
2N/A
2N/A case '\\':
2N/A (void) getc(f);
2N/A continue;
2N/A case '\t':
2N/A case '\n':
2N/A a->len *= sign;
2N/A _mp_xfree(&x);
2N/A return (0);
2N/A case ' ':
2N/A continue;
2N/A case '-':
2N/A sign = -sign;
2N/A continue;
2N/A default:
2N/A if (c >= '0' && c <= '9') {
2N/A qy = c - '0';
2N/A mp_mult(&x, &ten, a);
2N/A mp_madd(a, &y, a);
2N/A _mp_move(a, &x);
2N/A continue;
2N/A } else {
2N/A (void) ungetc(c, stdin);
2N/A a->len *= sign;
2N/A return (0);
2N/A }
2N/A }
2N/A return (EOF);
2N/A}
2N/A
2N/Astatic void
2N/Am_out(MINT *a, short b, FILE *f)
2N/A{
2N/A int sign, xlen, i;
2N/A short r;
2N/A MINT x;
2N/A
2N/A char *obuf;
2N/A char *bp;
2N/A
2N/A if (a == NULL)
2N/A return;
2N/A sign = 1;
2N/A xlen = a->len;
2N/A if (xlen < 0) {
2N/A xlen = -xlen;
2N/A sign = -1;
2N/A }
2N/A if (xlen == 0) {
2N/A (void) fprintf(f, "0\n");
2N/A return;
2N/A }
2N/A x.len = xlen;
2N/A x.val = _mp_xalloc(xlen, "m_out");
2N/A for (i = 0; i < xlen; i++)
2N/A x.val[i] = a->val[i];
2N/A obuf = malloc(7 * (size_t)xlen);
2N/A bp = obuf + 7 * xlen - 1;
2N/A *bp-- = 0;
2N/A while (x.len > 0) {
2N/A for (i = 0; i < 10 && x.len > 0; i++) {
2N/A mp_sdiv(&x, b, &x, &r);
2N/A *bp-- = (char)(r + '0');
2N/A }
2N/A if (x.len > 0)
2N/A *bp-- = ' ';
2N/A }
2N/A if (sign == -1)
2N/A *bp-- = '-';
2N/A (void) fprintf(f, "%s\n", bp + 1);
2N/A free(obuf);
2N/A _mp_xfree(&x);
2N/A}
2N/A
2N/Astatic void s_div(MINT *, short, MINT *, short *);
2N/A
2N/Avoid
2N/Amp_sdiv(MINT *a, short n, MINT *q, short *r)
2N/A{
2N/A MINT x, y;
2N/A short sign;
2N/A
2N/A sign = 1;
2N/A x.len = a->len;
2N/A x.val = a->val;
2N/A if (n < 0) {
2N/A sign = -sign;
2N/A n = -n;
2N/A }
2N/A if (x.len < 0) {
2N/A sign = -sign;
2N/A x.len = -x.len;
2N/A }
2N/A s_div(&x, n, &y, r);
2N/A _mp_xfree(q);
2N/A q->val = y.val;
2N/A q->len = sign * y.len;
2N/A *r = *r * sign;
2N/A}
2N/A
2N/Astatic void
2N/As_div(MINT *a, short n, MINT *q, short *r)
2N/A{
2N/A int qlen;
2N/A int i;
2N/A int x;
2N/A short *qval;
2N/A short *aval;
2N/A
2N/A x = 0;
2N/A qlen = a->len;
2N/A q->val = _mp_xalloc(qlen, "s_div");
2N/A aval = a->val + qlen;
2N/A qval = q->val + qlen;
2N/A for (i = qlen - 1; i >= 0; i--) {
2N/A x = x * 0100000 + *--aval;
2N/A *--qval = (short)(x / n);
2N/A x = x % n;
2N/A }
2N/A *r = (short)x;
2N/A if (qlen && q->val[qlen-1] == 0)
2N/A qlen--;
2N/A q->len = qlen;
2N/A if (qlen == 0)
2N/A free(q->val);
2N/A}
2N/A
2N/Aint
2N/Amp_min(MINT *a)
2N/A{
2N/A return (m_in(a, 10, stdin));
2N/A}
2N/A
2N/Aint
2N/Amp_omin(MINT *a)
2N/A{
2N/A return (m_in(a, 8, stdin));
2N/A}
2N/A
2N/Avoid
2N/Amp_mout(MINT *a)
2N/A{
2N/A m_out(a, 10, stdout);
2N/A}
2N/A
2N/Avoid
2N/Amp_omout(MINT *a)
2N/A{
2N/A m_out(a, 8, stdout);
2N/A}
2N/A
2N/Avoid
2N/Amp_fmout(MINT *a, FILE *f)
2N/A{
2N/A m_out(a, 10, f);
2N/A}
2N/A
2N/Aint
2N/Amp_fmin(MINT *a, FILE *f)
2N/A{
2N/A return (m_in(a, 10, f));
2N/A}