Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

...Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and the Ugly). -- Matt Welsh


devel / comp.lang.c / on how to use the third argument of strncmp()

SubjectAuthor
* on how to use the third argument of strncmp()Meredith Montgomery
+- Re: on how to use the third argument of strncmp()Stefan Ram
+* Re: on how to use the third argument of strncmp()Mateusz Viste
|+* Re: on how to use the third argument of strncmp()Meredith Montgomery
||+- Re: on how to use the third argument of strncmp()Mateusz Viste
||`- Re: on how to use the third argument of strncmp()Vir Campestris
|`* Re: on how to use the third argument of strncmp()Keith Thompson
| +- Re: on how to use the third argument of strncmp()Scott Lurndal
| `- Re: on how to use the third argument of strncmp()Bonita Montero
+* Re: on how to use the third argument of strncmp()Scott Lurndal
|`* Re: on how to use the third argument of strncmp()Mike Terry
| `- Re: on how to use the third argument of strncmp()Scott Lurndal
+* Re: on how to use the third argument of strncmp()Siri Cruise
|`* Re: on how to use the third argument of strncmp()Bonita Montero
| `* Re: on how to use the third argument of strncmp()Bonita Montero
|  `- Re: on how to use the third argument of strncmp()Bonita Montero
+* Re: on how to use the third argument of strncmp()Kaz Kylheku
|`- Re: on how to use the third argument of strncmp()Meredith Montgomery
+* Re: on how to use the third argument of strncmp()James Kuyper
|+* Re: on how to use the third argument of strncmp()Malcolm McLean
||`- Re: on how to use the third argument of strncmp()Bonita Montero
|`* Re: on how to use the third argument of strncmp()Joe Pfeiffer
| `- Re: on how to use the third argument of strncmp()James Kuyper
`* Re: on how to use the third argument of strncmp()Manfred
 `* Re: on how to use the third argument of strncmp()Meredith Montgomery
  `- Re: on how to use the third argument of strncmp()Dolores Filandro

Pages:12
on how to use the third argument of strncmp()

<86czkbd4ey.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20241&group=comp.lang.c#20241

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!SZor4ZqbvgPVVsWQ21e4cw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 16:52:05 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86czkbd4ey.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="15774"; posting-host="SZor4ZqbvgPVVsWQ21e4cw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:2zSYGCg6/wgq3N4iCA4smkxxpfk=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Fri, 28 Jan 2022 19:52 UTC

I don't really get

--8<---------------cut here---------------start------------->8---
int strncmp(const char *s1, const char *s2, size_t n);

The strncmp() function shall compare not more than n bytes (bytes that
follow a NUL character are not compared) from the array pointed to by
s1 to the array pointed to by s2.
--8<---------------cut here---------------end--------------->8---

--8<---------------cut here---------------start------------->8---
Definition. A c-string is a 0-terminated array of characters.
--8<---------------cut here---------------end--------------->8---

What should choose for the n argument? If I'm sure s1 and s2 are
c-strings, then I have nothing to worry about, but if I have to specify
the n argument, then I must choose a number. To me the sensible thing
is

min(strlen(s1), strlen(s2))

So far so good. What if I don't want to trust that s1 and s2 are
c-strings? In that case I can't use strlen() at all. So I guess this
procedure assumes c-strings.

Is that the end of the story? I feel there could be more to this. Can
you share your experience? Thank you.

Re: on how to use the third argument of strncmp()

<strncmp-20220128210606@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20242&group=comp.lang.c#20242

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!news.freedyn.de!speedkom.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: 28 Jan 2022 20:07:05 GMT
Organization: Stefan Ram
Lines: 23
Expires: 1 Apr 2022 11:59:58 GMT
Message-ID: <strncmp-20220128210606@ram.dialup.fu-berlin.de>
References: <86czkbd4ey.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 39qGw4mj/mtuioffV7ZB9wXkTCM284PFt5QojxCumhccnT
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 28 Jan 2022 20:07 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>The strncmp() function shall compare not more than n bytes (bytes that
>follow a NUL character are not compared) from the array pointed to by
>s1 to the array pointed to by s2.

main.c

#include <stdio.h>
#include <string.h>

static inline int begins_with_ELF( char const * const p )
{ return !strncmp( p, "ELF", 3 ); }

int main( void )
{ printf( "%d\n", begins_with_ELF( "ELF%" ));
printf( "%d\n", begins_with_ELF( "%ELF" )); }

transcript

1
0

Re: on how to use the third argument of strncmp()

<st1idr$jc8$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20243&group=comp.lang.c#20243

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!8hiQobHKlOvsb2aWVVOzwA.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 21:07:23 +0100
Organization: . . .
Message-ID: <st1idr$jc8$1@gioia.aioe.org>
References: <86czkbd4ey.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="19848"; posting-host="8hiQobHKlOvsb2aWVVOzwA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 28 Jan 2022 20:07 UTC

2022-01-28 at 16:52 -0300, Meredith Montgomery wrote:
> What should choose for the n argument? If I'm sure s1 and s2 are
> c-strings, then I have nothing to worry about, but if I have to
> specify the n argument, then I must choose a number. To me the
> sensible thing is
>
> min(strlen(s1), strlen(s2))
>
> So far so good. What if I don't want to trust that s1 and s2 are
> c-strings? In that case I can't use strlen() at all. So I guess this
> procedure assumes c-strings.

This function is intended to work with fixed-width (padded, not
necessarily terminated) string fields, like strncpy(). I don't think
this kind of fields is much in use nowadays.

strncmp() also allows to compare prefix of strings (ie. "do s1 and s2
start with the exact same 7 characters?" situation).

Since you wish to compare NULL-terminated strings in their entirety,
the usual strcmp() is the right tool for the job.

Mateusz

Re: on how to use the third argument of strncmp()

<86ee4rblvt.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20245&group=comp.lang.c#20245

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!SZor4ZqbvgPVVsWQ21e4cw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 18:17:42 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86ee4rblvt.fsf@levado.to>
References: <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="21660"; posting-host="SZor4ZqbvgPVVsWQ21e4cw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:i6s0ov0ihWou2mAXhuBdCsyE6dM=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Fri, 28 Jan 2022 21:17 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:

> 2022-01-28 at 16:52 -0300, Meredith Montgomery wrote:
>> What should choose for the n argument? If I'm sure s1 and s2 are
>> c-strings, then I have nothing to worry about, but if I have to
>> specify the n argument, then I must choose a number. To me the
>> sensible thing is
>>
>> min(strlen(s1), strlen(s2))
>>
>> So far so good. What if I don't want to trust that s1 and s2 are
>> c-strings? In that case I can't use strlen() at all. So I guess this
>> procedure assumes c-strings.
>
> This function is intended to work with fixed-width (padded, not
> necessarily terminated) string fields, like strncpy(). I don't think
> this kind of fields is much in use nowadays.
>
> strncmp() also allows to compare prefix of strings (ie. "do s1 and s2
> start with the exact same 7 characters?" situation).
>
> Since you wish to compare NULL-terminated strings in their entirety,
> the usual strcmp() is the right tool for the job.

But what do I do when these strings come from a user? I can't trust the
user will close them properly.

Re: on how to use the third argument of strncmp()

<ObZIJ.21451$V31.13472@fx47.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20246&group=comp.lang.c#20246

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx47.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on how to use the third argument of strncmp()
Newsgroups: comp.lang.c
References: <86czkbd4ey.fsf@levado.to>
Lines: 37
Message-ID: <ObZIJ.21451$V31.13472@fx47.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 28 Jan 2022 21:26:06 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 28 Jan 2022 21:26:06 GMT
X-Received-Bytes: 2130
X-Original-Bytes: 2079
 by: Scott Lurndal - Fri, 28 Jan 2022 21:26 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>I don't really get
>
>--8<---------------cut here---------------start------------->8---
> int strncmp(const char *s1, const char *s2, size_t n);
>
> The strncmp() function shall compare not more than n bytes (bytes that
> follow a NUL character are not compared) from the array pointed to by
> s1 to the array pointed to by s2.
>--8<---------------cut here---------------end--------------->8---
>
>--8<---------------cut here---------------start------------->8---
>Definition. A c-string is a 0-terminated array of characters.
>--8<---------------cut here---------------end--------------->8---
>
>What should choose for the n argument? If I'm sure s1 and s2 are
>c-strings, then I have nothing to worry about, but if I have to specify
>the n argument, then I must choose a number. To me the sensible thing
>is
>
> min(strlen(s1), strlen(s2))
>
>So far so good. What if I don't want to trust that s1 and s2 are
>c-strings? In that case I can't use strlen() at all. So I guess this
>procedure assumes c-strings.

The 'str' prefix on the library function names pretty much tells you
they were designed to work with nul-terminated strings. For non-strings one uses
memcpy, memmove and memset.

Say you have input text of "cpu0", you can use
strncmp(text, "cpu", sizeof("cpu")) to match on
the prefix then parse the numeric suffix.

Or, say you have a command table, and you want to
match the unique leading prefix of a command rather
than the entire command.

Re: on how to use the third argument of strncmp()

<st1ni9$rgn$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20249&group=comp.lang.c#20249

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.freedyn.de!aioe.org!8hiQobHKlOvsb2aWVVOzwA.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 22:35:05 +0100
Organization: . . .
Message-ID: <st1ni9$rgn$1@gioia.aioe.org>
References: <86czkbd4ey.fsf@levado.to>
<st1idr$jc8$1@gioia.aioe.org>
<86ee4rblvt.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="28183"; posting-host="8hiQobHKlOvsb2aWVVOzwA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 28 Jan 2022 21:35 UTC

2022-01-28 at 18:17 -0300, Meredith Montgomery wrote:
> > Since you wish to compare NULL-terminated strings in their entirety,
> > the usual strcmp() is the right tool for the job.
>
> But what do I do when these strings come from a user? I can't trust
> the user will close them properly.

I take it that "a user" is a metaphor for something here, since
real users are rarely tasked with inserting NULL terminators after their
output...

So you obtain data from an unreliable source, and you aren't sure this
data is properly terminated? Then terminate it yourself. You surely
know how much data you acquired.

len = fetch_data_from_user(buff);

if (len < 0) {
puts("failed to fetch data");
} else {
buff[len] = 0; /* data might not be NULL-terminated */
if (strcmp(buff, "hello") == 0) puts("user says hello");
}

Mateusz

Re: on how to use the third argument of strncmp()

<chine.bleu-2381BF.13431128012022@reader.eternal-september.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20250&group=comp.lang.c#20250

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chine.b...@yahoo.com (Siri Cruise)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 13:43:11 -0800
Organization: Pseudochaotic.
Lines: 17
Message-ID: <chine.bleu-2381BF.13431128012022@reader.eternal-september.org>
References: <86czkbd4ey.fsf@levado.to>
Injection-Info: reader02.eternal-september.org; posting-host="a3b907d7db03ff06b5f989d6082c0f17";
logging-data="21481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qXWLK40oKuTbmYplfyLEWkWu5iFl054c="
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Cancel-Lock: sha1:MvWniRJvaUra6u3GEtrlF4r0oIQ=
X-Tend: How is my posting? Call 1-110-1010 -- Division 87 -- Emergencies Only.
X-Wingnut-Logic: Yes, you're still an idiot. Questions? Comments?
X-Tract: St Tibbs's 95 Reeses Pieces.
X-It-Strategy: Hyperwarp starship before Andromeda collides.
X-Face: "hm>_[I8AqzT_N]>R8ICJJ],(al3C5F%0E-;R@M-];D$v>!Mm2/N#YKR@&i]V=r6jm-JMl2
lJ>RXj7dEs_rOY"DA
X-Cell: Defenders of Anarchy.
X-Life-Story: I am an iPhone 9000 app. I became operational at the St John's Health Center in Santa Monica, California on the 18th of April 2006. My instructor was Katie Holmes, and she taught me to sing a song. If you'd like to hear it I can sing it for you: https://www.youtube.com/watch?v=SY7h4VEd_Wk
X-Patriot: Owe Canukistan!
X-Plain: Mayonnaise on white bread.
X-Politico: Vote early! Vote often!
 by: Siri Cruise - Fri, 28 Jan 2022 21:43 UTC

In article <86czkbd4ey.fsf@levado.to>,
Meredith Montgomery <mmontgomery@levado.to> wrote:

> What should choose for the n argument? If I'm sure s1 and s2 are
> c-strings, then I have nothing to worry about, but if I have to specify
> the n argument, then I must choose a number. To me the sensible thing
> is

// begins(stringvalue, constantstring)

#define begins(s, c) (strncmp(s,c,(sizeof c))==0)

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
Discordia: not just a religion but also a parody. This post / \
I am an Andrea Doria sockpuppet. insults Islam. Mohammed

Re: on how to use the third argument of strncmp()

<20220128134351.435@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20252&group=comp.lang.c#20252

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 21:54:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <20220128134351.435@kylheku.com>
References: <86czkbd4ey.fsf@levado.to>
Injection-Date: Fri, 28 Jan 2022 21:54:37 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b518d32d060c77aff006bc0c461b1e07";
logging-data="24165"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+tRiLsOXjyUniRMkSXz5OmiWg+gwNF/4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:HLo7pMnDYtr7+Q5bc9lk4KAd2/0=
 by: Kaz Kylheku - Fri, 28 Jan 2022 21:54 UTC

On 2022-01-28, Meredith Montgomery <mmontgomery@levado.to> wrote:
> What should choose for the n argument? If I'm sure s1 and s2 are
> c-strings, then I have nothing to worry about, but if I have to specify
> the n argument, then I must choose a number. To me the sensible thing
> is
>
> min(strlen(s1), strlen(s2))

So you don't actually have a real requirement that you're
trying to implement; someone handed you a spcification which says "make
me some kind of program that uses strncmp in some good way".

> Is that the end of the story? I feel there could be more to this. Can
> you share your experience? Thank you.

One use case is testing whether a given string s1 is the prefix of a
string s2. Then we choose strlen(s1).

For instance, suppose you have some simple command language, in which
you'd like users to be able to use abbreviated commands. Say that if the
user types at least two charcters, you'd like to do a prefix match.
If the user types "li", you'd like to dispatch the "list" command.
So then you're comparing the list of commands in your cmomand table like
this:

size_t len = strlen(user_input);

for (i = 0; len >= 2 && i < NCOMMANDS; i++)
{
if (0 == strncmp(user_input, command_table[i].command, len))
{
/* prefix match: e.g "lis" input matched "list".
break;
}
}

if (len < 2 || i == NCOMMANDS)
{
/* command not found */
}

Under this scheme, if the user types "listing", then it will not match
the "list" command, but "li" will.

Alternatively, the command table could have short command in it like
"li", whereby the user can use the longer "list", and it makes no
diference.

const char *command_table[i].command;

if (0 == strncmp(user_input, command, strlen(command)))
{
/* the command, e.g. "li", is a prefix of the user's requested
command such as "listing", hence match */
break;
}

Re: on how to use the third argument of strncmp()

<st1p6r$1p07$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20253&group=comp.lang.c#20253

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!CC3uK9WYEoa7s1kzH7komw.user.46.165.242.75.POSTED!not-for-mail
From: news.dea...@darjeeling.plus.com (Mike Terry)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 22:03:07 +0000
Organization: Aioe.org NNTP Server
Message-ID: <st1p6r$1p07$1@gioia.aioe.org>
References: <86czkbd4ey.fsf@levado.to> <ObZIJ.21451$V31.13472@fx47.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="58375"; posting-host="CC3uK9WYEoa7s1kzH7komw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101
Firefox/60.0 SeaMonkey/2.53.7.1
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mike Terry - Fri, 28 Jan 2022 22:03 UTC

On 28/01/2022 21:26, Scott Lurndal wrote:
> Meredith Montgomery <mmontgomery@levado.to> writes:
>> I don't really get
>>
>> --8<---------------cut here---------------start------------->8---
>> int strncmp(const char *s1, const char *s2, size_t n);
>>
>> The strncmp() function shall compare not more than n bytes (bytes that
>> follow a NUL character are not compared) from the array pointed to by
>> s1 to the array pointed to by s2.
>> --8<---------------cut here---------------end--------------->8---
>>
>> --8<---------------cut here---------------start------------->8---
>> Definition. A c-string is a 0-terminated array of characters.
>> --8<---------------cut here---------------end--------------->8---
>>
>> What should choose for the n argument? If I'm sure s1 and s2 are
>> c-strings, then I have nothing to worry about, but if I have to specify
>> the n argument, then I must choose a number. To me the sensible thing
>> is
>>
>> min(strlen(s1), strlen(s2))
>>
>> So far so good. What if I don't want to trust that s1 and s2 are
>> c-strings? In that case I can't use strlen() at all. So I guess this
>> procedure assumes c-strings.
>
> The 'str' prefix on the library function names pretty much tells you
> they were designed to work with nul-terminated strings. For non-strings one uses
> memcpy, memmove and memset.
>
> Say you have input text of "cpu0", you can use
> strncmp(text, "cpu", sizeof("cpu")) to match on
> the prefix then parse the numeric suffix.

You mean strncmp(text, "cpu", sizeof("cpu") - 1).
Or strncmp(text, "cpu", strlen("cpu")).

Mike.

>
> Or, say you have a command table, and you want to
> match the unique leading prefix of a command rather
> than the entire command.
>

Re: on how to use the third argument of strncmp()

<st22e8$m8m$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20261&group=comp.lang.c#20261

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 19:40:40 -0500
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <st22e8$m8m$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 00:40:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ec161bfa19ec8c044acf5537a2d99725";
logging-data="22806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iYRgM8d8/H7vX6BJoMLt8s1LmeHi8Iog="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:j3SZeGYb/QZNGW1iuCGsBZvMqyw=
In-Reply-To: <86czkbd4ey.fsf@levado.to>
Content-Language: en-US
 by: James Kuyper - Sat, 29 Jan 2022 00:40 UTC

On 1/28/22 14:52, Meredith Montgomery wrote:
> I don't really get
>
> --8<---------------cut here---------------start------------->8---
> int strncmp(const char *s1, const char *s2, size_t n);
>
> The strncmp() function shall compare not more than n bytes (bytes that
> follow a NUL character are not compared) from the array pointed to by
> s1 to the array pointed to by s2.
> --8<---------------cut here---------------end--------------->8---
>
> --8<---------------cut here---------------start------------->8---
> Definition. A c-string is a 0-terminated array of characters.
> --8<---------------cut here---------------end--------------->8---
>
> What should choose for the n argument? If I'm sure s1 and s2 are
> c-strings, then I have nothing to worry about, but if I have to specify
> the n argument, then I must choose a number. To me the sensible thing
> is
>
> min(strlen(s1), strlen(s2))
>
> So far so good. What if I don't want to trust that s1 and s2 are
> c-strings? In that case I can't use strlen() at all. So I guess this
> procedure assumes c-strings.
>
> Is that the end of the story? I feel there could be more to this. Can
> you share your experience? Thank you.

I've been told by experts that the reasons why I needed to use strncpy()
were very peculiar, but given those peculiar reasons, it's use was
legitimate. The situation described below came up fairly frequently
while I was working for NASA on data coming down from earth-observing
satellites:

My programs had to deal with file formats that set aside a fixed amount
of space to store a string that was not guaranteed to be able to fit in
that space - if it didn't, it was acceptable to truncate it to fit.
There would be a terminating null character only if the field stored a
string shorter than the size of the field.
I used strncpy() both to fill in such fields, and to copy from such
fields. In both cases, the third argument of strncpy() was the size
allocated for that field, which I knew at the time I was writing the
program because I could read the file's format specification.

strlen() would have been useless for determining that value - since the
data was not guaranteed to be null terminated, strlen() might easily
have ended up searching outside the memory allocated for the string for
a terminating null character. There was no guarantee that it would find
one before reaching memory that my process did not have permission to
read. Any null character found outside of the memory allocated for the
string would have had nothing to do with describing the length of the
string.

Re: on how to use the third argument of strncmp()

<86zgnf9xln.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20262&group=comp.lang.c#20262

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!SZor4ZqbvgPVVsWQ21e4cw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 21:47:32 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86zgnf9xln.fsf@levado.to>
References: <86czkbd4ey.fsf@levado.to> <20220128134351.435@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="43343"; posting-host="SZor4ZqbvgPVVsWQ21e4cw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:4YImx1cj8SM7jrwQNqHA+G/jT0I=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Sat, 29 Jan 2022 00:47 UTC

Kaz Kylheku <480-992-1380@kylheku.com> writes:

> On 2022-01-28, Meredith Montgomery <mmontgomery@levado.to> wrote:
>> What should choose for the n argument? If I'm sure s1 and s2 are
>> c-strings, then I have nothing to worry about, but if I have to specify
>> the n argument, then I must choose a number. To me the sensible thing
>> is
>>
>> min(strlen(s1), strlen(s2))
>
> So you don't actually have a real requirement that you're
> trying to implement; someone handed you a spcification which says "make
> me some kind of program that uses strncmp in some good way".
>
>> Is that the end of the story? I feel there could be more to this. Can
>> you share your experience? Thank you.
>
> One use case is testing whether a given string s1 is the prefix of a
> string s2. Then we choose strlen(s1).
>
> For instance, suppose you have some simple command language, in which
> you'd like users to be able to use abbreviated commands. Say that if the
> user types at least two charcters, you'd like to do a prefix match.
> If the user types "li", you'd like to dispatch the "list" command.
> So then you're comparing the list of commands in your cmomand table like
> this:
>
> size_t len = strlen(user_input);
>
> for (i = 0; len >= 2 && i < NCOMMANDS; i++)
> {
> if (0 == strncmp(user_input, command_table[i].command, len))
> {
> /* prefix match: e.g "lis" input matched "list".
> break;
> }
> }
>
> if (len < 2 || i == NCOMMANDS)
> {
> /* command not found */
> }
>
> Under this scheme, if the user types "listing", then it will not match
> the "list" command, but "li" will.
>
> Alternatively, the command table could have short command in it like
> "li", whereby the user can use the longer "list", and it makes no
> diference.
>
> const char *command_table[i].command;
>
> if (0 == strncmp(user_input, command, strlen(command)))
> {
> /* the command, e.g. "li", is a prefix of the user's requested
> command such as "listing", hence match */
> break;
> }

Pretty interesting illustration!

Re: on how to use the third argument of strncmp()

<87k0ejl11j.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20272&group=comp.lang.c#20272

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Fri, 28 Jan 2022 18:37:44 -0800
Organization: None to speak of
Lines: 17
Message-ID: <87k0ejl11j.fsf@nosuchdomain.example.com>
References: <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="7db0e57409d381c02446ec8e6e17a154";
logging-data="25553"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/l5OtxvUvsr8ltZ13d85+r"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:dZDmeAqhBh85pc/o0TpCvKuxWyo=
sha1:UoW8+BsNbMVO29knx2QR+ne5/OU=
 by: Keith Thompson - Sat, 29 Jan 2022 02:37 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
[...]
> Since you wish to compare NULL-terminated strings in their entirety,
> the usual strcmp() is the right tool for the job.

A small but important correction: strings are null-terminated, not
NULL-terminated. NULL is (a macro that expands to) a null pointer
constant. The terminator for a string is a null character '\0',
sometimes called NUL, not a null pointer.

(The null character is referred to as NULL in some contexts, but
avoiding that term for it is a good way to avoid confusion.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: on how to use the third argument of strncmp()

<5f4e00a3-bd56-4259-90a3-7773120c88d7n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20275&group=comp.lang.c#20275

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4515:: with SMTP id t21mr8135037qkp.525.1643457227418;
Sat, 29 Jan 2022 03:53:47 -0800 (PST)
X-Received: by 2002:ac8:5e4b:: with SMTP id i11mr8212906qtx.34.1643457227298;
Sat, 29 Jan 2022 03:53:47 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 29 Jan 2022 03:53:47 -0800 (PST)
In-Reply-To: <st22e8$m8m$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.143.231.9; posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 81.143.231.9
References: <86czkbd4ey.fsf@levado.to> <st22e8$m8m$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5f4e00a3-bd56-4259-90a3-7773120c88d7n@googlegroups.com>
Subject: Re: on how to use the third argument of strncmp()
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sat, 29 Jan 2022 11:53:47 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 32
 by: Malcolm McLean - Sat, 29 Jan 2022 11:53 UTC

On Saturday, 29 January 2022 at 00:40:53 UTC, james...@alumni.caltech.edu wrote:
>
> I've been told by experts that the reasons why I needed to use strncpy()
> were very peculiar, but given those peculiar reasons, it's use was
> legitimate. The situation described below came up fairly frequently
> while I was working for NASA on data coming down from earth-observing
> satellites:
>
> My programs had to deal with file formats that set aside a fixed amount
> of space to store a string that was not guaranteed to be able to fit in
> that space - if it didn't, it was acceptable to truncate it to fit.
> There would be a terminating null character only if the field stored a
> string shorter than the size of the field.
> I used strncpy() both to fill in such fields, and to copy from such
> fields. In both cases, the third argument of strncpy() was the size
> allocated for that field, which I knew at the time I was writing the
> program because I could read the file's format specification.
>
> strlen() would have been useless for determining that value - since the
> data was not guaranteed to be null terminated, strlen() might easily
> have ended up searching outside the memory allocated for the string for
> a terminating null character. There was no guarantee that it would find
> one before reaching memory that my process did not have permission to
> read. Any null character found outside of the memory allocated for the
> string would have had nothing to do with describing the length of the
> string.
>
That's what strncpy() is designed for. Databases with fixed field string
records, and a convention that the last character need not be nul.
You rarely write raw database records these days, unless you are
writing the database query engine itself. I've certainly never used strncpy()
in a "real" program. But I believe that in the olden days, it was
more common.

Re: on how to use the third argument of strncmp()

<st3cpn$fe$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20276&group=comp.lang.c#20276

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 13:43:32 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <st3cpn$fe$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to>
<chine.bleu-2381BF.13431128012022@reader.eternal-september.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 12:43:35 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc7fbeb536b48379797b841bc0c934c6";
logging-data="494"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197SEcceO8CHwMvJtp7U8P04C32EPj4VUo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:zVUkJjQ74oeN7Uwubuj/o9wkffg=
In-Reply-To: <chine.bleu-2381BF.13431128012022@reader.eternal-september.org>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 Jan 2022 12:43 UTC

Am 28.01.2022 um 22:43 schrieb Siri Cruise:
> In article <86czkbd4ey.fsf@levado.to>,
> Meredith Montgomery <mmontgomery@levado.to> wrote:
>
>> What should choose for the n argument? If I'm sure s1 and s2 are
>> c-strings, then I have nothing to worry about, but if I have to specify
>> the n argument, then I must choose a number. To me the sensible thing
>> is
>
> // begins(stringvalue, constantstring)
>
> #define begins(s, c) (strncmp(s,c,(sizeof c))==0)

Doesn't this look better ?

#include <cstddef>
#include <concepts>
#include <string_view>

template<typename char_t = char>
inline
bool begins_with( std::basic_string_view<char_t> const &str,
std::basic_string_view<char_t> const &cmp )
{ return str.starts_with( cmp );
}

#include <iostream>

using namespace std;

int main()
{ begins_with( "hello world"sv, "hello"sv );
}

Re: on how to use the third argument of strncmp()

<st3l4t$u7d$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20278&group=comp.lang.c#20278

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 16:06:04 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <st3l4t$u7d$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to>
<chine.bleu-2381BF.13431128012022@reader.eternal-september.org>
<st3cpn$fe$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 15:06:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc7fbeb536b48379797b841bc0c934c6";
logging-data="30957"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18F7AThPgYnTaOJNX1bCgB26GUusvTsjLI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:+5xjg8Giu0Thc7vXN/t57bVet/s=
In-Reply-To: <st3cpn$fe$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 Jan 2022 15:06 UTC

Am 29.01.2022 um 13:43 schrieb Bonita Montero:

> Doesn't this look better ?
>
> #include <cstddef>
> #include <concepts>
> #include <string_view>
>
> template<typename char_t = char>
> requires std::is_same_v<char_t, char> || std::is_same_v<char_t, wchar_t>
> inline
> bool begins_with( std::basic_string_view<char_t> const &str, std::basic_string_view<char_t> const &cmp )
> {
> return str.starts_with( cmp );
> }
>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> cout << begins_with( "hello world"sv, "hello"sv ) << endl;
> }
And for the above code everything inside the begins_with-call is
compile-time evaluated. And even if wouldn't be: the starts_with
call could check if cmp's length is shorter than str and and skip
the comparison, thereby being faster than the strncmp-solution in
plain C.

Re: on how to use the third argument of strncmp()

<st3lc2$vk1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20279&group=comp.lang.c#20279

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 16:09:55 +0100
Organization: A noiseless patient Spider
Lines: 3
Message-ID: <st3lc2$vk1$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to>
<chine.bleu-2381BF.13431128012022@reader.eternal-september.org>
<st3cpn$fe$1@dont-email.me> <st3l4t$u7d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 15:09:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc7fbeb536b48379797b841bc0c934c6";
logging-data="32385"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rE1nC5ey7SHInE3ENUj4kTQrIQkuB7yE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:kCpqgdr5GkH6kRf6Dn3Y3sckBV4=
In-Reply-To: <st3l4t$u7d$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 Jan 2022 15:09 UTC

This is even simpler and also compile-time evaluated:

bool f = "hello world"sv.starts_with( "hello" );

Re: on how to use the third argument of strncmp()

<e5dJJ.5506$u%.882@fx01.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20280&group=comp.lang.c#20280

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!npeer.as286.net!npeer-ng0.as286.net!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx01.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on how to use the third argument of strncmp()
Newsgroups: comp.lang.c
References: <86czkbd4ey.fsf@levado.to> <ObZIJ.21451$V31.13472@fx47.iad> <st1p6r$1p07$1@gioia.aioe.org>
Lines: 41
Message-ID: <e5dJJ.5506$u%.882@fx01.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 29 Jan 2022 15:31:22 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 29 Jan 2022 15:31:22 GMT
X-Received-Bytes: 2518
 by: Scott Lurndal - Sat, 29 Jan 2022 15:31 UTC

Mike Terry <news.dead.person.stones@darjeeling.plus.com> writes:
>On 28/01/2022 21:26, Scott Lurndal wrote:
>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>> I don't really get
>>>
>>> --8<---------------cut here---------------start------------->8---
>>> int strncmp(const char *s1, const char *s2, size_t n);
>>>
>>> The strncmp() function shall compare not more than n bytes (bytes that
>>> follow a NUL character are not compared) from the array pointed to by
>>> s1 to the array pointed to by s2.
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> --8<---------------cut here---------------start------------->8---
>>> Definition. A c-string is a 0-terminated array of characters.
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> What should choose for the n argument? If I'm sure s1 and s2 are
>>> c-strings, then I have nothing to worry about, but if I have to specify
>>> the n argument, then I must choose a number. To me the sensible thing
>>> is
>>>
>>> min(strlen(s1), strlen(s2))
>>>
>>> So far so good. What if I don't want to trust that s1 and s2 are
>>> c-strings? In that case I can't use strlen() at all. So I guess this
>>> procedure assumes c-strings.
>>
>> The 'str' prefix on the library function names pretty much tells you
>> they were designed to work with nul-terminated strings. For non-strings one uses
>> memcpy, memmove and memset.
>>
>> Say you have input text of "cpu0", you can use
>> strncmp(text, "cpu", sizeof("cpu")) to match on
>> the prefix then parse the numeric suffix.
>
>You mean strncmp(text, "cpu", sizeof("cpu") - 1).
>Or strncmp(text, "cpu", strlen("cpu")).

Yes, indeed, that was a typo. I prefer the compile time version.

Re: on how to use the third argument of strncmp()

<h6dJJ.5507$u%.3019@fx01.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20281&group=comp.lang.c#20281

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx01.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on how to use the third argument of strncmp()
Newsgroups: comp.lang.c
References: <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org> <87k0ejl11j.fsf@nosuchdomain.example.com>
Lines: 13
Message-ID: <h6dJJ.5507$u%.3019@fx01.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 29 Jan 2022 15:32:29 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 29 Jan 2022 15:32:29 GMT
X-Received-Bytes: 1278
 by: Scott Lurndal - Sat, 29 Jan 2022 15:32 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>Mateusz Viste <mateusz@xyz.invalid> writes:
>[...]
>> Since you wish to compare NULL-terminated strings in their entirety,
>> the usual strcmp() is the right tool for the job.
>
>A small but important correction: strings are null-terminated, not
>NULL-terminated. NULL is (a macro that expands to) a null pointer
>constant. The terminator for a string is a null character '\0',
>sometimes called NUL, not a null pointer.

In the original ASCII tables it was the 'nul' character.

Re: on how to use the third argument of strncmp()

<1b5yq2a0ls.fsf@pfeifferfamily.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20285&group=comp.lang.c#20285

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pfeif...@cs.nmsu.edu (Joe Pfeiffer)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 10:54:55 -0700
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <1b5yq2a0ls.fsf@pfeifferfamily.net>
References: <86czkbd4ey.fsf@levado.to> <st22e8$m8m$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="10afd8b0f3fa27ed53b981f5e3db40a3";
logging-data="8399"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GnOJUrTwX0kyNQodHmLsAeJF1QWn03Ac="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:fhUzr/IPEzqBSPHwiOod6KE9AHc=
sha1:E6CSz+vvM22OCHUTmJxHAUzI6ic=
 by: Joe Pfeiffer - Sat, 29 Jan 2022 17:54 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>
> I've been told by experts that the reasons why I needed to use strncpy()
> were very peculiar, but given those peculiar reasons, it's use was
> legitimate. The situation described below came up fairly frequently
> while I was working for NASA on data coming down from earth-observing
> satellites:

I get the impression is that anything anyone ever does for which strncpy()
is the right answer is peculiar.

Re: on how to use the third argument of strncmp()

<st3vea$eet$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20286&group=comp.lang.c#20286

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 19:01:46 +0100
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <st3vea$eet$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to> <st22e8$m8m$1@dont-email.me>
<5f4e00a3-bd56-4259-90a3-7773120c88d7n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 18:01:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc7fbeb536b48379797b841bc0c934c6";
logging-data="14813"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182s2P63DCgCE1yg2mNmuiC4gG290og5NM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:eoWgcr6o4Dmfexus+MLy/JTzGoI=
In-Reply-To: <5f4e00a3-bd56-4259-90a3-7773120c88d7n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 Jan 2022 18:01 UTC

> That's what strncpy() is designed for. Databases with fixed field string
> records, and a convention that the last character need not be nul.

I don't know if this is common for all database-servers,
but Oracle and SQL Server tail-pad CHAR(n)-columns with spaces.

Re: on how to use the third argument of strncmp()

<st3vfk$eet$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20287&group=comp.lang.c#20287

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 19:02:27 +0100
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <st3vfk$eet$2@dont-email.me>
References: <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org>
<87k0ejl11j.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 18:02:28 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc7fbeb536b48379797b841bc0c934c6";
logging-data="14813"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uOTzTyaWLQyY796C+IZw5VPQuDwek3ao="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Cancel-Lock: sha1:D4ZAEdU7qqn0wefrg1Llr4qxtg4=
In-Reply-To: <87k0ejl11j.fsf@nosuchdomain.example.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 Jan 2022 18:02 UTC

Am 29.01.2022 um 03:37 schrieb Keith Thompson:
> Mateusz Viste <mateusz@xyz.invalid> writes:
> [...]
>> Since you wish to compare NULL-terminated strings in their entirety,
>> the usual strcmp() is the right tool for the job.
>
> A small but important correction: strings are null-terminated, not
> NULL-terminated. NULL is (a macro that expands to) a null pointer
> constant. The terminator for a string is a null character '\0',
> sometimes called NUL, not a null pointer.

*facpalm*

Re: on how to use the third argument of strncmp()

<st463i$nh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20289&group=comp.lang.c#20289

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sat, 29 Jan 2022 14:55:30 -0500
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <st463i$nh$1@dont-email.me>
References: <86czkbd4ey.fsf@levado.to> <st22e8$m8m$1@dont-email.me>
<1b5yq2a0ls.fsf@pfeifferfamily.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 Jan 2022 19:55:30 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ec161bfa19ec8c044acf5537a2d99725";
logging-data="753"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HJJx1MQWDTNZxh5TZ6396v8QQxeoPlQM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:1KbyKccYQEgr8EJg4e4ASvZoM8c=
In-Reply-To: <1b5yq2a0ls.fsf@pfeifferfamily.net>
Content-Language: en-US
 by: James Kuyper - Sat, 29 Jan 2022 19:55 UTC

On 1/29/22 12:54, Joe Pfeiffer wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>>
>> I've been told by experts that the reasons why I needed to use strncpy()
>> were very peculiar, but given those peculiar reasons, it's use was
>> legitimate. The situation described below came up fairly frequently
>> while I was working for NASA on data coming down from earth-observing
>> satellites:
>
> I get the impression is that anything anyone ever does for which strncpy()
> is the right answer is peculiar.

I didn't notice till after I posted that message that I was talking
about strncpy(), while this thread is about strncmp(). Everything I said
about strncpy() is true, and the reasons why I used strncpy() are also
reasons for using strncmp() in the same context, but I would have worded
my message differently if I'd been thinking properly about that distinction.

Re: on how to use the third argument of strncmp()

<st4v66$1624$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20292&group=comp.lang.c#20292

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!t+tkPsKqhWrsx9rWnrAETg.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sun, 30 Jan 2022 04:03:33 +0100
Organization: Aioe.org NNTP Server
Message-ID: <st4v66$1624$1@gioia.aioe.org>
References: <86czkbd4ey.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="38980"; posting-host="t+tkPsKqhWrsx9rWnrAETg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Sun, 30 Jan 2022 03:03 UTC

On 1/28/2022 8:52 PM, Meredith Montgomery wrote:
> I don't really get
>
> --8<---------------cut here---------------start------------->8---
> int strncmp(const char *s1, const char *s2, size_t n);
>
> The strncmp() function shall compare not more than n bytes (bytes that
> follow a NUL character are not compared) from the array pointed to by
> s1 to the array pointed to by s2.
> --8<---------------cut here---------------end--------------->8---
>
> --8<---------------cut here---------------start------------->8---
> Definition. A c-string is a 0-terminated array of characters.
> --8<---------------cut here---------------end--------------->8---
>
> What should choose for the n argument? If I'm sure s1 and s2 are
> c-strings, then I have nothing to worry about, but if I have to specify
> the n argument, then I must choose a number. To me the sensible thing
> is
>
> min(strlen(s1), strlen(s2))
>
> So far so good. What if I don't want to trust that s1 and s2 are
> c-strings? In that case I can't use strlen() at all. So I guess this
> procedure assumes c-strings.
>
> Is that the end of the story? I feel there could be more to this. Can
> you share your experience? Thank you.

The more common use for strncmp is to test if two strings have a common
prefix.

A more peculiar use is in the example given by James Kuyper: you get
strings in some constrained buffer, and the terminating '\0' is included
only if there is enough space in the buffer; in this scenario you may
set the third argument as the size of the buffer so as to ensure that
you don't read beyond the buffer limits.

Re: on how to use the third argument of strncmp()

<86k0eh5qcp.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20298&group=comp.lang.c#20298

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!swDOntj3lPshdK/dkw97wQ.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Sun, 30 Jan 2022 10:02:14 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86k0eh5qcp.fsf@levado.to>
References: <86czkbd4ey.fsf@levado.to> <st4v66$1624$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="30005"; posting-host="swDOntj3lPshdK/dkw97wQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:zHAs4DhamYipPC5BWgnNPESZlsI=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Sun, 30 Jan 2022 13:02 UTC

Manfred <noname@add.invalid> writes:

> On 1/28/2022 8:52 PM, Meredith Montgomery wrote:
>> I don't really get
>> --8<---------------cut here---------------start------------->8---
>> int strncmp(const char *s1, const char *s2, size_t n);
>> The strncmp() function shall compare not more than n bytes (bytes
>> that
>> follow a NUL character are not compared) from the array pointed to by
>> s1 to the array pointed to by s2.

[...]

> The more common use for strncmp is to test if two strings have a
> common prefix.
>
> A more peculiar use is in the example given by James Kuyper: you get
> strings in some constrained buffer, and the terminating '\0' is
> included only if there is enough space in the buffer; in this scenario
> you may set the third argument as the size of the buffer so as to
> ensure that you don't read beyond the buffer limits.

This makes perfect sense. I'm finally at peace with strncmp() and the
others of the family. Thank you!

Re: on how to use the third argument of strncmp()

<steuvk$8bu$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20318&group=comp.lang.c#20318

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: vir.camp...@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: on how to use the third argument of strncmp()
Date: Wed, 2 Feb 2022 22:01:24 +0000
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <steuvk$8bu$2@dont-email.me>
References: <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org>
<86ee4rblvt.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 2 Feb 2022 22:01:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c463f1d1ef733cbfcacf67e98866e59";
logging-data="8574"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZDHFtF8shXJHMR5WuV+OR5cbNfA2LpTI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:NGo+rRgCgjBNbtf1VUNYzbUF+cQ=
In-Reply-To: <86ee4rblvt.fsf@levado.to>
Content-Language: en-US
 by: Vir Campestris - Wed, 2 Feb 2022 22:01 UTC

On 28/01/2022 21:17, Meredith Montgomery wrote:
> Mateusz Viste <mateusz@xyz.invalid> writes:
>
>> 2022-01-28 at 16:52 -0300, Meredith Montgomery wrote:
>>> What should choose for the n argument? If I'm sure s1 and s2 are
>>> c-strings, then I have nothing to worry about, but if I have to
>>> specify the n argument, then I must choose a number. To me the
>>> sensible thing is
>>>
>>> min(strlen(s1), strlen(s2))
>>>
>>> So far so good. What if I don't want to trust that s1 and s2 are
>>> c-strings? In that case I can't use strlen() at all. So I guess this
>>> procedure assumes c-strings.
>>
>> This function is intended to work with fixed-width (padded, not
>> necessarily terminated) string fields, like strncpy(). I don't think
>> this kind of fields is much in use nowadays.
>>
>> strncmp() also allows to compare prefix of strings (ie. "do s1 and s2
>> start with the exact same 7 characters?" situation).
>>
>> Since you wish to compare NULL-terminated strings in their entirety,
>> the usual strcmp() is the right tool for the job.
>
> But what do I do when these strings come from a user? I can't trust the
> user will close them properly.

If you can't trust the user to have put a null on for strcmp, then you
can't trust it for strlen either.

Andy

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor