From 4d56300a76f894911f1e1978fff22f747045d26e Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 26 Feb 2022 23:18:36 +0100 Subject: [PATCH] compat with std-99 (centos6, gcc 4.4.7) --- src/nsub.d | 2 +- src/nsub/nsub.c | 14 +++++++------- src/nsub/nsub_read_lrc.c | 8 ++++++-- src/nsub/nsub_write_lrc.c | 2 +- src/nsub/nsub_write_webvtt.c | 2 +- src/utils.d | 2 +- src/utils/array.c | 1 - src/utils/desktop.c | 25 ++++++++++++------------- src/utils/utils.c | 29 +++++++++++++++++++++++++++++ src/utils/utils.h | 24 ++++++++++++++++++++---- 10 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 src/utils/utils.c diff --git a/src/nsub.d b/src/nsub.d index 09f551e..da878cf 100644 --- a/src/nsub.d +++ b/src/nsub.d @@ -1,4 +1,4 @@ -CFLAGS += -Wall -I./ +CFLAGS += -Wall -I./ -std=c99 CXXFLAGS += -Wall -I./ PREFIX = /usr/local diff --git a/src/nsub/nsub.c b/src/nsub/nsub.c index 780eff3..2d91c62 100644 --- a/src/nsub/nsub.c +++ b/src/nsub/nsub.c @@ -22,6 +22,7 @@ #include "nsub.h" #include "utils/array.h" +#include "utils/utils.h" /* Public */ @@ -69,7 +70,7 @@ void song_add_unknown(song_t *song, char *text) { lyric.start = 0; lyric.stop = 0; lyric.name = NULL; - lyric.text = text ? strdup(text) : NULL; + lyric.text = text ? utils_strdup(text) : NULL; array_add(song->lyrics, &lyric); } @@ -91,7 +92,7 @@ void song_add_comment(song_t *song, char *comment) { lyric.start = 0; lyric.stop = 0; lyric.name = NULL; - lyric.text = comment ? strdup(comment) : NULL; + lyric.text = comment ? utils_strdup(comment) : NULL; array_add(song->lyrics, &lyric); } @@ -103,15 +104,15 @@ void song_add_lyric(song_t *song, int start, int stop, char *name, char *text) { lyric.num = song->current_num; lyric.start = start; lyric.stop = stop; - lyric.name = name ? strdup(name) : NULL; - lyric.text = text ? strdup(text) : NULL; + lyric.name = name ? utils_strdup(name) : NULL; + lyric.text = text ? utils_strdup(text) : NULL; array_add(song->lyrics, &lyric); } void song_add_meta(song_t *song, char *key, char *value) { meta_t meta; - meta.key = key ? strdup(key) : NULL; - meta.value = value ? strdup(value) : NULL; + meta.key = key ? utils_strdup(key) : NULL; + meta.value = value ? utils_strdup(value) : NULL; array_add(song->metas, &meta); } @@ -158,4 +159,3 @@ int nsub_write(FILE *out, song_t *song, NSUB_FORMAT fmt, int apply_offset) { return 0; } } - diff --git a/src/nsub/nsub_read_lrc.c b/src/nsub/nsub_read_lrc.c index 7d7f389..b9b9d08 100644 --- a/src/nsub/nsub_read_lrc.c +++ b/src/nsub/nsub_read_lrc.c @@ -20,6 +20,7 @@ #include #include "nsub.h" +#include "utils/utils.h" /* Declarations */ @@ -86,7 +87,7 @@ int nsub_read_lrc(song_t *song, char *line) { line[colon] = '\0'; line[end] = '\0'; if (!strcmp("language", line + 1)) { - song->lang = strdup(line + text_offset); + song->lang = utils_strdup(line + text_offset); } else { song_add_meta(song, line + 1, line + text_offset); } @@ -210,7 +211,7 @@ int lrc_millisec(char *line) { } int imult = 0; - for (ssize_t i = end; i >= 0; i--) { + for (size_t i = end; 1; i--) { char car = line[i]; int digit = (car >= '0' && car <= '9'); @@ -219,6 +220,9 @@ int lrc_millisec(char *line) { if (car == '-') total = -total; + + if (i == 0) + break; } return total; diff --git a/src/nsub/nsub_write_lrc.c b/src/nsub/nsub_write_lrc.c index 704b2e2..cb38ddd 100644 --- a/src/nsub/nsub_write_lrc.c +++ b/src/nsub/nsub_write_lrc.c @@ -131,7 +131,7 @@ char *nsub_lrc_time_str(int time, int show_sign) { if (h) { sprintf(time_str, "%s%d:%02d:%02d.%02d", sign, h, m, s, c); } else { - sprintf(time_str, "%s%d:%02d.%02d", sign, m, s, c); + sprintf(time_str, "%s%02d:%02d.%02d", sign, m, s, c); } return time_str; diff --git a/src/nsub/nsub_write_webvtt.c b/src/nsub/nsub_write_webvtt.c index c94ba7c..b856776 100644 --- a/src/nsub/nsub_write_webvtt.c +++ b/src/nsub/nsub_write_webvtt.c @@ -117,7 +117,7 @@ char *nsub_webvtt_time_str(int time, int show_sign) { if (h) { sprintf(time_str, "%s%d:%02d:%02d.%03d", sign, h, m, s, c); } else { - sprintf(time_str, "%s%d:%02d.%03d", sign, m, s, c); + sprintf(time_str, "%s%02d:%02d.%03d", sign, m, s, c); } return time_str; diff --git a/src/utils.d b/src/utils.d index 624473b..7cf29f1 100644 --- a/src/utils.d +++ b/src/utils.d @@ -1,4 +1,4 @@ -CFLAGS += -Wall -I./ +CFLAGS += -Wall -I./ -std=c99 CXXFLAGS += -Wall -I./ PREFIX = /usr/local diff --git a/src/utils/array.c b/src/utils/array.c index 33be882..77c75f8 100644 --- a/src/utils/array.c +++ b/src/utils/array.c @@ -24,7 +24,6 @@ #include "array.h" -typedef struct array_p array; struct array_p { size_t elem_size; size_t count; diff --git a/src/utils/desktop.c b/src/utils/desktop.c index 7e6022e..eee8c9f 100644 --- a/src/utils/desktop.c +++ b/src/utils/desktop.c @@ -25,8 +25,7 @@ #include #include -#include "array.h" -#include "desktop.h" +#include "utils.h" #define EXT "desktop" @@ -55,7 +54,7 @@ desktop *new_desktop(const char filename[], int best_size) { me->id = 0; // Copy name - me->name = strdup(filename); + me->name = utils_strdup(filename); // Get extension an remove ".desktop" from name char *ext = rindex(me->name, '.'); @@ -66,7 +65,7 @@ desktop *new_desktop(const char filename[], int best_size) { me->name[idot] = '\0'; } if (ext) - ext = strdup(ext); + ext = utils_strdup(ext); // If PNG of the same name, use as default icon me->icon_file = desktop_find_icon(me->name, best_size); @@ -78,7 +77,7 @@ desktop *new_desktop(const char filename[], int best_size) { slash = rindex(me->name, '/'); } if (slash) { - char *copy = strdup(slash + 1); + char *copy = utils_strdup(slash + 1); free(me->name); me->name = copy; } @@ -143,14 +142,14 @@ desktop *new_desktop(const char filename[], int best_size) { n = strlen(startsWith); if (!strncmp(line, startsWith, n)) { free(me->name); - me->name = strdup(line + n); + me->name = utils_strdup(line + n); } startsWith = "Exec="; n = strlen(startsWith); if (!strncmp(line, startsWith, n)) { free(me->exec); - me->exec = strdup(line + n); + me->exec = utils_strdup(line + n); // TODO: %f %F %u %U %i %c %k: inject values instead char *cars = "ifFuUck"; for (char *ptr = index(me->exec, '%'); ptr; ptr = index(ptr, '%')) { @@ -166,7 +165,7 @@ desktop *new_desktop(const char filename[], int best_size) { n = strlen(startsWith); if (!strncmp(line, startsWith, n)) { free(me->icon); - me->icon = strdup(line + n); + me->icon = utils_strdup(line + n); } } @@ -355,17 +354,17 @@ char *desktop_find_icon(const char basename[], int icon_size) { if (!strncmp(line, startsWith, n)) { free(theme); if (line[n] == '"') { - theme = strdup(line + n + 1); + theme = utils_strdup(line + n + 1); theme[strlen(theme) - 1] = '\0'; } else { - theme = strdup(line + n); + theme = utils_strdup(line + n); } } } if (!theme || !theme[0]) { - theme = strdup(""); - ltheme = strdup(""); + theme = utils_strdup(""); + ltheme = utils_strdup(""); } else { tmp = theme; theme = desktop_concat("/usr/share/icons/", tmp, "/", NULL); @@ -381,7 +380,7 @@ char *desktop_find_icon(const char basename[], int icon_size) { return NULL; // exact match - tmp = strdup(basename); + tmp = utils_strdup(basename); if (desktop_test_file(tmp)) return tmp; free(tmp); diff --git a/src/utils/utils.c b/src/utils/utils.c new file mode 100644 index 0000000..4bdeb6d --- /dev/null +++ b/src/utils/utils.c @@ -0,0 +1,29 @@ +/* + * CUtils: some small C utilities + * + * Copyright (C) 2022 Niki Roo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "utils.h" + +char *utils_strdup(const char *source) { + size_t sz = strlen(source); + char *new = malloc((sz + 1) * sizeof(char)); + strcpy(new, source); + return new; +} diff --git a/src/utils/utils.h b/src/utils/utils.h index 1fd1e46..09be644 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -17,12 +17,28 @@ * along with this program. If not, see . */ +/** + * @file utils.h + * @author Niki + * @date 2020 + * + * @brief Include all the other .H as well as a C99-compatible utils_strdup function. + */ #ifndef UTILS_H #define UTILS_H -#include "../../nsub/utils/array.h" -#include "../../nsub/utils/desktop.h" -#include "../../nsub/utils/print.h" -#include "../../nsub/utils/timing.h" +#include "array.h" +#include "desktop.h" +#include "print.h" +#include "timing.h" + +/** + * A C99-compatible strdup function. + * + * @param source the source string to copy + * + * @return a new string (malloc'ed) which your are now respionsible of + */ +char *utils_strdup(const char *source); #endif // UTILS_H -- 2.27.0