Browse Source

Merge pull request #136 from vcatechnology/msvc-fixes

MSVC Compilation Fixes
bel2125 10 years ago
parent
commit
c21748054f
7 changed files with 102 additions and 51 deletions
  1. 25 22
      .gitattributes
  2. 9 1
      .gitignore
  3. 2 0
      include/civetweb.h
  4. 44 10
      src/civetweb.c
  5. 14 10
      src/main.c
  6. 1 1
      src/md5.inl
  7. 7 7
      test/windows.cgi.cmd

+ 25 - 22
.gitattributes

@@ -1,22 +1,25 @@
-# Auto detect text files and perform LF normalization
-* -text
-
-# Custom for Visual Studio
-*.cs     diff=csharp
-*.sln    merge=union
-*.csproj merge=union
-*.vbproj merge=union
-*.fsproj merge=union
-*.dbproj merge=union
-
-# Standard to msysgit
-*.doc	 diff=astextplain
-*.DOC	 diff=astextplain
-*.docx diff=astextplain
-*.DOCX diff=astextplain
-*.dot  diff=astextplain
-*.DOT  diff=astextplain
-*.pdf  diff=astextplain
-*.PDF	 diff=astextplain
-*.rtf	 diff=astextplain
-*.RTF	 diff=astextplain
+# Auto detect text files and perform LF normalization
+* -text
+
+# Custom for Visual Studio
+*.cs     diff=csharp
+*.sln    merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
+
+# Standard to msysgit
+*.doc    diff=astextplain
+*.DOC    diff=astextplain
+*.docx   diff=astextplain
+*.DOCX   diff=astextplain
+*.dot    diff=astextplain
+*.DOT    diff=astextplain
+*.pdf    diff=astextplain
+*.PDF    diff=astextplain
+*.rtf    diff=astextplain
+*.RTF    diff=astextplain
+
+# Preserver Windows specfic lines endings
+*.cmd text eol=crlf

+ 9 - 1
.gitignore

@@ -9,11 +9,19 @@ out
 *.msi
 *.exe
 *.zip
-Output
+[oO]utput
+[tT]esting
 
 *.o
 
 #################
+## CMake
+#################
+/CMakeCache.txt
+/CMakeFiles
+/mingw
+
+#################
 ## Eclipse
 #################
 

+ 2 - 0
include/civetweb.h

@@ -34,6 +34,8 @@
 #else
 #define CIVETWEB_API
 #endif
+#elif __GNUC__ >= 4
+#define CIVETWEB_API __attribute__((visibility ("default")))
 #else
 #define CIVETWEB_API
 #endif

+ 44 - 10
src/civetweb.c

@@ -59,6 +59,14 @@
 #pragma warning(disable : 4127)
 /* non-constant aggregate initializer: issued due to missing C99 support */
 #pragma warning(disable : 4204)
+/* padding added after data member */
+#pragma warning (disable : 4820)
+/* not defined as a preprocessor macro, replacing with '0' for '#if/#elif' */
+#pragma warning (disable : 4668)
+/* no function prototype given: converting '()' to '(void)' */
+#pragma warning (disable : 4255)
+/* function has been selected for automatic inline expansion */
+#pragma warning (disable : 4711)
 #endif
 
 /* This code uses static_assert to check some conditions.
@@ -1151,7 +1159,7 @@ static void mg_set_thread_name(const char *name)
 		THREADNAME_INFO info;
 		info.dwType = 0x1000;
 		info.szName = threadName;
-		info.dwThreadID = -1;
+		info.dwThreadID = ~0U;
 		info.dwFlags = 0;
 
 		RaiseException(0x406D1388,
@@ -2524,7 +2532,7 @@ static int poll(struct pollfd *pfd, unsigned int n, int milliseconds)
 }
 #endif /* HAVE_POLL */
 
-static void set_close_on_exec(int sock,
+static void set_close_on_exec(SOCKET sock,
                               struct mg_connection *conn /* may be null */)
 {
 	(void)conn; /* Unused. */
@@ -2756,7 +2764,7 @@ mg_stat(struct mg_connection *conn, const char *path, struct file *filep)
 	return filep->membuf != NULL || filep->modification_time != (time_t)0;
 }
 
-static void set_close_on_exec(int fd,
+static void set_close_on_exec(SOCKET fd,
                               struct mg_connection *conn /* may be null */)
 {
 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
@@ -2933,7 +2941,12 @@ push(FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len)
 			if (ferror(fp))
 				n = -1;
 		} else {
-			n = (int)send(sock, buf + sent, (size_t)k, MSG_NOSIGNAL);
+#ifdef _WIN32
+			typedef int len_t;
+#else
+			typedef size_t len_t;
+#endif
+			n = (int)send(sock, buf + sent, (len_t)k, MSG_NOSIGNAL);
 		}
 
 		if (n <= 0)
@@ -2975,7 +2988,12 @@ static int pull(FILE *fp, struct mg_connection *conn, char *buf, int len)
 			nread = SSL_read(conn->ssl, buf, len);
 #endif
 		} else {
-			nread = (int)recv(conn->client.sock, buf, (size_t)len, 0);
+#ifdef _WIN32
+			typedef int len_t;
+#else
+			typedef size_t len_t;
+#endif
+			nread = (int)recv(conn->client.sock, buf, (len_t)len, 0);
 		}
 		if (conn->ctx->stop_flag) {
 			return -1;
@@ -4584,8 +4602,16 @@ static SOCKET conn2(struct mg_context *ctx /* may be null */,
 		snprintf(ebuf, ebuf_len, "%s", "NULL host");
 	} else if (use_ssl && SSLv23_client_method == NULL) {
 		snprintf(ebuf, ebuf_len, "%s", "SSL is not initialized");
-		/* TODO(lsm): use something threadsafe instead of gethostbyname() */
+#ifdef _MSC_VER
+#pragma warning(push)
+/* TODO(lsm): use something threadsafe instead of gethostbyname() */
+/* getaddrinfo is the replacement here but isn't cross platform */
+#pragma warning(disable: 4996)
+#endif
 	} else if ((he = gethostbyname(host)) == NULL) {
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
 		snprintf(
 		    ebuf, ebuf_len, "gethostbyname(%s): %s", host, strerror(ERRNO));
 	} else if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
@@ -5695,10 +5721,10 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog)
 	}
 
 	/* Make sure child closes all pipe descriptors. It must dup them to 0,1 */
