char* pakfire_string_replace(const char* s, const char* pattern, const char* repl);
char** pakfire_split_string(const char* s, char delim);
-char* pakfire_format_size(double size);
+int pakfire_format_size(char* dst, size_t length, double value);
char* pakfire_format_date(time_t t);
int pakfire_path_exists(const char* path);
}
static void pakfire_package_dump_add_line_size(char** str, const char* key, unsigned long long size) {
- char* val = pakfire_format_size(size);
+ char buffer[128];
+ pakfire_format_size(buffer, sizeof(buffer) - 1, size);
- if (val) {
- pakfire_package_dump_add_line(str, key, val);
- free(val);
- }
+ pakfire_package_dump_add_line(str, key, buffer);
}
PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) {
}
static void pakfire_transaction_add_package(char** str, size_t width, PakfirePackage pkg) {
+ char size[128];
+
PakfireRepo repo = pakfire_package_get_repo(pkg);
- unsigned long long size = pakfire_package_get_size(pkg);
- char* size_str = pakfire_format_size(size);
+ // Format size
+ pakfire_format_size(size, sizeof(size) - 1, pakfire_package_get_size(pkg));
pakfire_transaction_add_line(str, width,
pakfire_package_get_name(pkg),
pakfire_package_get_arch(pkg),
pakfire_package_get_evr(pkg),
pakfire_repo_get_name(repo),
- size_str
+ size
);
pakfire_repo_unref(repo);
- free(size_str);
}
static void pakfire_transaction_add_separator(char** str, size_t width) {
}
static void pakfire_transaction_add_usage_line(char** str, size_t width, const char* headline, ssize_t size) {
- char* s = pakfire_format_size(size);
-
- asprintf(str, "%s%-21s: %s\n", *str, headline, s);
+ char buffer[128];
+ pakfire_format_size(buffer, sizeof(buffer) - 1, size);
- free(s);
+ asprintf(str, "%s%-21s: %s\n", *str, headline, buffer);
}
PAKFIRE_EXPORT char* pakfire_transaction_dump(PakfireTransaction transaction, size_t width) {
return ret;
}
-char* pakfire_format_size(double size) {
- char string[128];
+int pakfire_format_size(char* dst, size_t length, double value) {
const char* units[] = {" ", "k", "M", "G", "T", NULL};
const char** unit = units;
- while (*(unit + 1) && size >= 1024.0) {
- size /= 1024.0;
+ while (*(unit + 1) && value >= 1024.0) {
+ value /= 1024.0;
unit++;
}
- snprintf(string, sizeof(string) - 1, "%.0f%s", round(size), *unit);
-
- return strdup(string);
+ return snprintf(dst, length, "%.0f%s", round(value), *unit);
}
#pragma GCC diagnostic push