New upstream version 2.3
SZ Lin (林上智)
2 years ago
0 | version: 2 | |
1 | ||
2 | jobs: | |
3 | compile: | |
4 | docker: | |
5 | - image: ubuntu:20.04 | |
6 | working_directory: ~/bcal | |
7 | environment: | |
8 | CI_FORCE_TEST: 1 | |
9 | steps: | |
10 | - run: | |
11 | command: | | |
12 | apt update -qq | |
13 | DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get -y install tzdata | |
14 | apt install -y --no-install-recommends git make libreadline-dev | |
15 | apt install -y --no-install-recommends gcc-7 gcc-8 gcc-9 gcc-10 | |
16 | apt install -y --no-install-recommends clang-6.0 clang-7 clang-8 clang-9 clang-10 clang-11 clang-tidy-11 | |
17 | - checkout | |
18 | - run: | |
19 | command: | | |
20 | export CFLAGS=-Werror | |
21 | make clean | |
22 | echo | |
23 | echo "########## gcc-7 ##########" | |
24 | CC=gcc-7 make strip | |
25 | ls -l bcal | |
26 | make clean | |
27 | echo | |
28 | echo "########## gcc-8 ##########" | |
29 | CC=gcc-8 make strip | |
30 | ls -l bcal | |
31 | make clean | |
32 | echo | |
33 | echo "########## gcc-9 ##########" | |
34 | CC=gcc-9 make strip | |
35 | ls -l bcal | |
36 | make clean | |
37 | echo | |
38 | echo "########## gcc-10 ##########" | |
39 | CC=gcc-10 make strip | |
40 | ls -l bcal | |
41 | make clean | |
42 | echo | |
43 | echo "########## clang-6 ##########" | |
44 | CC=clang-6.0 make strip | |
45 | ls -l bcal | |
46 | make clean | |
47 | echo | |
48 | echo "########## clang-7 ##########" | |
49 | CC=clang-7 make strip | |
50 | ls -l bcal | |
51 | make clean | |
52 | echo | |
53 | echo "########## clang-8 ##########" | |
54 | CC=clang-8 make strip | |
55 | ls -l bcal | |
56 | make clean | |
57 | echo | |
58 | echo "########## clang-9 ##########" | |
59 | CC=clang-9 make strip | |
60 | ls -l bcal | |
61 | make clean | |
62 | echo | |
63 | echo "########## clang-10 ##########" | |
64 | CC=clang-10 make strip | |
65 | ls -l bcal | |
66 | make clean | |
67 | echo | |
68 | echo "########## clang-11 ##########" | |
69 | CC=clang-11 make strip | |
70 | ls -l bcal | |
71 | make clean | |
72 | echo | |
73 | echo "########## clang-tidy-11 ##########" | |
74 | clang-tidy-11 **/*.h **/*.c -- -Iinc | |
75 | ||
76 | package-and-publish: | |
77 | machine: true | |
78 | working_directory: ~/bcal | |
79 | steps: | |
80 | - checkout | |
81 | - run: | |
82 | name: "auto-generate packages" | |
83 | command: | | |
84 | # Create dist directory if it doesn't exist | |
85 | mkdir ./dist | |
86 | # Clean up | |
87 | rm -rf ./dist/* | |
88 | # Pack source | |
89 | git archive -o ../${CIRCLE_PROJECT_REPONAME}-${CIRCLE_TAG}.tar.gz --format tar.gz --prefix=${CIRCLE_PROJECT_REPONAME}-${CIRCLE_TAG#v}/ ${CIRCLE_TAG} | |
90 | # Use latest installed python3 from pyenv | |
91 | export PYENV_VERSION="$(pyenv versions | grep -Po '\b3\.\d+\.\d+' | tail -1)" | |
92 | #pip install packagecore | |
93 | #packagecore -c misc/packagecore/packagecore.yaml -o ./dist/ ${CIRCLE_TAG#v} | |
94 | # Move source pack to dist | |
95 | mv ../${CIRCLE_PROJECT_REPONAME}-${CIRCLE_TAG}.tar.gz ./dist/ | |
96 | ||
97 | - run: | |
98 | name: "publish to GitHub" | |
99 | command: | | |
100 | go get github.com/tcnksm/ghr | |
101 | ghr -t ${GITHUB_API_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -replace ${CIRCLE_TAG} ./dist/ | |
102 | ||
103 | workflows: | |
104 | version: 2 | |
105 | ||
106 | test: | |
107 | jobs: &all-tests | |
108 | - compile | |
109 | ||
110 | nightly: | |
111 | triggers: | |
112 | - schedule: | |
113 | cron: "0 0 * * 6" | |
114 | filters: | |
115 | branches: | |
116 | only: | |
117 | - master | |
118 | jobs: *all-tests | |
119 | ||
120 | publish-github-release: | |
121 | jobs: | |
122 | - package-and-publish: | |
123 | filters: | |
124 | tags: | |
125 | only: /^v.*/ | |
126 | branches: | |
127 | ignore: /.*/ |
0 | 0 | --- |
1 | Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,-misc-unused-parameters,google-runtime-int,-llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-braces-around-statements,-readability-isolate-declaration' | |
1 | Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,-misc-unused-parameters,google-runtime-int,-llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-braces-around-statements,-readability-function-cognitive-complexity,-readability-isolate-declaration,-bugprone-easily-swappable-parameters' | |
2 | 2 | WarningsAsErrors: '*' |
3 | 3 | HeaderFilterRegex: '.*(?<!lookup3.c)$' |
4 | 4 | FormatStyle: 'file' |
0 | custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RMLTQ76JSXJ4Q | |
0 | github: jarun |
0 | name: ci | |
1 | ||
2 | on: | |
3 | push: | |
4 | branches: [master] | |
5 | pull_request: | |
6 | branches: [master] | |
7 | ||
8 | jobs: | |
9 | macOS-gcc: | |
10 | runs-on: macOS-latest | |
11 | steps: | |
12 | - uses: actions/checkout@v2 | |
13 | - name: Compile with gcc | |
14 | env: | |
15 | CC: gcc | |
16 | run: | | |
17 | brew upgrade python | |
18 | pip3 install logilab-common | |
19 | pip3 install pytest | |
20 | make clean | |
21 | make | |
22 | pytest ./test.py | |
23 | make clean | |
24 | macOS-clang: | |
25 | runs-on: macOS-latest | |
26 | steps: | |
27 | - uses: actions/checkout@v2 | |
28 | - name: Compile with clang | |
29 | env: | |
30 | CC: clang | |
31 | run: | | |
32 | brew update | |
33 | brew upgrade python | |
34 | pip3 install logilab-common | |
35 | pip3 install pytest | |
36 | brew install llvm | |
37 | export PATH="/usr/local/opt/llvm/bin:$PATH" | |
38 | make clean | |
39 | make | |
40 | pytest ./test.py | |
41 | make clean | |
42 | clang-tidy **/*.h **/*.c -- -Iinc |
0 | name: 'Lock threads' | |
1 | ||
2 | on: | |
3 | schedule: | |
4 | - cron: '0 0 * * *' | |
5 | ||
6 | jobs: | |
7 | lock: | |
8 | runs-on: ubuntu-latest | |
9 | steps: | |
10 | - uses: dessant/lock-threads@v2 | |
11 | with: | |
12 | github-token: ${{ github.token }} | |
13 | issue-lock-inactive-days: '30' | |
14 | issue-lock-reason: '' | |
15 | pr-lock-inactive-days: '30' | |
16 | pr-lock-reason: '' |
0 | 0 | language: c |
1 | arch : | |
2 | - amd64 | |
3 | - ppc64le | |
1 | 4 | sudo: required |
2 | 5 | services: |
3 | 6 | - docker |
8 | 11 | matrix: |
9 | 12 | include: |
10 | 13 | - os: linux |
11 | dist: trusty | |
14 | dist: focal | |
12 | 15 | compiler: gcc |
13 | 16 | - os: linux |
14 | dist: trusty | |
17 | dist: focal | |
15 | 18 | compiler: clang |
16 | 19 | # - os: linux |
17 | 20 | # compiler: musl-gcc |
21 | 24 | # - musl |
22 | 25 | # - musl-dev |
23 | 26 | # - musl-tools |
24 | - os: osx | |
25 | compiler: gcc | |
26 | - os: osx | |
27 | compiler: clang | |
27 | # - os: osx | |
28 | # compiler: gcc | |
29 | # - os: osx | |
30 | # compiler: clang | |
28 | 31 | |
29 | 32 | install: |
30 | 33 | - if [ $TRAVIS_OS_NAME = linux ]; then |
51 | 54 | - rm -rf $REPO/.git |
52 | 55 | - tar -czf $REPO-${TRAVIS_TAG}.tar.gz $REPO |
53 | 56 | - cd $REPO |
54 | - sudo apt-get update -qy | |
55 | - sudo apt-get install -qy python3 python3-pip | |
56 | - sudo python3 -m pip install --upgrade pip | |
57 | - sudo python3 -m pip install --upgrade packagecore setuptools | |
58 | - packagecore -o dist/ "${TRAVIS_TAG#v}" | |
59 | 57 | - mv ../$REPO-${TRAVIS_TAG}.tar.gz dist/ |
60 | 58 | |
61 | 59 | deploy: |
0 | bcal v2.3 | |
1 | 2021-10-09 | |
2 | ||
3 | - show binary conversion output in octets for readabilityA | |
4 | - handle `^D` | |
5 | - quit program or 'exit' or 'quit' | |
6 | - fix broken terminal with calc as backend on "undefined input" (#36) | |
7 | ||
8 | ------------------------------------------------------------------------------- | |
9 | ||
0 | 10 | bcal v2.2 |
1 | 11 | 2020-01-07 |
2 | 12 |
2 | 2 | <p align="center"> |
3 | 3 | <a href="https://github.com/jarun/bcal/releases/latest"><img src="https://img.shields.io/github/release/jarun/bcal.svg?maxAge=600" alt="Latest release" /></a> |
4 | 4 | <a href="https://repology.org/project/bcal/versions"><img src="https://repology.org/badge/tiny-repos/bcal.svg" alt="Availability"></a> |
5 | <a href="https://travis-ci.org/jarun/bcal"><img src="https://img.shields.io/travis/jarun/bcal/master.svg" alt="Build Status" /></a> | |
5 | <a href="https://travis-ci.org/jarun/bcal"><img src="https://img.shields.io/travis/jarun/bcal/master.svg?label=travis" alt="Travis CI Status" /></a> | |
6 | <a href="https://github.com/jarun/bcal/actions"><img src="https://github.com/jarun/bcal/workflows/ci/badge.svg?branch=master" alt="GitHub CI Status" /></a> | |
6 | 7 | <a href="https://scan.coverity.com/projects/jarun-bcal"><img src="https://img.shields.io/coverity/scan/17148.svg" alt="Coverity Scan Build Status" /></a> |
7 | <a href="https://github.com/jarun/bcal/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-GPLv3-yellow.svg?maxAge=2592000" alt="License" /></a> | |
8 | <a href="https://github.com/jarun/bcal/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-GPLv3-yellowgreen.svg?maxAge=2592000" alt="License" /></a> | |
8 | 9 | </p> |
9 | 10 | |
10 | 11 | <p align="center"> |
11 | <a href="https://asciinema.org/a/168719"><img src="https://asciinema.org/a/168719.png" alt="bcal_asciicast" width="600"/></a> | |
12 | <a href="https://asciinema.org/a/168719"><img src="https://asciinema.org/a/168719.svg" alt="bcal_asciicast" width="600"/></a> | |
12 | 13 | </p> |
13 | 14 | |
14 | `bcal` (*Byte CALculator*) is a REPL CLI utility for storage expression evaluation, unit conversion and address calculation. If you can't calculate the hex address offset for (512 - 16) MiB, or the value when the 43<sup>rd</sup> bit of a 64-bit address is set mentally, `bcal` is for you. | |
15 | ||
16 | It has a [`bc`](https://www.gnu.org/software/bc/manual/html_mono/bc.html) mode for general-purpose numerical calculations. Alternatively, it can also invoke [`calc`](http://www.isthe.com/chongo/tech/comp/calc/) which works better with expressions involving multiple bases. | |
17 | ||
18 | `bcal` follows Ubuntu's standard unit conversion and notation [policy](https://wiki.ubuntu.com/UnitsPolicy). Only 64-bit operating systems are supported. | |
19 | ||
20 | *Love smart and efficient utilities? Explore [my repositories](https://github.com/jarun?tab=repositories). Buy me a cup of coffee if they help you.* | |
21 | ||
22 | <p align="center"> | |
23 | <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RMLTQ76JSXJ4Q"><img src="https://img.shields.io/badge/PayPal-donate-1eb0fc.svg" alt="Donate via PayPal!" /></a> | |
24 | </p> | |
15 | `bcal` (*Byte CALculator*) is a REPL CLI utility for storage expression (e.g. `"(2GiB * 2) / (2KiB >> 2)"`) evaluation, SI/IEC conversion, byte address calculation, base conversion and LBA/CHS calculation. It's very useful for those who deal with bits, bytes, addresses and binary prefixes frequently. | |
16 | ||
17 | It has a [`bc`](https://www.gnu.org/software/bc/manual/html_mono/bc.html) mode for general-purpose numerical calculations. Alternatively, it can also invoke the more featured [`calc`](http://www.isthe.com/chongo/tech/comp/calc/) which works better with expressions involving multiple bases. | |
18 | ||
19 | `bcal` uses [SI and IEC binary prefixes](https://en.wikipedia.org/wiki/Binary_prefix) and supports 64-bit Operating Systems only. | |
25 | 20 | |
26 | 21 | ### Table of Contents |
27 | 22 | |
44 | 39 | - REPL and single execution modes |
45 | 40 | - evaluate arithmetic expressions involving storage units |
46 | 41 | - perform general purpose calculations (using bc or calc) |
42 | - works with piped input or file redirection | |
47 | 43 | - convert to IEC/SI standard data storage units |
48 | 44 | - interactive mode with the last valid result stored for reuse |
49 | 45 | - show the address in bytes |
97 | 93 | `bcal` can be compiled and installed from source in the Termux environment on `aarch64` Android devices. Instructions: |
98 | 94 | |
99 | 95 | ``` |
100 | $ aria2c https://github.com/jarun/bcal/archive/master.zip | |
96 | $ wget https://github.com/jarun/bcal/archive/master.zip | |
101 | 97 | $ unzip bcal-master.zip |
102 | 98 | $ cd bcal-master/ |
103 | 99 | $ pkg install make clang readline-dev |
117 | 113 | positional arguments: |
118 | 114 | expr expression in decimal/hex operands |
119 | 115 | N [unit] capacity in B/KiB/MiB/GiB/TiB/kB/MB/GB/TB |
120 | see https://wiki.ubuntu.com/UnitsPolicy | |
116 | https://en.wikipedia.org/wiki/Binary_prefix | |
121 | 117 | default unit is B (byte), case is ignored |
122 | 118 | N can be decimal or '0x' prefixed hex value |
123 | 119 | |
143 | 139 | |
144 | 140 | - **Interactive mode**: `bcal` enters the REPL mode if no arguments are provided. Storage unit conversion, base conversion and expression evaluation are supported in this mode. The last valid result is stored in the variable **r**. |
145 | 141 | - **Expression**: Expression passed as argument in one-shot mode must be within double quotes. Inner spaces are ignored. Supported operators: `+`, `-`, `*`, `/`, `%` and C bitwise operators (except `~` due to storage width dependency). |
146 | - **N [unit]**: `N` can be a decimal or '0x' prefixed hex value. `unit` can be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB following Ubuntu policy. Default is byte. As all of these tokens are unique, `unit` is case-insensitive. | |
142 | - **N [unit]**: `N` can be a decimal or '0x' prefixed hex value. `unit` can be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB. Default is Byte. As all of these tokens are unique, `unit` is case-insensitive. | |
147 | 143 | - **Numeric representation**: Decimal and hex are recognized in expressions and unit conversions. Binary is also recognized in other operations. |
148 | 144 | - **Syntax**: Prefix hex inputs with `0x`, binary inputs with `0b`. |
149 | 145 | - **Precision**: 128 bits if `__uint128_t` is available or 64 bits for numerical conversions. Floating point operations use `long double`. Negative values in storage expressions are unsupported. Only 64-bit operating systems are supported. |
204 | 200 | bcal> b // Interactive mode |
205 | 201 | bc vars: scale = 10, ibase = 10, last = r |
206 | 202 | bc> 3.5 * 2.1 + 5.7 |
207 | 8. Help and additional information. | |
208 | ||
209 | $ man bcal | |
210 | $ bcal -h | |
203 | 8. Pipe input. | |
204 | ||
205 | $ printf '15 kib + 15 gib \n r / 5' | bcal -m | |
206 | $ printf '15 + 15 + 2' | bcal -bm | |
207 | 9. Redirect from file. | |
208 | ||
209 | $ cat expr | |
210 | 15 gib + 15 kib | |
211 | r / 5 | |
212 | $ bcal -m < expr | |
213 | 10. Use as a general-purpose calculator. | |
214 | ||
215 | $ bcal -b | |
211 | 216 | |
212 | 217 | ### Testing |
213 | 218 |
0 | .TH "BCAL" "1" "07 Jan 2020" "Version 2.2" "User Commands" | |
0 | .TH "BCAL" "1" "15 Oct 2021" "Version 2.3" "User Commands" | |
1 | 1 | .SH NAME |
2 | 2 | bcal \- Storage expression calculator. |
3 | 3 | .SH SYNOPSIS |
4 | 4 | .B bcal [-c N] [-f loc] [-s bytes] [expr] [N [unit]] [-b [expr]] [-m] [-d] [-h] |
5 | 5 | .SH DESCRIPTION |
6 | 6 | .B bcal |
7 | (Byte CALculator) is a command-line utility to help with numerical calculations and expressions involving data storage units, addressing, base conversion etc. | |
8 | .PP | |
9 | It invokes GNU \fBbc\fR for non-storage expressions. Alternatively, it can also invoke \fBcalc\fR (\fIhttp://www.isthe.com/chongo/tech/comp/calc/\fR) which works better with expressions involving multiple bases. To use \fBcalc\fR: | |
7 | (Byte CALculator) is a command-line utility to help with numerical calculations and expressions involving binary prefixes, SI/IEC conversion, byte addressing, base conversion, LBA/CHS calculation etc. | |
8 | .PP | |
9 | It invokes GNU \fBbc\fR for non-storage expressions. Alternatively, it can also invoke \fBcalc\fR (\fIhttp://www.isthe.com/chongo/tech/comp/calc/\fR). To use \fBcalc\fR: | |
10 | 10 | .PP |
11 | 11 | .EX |
12 | 12 | .B export BCAL_USE_CALC=1 |
13 | 13 | .EE |
14 | 14 | .PP |
15 | \fBbcal\fR follows Ubuntu's standard unit conversion and notation policy: | |
16 | .br | |
17 | .I https://wiki.ubuntu.com/UnitsPolicy | |
15 | \fBbcal\fR uses [SI and IEC binary prefixes]: | |
16 | .I https://en.wikipedia.org/wiki/Binary_prefix | |
18 | 17 | .PP |
19 | 18 | .SH FEATURES |
20 | 19 | .PP |
21 | 20 | * evaluate arithmetic expressions involving storage units |
22 | 21 | * perform general purpose calculations (using bc or calc) |
22 | * works with piped input or file redirection | |
23 | 23 | * convert to IEC/SI standard data storage units |
24 | 24 | * interactive mode with the last valid result stored for reuse |
25 | 25 | * show the address in bytes |
37 | 37 | \fBExpression\fR: Expression passed as argument in one-shot mode must be within double quotes. Inner spaces are ignored. Supported operators: +, -, *, /, % and C bitwise operators (except ~ due to storage width dependency). |
38 | 38 | .PP |
39 | 39 | .IP 3. 4 |
40 | \fBN [unit]\fR: \fIN\fR can be a decimal or '0x' prefixed hex value. \fIunit\fR can be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB following Ubuntu policy. Default is byte. As all of these tokens are unique, \fIunit\fR is case-insensitive. | |
40 | \fBN [unit]\fR: \fIN\fR can be a decimal or '0x' prefixed hex value. \fIunit\fR can be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB. Default is Byte. As all of these tokens are unique, \fIunit\fR is case-insensitive. | |
41 | 41 | .PP |
42 | 42 | .IP 4. 4 |
43 | 43 | \fBNumeric representation\fR: Decimal and hex are recognized in expressions and unit conversions. Binary is also recognized in other operations. |
183 | 183 | .B bc vars: scale = 10, ibase = 10, last = r |
184 | 184 | .B bc> 3.5 * 2.1 + 5.7 |
185 | 185 | .EE |
186 | .PP | |
187 | .IP 8. 4 | |
188 | Pipe input. | |
189 | .PP | |
190 | .EX | |
191 | .IP | |
192 | .B $ printf '15 kib + 15 gib \en r / 5' | bcal -m | |
193 | .B $ printf '15 + 15 + 2' | bcal -bm | |
194 | .EE | |
195 | .PP | |
196 | .IP 9. 4 | |
197 | Redirect from file. | |
198 | .PP | |
199 | .EX | |
200 | .IP | |
201 | .B $ cat expr | |
202 | .B 15 gib + 15 kib | |
203 | .B r / 5 | |
204 | .B $ bcal -m < expr | |
205 | .EE | |
206 | .PP | |
207 | .IP 10. 4 | |
208 | Use as a general-purpose calculator. | |
209 | .PP | |
210 | .EX | |
211 | .IP | |
212 | .B $ bcal -b | |
213 | .EE | |
186 | 214 | .SH AUTHORS |
187 | 215 | Arun Prakash Jana <engineerarun@gmail.com> |
188 | 216 | .SH HOME |
22 | 22 | #include <stdio.h> |
23 | 23 | #include <string.h> |
24 | 24 | #include <unistd.h> |
25 | #include <sys/types.h> | |
26 | #include <sys/wait.h> | |
27 | #include <signal.h> | |
25 | 28 | #include <readline/history.h> |
26 | 29 | #include <readline/readline.h> |
27 | 30 | #include "dslib.h" |
38 | 41 | #define FLOAT_WIDTH 40 |
39 | 42 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
40 | 43 | #define MAX_BITS 128 |
41 | #define _ALIGNMENT_MASK 0xF | |
44 | #define ALIGNMENT_MASK_4BIT 0xF | |
42 | 45 | |
43 | 46 | typedef unsigned char bool; |
44 | 47 | typedef unsigned char uchar; |
70 | 73 | uchar loglvl : 2; |
71 | 74 | } settings; |
72 | 75 | |
73 | static char *VERSION = "2.2"; | |
76 | static char *VERSION = "2.3"; | |
74 | 77 | static char *units[] = {"b", "kib", "mib", "gib", "tib", "kb", "mb", "gb", "tb"}; |
75 | 78 | static char *logarr[] = {"ERROR", "WARNING", "INFO", "DEBUG"}; |
76 | 79 | |
117 | 120 | static ulong *s, *d; |
118 | 121 | static size_t len, blocks; |
119 | 122 | static const uint lsize = sizeof(ulong); |
120 | static const uint _WSHIFT = (sizeof(ulong) == 8) ? 3 : 2; | |
123 | static const uint WORD_SHIFT = (sizeof(ulong) == 8) ? 3 : 2; | |
121 | 124 | |
122 | 125 | if (!src || !dest || !n) |
123 | 126 | return 0; |
133 | 136 | * To enable -O3 ensure src and dest are 16-byte aligned |
134 | 137 | * More info: http://www.felixcloutier.com/x86/MOVDQA.html |
135 | 138 | */ |
136 | if ((n >= lsize) && (((ulong)src & _ALIGNMENT_MASK) == 0 && ((ulong)dest & _ALIGNMENT_MASK) == 0)) { | |
139 | if ((n >= lsize) && (((ulong)src & ALIGNMENT_MASK_4BIT) == 0 | |
140 | && ((ulong)dest & ALIGNMENT_MASK_4BIT) == 0)) { | |
137 | 141 | s = (ulong *)src; |
138 | 142 | d = (ulong *)dest; |
139 | blocks = n >> _WSHIFT; | |
143 | blocks = n >> WORD_SHIFT; | |
140 | 144 | n &= lsize - 1; |
141 | 145 | |
142 | 146 | while (blocks) { |
185 | 189 | |
186 | 190 | log(DEBUG, "expression: \"%s\"\n", expr); |
187 | 191 | |
192 | if (!strcmp(expr, "quit") || !strcmp(expr, "exit")) | |
193 | exit(0); | |
194 | ||
188 | 195 | if (pipe(pipe_pc) == -1 || pipe(pipe_cp) == -1) { |
189 | 196 | log(ERROR, "pipe()!\n"); |
190 | 197 | exit(EXIT_FAILURE); |
207 | 214 | dup2(pipe_cp[1], STDOUT_FILENO); // Give stdout to parent |
208 | 215 | dup2(pipe_cp[1], STDERR_FILENO); // Give stderr to parent |
209 | 216 | |
210 | ret = execlp(ptr, ptr, (char*) NULL); | |
217 | int ret = execlp(ptr, ptr, (char*) NULL); | |
211 | 218 | log(ERROR, "execlp() failed!\n"); |
212 | 219 | exit(ret); |
213 | 220 | } |
262 | 269 | exit(-1); |
263 | 270 | } |
264 | 271 | |
272 | if (write(pipe_pc[1], "quit\n", 5) != 5) { | |
273 | log(ERROR, "write(7)! [%s]\n", strerror(errno)); | |
274 | exit(-1); | |
275 | } | |
276 | ||
277 | if (cfg.calc) | |
278 | kill(pid, SIGTERM); | |
279 | ||
280 | close(pipe_cp[0]); | |
281 | close(pipe_pc[1]); | |
282 | ||
265 | 283 | buffer[ret] = '\0'; |
266 | 284 | |
267 | 285 | if (buffer[0] != '(') { |
270 | 288 | ++ptr; |
271 | 289 | |
272 | 290 | printf("%s", ptr); |
291 | if (cfg.calc && strstr(ptr, "is undefined")) | |
292 | return -1; | |
293 | ||
273 | 294 | len = bstrlcpy(lastres.p, ptr, NUM_LEN); |
274 | 295 | |
275 | 296 | /* remove newline appended at the end of result by bc */ |
301 | 322 | static void binprint(maxuint_t n) |
302 | 323 | { |
303 | 324 | int count = MAX_BITS - 1; |
304 | char binstr[MAX_BITS + 1] = {0}; | |
325 | int pos = MAX_BITS + (MAX_BITS >> 2) - 1; | |
326 | char binstr[MAX_BITS + (MAX_BITS >> 2) + 1] = {0}; | |
305 | 327 | |
306 | 328 | if (!n) { |
307 | printf("0b0"); | |
329 | printf("0"); | |
308 | 330 | return; |
309 | 331 | } |
310 | 332 | |
311 | 333 | while (n && count >= 0) { |
312 | binstr[count] = "01"[n & 1]; | |
334 | binstr[pos] = "01"[n & 1]; | |
335 | --pos; | |
336 | n >>= 1; | |
337 | if (n && count && !(count & 7)) { | |
338 | binstr[pos] = ' '; | |
339 | --pos; | |
340 | } | |
313 | 341 | --count; |
314 | n >>= 1; | |
315 | } | |
316 | ||
317 | ++count; | |
318 | ||
319 | printf("0b%s", binstr + count); | |
342 | } | |
343 | ||
344 | ++pos; | |
345 | ||
346 | printf("%s", binstr + pos); | |
320 | 347 | } |
321 | 348 | |
322 | 349 | static char *getstr_u128(maxuint_t n, char *buf) |
351 | 378 | |
352 | 379 | static void printval(maxfloat_t val, char *unit) |
353 | 380 | { |
354 | if (val - (maxuint_t)val == 0) | |
381 | if (val - (maxuint_t)val == 0) // NOLINT | |
355 | 382 | printf("%40s %s\n", getstr_u128((maxuint_t)val, uint_buf), unit); |
356 | 383 | else |
357 | 384 | printf("%s %s\n", getstr_f128(val, float_buf), unit); |
539 | 566 | /* Convert and print in IEC standard units */ |
540 | 567 | |
541 | 568 | printf("\n IEC standard (base 2)\n\n"); |
542 | val = bytes / (maxfloat_t)1024; | |
569 | val = (maxfloat_t)bytes / 1024; | |
543 | 570 | printval(val, "KiB"); |
544 | 571 | |
545 | val = bytes / (maxfloat_t)(1 << 20); | |
572 | val = (maxfloat_t)bytes / (1 << 20); | |
546 | 573 | printval(val, "MiB"); |
547 | 574 | |
548 | val = bytes / (maxfloat_t)(1 << 30); | |
575 | val = (maxfloat_t)bytes / (1 << 30); | |
549 | 576 | printval(val, "GiB"); |
550 | 577 | |
551 | val = bytes / (maxfloat_t)((maxuint_t)1 << 40); | |
578 | val = (maxfloat_t)bytes / ((unsigned long long)1 << 40); | |
552 | 579 | printval(val, "TiB"); |
553 | 580 | |
554 | 581 | /* Convert and print in SI standard values */ |
555 | 582 | |
556 | 583 | printf("\n SI standard (base 10)\n\n"); |
557 | val = bytes / (maxfloat_t)1000; | |
584 | val = (maxfloat_t)bytes / 1000; | |
558 | 585 | printval(val, "kB"); |
559 | 586 | |
560 | val = bytes / (maxfloat_t)1000000; | |
587 | val = (maxfloat_t)bytes / 1000000; | |
561 | 588 | printval(val, "MB"); |
562 | 589 | |
563 | val = bytes / (maxfloat_t)1000000000; | |
590 | val = (maxfloat_t)bytes / 1000000000; | |
564 | 591 | printval(val, "GB"); |
565 | 592 | |
566 | val = bytes / (maxfloat_t)1000000000000; | |
593 | val = (maxfloat_t)bytes / 1000000000000; | |
567 | 594 | printval(val, "TB"); |
568 | 595 | |
569 | 596 | return bytes; |
1020 | 1047 | return FALSE; |
1021 | 1048 | } |
1022 | 1049 | |
1023 | ||
1024 | *lba = param[3] * param[4] * param[0]; /* MH * MS * C */ | |
1025 | *lba += param[4] * param[1]; /* MS * H */ | |
1050 | *lba = (maxuint_t)param[3] * param[4] * param[0]; /* MH * MS * C */ | |
1051 | *lba += (maxuint_t)param[4] * param[1]; /* MS * H */ | |
1026 | 1052 | |
1027 | 1053 | *lba += param[2] - 1; /* S - 1 */ |
1028 | 1054 | |
1151 | 1177 | positional arguments:\n\ |
1152 | 1178 | expr expression in decimal/hex operands\n\ |
1153 | 1179 | N [unit] capacity in B/KiB/MiB/GiB/TiB/kB/MB/GB/TB\n\ |
1154 | see https://wiki.ubuntu.com/UnitsPolicy\n\ | |
1180 | https://en.wikipedia.org/wiki/Binary_prefix\n\ | |
1155 | 1181 | default unit is B (byte), case is ignored\n\ |
1156 | 1182 | N can be decimal or '0x' prefixed hex value\n\n\ |
1157 | 1183 | optional arguments:\n\ |
2110 | 2136 | |
2111 | 2137 | printf("q/double Enter -> quit, ? -> help\n"); |
2112 | 2138 | while ((tmp = readline(prompt)) != NULL) { |
2139 | if (!tmp) | |
2140 | exit(0); | |
2141 | ||
2113 | 2142 | /* Quit on double Enter */ |
2114 | 2143 | if (tmp[0] == '\0') { |
2115 | 2144 | if (enters == 1) { |
148 | 148 | |
149 | 149 | b'ERROR: invalid input\n\n', # 49 |
150 | 150 | b'ERROR: invalid input\n\n', # 50 |
151 | b' (b) 0b0\n (d) 0\n (h) 0x0\n\n', # 51 | |
152 | b' (b) 0b11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 52 | |
153 | b' (b) 0b11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 53 | |
154 | b' (b) 0b11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 54 | |
155 | b' (b) 0b1111111111111111111111111111111111111111111111111111111111111111\n (d) 18446744073709551615\n (h) 0xffffffffffffffff\n\n', # 55 | |
151 | b' (b) 0\n (d) 0\n (h) 0x0\n\n', # 51 | |
152 | b' (b) 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 52 | |
153 | b' (b) 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 53 | |
154 | b' (b) 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111\n (d) 340282366920938463463374607431768211455\n (h) 0xffffffffffffffffffffffffffffffff\n\n', # 54 | |
155 | b' (b) 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111\n (d) 18446744073709551615\n (h) 0xffffffffffffffff\n\n', # 55 | |
156 | 156 | b'ERROR: invalid input\n\n', # 56 |
157 | 157 | b'WARNING: result truncated\n0\n', # 57 |
158 | 158 | b'0\n', # 58 |