Compare commits

...

23 Commits

Author SHA1 Message Date
ad6cbf62af
Remove -flto option. 2024-05-21 04:27:20 +04:00
97664825e2
Remove strip option, this is up to user to decide. 2024-05-20 18:28:08 +04:00
35894ebafc
Remove unused LDFLAGS. 2024-05-20 18:11:44 +04:00
49c2cf90d4
Added a cyan color to a test input. 2024-05-19 23:22:26 +04:00
5f802d2084
Also added a check for build of a TARGET in a test target. 2024-05-19 23:18:05 +04:00
3611b94ab6
Added -p flag to install to preserve timestamps, also added -s flag to strip debug data of an executable . 2024-05-19 23:17:04 +04:00
00f15ca913
Updated README. 2024-05-19 23:15:16 +04:00
6c1288d741
An extra - was removed from -std= CFLAGS option. This prevented compiling by tcc.
Also added -flto to enable link time optimisation.
2024-05-19 22:14:29 +04:00
8e4f505ad0
No .o object files produced, remove it from .gitignore. 2024-05-19 21:58:29 +04:00
79ab374f86
Version was set to 1.1.3. 2024-05-19 21:57:38 +04:00
acc7655e83
Fixed a typo licence -> license. 2024-05-19 21:57:18 +04:00
c657ed047c
Made a proper Makefile as it should be fixing many errors, like ignoring VAR overwriting. Also, wherever meaningful, = was replaced with := that evaluates once. Also made use of so-called "Order-only prerequisites" in an install target to build a program if it is not already built. Also removed a dangerous rm -rf with rm -f for a LICENSE file and rmdir for a directory. 2024-05-19 05:29:13 +04:00
c083462a63
Actually, argv[i] cannot has a length of 0, so let's compare here against 1, meaning there was just a '-' was passed. 2024-05-19 05:23:47 +04:00
a469ae41a2
Replace exit() with simple return; 2024-05-19 05:21:35 +04:00
62a16292ec
main.c -> kwh-cost.c 2024-05-19 04:36:33 +04:00
9a8277dd07
Initialise the uninitialised. 2024-04-27 02:15:34 +04:00
a9a649d280
To make it compile with clang I added void to print_*() funcs' arguments. Also I had to remove inline specifiers because clang was throwing undefined reference. 2024-04-26 04:45:27 +04:00
4b4e64500a
Updated a README.md. 2024-04-26 04:35:58 +04:00
f24c7aafc1
Let's put some more safety to parse_arguments(). 2024-04-26 04:29:26 +04:00
328cbc3b0c
In PKGBUILD DESTDIR was removed in build(), and in package() was set to "$pkgdir". 2024-04-26 04:17:42 +04:00
4e016a3b4f
Do not set DESTDIR in Makefile. 2024-04-26 04:09:34 +04:00
43d86103c4
A version was set to 1.1.2. And a year 2023 was set to 2024. 2024-04-26 04:06:31 +04:00
9112fc018c
src directory as well as a separate header file is absolutely not necessary. Also fixed a typo price_kwh_to_wattspersecond -> price_kwh_to_wattsecond, because because a price per kilowatt-hour is being converted to watt-second. Also added a notice about a licence that is used. 2024-04-26 04:03:48 +04:00
7 changed files with 58 additions and 62 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
*.o
kwh-cost
.vscode

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021-2023 Alexander "Arav" Andreev <me@arav.su>
Copyright (c) 2021-2024 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:

View File

@ -1,30 +1,29 @@
TARGET=kwh-cost
TARGET := kwh-cost
DESTDIR:=
PREFIX:=/usr/local
DESTDIR ?=
PREFIX ?= /usr/local
CFLAGS=--std=c99 -O3 -Wall -Werror -Wextra -pedantic
CC ?= cc
CFLAGS := -std=c99 -O3 -Wall -Werror -Wextra -pedantic
SRC_DIR=src
DESTDIR=/
.PHONY: clean install uninstall test
${TARGET}: ${SRC_DIR}/main.c ${SRC_DIR}/main.h
${CC} ${CFLAGS} ${LDFLAGS} $< -o $@
${TARGET}: ${TARGET}.c
${CC} ${CFLAGS} $< -o $@
clean:
rm ${TARGET}
rm -f ${TARGET}
install:
install -Dm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
install -Dm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
install: | ${TARGET}
install -spDm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
install -pDm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/${TARGET}
rm -rf ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
rm -f ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
rmdir ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
test:
@echo -n "Does input \"3.68 120 30d\" gives 317.95? "
test: | ${TARGET}
@echo -en "Does input \e[36m\"3.68 120 30d\"\e[0m gives 317.95? "
@test "${shell ./kwh-cost 3.68 120 30d}" = "317.95" && { echo -e "\e[32mYES\e[0m"; } || { echo -e "\e[31mNO\e[0m"; }

View File

