我在谷歌搜索结果中推断出strings.h(来自这里)适用于UNIX系统.我想用以下宏来检查主机的操作系统是否是Linux/UNIX.听到有关它的建议将非常感激.提前致谢.
#include <strings.h>
Run Code Online (Sandbox Code Playgroud)
strings.h仅包含少数几个函数,其中大多数只是标准库中的函数(例如bcmp()<-> memcmp())的不同名称。如果您的代码使用这些函数,那么#ifdef为什么不只写自己的集合而不是抛出s呢?
然后,每个人都可以使用它们,并且可以愉快地进行无条件编译。
这是公共领域中未经测试的一组功能,您需要自己承担风险:
#include <string.h>
#include <ctype.h>
int bcmp(const void * p1, const void * p2, size_t n)
{
return memcmp( p1, p2, n);
}
void bcopy(const void * src, void * dst, size_t n)
{
memmove( dst, src, n); /* note different order of args - yuck */
}
void bzero(void * p, size_t n)
{
memset( p, 0, n);
}
char *index(const char * s, int c)
{
return strchr( s, c);
}
char *rindex(const char * s, int c)
{
return strrchr( s, c);
}
int strcasecmp(const char* s1, const char* s2)
{
for (;;) {
int c1 = tolower( *((unsigned char*) s1++));
int c2 = tolower( *((unsigned char*) s2++));
if ((c1 != c2) || (c1 == '\0')) {
return( c1 - c2);
}
}
}
int strncasecmp(const char* s1, const char* s2, size_t n)
{
for (; n != 0; --n) {
int c1 = tolower( *((unsigned char*) s1++));
int c2 = tolower( *((unsigned char*) s2++));
if ((c1 != c2) || (c1 == '\0')) {
return( c1 - c2);
}
}
return( 0);
}
int ffs(int v)
{
unsigned int x = v;
int c = 1;
/*
* adapted from from
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
*
* a modified binary search algorithm to count 0 bits from
* the right (lsb). This algorithm should work regardless
* of the size of ints on the platform.
*
*/
/* a couple special cases */
if (x == 0) return 0;
if (x & 1) return 1; /* probably pretty common */
c = 1;
while ((x & 0xffff) == 0) {
x >>= 16;
c += 16;
}
if ((x & 0xff) == 0) {
x >>= 8;
c += 8;
}
if ((x & 0x0f) == 0) {
x >>= 4;
c += 4;
}
if ((x & 0x03) == 0) {
x >>= 2;
c += 2;
}
c -= (x & 1);
c += 1; /* ffs() requires indexing bits from 1 */
/* ie., the lsb is bit 1, not bit 0 */
return c;
}
Run Code Online (Sandbox Code Playgroud)
一种选择是:
#ifndef _WIN32
#include <strings.h>
#endif
Run Code Online (Sandbox Code Playgroud)
根据 MSDN,_WIN32“为 Win32 和 Win64 应用程序定义。始终定义”。
当然,除 Windows 之外的操作系统的编译器或标准库可以自由定义_WIN32,但这将是......最不寻常的。