From aefc8cd3ff817adef2905086c4594871f8062e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B6ppner?= Date: Wed, 7 Nov 2018 14:20:40 +0100 Subject: [PATCH] libutil/libu2s: Move strlcpy() implementation to libutils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The strlcpy() implementation in libu2s is beneficial for other tools as well. Move the implementation to libutils and replace misc_strlcpy() in libu2s accordingly. Change the link order in zipl to make it build again. Signed-off-by: Jan Höppner --- include/lib/util_libc.h | 1 + libu2s/Makefile | 4 ++-- libu2s/misc.c | 27 --------------------------- libu2s/misc.h | 15 --------------- libu2s/u2s.c | 6 +++--- libutil/util_libc.c | 27 +++++++++++++++++++++++++++ zipl/src/Makefile | 4 ++-- 7 files changed, 35 insertions(+), 49 deletions(-) delete mode 100644 libu2s/misc.c delete mode 100644 libu2s/misc.h diff --git a/include/lib/util_libc.h b/include/lib/util_libc.h index dc6b3d09..394aca1a 100644 --- a/include/lib/util_libc.h +++ b/include/lib/util_libc.h @@ -130,6 +130,7 @@ char *util_strcat_realloc(char *str1, const char *str2); void util_str_toupper(char *str); char *util_strstrip(char *s); +size_t util_strlcpy(char *dest, const char *src, size_t size); #ifdef __cplusplus } diff --git a/libu2s/Makefile b/libu2s/Makefile index 523282b6..3e12a75e 100644 --- a/libu2s/Makefile +++ b/libu2s/Makefile @@ -4,9 +4,9 @@ lib = libu2s.a all: $(lib) -objects = u2s.o misc.o +objects = u2s.o -$(lib): $(objects) +$(lib): $(objects) $(rootdir)/libutil/libutil.a install: all diff --git a/libu2s/misc.c b/libu2s/misc.c deleted file mode 100644 index d29784cd..00000000 --- a/libu2s/misc.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Misc - Local helper functions - * - * Copyright IBM Corp. 2016, 2017 - * - * s390-tools is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include "lib/util_base.h" - -/* - * Helper function that copies a string safely - */ -size_t misc_strlcpy(char *dest, const char *src, size_t size) -{ - size_t str_len = strlen(src); - size_t len; - - if (size) { - len = MIN(size - 1, str_len); - memcpy(dest, src, len); - dest[len] = '\0'; - } - return str_len; -} diff --git a/libu2s/misc.h b/libu2s/misc.h deleted file mode 100644 index 53b73562..00000000 --- a/libu2s/misc.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Misc - Local helper functions - * - * Copyright IBM Corp. 2016, 2017 - * - * s390-tools is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef MISC_H -#define MISC_H - -size_t misc_strlcpy(char *, const char *, size_t); - -#endif /* MISC_H */ diff --git a/libu2s/u2s.c b/libu2s/u2s.c index 9ffafa78..90819622 100644 --- a/libu2s/u2s.c +++ b/libu2s/u2s.c @@ -20,7 +20,7 @@ #include #include "lib/u2s.h" -#include "misc.h" +#include "lib/util_libc.h" #define DEV_BUFFER_LENGTH 20 #define PATH_BUFFER_LENGTH 256 @@ -113,7 +113,7 @@ static int extract_busid(char *name, char *busid) { if (!start) return -1; start++; - len = misc_strlcpy(busid, start, BUSIDSIZE); + len = util_strlcpy(busid, start, BUSIDSIZE); if (len >= BUSIDSIZE) return -1; @@ -246,7 +246,7 @@ static int find_busid_in_proc(int maja, int mina, char *busid) while (fscanf(filp, "%[^(] %*[^)] ) at ( %d : %d %*[^\n]\n", bus, &majb, &minb) != EOF) { if ((maja == majb) && (mina == minb)) { - len = misc_strlcpy(busid, bus, BUSIDSIZE); + len = util_strlcpy(busid, bus, BUSIDSIZE); if (len < BUSIDSIZE) rc = 0; break; diff --git a/libutil/util_libc.c b/libutil/util_libc.c index 127b9211..d3e76766 100644 --- a/libutil/util_libc.c +++ b/libutil/util_libc.c @@ -228,3 +228,30 @@ char *util_strstrip(char *s) return s; } + +/** + * Copy \a src to buffer \a dest of size \a size. At most size - 1 + * chars will be copied. \a dest will always be NUL terminated. + * + * Note: If the return value is greater than or equal to size truncation + * occurred. + * + * @param[in] dest Destination buffer + * @param[in] src Source string + * @param[in] size Size of destination buffer + * + * @returns strlen Length of \a src string + */ +size_t util_strlcpy(char *dest, const char *src, size_t size) +{ + size_t str_len = strlen(src); + size_t len; + + if (size) { + len = MIN(size - 1, str_len); + memcpy(dest, src, len); + dest[len] = '\0'; + } + + return str_len; +} diff --git a/zipl/src/Makefile b/zipl/src/Makefile index 6278ef3b..1f39d069 100644 --- a/zipl/src/Makefile +++ b/zipl/src/Makefile @@ -7,8 +7,8 @@ ALL_CPPFLAGS += -I../include -I../boot \ -D_FILE_OFFSET_BITS=64 $(NO_PIE_CFLAGS) ALL_LDFLAGS += -Wl,-z,noexecstack $(NO_PIE_LDFLAGS) -libs = $(rootdir)/libutil/libutil.a \ - $(rootdir)/libu2s/libu2s.a +libs = $(rootdir)/libu2s/libu2s.a \ + $(rootdir)/libutil/libutil.a objects = misc.o error.o scan.o job.o boot.o bootmap.o disk.o \ install.o zipl.o $(rootdir)/zipl/boot/data.o