commit 2d61e19da96ca59e0cd196d8892d31922154a73a
Author: Claudio Alessi <smoppy@gmail.com>
Date: Wed, 13 Apr 2022 10:24:20 +0200
initial commit
Diffstat:
A | LICENSE | | | 21 | +++++++++++++++++++++ |
A | Makefile | | | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | README.md | | | 8 | ++++++++ |
A | config.mk | | | 25 | +++++++++++++++++++++++++ |
A | sbc.1 | | | 13 | +++++++++++++ |
A | sbc.c | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
6 files changed, 163 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT/X Consortium License
+
+© 2022 Claudio Alessi <smoppy at gmail dot com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/Makefile b/Makefile
@@ -0,0 +1,57 @@
+# sbc - simple base converter
+# See LICENSE file for copyright and license details.
+
+include config.mk
+
+APPNAME=sbc
+SRC = ${APPNAME}.c
+OBJ = ${SRC:.c=.o}
+
+all: options ${APPNAME}
+
+options:
+ @echo ${APPNAME} build options:
+ @echo "CFLAGS = ${CFLAGS}"
+ @echo "LDFLAGS = ${LDFLAGS}"
+ @echo "CC = ${CC}"
+
+.c.o:
+ @echo CC $<
+ @${CC} -c ${CFLAGS} $<
+
+${OBJ}: config.mk
+
+${APPNAME}: ${OBJ}
+ @echo CC -o $@
+ @${CC} -o $@ ${OBJ} ${LDFLAGS}
+
+clean:
+ @echo cleaning
+ @rm -f ${APPNAME} ${OBJ} ${APPNAME}-${VERSION}.tar.gz
+
+dist: clean
+ @echo creating dist tarball
+ @mkdir -p ${APPNAME}-${VERSION}
+ @cp -R LICENSE Makefile README config.mk \
+ ${APPNAME}.1 ${SRC} ${APPNAME}-${VERSION}
+ @tar -cf ${APPNAME}-${VERSION}.tar ${APPNAME}-${VERSION}
+ @gzip ${APPNAME}-${VERSION}.tar
+ @rm -rf ${APPNAME}-${VERSION}
+
+install: all
+ @echo installing executable file to ${DESTDIR}${PREFIX}/bin
+ @mkdir -p ${DESTDIR}${PREFIX}/bin
+ @cp -f ${APPNAME} ${DESTDIR}${PREFIX}/bin
+ @chmod 755 ${DESTDIR}${PREFIX}/bin/${APPNAME}
+ @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
+ @mkdir -p ${DESTDIR}${MANPREFIX}/man1
+ @sed "s/VERSION/${VERSION}/g" < ${APPNAME}.1 > ${DESTDIR}${MANPREFIX}/man1/${APPNAME}.1
+ @chmod 644 ${DESTDIR}${MANPREFIX}/man1/${APPNAME}.1
+
+uninstall:
+ @echo removing executable file from ${DESTDIR}${PREFIX}/bin
+ @rm -f ${DESTDIR}${PREFIX}/bin/${APPNAME}
+ @echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
+ @rm -f ${DESTDIR}${MANPREFIX}/man1/${APPNAME}.1
+
+.PHONY: all options clean dist install uninstall
diff --git a/README.md b/README.md
@@ -0,0 +1,8 @@
+simple base converter
+=====================
+sbc is a tool to convert between bases. The given value is converted and
+printed in decimal, hexadecimal, octal and binary.
+
+Status
+------
+sbc is considered complete and no further development is expected to happen.
diff --git a/config.mk b/config.mk
@@ -0,0 +1,25 @@
+# sbc - simple base converter
+VERSION = 0.1
+
+# Customize below to fit your system
+
+# paths
+PREFIX = /usr/local
+MANPREFIX = ${PREFIX}/share/man
+
+# includes and libs
+INCS =
+LIBS =
+
+# flags
+CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\"
+#CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
+CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os ${INCS} ${CPPFLAGS}
+LDFLAGS = -s ${LIBS}
+
+# Solaris
+#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\"
+#LDFLAGS = ${LIBS}
+
+# compiler and linker
+CC = cc
diff --git a/sbc.1 b/sbc.1
@@ -0,0 +1,13 @@
+.TH SBC 1 sbc\-VERSION
+.SH NAME
+sbc \- simple base converter
+.SH SYNOPSIS
+.B sbc
+.RB [ \-v ]
+.IR value
+.RB [
+.IR base
+.RB ]
+.SH DESCRIPTION
+sbc is a tool to convert between bases. The given value is converted and
+printed in decimal, hexadecimal, octal and binary.
diff --git a/sbc.c b/sbc.c
@@ -0,0 +1,39 @@
+/* simple base converter */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(int argc, char *argv[]) {
+ unsigned long n;
+ int len, base = 0, i, s;
+
+ if(argc < 2 || argc > 3) {
+ printf("Usage: %s <value> [base]\n", argv[0]);
+ return 1;
+ }
+ if(argv[2])
+ base = atoi(argv[2]);
+ else {
+ len = strlen(argv[1]);
+ if(argv[1][len-1] == 'b' && argv[1][1] != 'x')
+ base = 2;
+ }
+ n = strtoul(argv[1], NULL, base);
+ if(errno == ERANGE || errno == EINVAL)
+ return -1;
+ printf("%lu %#04lx %lo ", n, n, n);
+ for(s = 64 - 8; s > 0 && !(n >> s); s -= 8);
+ for(i = 0; i < 8 && s >= 0; i++) {
+ printf("%d", ((n >> s) << i) & 0x80 ? 1 : 0);
+ if(i == 8 - 1) {
+ s -= 8;
+ i = -1;
+ printf(" ");
+ }
+ }
+ printf("\n");
+ return 0;
+}