Skip to content

Commit

Permalink
Support POSIX basename() from musl libc (#123)
Browse files Browse the repository at this point in the history
Musl libc 1.2.5 removed the definition of the basename() function from
string.h and only provides it in libgen.h as the POSIX standard
defines it.

This change fixes compilation with musl libc 1.2.5.
````
build_dir/target-mips_24kc_musl/rtty-mbedtls/rtty-8.1.1/src/file.c:156:24: error: implicit declaration of function 'basename' [-Werror=implicit-function-declaration]
  156 |     const char *name = basename(path);
      |                        ^~~~~~~~
````

basename() modifies the input string, copy it first with strdup(), If
strdup() returns NULL the code will handle it.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  • Loading branch information
hauke authored Apr 15, 2024
1 parent c09881d commit eb6394c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <unistd.h>
#include <mntent.h>
#include <inttypes.h>
#include <libgen.h>
#include <sys/statvfs.h>
#include <linux/limits.h>
#include <sys/sysinfo.h>
Expand Down Expand Up @@ -153,13 +154,17 @@ static int start_upload_file(struct file_context *ctx, const char *path)
{
struct tty *tty = container_of(ctx, struct tty, file);
struct rtty *rtty = tty->rtty;
const char *name = basename(path);
const char *name;
struct stat st;
int fd;
char *dirc;

dirc = strdup(path);
name = basename(dirc);
fd = open(path, O_RDONLY);
if (fd < 0) {
log_err("open '%s' fail: %s\n", path, strerror(errno));
free(dirc);
return -1;
}

Expand All @@ -177,6 +182,7 @@ static int start_upload_file(struct file_context *ctx, const char *path)
ctx->remain_size = st.st_size;

log_info("upload file: %s, size: %" PRIu64 "\n", path, (uint64_t)st.st_size);
free(dirc);

return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/filectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <libgen.h>

#include "utils.h"
#include "file.h"
Expand Down Expand Up @@ -75,6 +76,7 @@ static void handle_file_control_msg(int fd, int sfd, const char *path)
{
struct file_control_msg msg;
struct buffer b = {};
char *dirc;

while (true) {
if (buffer_put_fd(&b, fd, -1, NULL) < 0)
Expand All @@ -90,7 +92,9 @@ static void handle_file_control_msg(int fd, int sfd, const char *path)
if (sfd > -1) {
close(sfd);
gettimeofday(&start_time, NULL);
printf("Transferring '%s'...Press Ctrl+C to cancel\n", basename(path));
dirc = strdup(path);
printf("Transferring '%s'...Press Ctrl+C to cancel\n", basename(dirc));
free(dirc);

if (total_size == 0) {
printf(" 100%% 0 B 0s\n");
Expand Down

0 comments on commit eb6394c

Please sign in to comment.