Compare commits
9 Commits
aa6b0cd453
...
64bcbfbc45
Author | SHA1 | Date |
---|---|---|
Alexander Andreev | 64bcbfbc45 | |
Alexander Andreev | 18a593e800 | |
Alexander Andreev | cd84aee61f | |
Alexander Andreev | caa57721f7 | |
Alexander Andreev | ca7859f8f8 | |
Alexander Andreev | 69ff91861b | |
Alexander Andreev | f4ce191ae7 | |
Alexander Andreev | b0f1b367d2 | |
Alexander Andreev | 044b898db3 |
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021 Alexander "Arav" Andreev <me@arav.top>
|
||||
Copyright (c) 2021-2023 Alexander "Arav" Andreev <me@arav.su>
|
||||
|
||||
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:
|
||||
|
||||
|
|
18
Makefile
18
Makefile
|
@ -1,15 +1,15 @@
|
|||
TARGET=kwh-cost
|
||||
|
||||
CFLAGS=--std=c2x -O3 -Wall -Werror -Wextra -pedantic
|
||||
DESTDIR:=
|
||||
PREFIX:=/usr/local
|
||||
|
||||
CFLAGS=--std=c99 -O3 -Wall -Werror -Wextra -pedantic
|
||||
|
||||
SRC_DIR=src
|
||||
OBJ_DIR=obj
|
||||
|
||||
DESTDIR=/
|
||||
|
||||
.PHONY: all ${TARGET} clean install uninstall
|
||||
|
||||
all: ${TARGET}
|
||||
.PHONY: clean install uninstall test
|
||||
|
||||
${TARGET}: ${SRC_DIR}/main.c ${SRC_DIR}/main.h
|
||||
${CC} ${CFLAGS} ${LDFLAGS} $< -o $@
|
||||
|
@ -18,12 +18,12 @@ clean:
|
|||
rm ${TARGET}
|
||||
|
||||
install:
|
||||
install -Dm755 ${TARGET} ${DESTDIR}usr/bin/${TARGET}
|
||||
install -Dm644 LICENSE ${DESTDIR}usr/share/licenses/${TARGET}/LICENSE
|
||||
install -Dm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
install -Dm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
|
||||
|
||||
uninstall:
|
||||
rm -f ${DESTDIR}usr/bin/${TARGET}
|
||||
rm -rf ${DESTDIR}usr/share/licenses/${TARGET}
|
||||
rm -f ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
rm -rf ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
|
||||
|
||||
test:
|
||||
@echo -n "Does input \"3.68 120 30d\" gives 317.95? "
|
||||
|
|
12
PKGBUILD
12
PKGBUILD
|
@ -1,16 +1,16 @@
|
|||
# Maintainer: Alexander "Arav" Andreev <me@arav.top>
|
||||
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
|
||||
pkgname=kwh-cost
|
||||
pkgver=1.1.0
|
||||
pkgver=1.1.1
|
||||
pkgrel=1
|
||||
pkgdesc="Calculates cost of electricity."
|
||||
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
|
||||
url="https://git.arav.top/Arav/kwh-cost"
|
||||
url="https://git.arav.su/Arav/kwh-cost"
|
||||
license=('MIT')
|
||||
makedepends=('git' 'gcc')
|
||||
makedepends=('gcc')
|
||||
provides=('kwh-cost')
|
||||
conflicts=('kwh-cost')
|
||||
options=('strip')
|
||||
source=('https://git.arav.top/Arav/kwh-cost/archive/1.1.0.tar.gz')
|
||||
source=("${pkgver}.tar.gz::https://git.arav.su/Arav/kwh-cost/archive/${pkgver}.tar.gz")
|
||||
md5sums=('SKIP')
|
||||
|
||||
build() {
|
||||
|
@ -20,5 +20,5 @@ build() {
|
|||
|
||||
package() {
|
||||
cd "$srcdir/$pkgname"
|
||||
make DESTDIR="$pkgdir/" install
|
||||
make DESTDIR="$pkgdir/" PREFIX="/usr" install
|
||||
}
|
36
src/main.c
36
src/main.c
|
@ -4,27 +4,25 @@
|
|||
|
||||
#include "main.h"
|
||||
|
||||
int main(const int argc, const char * const * const argv) {
|
||||
int main(const int argc, const char *const *const argv) {
|
||||
price_t price;
|
||||
wattage_t wattage;
|
||||
interval_t interval = 0;
|
||||
|
||||
int res = parse_arguments(argc, argv, &price, &wattage, &interval);
|
||||
if (res != 0) {
|
||||
if (res == 1)
|
||||
print_usage();
|
||||
else if (res == 2)
|
||||
print_version();
|
||||
exit(0);
|
||||
}
|
||||
switch (res) {
|
||||
case 1: print_usage(); exit(0);
|
||||
case 2: print_version(); exit(0);
|
||||
case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); exit(1);
|
||||
}
|
||||
|
||||
printf("%.2Lf\n", (price * wattage) * interval);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_arguments(const int argc, const char * const * const argv,
|
||||
price_t * const price, wattage_t * const wattage, interval_t * const interval) {
|
||||
int parse_arguments(const int argc, const char *const *const argv,
|
||||
price_t *const price, wattage_t *const wattage, interval_t *const interval) {
|
||||
if (argc < 2) return 1;
|
||||
|
||||
for (int i = 1, pos = 0; i < argc; ++i)
|
||||
|
@ -33,10 +31,12 @@ int parse_arguments(const int argc, const char * const * const argv,
|
|||
return 1;
|
||||
else if (argv[i][1] == 'v' || argv[i][2] == 'v')
|
||||
return 2;
|
||||
else
|
||||
return 99;
|
||||
} else if (argc < 4) {
|
||||
return 1;
|
||||
} else if (pos == 0) {
|
||||
*price = price_to_wattspersecond((price_t)atof(argv[i]));
|
||||
*price = price_kwh_to_wattspersecond((price_t)atof(argv[i]));
|
||||
++pos;
|
||||
} else if (pos == 1) {
|
||||
*wattage = (wattage_t)atof(argv[2]);
|
||||
|
@ -47,7 +47,7 @@ int parse_arguments(const int argc, const char * const * const argv,
|
|||
return 0;
|
||||
}
|
||||
|
||||
inline interval_t interval_to_seconds(const char * const interval) {
|
||||
inline interval_t interval_to_seconds(const char *const interval) {
|
||||
interval_t intrvl = (interval_t)atol(interval);
|
||||
|
||||
switch (interval[strlen(interval) - 1]) {
|
||||
|
@ -65,18 +65,18 @@ inline interval_t interval_to_seconds(const char * const interval) {
|
|||
}
|
||||
}
|
||||
|
||||
inline price_t price_to_wattspersecond(const price_t price) {
|
||||
inline price_t price_kwh_to_wattspersecond(const price_t price) {
|
||||
return price / 1000.f / 3600.f;
|
||||
}
|
||||
|
||||
inline void print_version() {
|
||||
puts("kwh-cost Ver. " VERSION "\n"
|
||||
"Copyright (C) 2021,2022 Alexander \"Arav\" Andreev <me@arav.top>. All rights reserved.\n"
|
||||
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.");
|
||||
fputs("kwh-cost Ver. " VERSION "\n"
|
||||
"Copyright (C) 2021-2023 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
|
||||
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
|
||||
}
|
||||
|
||||
inline void print_usage() {
|
||||
puts("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
|
||||
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
|
||||
"Options:\n"
|
||||
" --help, -h display this usage information.\n"
|
||||
" --version, -v display program version.\n"
|
||||
|
@ -87,5 +87,5 @@ inline void print_usage() {
|
|||
" this much power to be constantly used.\n"
|
||||
" You may specify other time units such as:\n"
|
||||
" m'inute, h'our, d'ay. Example: 5d 3h.\n"
|
||||
" All given intervals will be summed up.");
|
||||
" All given intervals will be summed up.\n", stderr);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ typedef long double wattage_t;
|
|||
typedef unsigned long interval_t;
|
||||
|
||||
int parse_arguments(const int argc, const char *const *const argv,
|
||||
price_t * const price, wattage_t * const wattage, interval_t * const interval);
|
||||
price_t *const price, wattage_t *const wattage, interval_t *const interval);
|
||||
|
||||
inline interval_t interval_to_seconds(const char * const interval);
|
||||
inline price_t price_to_wattspersecond(const price_t price);
|
||||
inline interval_t interval_to_seconds(const char *const interval);
|
||||
inline price_t price_kwh_to_wattspersecond(const price_t price);
|
||||
|
||||
inline void print_version();
|
||||
inline void print_usage();
|
||||
|
|
Loading…
Reference in New Issue