jcs
/amend
/amendments
/98
util: Sync with upstream
jcs made amendment 98 over 2 years ago
--- util.c Tue Sep 6 15:21:37 2022
+++ util.c Tue Sep 6 15:57:57 2022
@@ -1,7 +1,9 @@
/*
* Copyright (c) 2020-2022 joshua stein <jcs@jcs.org>
* Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
* 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.
@@ -300,6 +302,26 @@ xstrdup(const char *str, char *note)
return cp;
}
+char *
+xstrndup(const char *str, size_t maxlen, char *note)
+{
+ char *copy;
+ const char *cp;
+ size_t len;
+
+ /* strnlen */
+ for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
+ ;
+
+ len = (size_t)(cp - str);
+ copy = xmalloc(len + 1, note);
+ (void)memcpy(copy, str, len);
+ copy[len] = '\0';
+
+ return copy;
+}
+
+
/*
* String functions
*/
@@ -1630,6 +1652,44 @@ strndup(const char *str, size_t maxlen)
return copy;
}
+/*
+ * Get next token from string *stringp, where tokens are possibly-empty
+ * strings separated by characters from delim.
+ *
+ * Writes NULs into the string at *stringp to end tokens.
+ * delim need not remain constant from call to call.
+ * On return, *stringp points past the last NUL written (if there might
+ * be further tokens), or is NULL (if there are definitely no more tokens).
+ *
+ * If *stringp is NULL, strsep returns NULL.
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+ char *s;
+ const char *spanp;
+ int c, sc;
+ char *tok;
+
+ if ((s = *stringp) == NULL)
+ return (NULL);
+ for (tok = s;;) {
+ c = *s++;
+ spanp = delim;
+ do {
+ if ((sc = *spanp++) == c) {
+ if (c == 0)
+ s = NULL;
+ else
+ s[-1] = 0;
+ *stringp = s;
+ return (tok);
+ }
+ } while (sc != 0);
+ }
+ return (NULL);
+}
+
static struct format {
unsigned leftJustify : 1;
unsigned forceSign : 1;
--- util.h Tue Sep 6 14:40:32 2022
+++ util.h Tue Sep 6 15:53:02 2022
@@ -97,6 +97,7 @@ void * xcalloc(size_t, size_t, char *note);
void * xrealloc(void *src, size_t size);
void * xreallocarray(void *, size_t, size_t);
char * xstrdup(const char *, char *note);
+char * xstrndup(const char *str, size_t maxlen, char *note);
short getline(char *str, size_t len, char **ret);
const char * ordinal(unsigned short n);
@@ -158,8 +159,8 @@ void RestoreHiddenMenuBar(void);
size_t strlcat(char *dst, const char *src, size_t dsize);
size_t strlcpy(char *dst, const char *src, size_t dsize);
char * strndup(const char *str, size_t maxlen);
+char * strsep(char **stringp, const char *delim);
int snprintf(char *s, size_t size, const char *fmt, ...);
int vsnprintf(char *s, size_t size, const char *fmt, void *p);
-
#endif