473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Need to implement strdup, strnicmp and stricmp

I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.

Thanks
Jami

Jun 29 '06 #1
18 8111
"jamihuq" <ja*********@yahoo.com> wrote:
I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.


http://www.debian.org/
http://www.netbsd.org/
http://www.opensolaris.org/
http://www.freebsd.org/
http://www.gentoo.org/
http://www.openbsd.org/
Jun 29 '06 #2
jamihuq wrote:
I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.


glibc appears to implement strdup, although it shouldn't be taxing to
write your own. I'll guess from the names the other two are supposed
to be case independent comparisons. Looping through tolower() first
then doing a normal strcmp/strncmp would work at a guess (although
probably slower and needs a duplicating step to do non-destructively
compared to a locale aware comparison).

--
imalone
Jun 29 '06 #3
jamihuq wrote:

I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.


This is the current form of my case insensitive versions
of strcmp and strncmp:

int str_ccmp(const char *s1, const char *s2)
{
for (;;) {
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

if (c2 != c1) {
return c2 > c1 ? -1 : 1;
}
} else {
if (*s1 == '\0') {
return 0;
}
}
++s1;
++s2;
}
}

int str_cncmp(const char *s1, const char *s2, size_t n)
{
for (;;) {
if (n-- == 0) {
return 0;
}
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

if (c2 != c1) {
return c2 > c1 ? -1 : 1;
}
} else {
if (*s1 == '\0') {
return 0;
}
}
++s1;
++s2;
}
}

--
pete
Jun 29 '06 #4
pete wrote:

jamihuq wrote:

I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.


This is the current form of my case insensitive versions
of strcmp and strncmp:

[...]

I just ran into a similar situation. The Linux box doesn't have a
stricmp() function. Rather, it has an identical function called
strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jun 29 '06 #5
Kenneth Brody wrote:
The Linux box doesn't have a stricmp() function.
Rather, it has an identical function called strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?


No.

http://www.open-std.org/JTC1/SC22/WG14/www/docs/

n869 is very good for function descriptions
and comes in a text version.

Some people prefer to use n1124 these days.

--
pete
Jun 29 '06 #6
Kenneth Brody wrote:
pete wrote:

jamihuq wrote:

I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.


This is the current form of my case insensitive versions
of strcmp and strncmp:

[...]

I just ran into a similar situation. The Linux box doesn't have a
stricmp() function. Rather, it has an identical function called
strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?


No. strcasecmp is a POSIX extension and stricmp appears to be of
Microsoft origin. strcasecmp is more likely to be portable although,
as demonstrated elsethread, it is trivial to write your own version.

Robert Gamble

Jun 29 '06 #7
pete wrote:
Kenneth Brody wrote:
The Linux box doesn't have a stricmp() function.
Rather, it has an identical function called strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?


No.

http://www.open-std.org/JTC1/SC22/WG14/www/docs/

n869 is very good for function descriptions
and comes in a text version.

Some people prefer to use n1124 these days.


Aside from format preferences (txt versus pdf), is there any reason at
all to prefer n869 over n1124? n1124 contains, for free, the same
thing you would pay for by purchasing 9899:1999 plus TC1, TC2, and
other DR corrections all in one document. The reluctance some people
seem to have over recommending it over n869 puzzles me, is this due
solely to formatting reasons?

Robert Gamble

Jun 29 '06 #8
Robert Gamble wrote:
pete wrote:
Kenneth Brody wrote:
The Linux box doesn't have a stricmp() function.
Rather, it has an identical function called strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?

No.

http://www.open-std.org/JTC1/SC22/WG14/www/docs/

n869 is very good for function descriptions
and comes in a text version.

Some people prefer to use n1124 these days.


Aside from format preferences (txt versus pdf), is there any reason at
all to prefer n869 over n1124? n1124 contains, for free, the same
thing you would pay for by purchasing 9899:1999 plus TC1, TC2, and
other DR corrections all in one document. The reluctance some people
seem to have over recommending it over n869 puzzles me, is this due
solely to formatting reasons?


Probably the main reason is that most people will be using an
implementation that can be C89 compliant (possibly with the need of
appropriate options) but not C99 compliant.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jun 29 '06 #9
Flash Gordon wrote:
Robert Gamble wrote:
pete wrote:
Kenneth Brody wrote:

The Linux box doesn't have a stricmp() function.
Rather, it has an identical function called strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?
No.

http://www.open-std.org/JTC1/SC22/WG14/www/docs/

n869 is very good for function descriptions
and comes in a text version.

Some people prefer to use n1124 these days.


