Implement patty_strlcpy()
Implement patty_strlcpy() for portability and correctness, to provide BSD strlcpy() functionality for glibc systems while ensuring a nul terminated string is written to a destination, truncating the source string by one byte if need be to make room for the nul terminator
This commit is contained in:
parent
4722c859b6
commit
25da758257
8 changed files with 42 additions and 12 deletions
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include <patty/ax25.h>
|
||||
#include <patty/print.h>
|
||||
#include <patty/util.h>
|
||||
|
||||
#include <patty/bin/kiss.h>
|
||||
|
||||
|
@ -91,7 +92,7 @@ int main(int argc, char **argv) {
|
|||
goto error_client_socket;
|
||||
}
|
||||
|
||||
strncpy(ifreq.name, ifname, sizeof(ifreq.name)-1);
|
||||
patty_strlcpy(ifreq.name, ifname, sizeof(ifreq.name));
|
||||
|
||||
ifreq.state = PATTY_AX25_SOCK_PROMISC;
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
#include <patty/ax25.h>
|
||||
#include <patty/print.h>
|
||||
#include <patty/util.h>
|
||||
|
||||
#include <patty/bin/kiss.h>
|
||||
|
||||
|
@ -108,7 +109,7 @@ static int pty_promisc(patty_client *client,
|
|||
setsockopt_request.opt = PATTY_AX25_SOCK_IF;
|
||||
setsockopt_request.len = sizeof(ifreq);
|
||||
|
||||
strncpy(ifreq.name, ifname, sizeof(ifreq.name)-1);
|
||||
patty_strlcpy(ifreq.name, ifname, sizeof(ifreq.name));
|
||||
ifreq.state = PATTY_AX25_SOCK_PROMISC;
|
||||
|
||||
call = PATTY_CLIENT_SETSOCKOPT;
|
||||
|
@ -141,7 +142,7 @@ static int pty_promisc(patty_client *client,
|
|||
goto error_client_setsockopt;
|
||||
}
|
||||
|
||||
strncpy(pty, socket_response.path, len);
|
||||
patty_strlcpy(pty, socket_response.path, len);
|
||||
|
||||
return socket_response.fd;
|
||||
|
||||
|
|
8
include/patty/util.h
Normal file
8
include/patty/util.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef _PATTY_UTIL_H
|
||||
#define _PATTY_UTIL_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
ssize_t patty_strlcpy(char *dest, const char *src, size_t n);
|
||||
|
||||
#endif /* _PATTY_UTIL_H */
|
|
@ -9,11 +9,11 @@ LDFLAGS += -lutil
|
|||
|
||||
HEADERS = kiss.h kiss/tnc.h ax25/aprs_is.h ax25.h client.h ax25/if.h \
|
||||
ax25/frame.h ax25/sock.h ax25/route.h ax25/server.h daemon.h \
|
||||
error.h list.h hash.h dict.h timer.h print.h conf.h
|
||||
error.h list.h hash.h dict.h timer.h print.h util.h conf.h
|
||||
|
||||
OBJS = kiss.o tnc.o aprs_is.o ax25.o client.o if.o \
|
||||
frame.o sock.o route.o server.o daemon.o \
|
||||
error.o list.o hash.o dict.o timer.o print.o conf.o
|
||||
error.o list.o hash.o dict.o timer.o print.o util.o conf.o
|
||||
|
||||
VERSION_MAJOR = 0
|
||||
VERSION_MINOR = 0.1
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include <patty/ax25.h>
|
||||
#include <patty/daemon.h>
|
||||
#include <patty/util.h>
|
||||
|
||||
struct _patty_client_sock {
|
||||
int fd;
|
||||
|
@ -49,7 +50,7 @@ patty_client *patty_client_new(const char *path) {
|
|||
memset(&addr, '\0', sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
|
||||
strncpy(addr.sun_path, find_sock(path), sizeof(addr.sun_path)-1);
|
||||
patty_strlcpy(addr.sun_path, find_sock(path), sizeof(addr.sun_path));
|
||||
|
||||
if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
goto error_connect;
|
||||
|
@ -190,7 +191,7 @@ int patty_client_socket(patty_client *client,
|
|||
|
||||
sock->fd = response.fd;
|
||||
|
||||
strncpy(sock->path, response.path, sizeof(sock->path));
|
||||
patty_strlcpy(sock->path, response.path, sizeof(sock->path));
|
||||
|
||||
if ((fd = open(sock->path, O_RDWR)) < 0) {
|
||||
goto error_open;
|
||||
|
@ -423,7 +424,7 @@ int patty_client_accept(patty_client *client,
|
|||
remote->fd = message.fd;
|
||||
|
||||
memcpy(peer, &message.peer, sizeof(*peer));
|
||||
strncpy(remote->path, message.path, sizeof(remote->path));
|
||||
patty_strlcpy(remote->path, message.path, sizeof(remote->path));
|
||||
|
||||
if ((pty = open(remote->path, O_RDWR)) < 0) {
|
||||
goto error_open;
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <patty/ax25.h>
|
||||
#include <patty/kiss/tnc.h>
|
||||
#include <patty/hash.h>
|
||||
#include <patty/util.h>
|
||||
|
||||
typedef int (*patty_ax25_server_call)(patty_ax25_server *, int);
|
||||
|
||||
|
@ -458,7 +459,7 @@ int patty_ax25_server_if_add(patty_ax25_server *server,
|
|||
goto error_if_fd;
|
||||
}
|
||||
|
||||
strncpy(entry->name, name, sizeof(entry->name)-1);
|
||||
patty_strlcpy(entry->name, name, sizeof(entry->name));
|
||||
|
||||
entry->iface = iface;
|
||||
|
||||
|
@ -598,7 +599,7 @@ static int notify_accept(int local,
|
|||
}
|
||||
|
||||
if (path) {
|
||||
strncpy(message.path, path, sizeof(message.path)-1);
|
||||
patty_strlcpy(message.path, path, sizeof(message.path));
|
||||
}
|
||||
|
||||
return write(local, &message, sizeof(message));
|
||||
|
@ -1069,7 +1070,7 @@ static int listen_unix(patty_ax25_server *server, const char *path) {
|
|||
|
||||
memset(&addr, '\0', sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1);
|
||||
patty_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
|
||||
|
||||
if (bind(server->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
goto error_bind;
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <patty/ax25.h>
|
||||
#include <patty/kiss.h>
|
||||
#include <patty/kiss/tnc.h>
|
||||
#include <patty/util.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
|
@ -65,7 +66,7 @@ static int init_sock(patty_kiss_tnc *tnc, patty_kiss_tnc_info *info) {
|
|||
|
||||
memset(&addr, '\0', sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, info->device, sizeof(addr.sun_path)-1);
|
||||
patty_strlcpy(addr.sun_path, info->device, sizeof(addr.sun_path));
|
||||
|
||||
if (connect(tnc->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
goto error_connect;
|
||||
|
|
17
src/util.c
Normal file
17
src/util.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <patty/util.h>
|
||||
|
||||
ssize_t patty_strlcpy(char *dest, const char *src, size_t n) {
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<n-1; i++) {
|
||||
dest[i] = src[i];
|
||||
|
||||
if (src[i] == '\0') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dest[i] = '\0';
|
||||
|
||||
return i;
|
||||
}
|
Loading…
Add table
Reference in a new issue