@ -1,21 +1,20 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=kwh-cost
pkgver=1.1.1
pkgver=1.1.3
pkgrel=1
pkgdesc="Calculates cost of electricity."
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
url="https://git.arav.su/Arav/kwh-cost"
license=('MIT')
options=('strip')
source=("${pkgver}.tar.gz::https://git.arav.su/Arav/kwh-cost/archive/${pkgver}.tar.gz")
md5sums=('SKIP')
build() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/"
make
}
package() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/" PREFIX="/usr" install
make DESTDIR="$pkgdir" PREFIX="/usr" install
}

View File

@ -13,7 +13,7 @@ Options are `-v, --version` to show program's version; and `-h, --help` to show
`price` is a price per kWh. `wattage` is how many Watts being consumed. `interval` is for how long.
`interval` is in seconds, but you can specify a unit. Units are the following:
`m` for minutes; `h` for hours; and `d` for days. There could be as many `interval`s passed as you need.
`m|M` for minutes; `h|H` for hours; and `d|D` for days. There could be as many `interval`s passed as you need.
Here is an example:
@ -27,10 +27,12 @@ Will give 348.29.
### Manually
First run `make` to build a program, and then `make install` to copy executable in `/usr/bin/`. Later, use `make uninstall` to delete it, or just delete `/usr/bin/kwh-cost`.
Just run `make install` to build and copy executable in `/usr/local/bin/`, set a `PREFIX=` variable to change a default `/usr/local` path. Or just run `make` to only build an executable.
Use `make uninstall` to delete it (don't forget about the `PREFIX=` variable if you previously set it), or just delete `${PREFIX}/bin/kwh-cost` by hand.
A `LICENSE` file is also being copied to `${PREFIX}/share/licenses/kwh-cost/LICENSE`.
### For ArchLinux
You can take a [PKGBUILD](/Arav/kwh-cost/raw/branch/master/PKGBUILD) file and in a directory with it run `makepkg -i`.
If you want to use a pre-built package for Arch from Releases, then install it using `pacman -U` command.

View File

@ -2,18 +2,30 @@
#include <stdlib.h>
#include <string.h>
#include "main.h"
#define VERSION "1.1.3"
typedef long double price_t;
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);
interval_t interval_to_seconds(const char *const interval);
price_t price_kwh_to_wattsecond(const price_t price);
void print_version(void);
void print_usage(void);
int main(const int argc, const char *const *const argv) {
price_t price;
wattage_t wattage;
price_t price = 0;
wattage_t wattage = 0;
interval_t interval = 0;
int res = parse_arguments(argc, argv, &price, &wattage, &interval);
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);
switch (parse_arguments(argc, argv, &price, &wattage, &interval)) {
case 1: print_usage(); return 0;
case 2: print_version(); return 0;
case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); return 1;
}
printf("%.2Lf\n", (price * wattage) * interval);
@ -27,16 +39,19 @@ int parse_arguments(const int argc, const char *const *const argv,
for (int i = 1, pos = 0; i < argc; ++i)
if (argv[i][0] == '-') {
if (argv[i][1] == 'h' || argv[i][2] == 'h')
size_t l = strlen(argv[i]);
if (l == 1)
return 99;
if (argv[i][1] == 'h' || (argv[i][1] == '-' && l > 2 && argv[i][2] == 'h'))
return 1;
else if (argv[i][1] == 'v' || argv[i][2] == 'v')
else if (argv[i][1] == 'v' || (argv[i][1] == '-' && l > 2 && argv[i][2] == 'v'))
return 2;
else
return 99;
} else if (argc < 4) {
return 1;
} else if (pos == 0) {
*price = price_kwh_to_wattspersecond((price_t)atof(argv[i]));
*price = price_kwh_to_wattsecond((price_t)atof(argv[i]));
++pos;
} else if (pos == 1) {
*wattage = (wattage_t)atof(argv[2]);
@ -47,7 +62,7 @@ int parse_arguments(const int argc, const char *const *const argv,
return 0;
}
inline interval_t interval_to_seconds(const char *const interval) {
interval_t interval_to_seconds(const char *const interval) {
interval_t intrvl = (interval_t)atol(interval);
switch (interval[strlen(interval) - 1]) {
@ -65,17 +80,18 @@ inline interval_t interval_to_seconds(const char *const interval) {
}
}
inline price_t price_kwh_to_wattspersecond(const price_t price) {
price_t price_kwh_to_wattsecond(const price_t price) {
return price / 1000.f / 3600.f;
}
inline void print_version() {
void print_version(void) {
fputs("kwh-cost Ver. " VERSION "\n"
"Copyright (C) 2021-2023 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
"Copyright (C) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
"This program is distributed under the terms of MIT+NIGGER license.\n"
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
}
inline void print_usage() {
void print_usage(void) {
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
"Options:\n"
" --help, -h display this usage information.\n"

View File

@ -1,19 +0,0 @@
#ifndef _MAIN_H_
#define _MAIN_H_
#define VERSION "1.1.1"
typedef long double price_t;
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);
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();
#endif