#define prefix(str, find) (strncmp(str, find, strlen(find)) == 0)

enum {
	UNDEFINED,
	OTHER,
	Windows_Vista,
	Windows_8,
	Windows_7,
	Windows_XP,
	Windows_98,
	Windows_95,
	Windows_ME,
	Windows_CE,
	Windows_2000,
	Windows_2003,
	Windows,
	Unix,
	Mac_OS,
	Mac_OS_iPad,
	Mac_OS_iPhone,
	Chrome_OS,
	Android,
	SymbianOS,
	JavaME,
	MAX_OSES
};


short
getOS(char* userAgent)
{
	char*  temp;
	if (!(userAgent && *userAgent)) return UNDEFINED;
	if (!strcmp(userAgent, "-")) return UNDEFINED;

	if ((temp = strstr(userAgent, "Windows "))) {
		temp += strlen("Windows ");
		if (prefix(temp, "2000")) return Windows_2000;
		if (prefix(temp, "NT 5.0")) return Windows_2000;
		if (prefix(temp, "NT 5.1")) return Windows_XP;
		if (prefix(temp, "NT 5.2")) return Windows_2003;
		if (prefix(temp, "NT 6.2")) return Windows_8;
		if (prefix(temp, "NT 6.1")) return Windows_7;
		if (prefix(temp, "NT 6.")) return Windows_Vista;
		if (strstr(temp, "Mobile")) return Windows_CE;
		if (strstr(temp, "Phone")) return Windows_CE;
		if (strstr(temp, "XP")) return Windows_XP;
		if (strstr(temp, "CE")) return Windows_CE;
		if (strstr(temp, "NT")) return Windows_NT;
		if (strstr(temp, "98")) return Windows_98;
		if (strstr(temp, "95")) return Windows_95;
		if (strstr(temp, "ME")) return Windows_ME;
		return Windows;
	}

	if ((temp = strstr(userAgent, "Win"))) {
		temp += strlen("Win");
		if (strstr(temp, "NT")) return Windows_NT;
		if (strstr(temp, "98")) return Windows_98;
		if (strstr(temp, "95")) return Windows_95;
		if (strstr(temp, "ME")) return Windows_ME;
		if (strstr(temp, "CE")) return Windows_CE;
		return Windows;
	}

	if (strstr(userAgent, "J2ME")) return JavaME;
	if (strstr(userAgent, "Android")) return Android;
	if (strstr(userAgent, "iPad")) return Mac_OS_iPad;
	if (strstr(userAgent, "iPhone")) return Mac_OS_iPhone;
	if (strstr(userAgent, "Symbian") ||
		strstr(userAgent, "SymbOS") ||
		strstr(userAgent, "Series 80") ||
		strstr(userAgent, "Series80") ||
		strstr(userAgent, "Series60") ||
		strstr(userAgent, "Series 60")) return SymbianOS;

	if (strstr(userAgent, "Macintosh") ||
		strstr(userAgent, "MacOS") ||
		strstr(userAgent, "Mac OS") ||
		strstr(userAgent, "Mac_")) return Mac_OS;

	if (strstr(userAgent, "CrOS")) return Chrome_OS;

	if (strstr(userAgent, "Linux")	 ||
		strstr(userAgent, "FreeBSD") ||
		strstr(userAgent, "Solaris") ||
		strstr(userAgent, "SunOS")	 ||
		strstr(userAgent, "AIX")	 ||
		strstr(userAgent, "BSD")	 ||
		strstr(userAgent, "HP-UX")	 ||
		strstr(userAgent, "IRIX")	 ||
		strstr(userAgent, "OSF")	 ||
		strstr(userAgent, "OpenVMS") ||
		strstr(userAgent, "QNX")	 ||
		strstr(userAgent, "SCO")	 ||
		strstr(userAgent, "Unix")	 ||
		strstr(userAgent, "X11")) return Unix;

	return OTHER;
}

