⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 Serveez是一个服务器框架
💻 C
📖 第 1 页 / 共 2 页
字号:
static char *svz_neterror (int error){  static char message[MESSAGE_BUF_SIZE];  switch (error)    {    case WSAEACCES:      return "Permission denied.";    case WSAEADDRINUSE:      return "Address already in use.";    case WSAEADDRNOTAVAIL:      return "Cannot assign requested address.";    case WSAEAFNOSUPPORT:      return "Address family not supported by protocol family.";    case WSAEALREADY:      return "Operation already in progress.";    case WSAECONNABORTED:      return "Software caused connection abort.";    case WSAECONNREFUSED:      return "Connection refused.";    case WSAECONNRESET:      return "Connection reset by peer.";    case WSAEDESTADDRREQ:      return "Destination address required.";    case WSAEFAULT:      return "Bad address.";    case WSAEHOSTDOWN:      return "Host is down.";    case WSAEHOSTUNREACH:      return "No route to host.";    case WSAEINPROGRESS:      return "Operation now in progress.";    case WSAEINTR:      return "Interrupted function call.";    case WSAEINVAL:      return "Invalid argument.";    case WSAEISCONN:      return "Socket is already connected.";    case WSAEMFILE:      return "Too many open files.";    case WSAEMSGSIZE:      return "Message too long.";    case WSAENETDOWN:      return "Network is down.";    case WSAENETRESET:      return "Network dropped connection on reset.";    case WSAENETUNREACH:      return "Network is unreachable.";    case WSAENOBUFS:      return "No buffer space available.";    case WSAENOPROTOOPT:      return "Bad protocol option.";    case WSAENOTCONN:      return "Socket is not connected.";    case WSAENOTSOCK:      return "Socket operation on non-socket.";    case WSAEOPNOTSUPP:      return "Operation not supported.";    case WSAEPFNOSUPPORT:      return "Protocol family not supported.";    case WSAEPROCLIM:      return "Too many processes.";    case WSAEPROTONOSUPPORT:      return "Protocol not supported.";    case WSAEPROTOTYPE:      return "Protocol wrong type for socket.";    case WSAESHUTDOWN:      return "Cannot send after socket shutdown.";    case WSAESOCKTNOSUPPORT:      return "Socket type not supported.";    case WSAETIMEDOUT:      return "Connection timed out.";    case WSAEWOULDBLOCK:      return "Resource temporarily unavailable.";    case WSAHOST_NOT_FOUND:      return "Host not found.";    case WSANOTINITIALISED:      return "Successful WSAStartup not yet performed.";    case WSANO_DATA:      return "Valid name, no data record of requested type.";    case WSANO_RECOVERY:      return "This is a non-recoverable error.";    case WSASYSNOTREADY:      return "Network subsystem is unavailable.";    case WSATRY_AGAIN:      return "Non-authoritative host not found.";    case WSAVERNOTSUPPORTED:      return "WINSOCK.DLL version out of range.";    case WSAEDISCON:      return "Graceful shutdown in progress.";    default:      sprintf (message, "Network error code %d.", error);      break;    }  return message;}/* * Routine which forms a valid error message under Win32. It might either * use the @code{GetLastError()} or @code{WSAGetLastError()} in order to  * get a valid error code. */char *svz_syserror (int nr){  static char message[MESSAGE_BUF_SIZE];  /* save the last error */  svz_errno = nr;  /* return a net error if necessary */  if (nr >= WSABASEERR)    return svz_neterror (nr);  /*    * if the error is not valid (GetLastError returned zero)   * fall back to the errno variable of the usual crtdll.   */  if (!nr)    nr = errno;  /* return a sys error */  if (0 == FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |			  FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, nr,			  MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),			  (char *) message, MESSAGE_BUF_SIZE, NULL))    {      sprintf (message, "FormatMessage (%d): error code %ld", 	       nr, GetLastError ());      return message;    }  message[strlen (message) - 2] = 0;  return message;}/* * This variable contains the the runtime detected Win32 version. Its value * is setup in @code{svz_version()} and can be @code{Win32s} for Windows 3.x, * @code{Win95} for Windows 95, @code{Win98} for Windows 98, @code{WinNT3x} * for Windows NT 3.x, @code{WinNT4x} for Windows NT 4.x, @code{Win2k} for * Windows 2000, @code{WinXP} for Windows XP and @code{WinME} for Windows ME. */int svz_os_version = 0;#endif /* __MINGW32__ *//* * This routine is for detecting the operating system version of Win32  * and all Unices at runtime. You should call it at least once at startup. * It saves its result in the variable @code{svz_os_version} and prints an * appropriate message. */char *svz_sys_version (void){  static char os[256] = ""; /* contains the os string */#ifdef __MINGW32__  static char ver[][6] =    { " 32s", " 95", " 98", " NT", " NT", " 2000", " XP", " ME" };  OSVERSIONINFO osver;#elif HAVE_SYS_UTSNAME_H  struct utsname buf;#endif  /* detect only once */  if (os[0])    return os;#ifdef __MINGW32__ /* Windows */  osver.dwOSVersionInfoSize = sizeof (osver);  if (!GetVersionEx (&osver))    {      svz_log (LOG_ERROR, "GetVersionEx: %s\n", SYS_ERROR);      sprintf (os, "unknown Windows");    }  else    {      switch (osver.dwPlatformId)	{	case VER_PLATFORM_WIN32_NT: /* NT, Windows 2000 or Windows XP */	  if (osver.dwMajorVersion == 4)	    svz_os_version = WinNT4x;	  else if (osver.dwMajorVersion <= 3)	    svz_os_version = WinNT3x;	  else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion < 1)	    svz_os_version = Win2k;	  else if (osver.dwMajorVersion >= 5)	    svz_os_version = WinXP;	  break;	case VER_PLATFORM_WIN32_WINDOWS: /* Win95 or Win98 */	  if ((osver.dwMajorVersion > 4) ||	      ((osver.dwMajorVersion == 4) && (osver.dwMinorVersion > 0)))	    {	      if (osver.dwMinorVersion >= 90)		svz_os_version = WinME;	      else		svz_os_version = Win98;	    }	  else	    svz_os_version = Win95;	  break;	case VER_PLATFORM_WIN32s: /* Windows 3.x */	  svz_os_version = Win32s;	  break;	}      sprintf (os, "Windows%s %ld.%02ld %s%s(Build %ld)",	       ver[svz_os_version], 	       osver.dwMajorVersion, osver.dwMinorVersion,	       osver.szCSDVersion, osver.szCSDVersion[0] ? " " : "",	       osver.dwBuildNumber & 0xFFFF);    }#elif HAVE_UNAME /* !__MINGW32__ */  uname (&buf);  sprintf (os, "%s %s on %s", buf.sysname, buf.release, buf.machine);#endif /* not HAVE_UNAME */  return os;}/* * Converts an unsigned integer to its decimal string representation  * returning a pointer to an internal buffer, so copy the result. */char *svz_itoa (unsigned int i){  static char buffer[32];  char *p = buffer + sizeof (buffer) - 1;  *p = '\0';  do    {      p--;      *p = (char) ((i % 10) + '0');    }  while ((i /= 10) != 0);  return p;}/* * Converts a given string @var{str} in decimal format to an unsigned integer. * Stops conversion on any invalid characters. */unsigned intsvz_atoi (char *str){  unsigned int i = 0;  while (*str >= '0' && *str <= '9')    {      i *= 10;      i += *str - '0';      str++;    }  return i;}/* * Returns the current working directory. The returned pointer needs to be * @code{svz_free()}'ed after usage. */char *svz_getcwd (void){  char *buf, *dir;  int len = 64;  buf = dir = NULL;  do    {      buf = svz_realloc (buf, len);      dir = getcwd (buf, len);      len *= 2;    }  while (dir == NULL);  return dir;}/* * This routine checks for the current and maximum limit of open files * of the current process. The function heavily depends on the underlying * platform. It tries to set the limit to the given @var{max_sockets}  * amount. */intsvz_openfiles (int max_sockets){#if HAVE_GETRLIMIT  struct rlimit rlim;#endif#if HAVE_GETDTABLESIZE  int openfiles;  if ((openfiles = getdtablesize ()) == -1)    {      svz_log (LOG_ERROR, "getdtablesize: %s\n", SYS_ERROR);      return -1;    }  svz_log (LOG_NOTICE, "file descriptor table size: %d\n", openfiles);#endif /* HAVE_GETDTABLESIZE */#if HAVE_GETRLIMIT# ifndef RLIMIT_NOFILE#  define RLIMIT_NOFILE RLIMIT_OFILE# endif  if (getrlimit (RLIMIT_NOFILE, &rlim) == -1)    {      svz_log (LOG_ERROR, "getrlimit: %s\n", SYS_ERROR);      return -1;    }  svz_log (LOG_NOTICE, "current open file limit: %d/%d\n",	   rlim.rlim_cur, rlim.rlim_max);  if ((int) rlim.rlim_max < (int) max_sockets ||       (int) rlim.rlim_cur < (int) max_sockets)    {      rlim.rlim_max = max_sockets;      rlim.rlim_cur = max_sockets;      if (setrlimit (RLIMIT_NOFILE, &rlim) == -1)	{	  svz_log (LOG_ERROR, "setrlimit: %s\n", SYS_ERROR);	  return -1;	}      getrlimit (RLIMIT_NOFILE, &rlim);      svz_log (LOG_NOTICE, "open file limit set to: %d/%d\n",	       rlim.rlim_cur, rlim.rlim_max);    }#elif defined (__MINGW32__)	/* HAVE_GETRLIMIT */  unsigned sockets = 100;  if (svz_os_version == Win95 ||       svz_os_version == Win98 || svz_os_version == WinME)    {      if (svz_os_version == Win95)	sockets = svz_windoze_get_reg_unsigned (MaxSocketKey,						MaxSocketSubKey,						MaxSocketSubSubKey, sockets);      else	sockets = svz_atoi (svz_windoze_get_reg_string (MaxSocketKey,							MaxSocketSubKey,							MaxSocketSubSubKey,							svz_itoa (sockets)));      svz_log (LOG_NOTICE, "current open file limit: %u\n", sockets);      if (sockets < (unsigned) max_sockets)	{	  sockets = max_sockets;	  if (svz_os_version == Win95)	    svz_windoze_set_reg_unsigned (MaxSocketKey,					  MaxSocketSubKey,					  MaxSocketSubSubKey, sockets);	  else	    svz_windoze_set_reg_string (MaxSocketKey,					MaxSocketSubKey,					MaxSocketSubSubKey, 					svz_itoa (sockets));	  svz_log (LOG_NOTICE, "open file limit set to: %u\n", sockets);	}    }#endif /* MINGW32__ */  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -