Solved

Windows and Unix compatibility

Posted on 2003-11-12
12
460 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
Comment Utility
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
Comment Utility
you've got:

_stricmp for strcasecmp
0
 

Author Comment

by:expertyasin
Comment Utility
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
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
>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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility

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

sigh....

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now