summaryrefslogtreecommitdiff
path: root/apps/plugins/lib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lib')
-rw-r--r--apps/plugins/lib/SOURCES1
-rw-r--r--apps/plugins/lib/configfile.c2
-rw-r--r--apps/plugins/lib/highscore.c5
-rw-r--r--apps/plugins/lib/strncpy.c125
-rw-r--r--apps/plugins/lib/wrappers.h2
5 files changed, 130 insertions, 5 deletions
diff --git a/apps/plugins/lib/SOURCES b/apps/plugins/lib/SOURCES
index 02adb7089c..72538fc2a0 100644
--- a/apps/plugins/lib/SOURCES
+++ b/apps/plugins/lib/SOURCES
@@ -6,6 +6,7 @@ playback_control.c
6rgb_hsv.c 6rgb_hsv.c
7buflib.c 7buflib.c
8display_text.c 8display_text.c
9strncpy.c
9#if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4) 10#if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4)
10grey_core.c 11grey_core.c
11grey_draw.c 12grey_draw.c
diff --git a/apps/plugins/lib/configfile.c b/apps/plugins/lib/configfile.c
index 5e1e776f39..21b66a317b 100644
--- a/apps/plugins/lib/configfile.c
+++ b/apps/plugins/lib/configfile.c
@@ -139,7 +139,7 @@ int configfile_load(const char *filename, struct configdata *cfg,
139 break; 139 break;
140 140
141 case TYPE_STRING: 141 case TYPE_STRING:
142 rb->strncpy(cfg[i].string, val, cfg[i].max); 142 rb->strlcpy(cfg[i].string, val, cfg[i].max);
143 break; 143 break;
144 } 144 }
145 } 145 }
diff --git a/apps/plugins/lib/highscore.c b/apps/plugins/lib/highscore.c
index e8e1c883b0..15ebb05f4d 100644
--- a/apps/plugins/lib/highscore.c
+++ b/apps/plugins/lib/highscore.c
@@ -72,7 +72,7 @@ int highscore_load(char *filename, struct highscore *scores, int num_scores)
72 72
73 scores[i].score = rb->atoi(score); 73 scores[i].score = rb->atoi(score);
74 scores[i].level = rb->atoi(level); 74 scores[i].level = rb->atoi(level);
75 rb->strncpy(scores[i].name, name, sizeof(scores[i].name)-1); 75 rb->strlcpy(scores[i].name, name, sizeof(scores[i].name));
76 i++; 76 i++;
77 } 77 }
78 rb->close(fd); 78 rb->close(fd);
@@ -100,8 +100,7 @@ int highscore_update(int score, int level, const char *name,
100 entry = scores + pos; 100 entry = scores + pos;
101 entry->score = score; 101 entry->score = score;
102 entry->level = level; 102 entry->level = level;
103 rb->strncpy(entry->name, name, sizeof(entry->name)); 103 rb->strlcpy(entry->name, name, sizeof(entry->name));
104 entry->name[sizeof(entry->name)-1] = '\0';
105 104
106 return pos; 105 return pos;
107} 106}
diff --git a/apps/plugins/lib/strncpy.c b/apps/plugins/lib/strncpy.c
new file mode 100644
index 0000000000..7c1973ba66
--- /dev/null
+++ b/apps/plugins/lib/strncpy.c
@@ -0,0 +1,125 @@
1/*
2FUNCTION
3 <<strncpy>>---counted copy string
4
5INDEX
6 strncpy
7
8ANSI_SYNOPSIS
9 #include <string.h>
10 char *strncpy(char *<[dst]>, const char *<[src]>, size_t <[length]>);
11
12TRAD_SYNOPSIS
13 #include <string.h>
14 char *strncpy(<[dst]>, <[src]>, <[length]>)
15 char *<[dst]>;
16 char *<[src]>;
17 size_t <[length]>;
18
19DESCRIPTION
20 <<strncpy>> copies not more than <[length]> characters from the
21 the string pointed to by <[src]> (including the terminating
22 null character) to the array pointed to by <[dst]>. If the
23 string pointed to by <[src]> is shorter than <[length]>
24 characters, null characters are appended to the destination
25 array until a total of <[length]> characters have been
26 written.
27
28RETURNS
29 This function returns the initial value of <[dst]>.
30
31PORTABILITY
32<<strncpy>> is ANSI C.
33
34<<strncpy>> requires no supporting OS subroutines.
35
36QUICKREF
37 strncpy ansi pure
38*/
39
40#include <string.h>
41#include <limits.h>
42
43/*SUPPRESS 560*/
44/*SUPPRESS 530*/
45
46/* Nonzero if either X or Y is not aligned on a "long" boundary. */
47#define UNALIGNED(X, Y) \
48 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
49
50#if LONG_MAX == 2147483647L
51#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
52#else
53#if LONG_MAX == 9223372036854775807L
54/* Nonzero if X (a long int) contains a NULL byte. */
55#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
56#else
57#error long int is not a 32bit or 64bit type.
58#endif
59#endif
60
61#ifndef DETECTNULL
62#error long int is not a 32bit or 64bit byte
63#endif
64
65#define TOO_SMALL(LEN) ((LEN) < sizeof (long))
66
67char *
68_DEFUN (strncpy, (dst0, src0),
69 char *dst0 _AND
70 _CONST char *src0 _AND
71 size_t count)
72{
73#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
74 char *dscan;
75 _CONST char *sscan;
76
77 dscan = dst0;
78 sscan = src0;
79 while (count > 0)
80 {
81 --count;
82 if ((*dscan++ = *sscan++) == '\0')
83 break;
84 }
85 while (count-- > 0)
86 *dscan++ = '\0';
87
88 return dst0;
89#else
90 char *dst = dst0;
91 _CONST char *src = src0;
92 long *aligned_dst;
93 _CONST long *aligned_src;
94
95 /* If SRC and DEST is aligned and count large enough, then copy words. */
96 if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
97 {
98 aligned_dst = (long*)dst;
99 aligned_src = (long*)src;
100
101 /* SRC and DEST are both "long int" aligned, try to do "long int"
102 sized copies. */
103 while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
104 {
105 count -= sizeof (long int);
106 *aligned_dst++ = *aligned_src++;
107 }
108
109 dst = (char*)aligned_dst;
110 src = (char*)aligned_src;
111 }
112
113 while (count > 0)
114 {
115 --count;
116 if ((*dst++ = *src++) == '\0')
117 break;
118 }
119
120 while (count-- > 0)
121 *dst++ = '\0';
122
123 return dst0;
124#endif /* not PREFER_SIZE_OVER_SPEED */
125}
diff --git a/apps/plugins/lib/wrappers.h b/apps/plugins/lib/wrappers.h
index 5eb45d02c8..b6fbd51a39 100644
--- a/apps/plugins/lib/wrappers.h
+++ b/apps/plugins/lib/wrappers.h
@@ -51,7 +51,7 @@
51#define strcpy rb->strcpy 51#define strcpy rb->strcpy
52#define strip_extension rb->strip_extension 52#define strip_extension rb->strip_extension
53#define strlen rb->strlen 53#define strlen rb->strlen
54#define strncpy rb->strncpy 54#define strlcpy rb->strlcpy
55#define strrchr rb->strrchr 55#define strrchr rb->strrchr
56 56
57#endif 57#endif