Copy in strlcpy from libbsd to remove dependency on libbsd (#37)

pull/54/head
Clayton Smith 2023-08-08 11:16:29 -04:00 zatwierdzone przez GitHub
rodzic 72a33f1397
commit 8d8d31c7c8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
8 zmienionych plików z 64 dodań i 10 usunięć

Wyświetl plik

@ -6,7 +6,6 @@ RUN dnf install -y \
arm-none-eabi-gcc-cs-c++ \ arm-none-eabi-gcc-cs-c++ \
arm-none-eabi-binutils-cs \ arm-none-eabi-binutils-cs \
arm-none-eabi-newlib \ arm-none-eabi-newlib \
libbsd-devel \
cmake cmake
COPY docker_build.sh /build.sh COPY docker_build.sh /build.sh

Wyświetl plik

@ -144,12 +144,9 @@ Software requirements:
On a Red Hat/Fedora Linux installation, the following packages can be installed: On a Red Hat/Fedora Linux installation, the following packages can be installed:
```bash ```bash
dnf install arm-none-eabi-gcc-cs arm-none-eabi-gcc-cs-c++ arm-none-eabi-binutils-cs arm-none-eabi-newlib libbsd-devel cmake openocd dnf install arm-none-eabi-gcc-cs arm-none-eabi-gcc-cs-c++ arm-none-eabi-binutils-cs arm-none-eabi-newlib cmake openocd
``` ```
Note that the code uses `strlcpy()` also in the test code, which requires `libbsd-dev` (Debian/Ubuntu) or
`libbsd-devel` (Red Hat/Fedora) to be installed for compilation to succeed.
### Steps to build the firmware ### Steps to build the firmware
1. Install the required software dependencies listed above 1. Install the required software dependencies listed above

Wyświetl plik

@ -2,6 +2,7 @@
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
#include "strlcpy.h"
#include "template.h" #include "template.h"
#include "hal/system.h" #include "hal/system.h"
#include "hal/delay.h" #include "hal/delay.h"

50
src/strlcpy.c 100644
Wyświetl plik

@ -0,0 +1,50 @@
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
/*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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 <sys/types.h>
#include <string.h>
/*
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}

8
src/strlcpy.h 100644
Wyświetl plik

@ -0,0 +1,8 @@
#ifndef __STRLCPY_H
#define __STRLCPY_H
#include <sys/types.h>
size_t strlcpy(char *dst, const char *src, size_t siz);
#endif

Wyświetl plik

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "utils.h" #include "utils.h"
#include "config.h" #include "config.h"
#include "strlcpy.h"
#include "template.h" #include "template.h"
size_t template_replace(char *dest, size_t dest_len, char *src, telemetry_data *data) size_t template_replace(char *dest, size_t dest_len, char *src, telemetry_data *data)

Wyświetl plik

@ -7,9 +7,9 @@ project(RS41ng_test C CXX)
set(BINARY ${CMAKE_PROJECT_NAME}) set(BINARY ${CMAKE_PROJECT_NAME})
file(GLOB_RECURSE USER_SOURCES "../src/config.c" "../src/codecs/*.c" "../src/template.c" "../src/utils.c") file(GLOB_RECURSE USER_SOURCES "../src/config.c" "../src/codecs/*.c" "../src/template.c" "../src/utils.c" "../src/strlcpy.c")
file(GLOB_RECURSE USER_SOURCES_CXX "../src/codecs/*.cpp") file(GLOB_RECURSE USER_SOURCES_CXX "../src/codecs/*.cpp")
file(GLOB_RECURSE USER_HEADERS "../src/codecs/*.h" "../src/template.h" "../src/utils.h" "../src/config.h") file(GLOB_RECURSE USER_HEADERS "../src/codecs/*.h" "../src/template.h" "../src/utils.h" "../src/config.h" "../src/strlcpy.h")
file(GLOB_RECURSE TEST_SOURCES "*.c") file(GLOB_RECURSE TEST_SOURCES "*.c")
file(GLOB_RECURSE TEST_SOURCES_CXX "*.cpp") file(GLOB_RECURSE TEST_SOURCES_CXX "*.cpp")
@ -17,8 +17,6 @@ file(GLOB_RECURSE TEST_HEADERS "*.h")
set(SOURCES ${TEST_SOURCES}) set(SOURCES ${TEST_SOURCES})
set(CMAKE_EXE_LINKER_FLAGS "-lbsd")
add_executable(${BINARY} ${TEST_SOURCES} ${USER_SOURCES}) add_executable(${BINARY} ${TEST_SOURCES} ${USER_SOURCES})
add_test(NAME ${BINARY} COMMAND ${BINARY}) add_test(NAME ${BINARY} COMMAND ${BINARY})

Wyświetl plik

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <bsd/string.h> #include "strlcpy.h"
#include "template.h" #include "template.h"
int main(void) int main(void)