Solved

Windows and Unix compatibility

Posted on 2003-11-12
12
464 Views
Last Modified: 2010-04-15
Hi,

One of my application in unix , i have used isblank("") and strcasecmp() functions .
but in windows , they are not working . Anybody knows what are the equvalent functions
in windows.

Thanks in advance
0
Comment
Question by:expertyasin
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9729858
dont know windows equivalent but you can code them yourself

int isblank ( char * in )
{
        if ( *in == ' ' || *in == '\t' || *in == '\n' )
                   return 1;
        else
                   return 0;
}

likewise, in the implementation of if toupper/tolower are available on windows, you can convert both strings to the same case and then use strcmp()
0
 
LVL 1

Accepted Solution

by:
yahelb earned 20 total points
ID: 9729963
you've got:

_stricmp for strcasecmp
0
 

Author Comment

by:expertyasin
ID: 9730462
Hi guys,

Both answers are useful to me .

Hi yahelb ,

  Can I use this stricmp() function in unix also . Is it compatible with Unix or i need to identify the OS and based on the code those two functions ?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9730886
>Can I use this stricmp() function in unix also .
No. All the functions with name started with '_' are Microsoft specific.

There is no ANSI equivalent for stricmp as far as I know.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9732123

Hi expertyasin,

All if the isxxxxx() macros are defined in ctype.h.  Include that header file and isblank() will work just fine.


strcasecmp() appears to be a proprietary API.  If the purpose of the function is to compare two strings without regard to case ("STRing1" will match "string1") then use the strncmp() function.  strncmp() is portable and should be supported on all platforms.


Good Luck,
Kent
0
 
LVL 5

Expert Comment

by:g0rath
ID: 9732507
when writing portable code you'll have to some odd hacks such as:

#ifdef __linux__
#include <unistd.h>
#end if

#ifdef WIN32
#define strcasecmp stricmp
#endif

#ifdef OTHER_OS
#define strcasecmp my_strcasecmp
#include "my_strcasecmp.h"
#endif

so in your code you just write once

strcasecmp(string1, string2);
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9733800

stricmp() is the "case insensitive" string compare used by Micro$oft and Borland.  strncmp() is the equivalent in unix/linux.

I know that Borland supports both stricmp() and strncmp() but I don't know about MS.


Since your original question was unix related, change all occurrences of "strcasecmp" to "strncmp" and you'll be fine.

Or take the approach suggested by g0rath and define an equivalence:

#define strcasecmp strncmp


The second suggestion is the "quick and dirty" way to get past the error, but it works just fine.  You might as well get used to typing strncmp() so please seriously consider changing "strcasecmp" to "strncmp".


Kent
0
 
LVL 5

Expert Comment

by:g0rath
ID: 9734238
uh according to the man page....unix side

// Both case insenstive, but one you specify the first N bytes
int  strncasecmp(const  char  * s1,  const  char  * s2,  size_t  n);
int  strcasecmp(const  char  * s1,  const  char  * s2);

// Both Lexical, but one you specify the first N bytes
int  strncmp(const  char  * s1,  const  char  * s2,  size_t  n);
int  strcmp(const  char  * s1,  const  char  * s2);


strcasecmp != strncmp under unix
but windows changes things to be not quite as compatible

Windows....
MSVC++

Perform a lowercase comparison of strings
int _stricmp( const char *string1, const char *string2 );

Compare characters of two strings without regard to case.
int _strnicmp( const char *string1, const char *string2, size_t count );

Lexical comparison
int strncmp( const char *string1, const char *string2, size_t count );


strcasecmp(unix) != strncmp(msvc++) ... now that my be true about Borland...but just be aware when comparing
0
 
LVL 45

Expert Comment

by:Kdo
ID: 10392245

Man.... where was head?  I meant stricmp() and strcmpi()....

sigh....

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question