calc.c (schily-2021-08-14.tar.bz2) | : | calc.c (schily-2021-09-18.tar.bz2) | ||
---|---|---|---|---|
/* @(#)calc.c 1.24 19/04/28 Copyright 1985-2019 J. Schilling */ | /* @(#)calc.c 1.27 21/08/20 Copyright 1985-2021 J. Schilling */ | |||
#include <schily/mconfig.h> | #include <schily/mconfig.h> | |||
#ifndef lint | #ifndef lint | |||
static UConst char sccsid[] = | static UConst char sccsid[] = | |||
"@(#)calc.c 1.24 19/04/28 Copyright 1985-2019 J. Schilling"; | "@(#)calc.c 1.27 21/08/20 Copyright 1985-2021 J. Schilling"; | |||
#endif | #endif | |||
/* | /* | |||
* Simples Taschenrechnerprogramm | * Simples Taschenrechnerprogramm | |||
* | * | |||
* Copyright (c) 1985-2019 J. Schilling | * Copyright (c) 1985-2021 J. Schilling | |||
*/ | */ | |||
/* | /* | |||
* The contents of this file are subject to the terms of the | * The contents of this file are subject to the terms of the | |||
* Common Development and Distribution License, Version 1.0 only | * Common Development and Distribution License, Version 1.0 only | |||
* (the "License"). You may not use this file except in compliance | * (the "License"). You may not use this file except in compliance | |||
* with the License. | * with the License. | |||
* | * | |||
* See the file CDDL.Schily.txt in this distribution for details. | * See the file CDDL.Schily.txt in this distribution for details. | |||
* A copy of the CDDL is also available via the Internet at | * A copy of the CDDL is also available via the Internet at | |||
* http://www.opensource.org/licenses/cddl1.txt | * http://www.opensource.org/licenses/cddl1.txt | |||
* | * | |||
* When distributing Covered Code, include this CDDL HEADER in each | * When distributing Covered Code, include this CDDL HEADER in each | |||
* file and include the License file CDDL.Schily.txt from this distribution. | * file and include the License file CDDL.Schily.txt from this distribution. | |||
*/ | */ | |||
#include <schily/stdio.h> | #include <schily/stdio.h> | |||
#include <schily/stdlib.h> | #include <schily/stdlib.h> | |||
#include <schily/utypes.h> | #include <schily/utypes.h> | |||
#include <schily/standard.h> | #include <schily/standard.h> | |||
#define GT_COMERR /* #define comerr gtcomerr */ | ||||
#define GT_ERROR /* #define error gterror */ | ||||
#include <schily/schily.h> | #include <schily/schily.h> | |||
#ifdef FERR_DEBUG | #ifdef FERR_DEBUG | |||
#include <schily/termios.h> | #include <schily/termios.h> | |||
#endif | #endif | |||
#include <schily/nlsdefs.h> | ||||
#define LLEN 100 | #define LLEN 100 | |||
#define LSHIFT 1000 | #define LSHIFT 1000 | |||
#define RSHIFT 1001 | #define RSHIFT 1001 | |||
LOCAL void usage __PR((int)); | LOCAL void usage __PR((int)); | |||
EXPORT int main __PR((int, char **)); | EXPORT int main __PR((int, char **)); | |||
LOCAL void prdig __PR((int)); | LOCAL void prdig __PR((int)); | |||
LOCAL void prlldig __PR((Llong)); | LOCAL void prlldig __PR((Llong)); | |||
skipping to change at line 84 | skipping to change at line 87 | |||
Llong arg1; | Llong arg1; | |||
Llong arg2; | Llong arg2; | |||
Llong ergebnis; | Llong ergebnis; | |||
Llong rest = (Llong)0; | Llong rest = (Llong)0; | |||
int iarg1; | int iarg1; | |||
int iarg2; | int iarg2; | |||
int iergebnis; | int iergebnis; | |||
int irest = 0; | int irest = 0; | |||
save_args(ac, av); | save_args(ac, av); | |||
(void) setlocale(LC_ALL, ""); | ||||
#ifdef USE_NLS | ||||
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ | ||||
#define TEXT_DOMAIN "calc" /* Use this only if it weren't */ | ||||
#endif | ||||
{ char *dir; | ||||
dir = searchfileinpath("share/locale", F_OK, | ||||
SIP_ANY_FILE|SIP_NO_PATH, NULL); | ||||
if (dir) | ||||
(void) bindtextdomain(TEXT_DOMAIN, dir); | ||||
else | ||||
#if defined(PROTOTYPES) && defined(INS_BASE) | ||||
(void) bindtextdomain(TEXT_DOMAIN, INS_BASE "/share/locale"); | ||||
#else | ||||
(void) bindtextdomain(TEXT_DOMAIN, "/usr/share/locale"); | ||||
#endif | ||||
(void) textdomain(TEXT_DOMAIN); | ||||
} | ||||
#endif /* USE_NLS */ | ||||
cac = --ac; | cac = --ac; | |||
cav = ++av; | cav = ++av; | |||
if (getallargs(&cac, &cav, "help,version", &help, &prversion) < 0) { | if (getallargs(&cac, &cav, "help,version", &help, &prversion) < 0) { | |||
errmsgno(EX_BAD, "Bad Option %s.\n", cav[0]); | errmsgno(EX_BAD, "Bad Option %s.\n", cav[0]); | |||
usage(EX_BAD); | usage(EX_BAD); | |||
} | } | |||
if (help) | if (help) | |||
usage(0); | usage(0); | |||
if (prversion) { | if (prversion) { | |||
printf("Calc release %s %s (%s-%s-%s) Copyright (C) 1985, 89-91, | gtprintf("Calc release %s %s (%s-%s-%s) Copyright (C) 1985, 89-91 | |||
1996, 2000-2019 Jörg Schilling\n", | , 1996, 2000-2021 %s\n", | |||
"1.24", "2019/04/28", | "1.27", "2021/08/20", | |||
HOST_CPU, HOST_VENDOR, HOST_OS); | HOST_CPU, HOST_VENDOR, HOST_OS, | |||
_("Jörg Schilling")); | ||||
exit(0); | exit(0); | |||
} | } | |||
putchar('?'); flush(); | putchar('?'); flush(); | |||
while (getline(eingabe, LLEN) >= 0 && !streql(eingabe, "quit")) { | while (getline(eingabe, LLEN) >= 0 && !streql(eingabe, "quit")) { | |||
opstr = eingabe; | opstr = eingabe; | |||
while (*opstr == ' ' || *opstr == '\t') | while (*opstr == ' ' || *opstr == '\t') | |||
opstr++; | opstr++; | |||
skipping to change at line 125 | skipping to change at line 151 | |||
/* | /* | |||
* Test des Formats und der Argumente | * Test des Formats und der Argumente | |||
*/ | */ | |||
i = xbreakline(opstr, " \t", argumente, 5); | i = xbreakline(opstr, " \t", argumente, 5); | |||
if (*argumente[i-1] == '\0') | if (*argumente[i-1] == '\0') | |||
i--; | i--; | |||
switch (i) { | switch (i) { | |||
case 1: | case 1: | |||
if (*astoll(argumente[0], &ergebnis) != '\0') { | if (*astoll(argumente[0], &ergebnis) != '\0') { | |||
printf("'%s' ist keine Zahl!\n?", argumente[0]); | gtprintf("'%s' ist keine Zahl!\n?", argumente[0]) ; | |||
continue; | continue; | |||
} | } | |||
iergebnis = (int)ergebnis; | iergebnis = (int)ergebnis; | |||
goto print; | goto print; | |||
case 2: | case 2: | |||
op = *argumente[0]; | op = *argumente[0]; | |||
if (op != '!' && op != '~') { | if (op != '!' && op != '~') { | |||
printf("Unzulässiger Operator für: "); | gtprintf("Unzulässiger Operator für: "); | |||
printf("'op argument1'\n?"); | gtprintf("'op argument1'\n?"); | |||
continue; | continue; | |||
} | } | |||
if (*astoll(argumente[1], &arg1) != '\0') { | if (*astoll(argumente[1], &arg1) != '\0') { | |||
printf("'%s' ist keine Zahl!\n?", argumente[1]); | gtprintf("'%s' ist keine Zahl!\n?", argumente[1]) ; | |||
continue; | continue; | |||
} | } | |||
break; | break; | |||
case 3: | case 3: | |||
if (*astoll(argumente[0], &arg1) != '\0') { | if (*astoll(argumente[0], &arg1) != '\0') { | |||
printf("'%s' ist keine Zahl!\n?", argumente[0]); | gtprintf("'%s' ist keine Zahl!\n?", argumente[0]) ; | |||
continue; | continue; | |||
} | } | |||
if (*astoll(argumente[2], &arg2) != '\0') { | if (*astoll(argumente[2], &arg2) != '\0') { | |||
printf("'%s' ist keine Zahl!\n?", argumente[2]); | gtprintf("'%s' ist keine Zahl!\n?", argumente[2]) ; | |||
continue; | continue; | |||
} | } | |||
break; | break; | |||
default: | default: | |||
printf("Die Eingabe hat nicht das richtige Format: "); | gtprintf("Die Eingabe hat nicht das richtige Format: "); | |||
printf("'argument1 op argument2'\n?"); | gtprintf("'argument1 op argument2'\n?"); | |||
continue; | continue; | |||
} | } | |||
/* | /* | |||
* Test der Operationssymbole | * Test der Operationssymbole | |||
*/ | */ | |||
op = 0; | op = 0; | |||
opstr = argumente[1]; | opstr = argumente[1]; | |||
if (i == 2) | if (i == 2) | |||
opstr = argumente[0]; | opstr = argumente[0]; | |||
if (streql(opstr, "<<")) { | if (streql(opstr, "<<")) { | |||
op = LSHIFT; | op = LSHIFT; | |||
} else if (streql(opstr, ">>")) { | } else if (streql(opstr, ">>")) { | |||
op = RSHIFT; | op = RSHIFT; | |||
} else if (opstr[1] != '\0') { | } else if (opstr[1] != '\0') { | |||
printf("Operationssymbole sind einstellig. Falsche Eingab e!\n?"); | gtprintf("Operationssymbole sind einstellig. Falsche Eing abe!\n?"); | |||
continue; | continue; | |||
} else if (!op) { | } else if (!op) { | |||
op = *opstr; | op = *opstr; | |||
} | } | |||
i = 0; | i = 0; | |||
iergebnis = (int)ergebnis; | iergebnis = (int)ergebnis; | |||
iarg1 = (int)arg1; | iarg1 = (int)arg1; | |||
iarg2 = (int)arg2; | iarg2 = (int)arg2; | |||
skipping to change at line 227 | skipping to change at line 253 | |||
case '!': | case '!': | |||
ergebnis = !arg1; | ergebnis = !arg1; | |||
iergebnis = !iarg1; | iergebnis = !iarg1; | |||
break; | break; | |||
case '~': | case '~': | |||
ergebnis = ~arg1; | ergebnis = ~arg1; | |||
iergebnis = ~iarg1; | iergebnis = ~iarg1; | |||
break; | break; | |||
case '%': | case '%': | |||
case '/': if (arg2 == 0) { | case '/': if (arg2 == 0) { | |||
printf("Division durch Null ist unzulaessig.\n?") ; | gtprintf("Division durch Null ist unzulaessig.\n? "); | |||
i = 1; | i = 1; | |||
break; | break; | |||
} else { | } else { | |||
/* | /* | |||
* 9223372036854775808 / 322122547200 | * 9223372036854775808 / 322122547200 | |||
* liefert eine Integer(32) Division durch 0 | * liefert eine Integer(32) Division durch 0 | |||
*/ | */ | |||
ergebnis = arg1 / arg2; | ergebnis = arg1 / arg2; | |||
rest = arg1 % arg2; | rest = arg1 % arg2; | |||
if (iarg2 == 0) { | if (iarg2 == 0) { | |||
skipping to change at line 256 | skipping to change at line 282 | |||
} | } | |||
if (op == '%') { | if (op == '%') { | |||
ergebnis = rest; | ergebnis = rest; | |||
iergebnis = irest; | iergebnis = irest; | |||
} | } | |||
break; | break; | |||
} | } | |||
default: | default: | |||
printf("Unzulaessiger Operator!\n?"); | gtprintf("Unzulaessiger Operator!\n?"); | |||
i = 1; | i = 1; | |||
break; | break; | |||
} | } | |||
if (i == 1) | if (i == 1) | |||
continue; | continue; | |||
print: | print: | |||
/* | /* | |||
* Ausgabe | * Ausgabe | |||
*/ | */ | |||
prdig(iergebnis); | prdig(iergebnis); | |||
if (op == '/') { | if (op == '/') { | |||
printf("\nRest (dezimal): %d\n", irest); | gtprintf("\nRest (dezimal): %d\n", irest); | |||
prdig(irest); | prdig(irest); | |||
} | } | |||
putchar('\n'); | putchar('\n'); | |||
prlldig(ergebnis); | prlldig(ergebnis); | |||
if (op == '/') { | if (op == '/') { | |||
printf("\nRest (dezimal): %lld\n", rest); | gtprintf("\nRest (dezimal): %lld\n", rest); | |||
prlldig(rest); | prlldig(rest); | |||
} | } | |||
putchar('\n'); | putchar('\n'); | |||
putchar('?'); flush(); | putchar('?'); flush(); | |||
} | } | |||
if (ferror(stdin)) { | if (ferror(stdin)) { | |||
#ifdef FERR_DEBUG | #ifdef FERR_DEBUG | |||
pid_t pgrp; | pid_t pgrp; | |||
ioctl(STDIN_FILENO, TIOCGPGRP, (char *)&pgrp); | ioctl(STDIN_FILENO, TIOCGPGRP, (char *)&pgrp); | |||
End of changes. 18 change blocks. | ||||
20 lines changed or deleted | 46 lines changed or added |