-	set_close_on_exec(fdin[0], conn);
-	set_close_on_exec(fdin[1], conn);
-	set_close_on_exec(fdout[0], conn);
-	set_close_on_exec(fdout[1], conn);
+	set_close_on_exec((SOCKET)fdin[0], conn);
+	set_close_on_exec((SOCKET)fdin[1], conn);
+	set_close_on_exec((SOCKET)fdout[0], conn);
+	set_close_on_exec((SOCKET)fdout[1], conn);
 
 	/* Parent closes only one side of the pipes.
 	 * If we don't mark them as closed, close() attempt before
@@ -9644,7 +9670,15 @@ static void get_system_name(char **sysName)
 	DWORD dwMinorVersion = 0;
 	DWORD dwBuild = 0;
 
+#ifdef _MSC_VER
+#pragma warning(push)
+// GetVersion was declared deprecated
+#pragma warning(disable: 4996)
+#endif
 	dwVersion = GetVersion();
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
 
 	dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
 	dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));

+ 14 - 10
src/main.c

@@ -1172,8 +1172,11 @@ static int get_password(const char *user,
 
 	unsigned char mem[4096], *p;
 	DLGTEMPLATE *dia = (DLGTEMPLATE *)mem;
-	int ok, y;
-	struct tstring_input_buf dlgprms = {passwd_len, passwd};
+	int ok;
+	short y;
+	struct tstring_input_buf dlgprms;
+	dlgprms.buffer = passwd;
+	dlgprms.buflen = passwd_len;
 
 	static struct {
 		DLGTEMPLATE template; /* 18 bytes */
@@ -1611,7 +1614,7 @@ static void change_password_file()
 	char strbuf[256], u[256], d[256];
 	HWND hDlg = NULL;
 	FILE *f;
-	int y, nelems;
+	short y, nelems;
 	unsigned char mem[4096], *p;
 	DLGTEMPLATE *dia = (DLGTEMPLATE *)mem;
 	const char *domain = mg_get_option(g_ctx, "authentication_domain");
@@ -1732,7 +1735,7 @@ static void change_password_file()
 		}
 		fclose(f);
 
-		y = (WORD)((nelems + 1) * HEIGHT + 10);
+		y = (nelems + 1) * HEIGHT + 10;
 		add_control(&p,
 		            dia,
 		            0x80,
@@ -1766,7 +1769,7 @@ static void change_password_file()
 		            12,
 		            domain);
 
-		y = (WORD)((nelems + 2) * HEIGHT + 10);
+		y = (nelems + 2) * HEIGHT + 10;
 		add_control(&p,
 		            dia,
 		            0x80,
@@ -1809,7 +1812,8 @@ static int manage_service(int action)
 	static const char *service_name =
 	    "Civetweb"; /* TODO: check using server_name instead of service_name */
 	SC_HANDLE hSCM = NULL, hService = NULL;
-	SERVICE_DESCRIPTION descr = {g_server_name};
+	SERVICE_DESCRIPTION descr;
+	descr.lpDescription = g_server_name;
 	char path[PATH_MAX + 20] = ""; /* Path to executable plus magic argument */
 	int success = 1;
 
@@ -1866,7 +1870,9 @@ WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
 	static SERVICE_TABLE_ENTRY service_table[2];
 	int service_installed;
-	char buf[200], *service_argv[] = {__argv[0], NULL};
+	char buf[200], *service_argv[2];
+	service_argv[0] = __argv[0];
+	service_argv[1] = NULL;
 	POINT pt;
 	HMENU hMenu;
 	static UINT s_uTaskbarRestart; /* for taskbar creation */
@@ -2062,9 +2068,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show)
 	return (int)msg.wParam;
 }
 
-#if defined(CONSOLE)
-void main(void) { WinMain(0, 0, 0, 0); }
-#endif
+int main(void) { return WinMain(0, 0, 0, 0); }
 
 #elif defined(USE_COCOA)
 #import <Cocoa/Cocoa.h>

+ 1 - 1
src/md5.inl

@@ -416,7 +416,7 @@ md5_append(md5_state_t *pms, const md5_byte_t *data, size_t nbytes)
 		return;
 
 	/* Update the message length. */
-	pms->count[1] += nbytes >> 29;
+	pms->count[1] += (md5_word_t)(nbytes >> 29);
 	pms->count[0] += nbits;
 	if (pms->count[0] < nbits)
 		pms->count[1]++;

+ 7 - 7
test/windows.cgi.cmd

@@ -1,7 +1,7 @@
-@echo off
-echo HTTP/1.1 200 OK
-echo Connection: close
-echo.
-echo CGI test:
-echo.
-set
+@echo off
+echo HTTP/1.1 200 OK
+echo Connection: close
+echo.
+echo CGI test:
+echo.
+set