summaryrefslogtreecommitdiffstats
path: root/firmware
diff options
context:
space:
mode:
authorNils Wallménius <nils@rockbox.org>2009-07-14 13:57:45 +0000
committerNils Wallménius <nils@rockbox.org>2009-07-14 13:57:45 +0000
commit3d4701a6e41616cf581a297bab1451cf2db70249 (patch)
treef845837c96ffbed7d59ddf8308f3b3e7c40cb8c9 /firmware
parentc2900a1bacd5d98b57a0d15ea2add1bc08764057 (diff)
downloadrockbox-3d4701a6e41616cf581a297bab1451cf2db70249.tar.gz
rockbox-3d4701a6e41616cf581a297bab1451cf2db70249.tar.bz2
rockbox-3d4701a6e41616cf581a297bab1451cf2db70249.zip
FS#10080
* Move strncpy() from core to the pluginlib * Introduce strlcpy() and use that instead in most places (use memcpy in a few) in core and some plugins * Drop strncpy() from the codec api as no codec used it * Bump codec and plugin api versions git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21863 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES2
-rw-r--r--firmware/common/dir_uncached.c11
-rw-r--r--firmware/common/dircache.c28
-rw-r--r--firmware/common/file.c3
-rw-r--r--firmware/common/strlcpy.c52
-rw-r--r--firmware/common/strncpy.c125
-rw-r--r--firmware/drivers/fat.c4
-rw-r--r--firmware/drivers/lcd-16bit.c2
-rw-r--r--firmware/drivers/lcd-1bit-vert.c2
-rw-r--r--firmware/drivers/lcd-2bit-vert.c2
-rw-r--r--firmware/drivers/lcd-2bit-vi.c2
-rw-r--r--firmware/drivers/lcd-charcell.c2
-rw-r--r--firmware/general.c4
-rw-r--r--firmware/include/string.h3
-rw-r--r--firmware/logf.c2
15 files changed, 84 insertions, 160 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 50c57e3323..8e1cef31e0 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -52,7 +52,7 @@ common/strcmp.c
common/strnatcmp.c
common/strcpy.c
common/strncmp.c
-common/strncpy.c
+common/strlcpy.c
common/strrchr.c
common/strtok.c
common/strstr.c
diff --git a/firmware/common/dir_uncached.c b/firmware/common/dir_uncached.c
index c6af145930..25677a0903 100644
--- a/firmware/common/dir_uncached.c
+++ b/firmware/common/dir_uncached.c
@@ -58,8 +58,7 @@ int strip_volume(const char* name, char* namecopy)
name = "/"; /* else this must be the root dir */
}
- strncpy(namecopy, name, MAX_PATH);
- namecopy[MAX_PATH-1] = '\0';
+ strlcpy(namecopy, name, MAX_PATH);
return volume;
}
@@ -120,8 +119,7 @@ DIR_UNCACHED* opendir_uncached(const char* name)
volume = strip_volume(name, namecopy);
pdir->volumecounter = 0;
#else
- strncpy(namecopy,name,sizeof(namecopy)); /* just copy */
- namecopy[sizeof(namecopy)-1] = '\0';
+ strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
#endif
if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
@@ -204,7 +202,7 @@ struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
if ( !entry.name[0] )
return NULL;
- strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) );
+ strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
theent->attribute = entry.attr;
theent->size = entry.filesize;
theent->startcluster = entry.firstcluster;
@@ -230,8 +228,7 @@ int mkdir_uncached(const char *name)
return -1;
}
- strncpy(namecopy,name,sizeof(namecopy));
- namecopy[sizeof(namecopy)-1] = 0;
+ strlcpy(namecopy, name, sizeof(namecopy));
/* Split the base name and the path */
end = strrchr(namecopy, '/');
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index 01ed72adc1..e846d55452 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -232,11 +232,11 @@ static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
return -2;
td->pathpos = strlen(dircache_cur_path);
- strncpy(&dircache_cur_path[td->pathpos], "/",
- sizeof(dircache_cur_path) - td->pathpos - 1);
+ strlcpy(&dircache_cur_path[td->pathpos], "/",
+ sizeof(dircache_cur_path) - td->pathpos);
#ifdef SIMULATOR
- strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
- sizeof(dircache_cur_path) - td->pathpos - 2);
+ strlcpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
+ sizeof(dircache_cur_path) - td->pathpos - 1);
td->newdir = opendir_uncached(dircache_cur_path);
if (td->newdir == NULL)
@@ -245,8 +245,8 @@ static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
return -3;
}
#else
- strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
- sizeof(dircache_cur_path) - td->pathpos - 2);
+ strlcpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
+ sizeof(dircache_cur_path) - td->pathpos - 1);
td->newdir = *td->dir;
if (fat_opendir(IF_MV2(volume,) &td->newdir,
@@ -399,7 +399,7 @@ static struct dircache_entry* dircache_get_entry(const char *path,
char* part;
char* end;
- strncpy(namecopy, path, sizeof(namecopy) - 1);
+ strlcpy(namecopy, path, sizeof(namecopy));
cache_entry = dircache_root;
before = NULL;
@@ -926,7 +926,7 @@ static struct dircache_entry* dircache_new_entry(const char *path, int attribute
char *new;
long last_cache_size = dircache_size;
- strncpy(basedir, path, sizeof(basedir)-1);
+ strlcpy(basedir, path, sizeof(basedir));
new = strrchr(basedir, '/');
if (new == NULL)
{
@@ -997,8 +997,8 @@ void dircache_bind(int fd, const char *path)
{
if (fdbind_idx >= MAX_PENDING_BINDINGS)
return ;
- strncpy(fdbind_cache[fdbind_idx].path, path,
- sizeof(fdbind_cache[fdbind_idx].path)-1);
+ strlcpy(fdbind_cache[fdbind_idx].path, path,
+ sizeof(fdbind_cache[fdbind_idx].path));
fdbind_cache[fdbind_idx].fd = fd;
fdbind_idx++;
return ;
@@ -1141,7 +1141,7 @@ void dircache_rename(const char *oldpath, const char *newpath)
/* Generate the absolute path for destination if necessary. */
if (newpath[0] != '/')
{
- strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
+ strlcpy(absolute_path, oldpath, sizeof(absolute_path));
p = strrchr(absolute_path, '/');
if (!p)
{
@@ -1151,7 +1151,7 @@ void dircache_rename(const char *oldpath, const char *newpath)
}
*p = '\0';
- strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
+ strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
newpath = absolute_path;
}
@@ -1246,7 +1246,7 @@ struct dircache_entry* readdir_cached(DIR_CACHED* dir)
if (regentry == NULL)
return NULL;
- strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
+ strlcpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH);
dir->secondary_entry.size = regentry->size;
dir->secondary_entry.startcluster = regentry->startcluster;
dir->secondary_entry.attribute = regentry->attribute;
@@ -1268,7 +1268,7 @@ struct dircache_entry* readdir_cached(DIR_CACHED* dir)
dir->entry = ce->next;
- strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
+ strlcpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH);
/* Can't do `dir->secondary_entry = *ce`
because that modifies the d_name pointer. */
dir->secondary_entry.size = ce->size;
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 7c01f03817..3d7722f687 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -132,8 +132,7 @@ static int open_internal(const char* pathname, int flags, bool use_cache)
}
#endif
- strncpy(pathnamecopy,pathname,sizeof(pathnamecopy));
- pathnamecopy[sizeof(pathnamecopy)-1] = 0;
+ strlcpy(pathnamecopy, pathname, sizeof(pathnamecopy));
/* locate filename */
name=strrchr(pathnamecopy+1,'/');
diff --git a/firmware/common/strlcpy.c b/firmware/common/strlcpy.c
new file mode 100644
index 0000000000..ac30ef01fe
--- /dev/null
+++ b/firmware/common/strlcpy.c
@@ -0,0 +1,52 @@
+/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
+
+/*
+ * Copyright (c) 1998 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 src to string dst of size siz. At most siz-1 characters
+ * will be copied. Always NUL terminates (unless siz == 0).
+ * Returns strlen(src); if retval >= siz, truncation occurred.
+ */
+size_t
+strlcpy(char *dst, const char *src, size_t siz)
+{
+ char *d = dst;
+ const char *s = src;
+ size_t n = siz;
+
+ /* Copy as many bytes as will fit */
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*d++ = *s++) == '\0')
+ break;
+ }
+ }
+
+ /* Not enough room in dst, add NUL and traverse rest of src */
+ if (n == 0) {
+ if (siz != 0)
+ *d = '\0'; /* NUL-terminate dst */
+ while (*s++)
+ ;
+ }
+
+ return(s - src - 1); /* count does not include NUL */
+}
+
diff --git a/firmware/common/strncpy.c b/firmware/common/strncpy.c
deleted file mode 100644
index 7c1973ba66..0000000000
--- a/firmware/common/strncpy.c
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
-FUNCTION
- <<strncpy>>---counted copy string
-
-INDEX
- strncpy
-
-ANSI_SYNOPSIS
- #include <string.h>
- char *strncpy(char *<[dst]>, const char *<[src]>, size_t <[length]>);
-
-TRAD_SYNOPSIS
- #include <string.h>
- char *strncpy(<[dst]>, <[src]>, <[length]>)
- char *<[dst]>;
- char *<[src]>;
- size_t <[length]>;
-
-DESCRIPTION
- <<strncpy>> copies not more than <[length]> characters from the
- the string pointed to by <[src]> (including the terminating
- null character) to the array pointed to by <[dst]>. If the
- string pointed to by <[src]> is shorter than <[length]>
- characters, null characters are appended to the destination
- array until a total of <[length]> characters have been
- written.
-
-RETURNS
- This function returns the initial value of <[dst]>.
-
-PORTABILITY
-<<strncpy>> is ANSI C.
-
-<<strncpy>> requires no supporting OS subroutines.
-
-QUICKREF
- strncpy ansi pure
-*/
-
-#include <string.h>
-#include <limits.h>
-
-/*SUPPRESS 560*/
-/*SUPPRESS 530*/
-
-/* Nonzero if either X or Y is not aligned on a "long" boundary. */
-#define UNALIGNED(X, Y) \
- (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
-
-#if LONG_MAX == 2147483647L
-#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
-#else
-#if LONG_MAX == 9223372036854775807L
-/* Nonzero if X (a long int) contains a NULL byte. */
-#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
-#else
-#error long int is not a 32bit or 64bit type.
-#endif
-#endif
-
-#ifndef DETECTNULL
-#error long int is not a 32bit or 64bit byte
-#endif
-
-#define TOO_SMALL(LEN) ((LEN) < sizeof (long))
-
-char *
-_DEFUN (strncpy, (dst0, src0),
- char *dst0 _AND
- _CONST char *src0 _AND
- size_t count)
-{
-#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
- char *dscan;
- _CONST char *sscan;
-
- dscan = dst0;
- sscan = src0;
- while (count > 0)
- {
- --count;
- if ((*dscan++ = *sscan++) == '\0')
- break;
- }
- while (count-- > 0)
- *dscan++ = '\0';
-
- return dst0;
-#else
- char *dst = dst0;
- _CONST char *src = src0;
- long *aligned_dst;
- _CONST long *aligned_src;
-
- /* If SRC and DEST is aligned and count large enough, then copy words. */
- if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
- {
- aligned_dst = (long*)dst;
- aligned_src = (long*)src;
-
- /* SRC and DEST are both "long int" aligned, try to do "long int"
- sized copies. */
- while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
- {
- count -= sizeof (long int);
- *aligned_dst++ = *aligned_src++;
- }
-
- dst = (char*)aligned_dst;
- src = (char*)aligned_src;
- }
-
- while (count > 0)
- {
- --count;
- if ((*dst++ = *src++) == '\0')
- break;
- }
-
- while (count-- > 0)
- *dst++ = '\0';
-
- return dst0;
-#endif /* not PREFER_SIZE_OVER_SPEED */
-}
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index 2ff4c61ac4..a710593a69 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -1183,7 +1183,7 @@ static int write_long_name(struct fat_file* file,
/* shortname entry */
unsigned short date=0, time=0, tenth=0;
LDEBUGF("Shortname entry: %s\n", shortname);
- strncpy(entry + FATDIR_NAME, shortname, 11);
+ memcpy(entry + FATDIR_NAME, shortname, 11);
entry[FATDIR_ATTR] = is_directory?FAT_ATTR_DIRECTORY:0;
entry[FATDIR_NTRES] = 0;
@@ -1271,7 +1271,7 @@ static int add_dir_entry(struct fat_dir* dir,
/* The "." and ".." directory entries must not be long names */
if(dotdir) {
int i;
- strncpy(shortname, name, 12);
+ strlcpy(shortname, name, 12);
for(i = strlen(shortname); i < 12; i++)
shortname[i] = ' ';
diff --git a/firmware/drivers/lcd-16bit.c b/firmware/drivers/lcd-16bit.c
index ef0865fc8c..882bfa0854 100644
--- a/firmware/drivers/lcd-16bit.c
+++ b/firmware/drivers/lcd-16bit.c
@@ -1152,7 +1152,7 @@ void lcd_puts_scroll_style_offset(int x, int y, const unsigned char *string,
}
end = strchr(s->line, '\0');
- strncpy(end, string, current_vp->width/2);
+ strlcpy(end, string, current_vp->width/2);
s->vp = current_vp;
s->y = y;
diff --git a/firmware/drivers/lcd-1bit-vert.c b/firmware/drivers/lcd-1bit-vert.c
index 5fb652431c..f11fd6fdf9 100644
--- a/firmware/drivers/lcd-1bit-vert.c
+++ b/firmware/drivers/lcd-1bit-vert.c
@@ -845,7 +845,7 @@ void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
}
end = strchr(s->line, '\0');
- strncpy(end, string, current_vp->width/2);
+ strlcpy(end, string, current_vp->width/2);
s->vp = current_vp;
s->y = y;
diff --git a/firmware/drivers/lcd-2bit-vert.c b/firmware/drivers/lcd-2bit-vert.c
index 801927eb37..57d27d2ead 100644
--- a/firmware/drivers/lcd-2bit-vert.c
+++ b/firmware/drivers/lcd-2bit-vert.c
@@ -1154,7 +1154,7 @@ void lcd_puts_scroll_style_offset(int x, int y, const unsigned char *string,
}
end = strchr(s->line, '\0');
- strncpy(end, (char *)string, current_vp->width/2);
+ strlcpy(end, (char *)string, current_vp->width/2);
s->vp = current_vp;
s->y = y;
diff --git a/firmware/drivers/lcd-2bit-vi.c b/firmware/drivers/lcd-2bit-vi.c
index 1c0a717d13..0a73f0dd25 100644
--- a/firmware/drivers/lcd-2bit-vi.c
+++ b/firmware/drivers/lcd-2bit-vi.c
@@ -1167,7 +1167,7 @@ void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
}
end = strchr(s->line, '\0');
- strncpy(end, (char *)string, current_vp->width/2);
+ strlcpy(end, (char *)string, current_vp->width/2);
s->vp = current_vp;
s->y = y;
diff --git a/firmware/drivers/lcd-charcell.c b/firmware/drivers/lcd-charcell.c
index ca5809899f..33337daf19 100644
--- a/firmware/drivers/lcd-charcell.c
+++ b/firmware/drivers/lcd-charcell.c
@@ -496,7 +496,7 @@ void lcd_puts_scroll_offset(int x, int y, const unsigned char *string,
}
end = strchr(s->line, '\0');
- strncpy(end, string, utf8seek(s->line, current_vp->width));
+ strlcpy(end, string, utf8seek(s->line, current_vp->width));
s->vp = current_vp;
s->y = y;
diff --git a/firmware/general.c b/firmware/general.c
index 14b2b55bf9..6f7238ead1 100644
--- a/firmware/general.c
+++ b/firmware/general.c
@@ -110,7 +110,7 @@ char *create_numbered_filename(char *buffer, const char *path,
char fmtstring[12];
if (buffer != path)
- strncpy(buffer, path, MAX_PATH);
+ strlcpy(buffer, path, MAX_PATH);
pathlen = strlen(buffer);
@@ -185,7 +185,7 @@ char *create_datetime_filename(char *buffer, const char *path,
last_tm = *tm;
if (buffer != path)
- strncpy(buffer, path, MAX_PATH);
+ strlcpy(buffer, path, MAX_PATH);
pathlen = strlen(buffer);
snprintf(buffer + pathlen, MAX_PATH - pathlen,
diff --git a/firmware/include/string.h b/firmware/include/string.h
index 32b86cd2b0..6085c10eb6 100644
--- a/firmware/include/string.h
+++ b/firmware/include/string.h
@@ -35,13 +35,14 @@ char *_EXFUN(strerror,(int));
size_t _EXFUN(strlen,(const char *));
char *_EXFUN(strncat,(char *, const char *, size_t));
int _EXFUN(strncmp,(const char *, const char *, size_t));
-char *_EXFUN(strncpy,(char *, const char *, size_t));
char *_EXFUN(strpbrk,(const char *, const char *));
char *_EXFUN(strrchr,(const char *, int));
size_t _EXFUN(strspn,(const char *, const char *));
char *_EXFUN(strstr,(const char *, const char *));
char *_EXFUN(strcasestr,(const char *, const char *));
+size_t strlcpy(char *dst, const char *src, size_t siz);
+
#ifndef _REENT_ONLY
char *_EXFUN(strtok,(char *, const char *));
#endif
diff --git a/firmware/logf.c b/firmware/logf.c
index a704ad2d67..6e3e532450 100644
--- a/firmware/logf.c
+++ b/firmware/logf.c
@@ -145,7 +145,7 @@ void _logf(const char *format, ...)
while(len > MAX_LOGF_ENTRY)
{
ptr = logfbuffer[logfindex];
- strncpy(ptr, buf + tlen, MAX_LOGF_ENTRY-1);
+ strlcpy(ptr, buf + tlen, MAX_LOGF_ENTRY);
ptr[MAX_LOGF_ENTRY] = LOGF_TERMINATE_CONTINUE_LINE;
logfindex++;
check_logfindex();