Aside from format preferences (txt versus pdf), is there any reason at
all to prefer n869 over n1124? n1124 contains, for free, the same
thing you would pay for by purchasing 9899:1999 plus TC1, TC2, and
other DR corrections all in one document. The reluctance some people
seem to have over recommending it over n869 puzzles me, is this due
solely to formatting reasons?


Probably the main reason is that most people will be using an
implementation that can be C89 compliant (possibly with the need of
appropriate options) but not C99 compliant.


n869 is the last public C99 draft...

Robert Gamble

Jun 29 '06 #10
In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
This is the current form of my case insensitive versions
of strcmp and strncmp: [snippage] int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);


I would convert both to lowercase myself. In German, the eszet
character (ß -- code point 0xdf -- in ISO Latin 1) is lowercase
but has no uppercase equivalent (it has to be written as SS). I
suspect most toupper()s will leave it alone, so the code will
"work right" anyway; and it is possible there are other languages
that have an uppercase character with no lowercase equivalent; but
the existence of this one example is enough for me to favor
conversion to lowercase.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 29 '06 #11


Chris Torek wrote On 06/29/06 17:52,:
In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
This is the current form of my case insensitive versions
of strcmp and strncmp:


[snippage]
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);



I would convert both to lowercase myself. In German, the eszet
character (ß -- code point 0xdf -- in ISO Latin 1) is lowercase
but has no uppercase equivalent (it has to be written as SS). I
suspect most toupper()s will leave it alone, so the code will
"work right" anyway; and it is possible there are other languages
that have an uppercase character with no lowercase equivalent; but
the existence of this one example is enough for me to favor
conversion to lowercase.


The snipped part of the code has already established
that *s1 != *s2. Things might be different if he were
doing a wholesale case conversion followed by an ordinary
strcmp(), but that's not the, er, case.

--
Er*********@sun.com

Jun 29 '06 #12
Robert Gamble wrote:

pete wrote:
Kenneth Brody wrote:
The Linux box doesn't have a stricmp() function.
Rather, it has an identical function called strcasecmp().

#define stricmp(str1,str2) strcasecmp(str1,str2)

There is probably an equivalent to strnicmp() as well.

Are either of these functions part of the C standard?


No.

http://www.open-std.org/JTC1/SC22/WG14/www/docs/

n869 is very good for function descriptions
and comes in a text version.

Some people prefer to use n1124 these days.


Aside from format preferences (txt versus pdf), is there any reason at
all to prefer n869 over n1124?


No.

I quote ISO/IEC 9899:1999 (E), when I have to,
like for example, when the topic is "negative zero".

--
pete
Jun 29 '06 #13
Chris Torek wrote:

In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
This is the current form of my case insensitive versions
of strcmp and strncmp:

[snippage]
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);


I would convert both to lowercase myself. In German, the eszet
character (ß -- code point 0xdf -- in ISO Latin 1) is lowercase
but has no uppercase equivalent (it has to be written as SS). I
suspect most toupper()s will leave it alone, so the code will
"work right" anyway; and it is possible there are other languages
that have an uppercase character with no lowercase equivalent; but
the existence of this one example is enough for me to favor
conversion to lowercase.


The standard says:

