Skip to content

Commit

Permalink
Introduce shared strlcpy() and strlcat() implementations
Browse files Browse the repository at this point in the history
These were already used internally in some source code files for
non-Android builds. Share a single common implementations in utils.c and
make it available throughout sigma_dut to make this cleaner.

Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
  • Loading branch information
Peng Xu authored and jmalinen committed May 14, 2017
1 parent 8c9c156 commit 769731a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 72 deletions.
48 changes: 0 additions & 48 deletions miracast.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,54 +50,6 @@ const char *ipaddr (in_addr_t addr)
#endif /* MIRACAST_DHCP_M */


#ifndef ANDROID

static size_t strlcpy(char *dest, const char *src, size_t siz)
{
const char *s = src;
size_t left = siz;

if (left) {
/* Copy string up to the maximum size of the dest buffer */
while (--left != 0) {
if ((*dest++ = *s++) == '\0')
break;
}
}

if (left == 0) {
/* Not enough room for the string; force NUL-termination */
if (siz != 0)
*dest = '\0';
while (*s++)
; /* determine total src string length */
}

return s - src - 1;
}


static size_t strlcat(char *dst, const char *str, size_t size)
{
char *pos;
size_t dstlen, srclen, copy;

srclen = strlen(str);
for (pos = dst; pos - dst < size && *dst; pos++)
;
dstlen = pos - dst;
if (*dst)
return dstlen + srclen;
if (dstlen + srclen + 1 > size)
copy = size - dstlen - 1;
else
copy = srclen;
memcpy(pos, str, copy);
pos[copy] = '\0';
return dstlen + srclen;
}

#endif /* ANDROID */


static int miracast_load(struct sigma_dut *dut)
Expand Down
6 changes: 6 additions & 0 deletions sigma_dut.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ int parse_mac_address(struct sigma_dut *dut, const char *arg,
unsigned int channel_to_freq(unsigned int channel);
unsigned int freq_to_channel(unsigned int freq);

#ifndef ANDROID
size_t strlcpy(char *dest, const char *src, size_t siz);
size_t strlcat(char *dst, const char *str, size_t size);
#endif /* ANDROID */


/* uapsd_stream.c */
void receive_uapsd(struct sigma_stream *s);
void send_uapsd_console(struct sigma_stream *s);
Expand Down
50 changes: 50 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,53 @@ unsigned int freq_to_channel(unsigned int freq)
return (freq - 5000) / 5;
return 0;
}


#ifndef ANDROID

size_t strlcpy(char *dest, const char *src, size_t siz)
{
const char *s = src;
size_t left = siz;

if (left) {
/* Copy string up to the maximum size of the dest buffer */
while (--left != 0) {
if ((*dest++ = *s++) == '\0')
break;
}
}

if (left == 0) {
/* Not enough room for the string; force NUL-termination */
if (siz != 0)
*dest = '\0';
while (*s++)
; /* determine total src string length */
}

return s - src - 1;
}


size_t strlcat(char *dst, const char *str, size_t size)
{
char *pos;
size_t dstlen, srclen, copy;

srclen = strlen(str);
for (pos = dst; pos - dst < size && *dst; pos++)
;
dstlen = pos - dst;
if (*dst)
return dstlen + srclen;
if (dstlen + srclen + 1 > size)
copy = size - dstlen - 1;
else
copy = srclen;
memcpy(pos, str, copy);
pos[copy] = '\0';
return dstlen + srclen;
}

#endif /* ANDROID */
26 changes: 2 additions & 24 deletions wpa_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,8 @@
#define os_snprintf snprintf
#define os_strlen strlen
#define os_strncmp strncmp

static size_t os_strlcpy(char *dest, const char *src, size_t siz)
{
const char *s = src;
size_t left = siz;

if (left) {
/* Copy string up to the maximum size of the dest buffer */
while (--left != 0) {
if ((*dest++ = *s++) == '\0')
break;
}
}

if (left == 0) {
/* Not enough room for the string; force NUL-termination */
if (siz != 0)
*dest = '\0';
while (*s++)
; /* determine total src string length */
}

return s - src - 1;
}
#define os_strlcpy strlcpy
#include "sigma_dut.h"

#ifdef CONFIG_CTRL_IFACE

Expand Down

0 comments on commit 769731a

Please sign in to comment.