[PATCH 15/21] inmates: Add strlen and strncmp to library

43 views
Skip to first unread message

Jan Kiszka

unread,
Jan 10, 2016, 3:58:01 AM1/10/16
to jailho...@googlegroups.com
From: Jan Kiszka <jan.k...@siemens.com>

Add simplistic but generic implementations of strlen and strncmp to the
inmate library. Both will be used for the command line parser.

Signed-off-by: Jan Kiszka <jan.k...@siemens.com>
---
inmates/lib/inmate_common.h | 2 ++
inmates/lib/string.c | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+)

diff --git a/inmates/lib/inmate_common.h b/inmates/lib/inmate_common.h
index 30d0d39..382b81b 100644
--- a/inmates/lib/inmate_common.h
+++ b/inmates/lib/inmate_common.h
@@ -40,6 +40,8 @@ void printk(const char *fmt, ...);

void *memset(void *s, int c, unsigned long n);
void *memcpy(void *d, const void *s, unsigned long n);
+unsigned long strlen(const char *s);
+int strncmp(const char *s1, const char *s2, unsigned long n);

void inmate_main(void);

diff --git a/inmates/lib/string.c b/inmates/lib/string.c
index af80517..a6a3874 100644
--- a/inmates/lib/string.c
+++ b/inmates/lib/string.c
@@ -20,3 +20,27 @@ void *memset(void *s, int c, unsigned long n)
*p++ = c;
return s;
}
+
+unsigned long strlen(const char *s1)
+{
+ unsigned long len = 0;
+
+ while (*s1++)
+ len++;
+
+ return len;
+}
+
+int strncmp(const char *s1, const char *s2, unsigned long n)
+{
+ int diff;
+
+ while (n-- > 0) {
+ diff = *s1 - *s2;
+ if (diff)
+ return diff;
+ if (*s1 == 0)
+ break;
+ }
+ return 0;
+}
--
2.1.4

peter0...@gmail.com

unread,
Apr 5, 2016, 10:36:24 AM4/5/16
to Jailhouse, jan.k...@web.de
Hello Jan,

does "diff = *s1 - *s2;" in strncmp should be "diff = *s1++ - *s2++"?

otherwise it would only compare first character?

best regards
peter

Jan Kiszka於 2016年1月10日星期日 UTC+8下午4時58分01秒寫道:

Jan Kiszka

unread,
Apr 5, 2016, 10:41:24 AM4/5/16
to peter0...@gmail.com, Jailhouse
On 2016-04-05 07:36, peter0...@gmail.com wrote:
> Hello Jan,
>
> does "diff = *s1 - *s2;" in strncmp should be "diff = *s1++ - *s2++"?
>
> otherwise it would only compare first character?

Indeed. Please send a patch.

Thanks,
Jan

peter0...@gmail.com

unread,
Apr 5, 2016, 2:01:13 PM4/5/16
to Jailhouse, peter0...@gmail.com, jan.k...@web.de
inmates: lib: fix strncmp

Signed-off-by: ShengYen Peng <peter0...@gmail.com>
---
inmates/lib/string.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/inmates/lib/string.c b/inmates/lib/string.c
index a6a3874..ea55c5b 100644
--- a/inmates/lib/string.c
+++ b/inmates/lib/string.c
@@ -41,6 +41,8 @@ int strncmp(const char *s1, const char *s2, unsigned long n)
return diff;
if (*s1 == 0)
break;
+ s1++;
+ s2++;
}
return 0;
}



Jan Kiszka於 2016年4月5日星期二 UTC+8下午10時41分24秒寫道:

Jan Kiszka

unread,
Apr 7, 2016, 12:11:42 PM4/7/16
to peter0...@gmail.com, Jailhouse
On 2016-04-05 11:01, peter0...@gmail.com wrote:
> inmates: lib: fix strncmp
>
> Signed-off-by: ShengYen Peng <peter0...@gmail.com>
> ---
> inmates/lib/string.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/inmates/lib/string.c b/inmates/lib/string.c
> index a6a3874..ea55c5b 100644
> --- a/inmates/lib/string.c
> +++ b/inmates/lib/string.c
> @@ -41,6 +41,8 @@ int strncmp(const char *s1, const char *s2, unsigned long n)
> return diff;
> if (*s1 == 0)
> break;
> + s1++;
> + s2++;
> }
> return 0;
> }

The patch is whitespace-damaged, probably due to your email client. I'll
fix this up when merging. Please take care next time, though. And please
also write at least one sentence in a commit log. Here I will add

"We only tested the first character of the strings so far. Make sure to
advance the pointers correctly."

Thanks!
Jan

Reply all
Reply to author
Forward
0 new messages