[#3] If the argument is a character for which islower is
true and there are one or more corresponding characters, as
specified by the current locale, for which isupper is true,
the toupper function returns one of the corresponding
characters (always the same one for any given locale);
otherwise, the argument is returned unchanged.

.... which means that toupper returns the eszet argument unchanged.

I don't understand what are the consequences
of the worst possible case, that you see.

--
pete
Jun 30 '06 #14
"jamihuq" <ja*********@yahoo.com> wrote:
# I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
# have an implementation in the OSs string.h function. Does someone have
# the implementation for these functions and can you please post them.

char *strdup(char *s) {
return strcpy(malloc(strlen(s)+1),s);
}

int stricmp(char *s,char *t) {
int cc;
do cc = tolower(*s++) - tolower(*t++); while (!cc && s[-1]);
return cc;
}

int strnicmp(char *s,char *t,int n) {
int cc;
if (n==0) return 0;
do cc = tolower(*s++) - tolower(*t++); while (!cc && s[-1] && --n>0);
return cc;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore.
Jun 30 '06 #15
SM Ryan said:
"jamihuq" <ja*********@yahoo.com> wrote:
# I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't
# have an implementation in the OSs string.h function. Does someone have
# the implementation for these functions and can you please post them.

char *strdup(char *s) {
Invasion of implementation namespace. Make s a const char * instead.
return strcpy(malloc(strlen(s)+1),s);
Undefined behaviour if malloc fails.
int stricmp(char *s,char *t) {
Invasion of implementation namespace.
int cc;
do cc = tolower(*s++) - tolower(*t++); while (!cc && s[-1]);
Possible underflow issue here if *s is CHAR_MIN and t is positive. Also, the
negative offset will upset some maintenance programmers. Finally, UB if *s
or *t is not representable as an unsigned char.

int cmpistr(const char *s, const char *t)
{
unsigned char c, d;
int diff = 0;
while(diff == 0 && *s != '\0' && *t != '\0')
{
c = *s++;
d = *t++;
diff = (c > d) - (c < d);
}

if(0 == diff)
{
diff = *s ? 1 : -1;
}

return diff;
}
int strnicmp(char *s,char *t,int n) {
int cc;
if (n==0) return 0;
do cc = tolower(*s++) - tolower(*t++); while (!cc && s[-1] && --n>0);
return cc;


Much the same comments as above apply here too.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 30 '06 #16
>Chris Torek wrote:
I would [use tolower()] myself [on concern for a lowercase character
that lacks an uppercase equivalent].

In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:The standard says:

[#3] If the argument is a character for which islower is
true and there are one or more corresponding characters, as
specified by the current locale, for which isupper is true,
the toupper function returns one of the corresponding
characters (always the same one for any given locale);
otherwise, the argument is returned unchanged.

... which means that toupper returns the eszet argument unchanged.


My C89 standard is not easily accessible, but I *think* this text
is new in C99 (or perhaps C95). C89's locale support was not very
well tacked-down, as I recall.

Of course, as Eric Sosman pointed out, your original code was
completely safe anyway.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 30 '06 #17
Chris Torek <no****@torek.net> writes:
Chris Torek wrote:
I would [use tolower()] myself [on concern for a lowercase character

that lacks an uppercase equivalent].

In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
The standard says:

[#3] If the argument is a character for which islower is
true and there are one or more corresponding characters, as
specified by the current locale, for which isupper is true,
the toupper function returns one of the corresponding
characters (always the same one for any given locale);
otherwise, the argument is returned unchanged.

... which means that toupper returns the eszet argument unchanged.


My C89 standard is not easily accessible, but I *think* this text
is new in C99 (or perhaps C95). C89's locale support was not very
well tacked-down, as I recall.


Here's what C90 says:

7.3.2.2 The toupper function

Synopsis
#include <ctype.h>
int toupper(int c);

Description
The toupper function converts a lowercase letter to the
corresponding uppercase letter.

Returns
If the argument is a character for which islower is true and
there is a corresponding character for which isupper is true,
the toupper function returns the corresponding character;
otherwise, the argument is returned unchanged.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 30 '06 #18
Chris Torek wrote:
Chris Torek wrote:
I would [use tolower()] myself [on concern for a lowercase character
that lacks an uppercase equivalent].

In article <44***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
The standard says:

[#3] If the argument is a character for which islower is
true and there are one or more corresponding characters, as
specified by the current locale, for which isupper is true,
the toupper function returns one of the corresponding
characters (always the same one for any given locale);
otherwise, the argument is returned unchanged.

... which means that toupper returns the eszet argument unchanged.


My C89 standard is not easily accessible, but I *think* this text
is new in C99 (or perhaps C95). C89's locale support was not very
well tacked-down, as I recall.


Well, it doesn't use the word "locale", here:

ISO/IEC 9899: 1990
7.3.2.2 The toupper function
Returns
If the argument is a character for which islower is true
and there is a corresponding character for which isupper is true,
the toupper function returns the corresponding character;
otherwise, the argument is returned unchanged.
Of course, as Eric Sosman pointed out, your original code was
completely safe anyway.


Thank you, to both of you.

--
pete
Jun 30 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Grumble | last post by:
As far as I can tell, strdup() is neither in C89 nor in C99. Is that correct? <OT>Is it in POSIX perhaps?</OT>
6
by: Ronchese | last post by:
Hi all. I need implement a interface in my new dll project. The problem is the interface class have so much methods, properties and events, and seems that i need implement that manually. ...
2
by: Jonathan | last post by:
Hi I need implement loosely coupled events to raise events in a Classibrary project from one Windows Application project, and hanlde these from other Windows Application project. I found this...
11
by: Manuel | last post by:
Hi, I need implement a map of member functions of some class. This map is formed by a string and a pointer to the member function. The problem is that the map need that the object saved are...
0
by: =?Utf-8?B?THVjYXM=?= | last post by:
Id like implement event columnchange in my bindinlist like Delphi It is possible? How can I do? I have two class Pedido and class Item<bindinglistI need implement columnchange in class item. ...
5
by: lovecreatesbea... | last post by:
I am trying the FREE net-snmp library things. Like the FREE libxml++ library, it lacks of a reasonable document on its API. Its very begining example code compiles and runs. It says "blah blah .."...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.