summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtest/gentestfiles.sh17
-rw-r--r--trashsys.c42
2 files changed, 38 insertions, 21 deletions
diff --git a/test/gentestfiles.sh b/test/gentestfiles.sh
index da113d8..d469964 100755
--- a/test/gentestfiles.sh
+++ b/test/gentestfiles.sh
@@ -4,4 +4,19 @@ touch ts_file1.txt
touch ts_file2.txt
touch ts_file3.txt
touch thisfile.txt
-dd if=/dev/zero of=./1gb.img bs=100M count=10
+
+dd if=/dev/zero of=./1gib.img bs=1GiB count=1 # 1024 exactly, should show 1GiB on tsr -l
+dd if=/dev/zero of=./1000mb.img bs=1000MB count=1 # 1000Mib should show 1000MiB on tsr -l
+dd if=/dev/zero of=./1.1gib.img bs=1100MiB count=1 # 1100MiB should show 1.1 Gib on tsr -l
+
+dd if=/dev/zero of=./1mb.img bs=1MB count=1 # MB =/= MiB
+dd if=/dev/zero of=./1mib.img bs=1MiB count=1 # 1 Mib
+dd if=/dev/zero of=./500kib.img bs=500KiB count=1 # 500 Kib
+dd if=/dev/zero of=./1023kib.img bs=1023KiB count=1 # 1023 Kib
+dd if=/dev/zero of=./1024kib.img bs=1024KiB count=1 # 1 Mib
+
+dd if=/dev/zero of=./1000b.img bs=1000B count=1
+dd if=/dev/zero of=./1KiB.img bs=1KiB count=1
+dd if=/dev/zero of=./1023b.img bs=1023B count=1
+dd if=/dev/zero of=./1024b.img bs=1024B count=1
+dd if=/dev/zero of=./1KB.img bs=1KB count=1 # KB =/= KiB
diff --git a/trashsys.c b/trashsys.c
index 237c757..79d9723 100644
--- a/trashsys.c
+++ b/trashsys.c
@@ -22,6 +22,10 @@
#define NOFILE 3
#define FUNCTION_FAILURE -1
#define FUNCTION_SUCCESS 0
+#define BYTES 10
+#define KiB 210
+#define MiB 220
+#define GiB 230
#define REM_SZ(remsz, final) (remsz - strlen(final))
#define USAGE_OUT(stream) (fprintf(stream, "%s", USAGE))
@@ -525,28 +529,24 @@ char *rawtime_to_readable(time_t rawtime) {
return pretty_time;
}
-int bytes_to_readable_str(size_t bytes, char *str, size_t str_len) { // rough estimation of MiB
+char *bytes_to_readable_str(size_t bytes, char *str, size_t str_len) {
char tmp_str[str_len];
- double f_bytes;
- char *float_precision;
- char *fov1 = "%.1f";
- char *feq0 = "%.0f";
- char *fun1 = "%f";
- f_bytes = (double)bytes / 1024 / 1024;
- if(f_bytes > 1) {
- float_precision = fov1;
- } else if (f_bytes == 0) {
- float_precision = feq0;
- } else if (f_bytes < 1) {
- float_precision = fun1;
+ double f_bytes = (double)bytes;
+ int count = 0;
+ char *fff_a[] = {"B", "KiB", "MiB", "GiB"};
+
+ while (f_bytes >= 1024) {
+ f_bytes = f_bytes / 1024;
+ count++;
}
- snprintf(tmp_str, str_len, float_precision, f_bytes);
+
+ snprintf(tmp_str, str_len, "%0.1f", f_bytes);
if(concat_str(str, str_len, tmp_str) == NULL) {
- return FUNCTION_FAILURE;
+ return NULL;
}
- return FUNCTION_SUCCESS;
+ return fff_a[count];
}
int lfc_formatted (struct list_file_content *lfc, const bool L_used) {
@@ -573,16 +573,17 @@ int lfc_formatted (struct list_file_content *lfc, const bool L_used) {
fprintf(stdout, "Cannot convert time to readable\n");
return FUNCTION_FAILURE;
}
-
+ char *fff;
size_t str_len = 1024;
char readable_mib_str[str_len];
readable_mib_str[0] = '\0';
- bytes_to_readable_str(filesize_bytes, readable_mib_str, str_len);
+ fff = bytes_to_readable_str(filesize_bytes, readable_mib_str, str_len);
if (L_used == true) {
- fprintf(stdout, "ID: %s %s %s MiB %s bytes Trashed at: %s (unixtime: %s) originalpath: %s\n"
+ fprintf(stdout, "ID: %s %s %s %s %s Trashed at: %s (unixtime: %s) originalpath: %s\n"
, lfc->ID
, lfc->filename
, readable_mib_str
+ , fff
, lfc->filesize
, pretty_time
, lfc->time
@@ -591,10 +592,11 @@ int lfc_formatted (struct list_file_content *lfc, const bool L_used) {
free(pretty_time);
return FUNCTION_SUCCESS;
}
- fprintf(stdout, "ID: %s %s %s MiB Trashed at: %s\n"
+ fprintf(stdout, "ID: %s %s %s %s Trashed at: %s\n"
, lfc->ID
, lfc->filename
, readable_mib_str
+ , fff
, pretty_time
);
free(pretty_time);