commit 8e2092a3cb2ce4da5d69f973be5c3746db8b28b5
parent 66a143e8369852871a2bbdf7663a4bbf0799b8ed
Author: hhvn <dev@hhvn.uk>
Date: Tue, 12 Apr 2022 12:57:36 +0100
Move str functions to own file & cleanup
Diffstat:
M | Makefile | | | 2 | +- |
M | src/handle.c | | | 6 | +++--- |
M | src/hirc.h | | | 3 | ++- |
M | src/main.c | | | 134 | ------------------------------------------------------------------------------- |
A | src/str.c | | | 141 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 147 insertions(+), 139 deletions(-)
diff --git a/Makefile b/Makefile
@@ -21,7 +21,7 @@ BIN = hirc
SRC = src/main.c src/mem.c src/handle.c src/hist.c \
src/nick.c src/chan.c src/serv.c src/ui.c \
src/format.c src/complete.c src/commands.c \
- src/config.c
+ src/config.c src/str.c
OBJ = $(SRC:.c=.o)
MAN = hirc.1
COMMIT = $(shell git log HEAD...HEAD~1 --pretty=format:%h)
diff --git a/src/handle.c b/src/handle.c
@@ -282,7 +282,7 @@ handle_PRIVMSG) {
chan_setold(priv, 0);
hist_addp(priv->history, msg, act_direct, HIST_DFL);
- } else if (nick_isself(nick) && !chrcmp(*target, "#&!+")) {
+ } else if (nick_isself(nick) && !strchr("#&!+", *target)) {
/* i'm messaging someone */
if ((priv = chan_get(&server->privs, target, -1)) == NULL)
priv = chan_add(server, &server->privs, target, 1);
@@ -426,9 +426,9 @@ handle_RPL_NAMREPLY) {
for (; *nicks && **nicks; nicks++) {
priv = ' ';
nick = *nicks;
- if (chrcmp(**nicks, supportedprivs)) {
+ if (strchr(supportedprivs, **nicks)) {
priv = **nicks;
- while (chrcmp(*nick, supportedprivs))
+ while (strchr(supportedprivs, *nick))
nick++;
}
if ((oldnick = nick_get(&chan->nicks, nick)) == NULL)
diff --git a/src/hirc.h b/src/hirc.h
@@ -48,8 +48,9 @@ char ** param_dup(char **p);
int read_line(int fd, char *buf, size_t buf_len);
int ircgets(struct Server *server, char *buf, size_t buf_len);
int ircprintf(struct Server *server, char *format, ...);
+
+/* str.c */
char * homepath(char *path);
-char chrcmp(char c, char *s);
char * struntil(char *str, char until);
int strisnum(char *str, int allowneg);
char * strntok(char *str, char *sep, int n);
diff --git a/src/main.c b/src/main.c
@@ -226,140 +226,6 @@ ircprintf(struct Server *server, char *format, ...) {
return ret;
}
-char *
-homepath(char *path) {
- static char ret[1024];
-
- if (*path == '~') {
- snprintf(ret, sizeof(ret), "%s/%s", getenv("HOME"), path + 1);
- return ret;
- }
-
- return path;
-}
-
-char
-chrcmp(char c, char *s) {
- for (; s && *s; s++)
- if (c == *s)
- return c;
-
- return 0;
-}
-
-char *
-struntil(char *str, char until) {
- static char ret[1024];
- int i;
-
- for (i=0; str && *str && i < 1024 && *str != until; i++, str++)
- ret[i] = *str;
-
- ret[i] = '\0';
- return ret;
-}
-
-int
-strisnum(char *str, int allowneg) {
- if (!str)
- return 0;
-
- if ((allowneg && *str == '-') || *str == '+')
- str += 1;
-
- for (; *str; str++)
- if (*str > '9' || *str < '0')
- return 0;
- return 1;
-}
-
-char *
-strntok(char *str, char *sep, int n) {
- static char buf[8192];
- char *ret, *save;
-
- strlcpy(buf, str, sizeof(buf));
-
- ret = strtok_r(buf, sep, &save);
- if (--n == 0)
- return ret;
- while ((ret = strtok_r(NULL, sep, &save)) != NULL)
- if (--n == 0)
- return ret;
- return NULL;
-}
-
-#define S_YEAR 31557600 /* 60*60*24*365.25 */
-#define S_MONTH 2629800 /* 60*60*24*(365.25 / 12) */
-#define S_WEEK 604800 /* 60*60*24*7 */
-#define S_DAY 86400 /* 60*60*24 */
-#define S_HOUR 3600 /* 60*60 */
-#define S_MIN 60
-
-char *
-strrdate(time_t secs) {
- static char ret[1024];
- size_t rc = 0;
- long shrt = config_getl("rdate.short");
- long avg = config_getl("rdate.averages");
- long verb = config_getl("rdate.verbose");
- int years = 0, months = 0, weeks = 0,
- days = 0, hours = 0, mins = 0;
-
- if (avg) {
- years = secs / S_YEAR;
- secs -= years * S_YEAR;
-
- months = secs / S_MONTH;
- secs -= months * S_MONTH;
- }
-
- weeks = secs / S_WEEK;
- secs -= weeks * S_WEEK;
-
- days = secs / S_DAY;
- secs -= days * S_DAY;
-
- hours = secs / S_HOUR;
- secs -= hours * S_HOUR;
-
- mins = secs / S_MIN;
- secs -= mins * S_MIN;
-
- if (years || (verb && avg))
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", years,
- shrt ? "y" : " year", !shrt && years != 1 ? "s" : "");
- if (months || (verb && avg))
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", months,
- shrt ? "mo" : " month", !shrt && months != 1 ? "s" : "");
- if (weeks || verb)
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", weeks,
- shrt ? "w" : " week", !shrt && weeks != 1 ? "s" : "");
- if (days || verb)
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", days,
- shrt ? "d" : " day", !shrt && days != 1 ? "s" : "");
- if (hours || verb)
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", hours,
- shrt ? "h" : " hour", !shrt && hours != 1 ? "s" : "");
- if (mins || verb)
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", mins,
- shrt ? "m" : " min", !shrt && mins != 1 ? "s" : "");
- if (secs || verb)
- rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", secs,
- shrt ? "s" : " sec", !shrt && secs != 1 ? "s" : "");
- if (rc >= 2)
- ret[rc - 2] = '\0';
- else
- ret[rc] = '\0';
-
- return ret;
-}
-
-void
-sighandler(int signal) {
- return;
-}
-
int
main(int argc, char *argv[]) {
struct Selected oldselected;
diff --git a/src/str.c b/src/str.c
@@ -0,0 +1,141 @@
+/*
+ * src/str.c from hirc
+ *
+ * Copyright (c) 2021-2022 hhvn <dev@hhvn.uk>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include <string.h>
+#include "hirc.h"
+
+char *
+homepath(char *path) {
+ static char ret[1024];
+
+ if (*path == '~') {
+ snprintf(ret, sizeof(ret), "%s/%s", getenv("HOME"), path + 1);
+ return ret;
+ }
+
+ return path;
+}
+
+char *
+struntil(char *str, char until) {
+ static char ret[1024];
+ int i;
+
+ for (i=0; str && *str && i < 1024 && *str != until; i++, str++)
+ ret[i] = *str;
+
+ ret[i] = '\0';
+ return ret;
+}
+
+int
+strisnum(char *str, int allowneg) {
+ if (!str)
+ return 0;
+
+ if ((allowneg && *str == '-') || *str == '+')
+ str += 1;
+
+ for (; *str; str++)
+ if (*str > '9' || *str < '0')
+ return 0;
+ return 1;
+}
+
+char *
+strntok(char *str, char *sep, int n) {
+ static char buf[8192];
+ char *ret, *save;
+
+ strlcpy(buf, str, sizeof(buf));
+
+ ret = strtok_r(buf, sep, &save);
+ if (--n == 0)
+ return ret;
+ while ((ret = strtok_r(NULL, sep, &save)) != NULL)
+ if (--n == 0)
+ return ret;
+ return NULL;
+}
+
+#define S_YEAR 31557600 /* 60*60*24*365.25 */
+#define S_MONTH 2629800 /* 60*60*24*(365.25 / 12) */
+#define S_WEEK 604800 /* 60*60*24*7 */
+#define S_DAY 86400 /* 60*60*24 */
+#define S_HOUR 3600 /* 60*60 */
+#define S_MIN 60
+
+char *
+strrdate(time_t secs) {
+ static char ret[1024];
+ size_t rc = 0;
+ long shrt = config_getl("rdate.short");
+ long avg = config_getl("rdate.averages");
+ long verb = config_getl("rdate.verbose");
+ int years = 0, months = 0, weeks = 0,
+ days = 0, hours = 0, mins = 0;
+
+ if (avg) {
+ years = secs / S_YEAR;
+ secs -= years * S_YEAR;
+
+ months = secs / S_MONTH;
+ secs -= months * S_MONTH;
+ }
+
+ weeks = secs / S_WEEK;
+ secs -= weeks * S_WEEK;
+
+ days = secs / S_DAY;
+ secs -= days * S_DAY;
+
+ hours = secs / S_HOUR;
+ secs -= hours * S_HOUR;
+
+ mins = secs / S_MIN;
+ secs -= mins * S_MIN;
+
+ if (years || (verb && avg))
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", years,
+ shrt ? "y" : " year", !shrt && years != 1 ? "s" : "");
+ if (months || (verb && avg))
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", months,
+ shrt ? "mo" : " month", !shrt && months != 1 ? "s" : "");
+ if (weeks || verb)
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", weeks,
+ shrt ? "w" : " week", !shrt && weeks != 1 ? "s" : "");
+ if (days || verb)
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", days,
+ shrt ? "d" : " day", !shrt && days != 1 ? "s" : "");
+ if (hours || verb)
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", hours,
+ shrt ? "h" : " hour", !shrt && hours != 1 ? "s" : "");
+ if (mins || verb)
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", mins,
+ shrt ? "m" : " min", !shrt && mins != 1 ? "s" : "");
+ if (secs || verb)
+ rc += snprintf(&ret[rc], sizeof(ret) - rc, "%d%s%s, ", secs,
+ shrt ? "s" : " sec", !shrt && secs != 1 ? "s" : "");
+ if (rc >= 2)
+ ret[rc - 2] = '\0';
+ else
+ ret[rc] = '\0';
+
+ return ret;
+}