changeset 4:4b2dafa26fe2 NGINX_0_1_2

nginx 0.1.2 *) Feature: the --user=USER, --group=GROUP, and --with-ld-opt=OPTIONS options in configure. *) Feature: the server_name directive supports *.domain.tld. *) Bugfix: the portability improvements. *) Bugfix: if configuration file was set in command line, the reconfiguration was impossible; bug appeared in 0.1.1. *) Bugfix: proxy module may get caught in an endless loop when sendfile is not used. *) Bugfix: with sendfile the response was not recoded according to the charset module directives; bug appeared in 0.1.1. *) Bugfix: very seldom bug in the kqueue processing. *) Bugfix: the gzip module compressed the proxied responses that was already compressed.
author Igor Sysoev <http://sysoev.ru>
date Thu, 21 Oct 2004 00:00:00 +0400
parents 8beaf7b3241f
children 985c56ebe724
files CHANGES CHANGES.ru auto/cc auto/cc/bcc auto/cc/conf auto/cc/gcc auto/cc/icc auto/cc/msvc auto/cc/name auto/cc/owc auto/endianess auto/feature auto/fmt/fmt auto/fmt/ptrfmt auto/headers auto/inc auto/include auto/init auto/install auto/lib/md5/conf auto/lib/md5/make auto/lib/openssl/conf auto/lib/pcre/conf auto/lib/pcre/make auto/lib/test auto/lib/zlib/conf auto/lib/zlib/make auto/make auto/options auto/os/conf auto/os/freebsd auto/os/linux auto/os/solaris auto/os/win32 auto/sources auto/stubs auto/types/sizeof auto/types/typedef auto/types/uintptr_t auto/unix conf/nginx.conf configure src/core/nginx.c src/core/nginx.h src/core/ngx_buf.c src/core/ngx_buf.h src/core/ngx_conf_file.c src/core/ngx_config.h src/core/ngx_connection.h src/core/ngx_core.h src/core/ngx_cycle.c src/core/ngx_log.c src/core/ngx_log.h src/core/ngx_output_chain.c src/core/ngx_string.c src/core/ngx_string.h src/core/ngx_times.c src/core/ngx_times.h src/event/modules/ngx_epoll_module.c src/event/modules/ngx_kqueue_module.c src/event/modules/ngx_rtsig_module.c src/event/ngx_event.c src/event/ngx_event.h src/event/ngx_event_accept.c src/event/ngx_event_connect.c src/event/ngx_event_openssl.c src/http/modules/ngx_http_charset_filter.c src/http/modules/ngx_http_chunked_filter.c src/http/modules/ngx_http_gzip_filter.c src/http/modules/ngx_http_headers_filter.c src/http/modules/ngx_http_range_filter.c src/http/modules/ngx_http_userid_filter.c src/http/modules/proxy/ngx_http_proxy_handler.c src/http/modules/proxy/ngx_http_proxy_handler.h src/http/modules/proxy/ngx_http_proxy_header.c src/http/modules/proxy/ngx_http_proxy_upstream.c src/http/ngx_http.c src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h src/http/ngx_http_header_filter.c src/http/ngx_http_log_handler.c src/http/ngx_http_request.c src/http/ngx_http_request.h src/http/ngx_http_write_filter.c src/os/unix/ngx_files.c src/os/unix/ngx_files.h src/os/unix/ngx_freebsd_config.h src/os/unix/ngx_freebsd_sendfile_chain.c src/os/unix/ngx_linux_config.h src/os/unix/ngx_linux_sendfile_chain.c src/os/unix/ngx_os.h src/os/unix/ngx_posix_config.h src/os/unix/ngx_posix_init.c src/os/unix/ngx_process.c src/os/unix/ngx_process.h src/os/unix/ngx_process_cycle.c src/os/unix/ngx_readv_chain.c src/os/unix/ngx_recv.c src/os/unix/ngx_send.c src/os/unix/ngx_setproctitle.c src/os/unix/ngx_socket.c src/os/unix/ngx_socket.h src/os/unix/ngx_solaris_config.h src/os/unix/ngx_thread.h src/os/unix/ngx_time.h src/os/unix/ngx_writev_chain.c
diffstat 106 files changed, 2489 insertions(+), 1456 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,27 @@
+
+Changes with nginx 0.1.2                                         21 Oct 2004
+
+    *) Feature: the --user=USER, --group=GROUP, and --with-ld-opt=OPTIONS 
+       options in configure.
+
+    *) Feature: the server_name directive supports *.domain.tld.
+
+    *) Bugfix: the portability improvements.
+
+    *) Bugfix: if configuration file was set in command line, the 
+       reconfiguration was impossible, bug appeared in 0.1.1.
+
+    *) Bugfix: proxy module may get caught in an endless loop when sendfile 
+       is not used.
+
+    *) Bugfix: with sendfile the response was not recoded according to the 
+       charset module directives, bug appeared in 0.1.1.
+
+    *) Bugfix: very seldom bug in the kqueue processing.
+
+    *) Bugfix: the gzip module compressed the proxied responses that was 
+       already compressed.
+
 
 Changes with nginx 0.1.1                                         11 Oct 2004
 
@@ -12,7 +36,7 @@ Changes with nginx 0.1.1                
 
     *) Bugfix: the "Location" header rewrite bug fixed while the proxing.
 
-    *) Bugfix: the ngx_http_chunked_module module bug fixed that caused an 
+    *) Bugfix: the ngx_http_chunked_module module may get caught in an 
        endless loop.
 
     *) Bugfix: the /dev/poll module bugs fixed.
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,3 +1,28 @@
+
+Изменения в nginx 0.1.2                                           21.10.2004
+
+    *) Добавление: параметры --user=USER, --group=GROUP и 
+       --with-ld-opt=OPTIONS в configure.
+
+    *) Добавление: директива server_name поддерживает *.domain.tld.
+
+    *) Исправление: улучшена переносимость на неизвестные платформы.
+
+    *) Исправление: нельзя переконфигурировать nginx, если конфигурационный 
+       файл указан в командной строке, ошибка появилась в 0.1.1.
+
+    *) Исправление: модуль проксировании мог привести к зацикливанию, если не 
+       использовался sendfile.
+
+    *) Исправление: при использовании sendfile текст ответа не 
+       перекодировался согласно директивам модуля charset, ошибка появилась в 
+       0.1.1.
+
+    *) Исправление: очень редкая ошибка при обработке kqueue.
+
+    *) Исправление: модуль сжатия сжимал уже сжатые ответы, полученные при 
+       проксировании.
+
 
 Изменения в nginx 0.1.1                                           11.10.2004
 
deleted file mode 100644
--- a/auto/cc
+++ /dev/null
@@ -1,376 +0,0 @@
-
-# Copyright (C) Igor Sysoev
-
-
-CFLAGS="$CFLAGS $CC_OPT"
-
-case $CC in
-
-    *gcc*)
-         # gcc 2.7.2.3, 2.8.1, 2.95.4,
-         #     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4
-
-         # optimizations
-         #CFLAGS="$CFLAGS -O2 -fomit-frame-pointer"
-
-         case $CPU in
-             pentium)
-                 # optimize for Pentium and Athlon
-                 CPU_OPT="-march=pentium"
-             ;;
-
-             pentiumpro)
-                 # optimize for Pentium Pro, Pentium II and Pentium III
-                 CPU_OPT="-march=pentiumpro"
-             ;;
-
-             pentium4)
-                 # optimize for Pentium 4, gcc 3.x
-                 CPU_OPT="-march=pentium4"
-             ;;
-         esac
-
-         # STUB for batch builds
-         if [ $CC = gcc27 ]; then CPU_OPT=; fi
-
-         CFLAGS="$CFLAGS $PIPE $CPU_OPT"
-
-         if [ ".$PCRE_OPT" = "." ]; then
-             PCRE_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
-         else
-             PCRE_OPT="$PCRE_OPT $PIPE"
-         fi
-
-         if [ ".$MD5_OPT" = "." ]; then
-             MD5_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
-         else
-             MD5_OPT="$MD5_OPT $PIPE"
-         fi
-
-         if [ ".$ZLIB_OPT" = "." ]; then
-             ZLIB_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
-         else
-             ZLIB_OPT="$ZLIB_OPT $PIPE"
-         fi
-
-         # warnings
-         CFLAGS="$CFLAGS -O -W"
-         CFLAGS="$CFLAGS -Wall -Wpointer-arith"
-         #CFLAGS="$CFLAGS -Wconversion"
-         #CFLAGS="$CFLAGS -Winline"
-
-         # we have a lot of the unused function arguments
-         CFLAGS="$CFLAGS -Wno-unused"
-
-         # stop on warning
-         CFLAGS="$CFLAGS -Werror"
-
-         # ANSI C warnings
-         #CFLAGS="$CFLAGS -pedantic"
-
-         # debug
-         CFLAGS="$CFLAGS -g"
-
-         # DragonFly's gcc3 generates DWARF
-         #CFLAGS="$CFLAGS -g -gstabs"
-
-         have=HAVE_GCC_VARIADIC_MACROS . auto/have
-
-         if [ ".$CPP" = "." ]; then
-             CPP="$CC -E"
-         fi
-
-         LINK="\$(CC)"
-
-         INCOPT="-I "
-         COMPOPT="-c"
-         OBJOUT="-o "
-         BINOUT="-o "
-         OBJEXT="o"
-
-         CC_STRONG="$CC -Wall -Werror"
-    ;;
-
-
-    *icc)
-         # Intel C++ compiler 7.1, 8.0
-
-         # optimizations
-         CFLAGS="$CFLAGS -O"
-         # inline functions declared with __inline
-         #CFLAGS="$CFLAGS -Ob1"
-         # inline any function, at the compiler's discretion
-         CFLAGS="$CFLAGS -Ob2"
-
-         # single-file IP optimizations
-         #IPO="-ip"
-         # multi-file IP optimizations 
-         IPO="-ipo -ipo_obj"
-         CFLAGS="$CFLAGS $IPO"
-         CORE_LINK="$CORE_LINK $IPO"
-         CORE_LINK="$CORE_LINK -opt_report_file=$OBJS/opt_report_file"
-
-         case $CPU in
-             pentium)
-                 # optimize for Pentium and Athlon
-                 CPU_OPT="-march=pentium"
-             ;;
-
-             pentiumpro)
-                 # optimize for Pentium Pro, Pentium II and Pentium III
-                 CPU_OPT="-mcpu=pentiumpro -march=pentiumpro"
-             ;;
-
-             pentium4)
-                 # optimize for Pentium 4, default
-                 CPU_OPT="-march=pentium4"
-             ;;
-         esac
-
-         CFLAGS="$CFLAGS $CPU_OPT"
-
-         if [ ".$PCRE_OPT" = "." ]; then
-             PCRE_OPT="-O $IPO $CPU_OPT"
-         fi
-
-         if [ ".$MD5_OPT" = "." ]; then
-             MD5_OPT="-O $IPO $CPU_OPT"
-         fi
-
-         if [ ".$ZLIB_OPT" = "." ]; then
-             ZLIB_OPT="-O $IPO $CPU_OPT"
-         fi
-
-         # warnings
-         CFLAGS="$CFLAGS -w1"
-         #CFLAGS="$CFLAGS -w2"
-
-         # stop on warning
-         CFLAGS="$CFLAGS -Werror"
-
-         # debug
-         CFLAGS="$CFLAGS -g"
-
-         have=HAVE_C99_VARIADIC_MACROS . auto/have
-
-         LINK="\$(CC)"
-
-         INCOPT="-I "
-         COMPOPT="-c"
-         OBJOUT="-o "
-         BINOUT="-o "
-         OBJEXT="o"
-
-         CC_STRONG="$CC -w1 -Werror"
-    ;;
-
-
-    cl)
-         # MSVC 6.0 SP2
-
-         # optimizations
-
-         # maximize speed
-         CFLAGS="$CFLAGS -O2"
-         # enable global optimization
-         CFLAGS="$CFLAGS -Og"
-         # enable intrinsic functions
-         CFLAGS="$CFLAGS -Oi"
-         # inline expansion
-         CFLAGS="$CFLAGS -Ob1"
-         # enable frame pointer omission
-         CFLAGS="$CFLAGS -Oy"
-         # disable stack checking calls
-         CFLAGS="$CFLAGS -Gs"
-
-         case $CPU in
-             pentium)
-                 # optimize for Pentium and Athlon
-                 CPU_OPT="-G5"
-             ;;
-
-             pentiumpro)
-                 # optimize for Pentium Pro, Pentium II and Pentium III
-                 CPU_OPT="-G6"
-             ;;
-
-             pentium4)
-                 # optimize for Pentium 4
-                 #CPU_OPT="-G7"
-             ;;
-         esac
-
-         CFLAGS="$CFLAGS $CPU_OPT"
-
-         # warnings
-         #CFLAGS="$CFLAGS -W3"
-         CFLAGS="$CFLAGS -W4"
-
-         # stop on warning
-         CFLAGS="$CFLAGS -WX"
-
-         # link with libcmt.lib, multithreaded
-         #LIBC="-MT"
-         # link with msvcrt.dll
-         LIBC="-MD"
-
-         CFLAGS="$CFLAGS $LIBC"
-
-         # disable logo
-         CFLAGS="$CFLAGS -nologo"
-
-         LINK="\$(CC)"
-
-         # link flags
-         CORE_LINK="$CORE_LINK -link -verbose:lib"
-
-         # debug
-         CFLAGS="$CFLAGS -Yd"
-         CORE_LINK="$CORE_LINK -debug -debugtype:coff"
-
-         # precompiled headers
-         CORE_DEPS="$CORE_DEPS ngx_config.pch"
-         PCH="ngx_config.pch"
-         BUILDPCH="-Ycngx_config.h"
-         USEPCH="-Yungx_config.h"
-
-         INCOPT="-I "
-         COMPOPT="-c"
-         OBJOUT="-Fo"
-         BINOUT="-Fe"
-         OBJEXT="obj"
-         BINEXT=".exe"
-         #DIRSEP='\\'
-    ;;
-
-
-    wcl386)
-         # Open Watcom C 1.0, 1.2
-
-         # optimizations
-
-         # maximize speed
-         CFLAGS="$CFLAGS -ot"
-         # reorder instructions for best pipeline usage
-         CFLAGS="$CFLAGS -op"
-         # inline intrinsic functions
-         CFLAGS="$CFLAGS -oi"
-         # inline expansion
-         CFLAGS="$CFLAGS -oe"
-         # disable stack checking calls
-         CFLAGS="$CFLAGS -s"
-
-         case $CPU in
-             pentium)
-                 # optimize for Pentium and Athlon
-                 # register-based arguments passing conventions
-                 CPU_OPT="-5r"
-                 # stack-based arguments passing conventions
-                 #CPU_OPT="-5s"
-             ;;
-
-             pentiumpro)
-                 # optimize for Pentium Pro, Pentium II and Pentium III
-                 # register-based arguments passing conventions
-                 CPU_OPT="-6r"
-                 # stack-based arguments passing conventions
-                 #CPU_OPT="-6s"
-             ;;
-         esac
-
-         CFLAGS="$CFLAGS $CPU_OPT"
-
-         # warnings
-         #CFLAGS="$CFLAGS -w3"
-         CFLAGS="$CFLAGS -wx"
-
-         # stop on warning
-         CFLAGS="$CFLAGS -we"
-
-         # built target is NT
-         CFLAGS="$CFLAGS -bt=nt"
-
-         # multithreaded
-         CFLAGS="$CFLAGS -bm"
-
-         # debug
-         CFLAGS="$CFLAGS -d2"
-
-         # quiet
-         CFLAGS="$CFLAGS -zq"
-
-         # Open Watcom C 1.2
-         #have=HAVE_C99_VARIADIC_MACROS . auto/have
-
-         # precompiled headers
-         CORE_DEPS="$CORE_DEPS $OBJS\\ngx_config.pch"
-         PCH="$OBJS\\ngx_config.pch"
-         BUILDPCH="-fhq=$OBJS\\ngx_config.pch"
-         USEPCH="-fh=$OBJS\\ngx_config.pch"
-
-         LINK="\$(CC)"
-
-         # link flags
-         CORE_LINK="$CORE_LINK -l=nt"
-
-         INCOPT="-i="
-         COMPOPT="-c"
-         OBJOUT="-fo"
-         BINOUT="-fe="
-         OBJEXT="obj"
-         BINEXT=".exe"
-         DIRSEP='\\'
-
-         MAKE_SL=YES
-    ;;
-
-
-    bcc32)
-         # Borland C++ 5.5
-
-         # optimizations
-
-         # maximize speed
-         CFLAGS="$CFLAGS -O2"
-
-         case $CPU in
-             pentium)
-                 # optimize for Pentium and Athlon
-                 CPU_OPT="-5"
-             ;;
-
-             pentiumpro)
-                 # optimize for Pentium Pro, Pentium II and Pentium III
-                 CPU_OPT="-6"
-             ;;
-         esac
-
-         CFLAGS="$CFLAGS $CPU_OPT"
-
-         # multithreaded
-         CFLAGS="$CFLAGS -tWM"
-
-         # stop on warning
-         CFLAGS="$CFLAGS -w!"
-
-         # disable logo
-         CFLAGS="$CFLAGS -q"
-
-         # precompiled headers
-         CORE_DEPS="$CORE_DEPS $OBJS\\ngx_config.csm"
-         PCH="$OBJS\\ngx_config.csm"
-         BUILDPCH="-H=$OBJS\\ngx_config.csm"
-         USEPCH="-Hu -H=$OBJS\\ngx_config.csm"
-
-         LINK="\$(CC)"
-
-         INCOPT="-I"
-         COMPOPT="-c"
-         OBJOUT="-o" 
-         BINOUT="-e"
-         OBJEXT="obj"
-         BINEXT=".exe"
-         DIRSEP='\\'
-    ;;
-
-esac
new file mode 100644
--- /dev/null
+++ b/auto/cc/bcc
@@ -0,0 +1,48 @@
+
+# Copyright (C) Igor Sysoev
+
+
+# Borland C++ 5.5
+
+# optimizations
+
+# maximize speed
+CFLAGS="$CFLAGS -O2"
+
+case $CPU in
+    pentium)
+        # optimize for Pentium and Athlon
+        CPU_OPT="-5"
+    ;;
+
+    pentiumpro)
+        # optimize for Pentium Pro, Pentium II and Pentium III
+        CPU_OPT="-6"
+    ;;
+esac
+
+CFLAGS="$CFLAGS $CPU_OPT"
+
+# multithreaded
+CFLAGS="$CFLAGS -tWM"
+
+# stop on warning
+CFLAGS="$CFLAGS -w!"
+
+# disable logo
+CFLAGS="$CFLAGS -q"
+
+# precompiled headers
+CORE_DEPS="$CORE_DEPS $OBJS/ngx_config.csm"
+NGX_PCH="$OBJS/ngx_config.csm"
+NGX_BUILD_PCH="-H=$OBJS/ngx_config.csm"
+NGX_USE_PCH="-Hu -H=$OBJS/ngx_config.csm"
+
+LINK="\$(CC)"
+
+ngx_include_opt="-I"
+ngx_objout="-o"
+ngx_binout="-e"
+ngx_objext="obj"
+ngx_binext=".exe"
+ngx_dirsep='\\'
new file mode 100644
--- /dev/null
+++ b/auto/cc/conf
@@ -0,0 +1,119 @@
+
+# Copyright (C) Igor Sysoev
+
+
+ngx_include_opt="-I "
+ngx_compile_opt="-c"
+ngx_objout="-o "
+ngx_binout="-o "
+ngx_objext="o"
+ngx_binext=
+
+ngx_regex_dirsep="\/"
+ngx_dirsep='/'
+
+ngx_regex_cont=' \\\
+	'
+ngx_cont=' \
+	'
+ngx_tab=' \
+		'
+ngx_spacer=
+
+. auto/cc/name
+
+if test -n "$CFLAGS"; then
+
+    CC_TEST_FLAGS="$CFLAGS $NGX_CC_OPT"
+
+    case $NGX_CC_NAME in
+
+        ccc)
+            # Compaq C V6.5-207
+
+            ngx_include_opt="-I"
+        ;;
+
+    esac
+
+else
+
+    case $NGX_CC_NAME in
+        gcc)
+            # gcc 2.7.2.3, 2.8.1, 2.95.4,
+            #     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4
+
+            . auto/cc/gcc
+        ;;
+
+        icc)
+            # Intel C++ compiler 7.1, 8.0
+
+            . auto/cc/icc
+        ;;
+
+#        ccc)
+#            # Compaq C V6.5-207
+#
+#            . auto/cc/ccc
+#        ;;
+
+#        acc)
+#            # aCC: HP ANSI C++ B3910B A.03.55.02
+#
+#            . auto/cc/acc
+#        ;;
+
+        msvc)
+            # MSVC 6.0 SP2
+
+            . auto/cc/msvc
+        ;;
+
+        owc)
+            # Open Watcom C 1.0, 1.2
+
+            . auto/cc/owc
+        ;;
+
+        bcc)
+            # Borland C++ 5.5
+
+            . auto/cc/bcc
+        ;;
+
+    esac
+
+    CC_TEST_FLAGS=$NGX_CC_OPT
+
+fi
+
+CFLAGS="$CFLAGS $NGX_CC_OPT"
+
+
+if [ "$PLATFORM" != win32 ]; then
+
+    ngx_feature="gcc variadic macros"
+    ngx_feature_name=HAVE_GCC_VARIADIC_MACROS
+    ngx_feature_run=yes
+    ngx_feature_incs="#include <stdio.h>
+#define var(dummy, args...)  sprintf(args)"
+    ngx_feature_libs=
+    ngx_feature_test="char  buf[30]; buf[0] = '0';
+                      var(0, buf, \"%d\", 1);
+                      if (buf[0] != '1') return 1"
+    . auto/feature
+
+
+    ngx_feature="C99 variadic macros"
+    ngx_feature_name=HAVE_C99_VARIADIC_MACROS
+    ngx_feature_run=yes
+    ngx_feature_incs="#include <stdio.h>
+#define var(dummy, ...) sprintf(__VA_ARGS__)"
+    ngx_feature_libs=
+    ngx_feature_test="char  buf[30]; buf[0] = '0';
+                      var(0, buf, \"%d\", 1);
+                      if (buf[0] != '1') return 1"
+    . auto/feature
+
+fi
new file mode 100644
--- /dev/null
+++ b/auto/cc/gcc
@@ -0,0 +1,109 @@
+
+# Copyright (C) Igor Sysoev
+
+
+# gcc 2.7.2.3, 2.8.1, 2.95.4,
+#     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4
+
+
+# Solaris 7's /usr/ccs/bin/as does not support "-pipe"
+
+CC_TEST_FLAGS="-pipe"
+
+ngx_feature="gcc -pipe switch" 
+ngx_feature_name=
+ngx_feature_run=no
+ngx_feature_incs=
+ngx_feature_libs=
+ngx_feature_test=
+. auto/feature
+
+CC_TEST_FLAGS=
+
+if [ $ngx_found = yes ]; then
+    PIPE="-pipe"
+fi
+
+
+# optimizations
+
+#CFLAGS="$CFLAGS -O2 -fomit-frame-pointer"
+
+case $CPU in
+    pentium)
+        # optimize for Pentium and Athlon
+        CPU_OPT="-march=pentium"
+    ;;
+
+    pentiumpro)
+        # optimize for Pentium Pro, Pentium II and Pentium III
+        CPU_OPT="-march=pentiumpro"
+    ;;
+
+    pentium4)
+        # optimize for Pentium 4, gcc 3.x
+        CPU_OPT="-march=pentium4"
+    ;;
+
+    sparc64)
+        # build 64-bit UltraSparc binary
+        CPU_OPT="-m64"
+        CORE_LINK="$CORE_LINK -m64"
+        CC_AUX_FLAGS="$CC_AUX_FLAGS -m64"
+    ;;
+
+esac
+
+
+# STUB for batch builds
+if [ $CC = gcc27 ]; then CPU_OPT=; fi
+
+
+CFLAGS="$CFLAGS $PIPE $CPU_OPT"
+
+if [ ".$PCRE_OPT" = "." ]; then
+    PCRE_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
+else
+    PCRE_OPT="$PCRE_OPT $PIPE"
+fi
+
+if [ ".$MD5_OPT" = "." ]; then
+    MD5_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
+else
+    MD5_OPT="$MD5_OPT $PIPE"
+fi
+
+if [ ".$ZLIB_OPT" = "." ]; then
+    ZLIB_OPT="-O2 -fomit-frame-pointer $PIPE $CPU_OPT"
+else
+    ZLIB_OPT="$ZLIB_OPT $PIPE"
+fi
+
+
+# warnings
+
+CFLAGS="$CFLAGS -O -W"
+CFLAGS="$CFLAGS -Wall -Wpointer-arith"
+#CFLAGS="$CFLAGS -Wconversion" 
+#CFLAGS="$CFLAGS -Winline"
+
+# we have a lot of the unused function arguments
+CFLAGS="$CFLAGS -Wno-unused"
+
+# stop on warning
+CFLAGS="$CFLAGS -Werror"
+
+# debug
+CFLAGS="$CFLAGS -g"
+
+# DragonFly's gcc3 generates DWARF
+#CFLAGS="$CFLAGS -g -gstabs"
+
+if [ ".$CPP" = "." ]; then
+    CPP="$CC -E"
+fi
+
+LINK="\$(CC)"
+
+
+CC_STRONG="-Wall -Werror"
new file mode 100644
--- /dev/null
+++ b/auto/cc/icc
@@ -0,0 +1,69 @@
+
+# Copyright (C) Igor Sysoev
+
+
+# Intel C++ compiler 7.1, 8.0
+
+# optimizations
+
+CFLAGS="$CFLAGS -O"
+# inline functions declared with __inline
+#CFLAGS="$CFLAGS -Ob1"
+# inline any function, at the compiler's discretion
+CFLAGS="$CFLAGS -Ob2"
+
+# single-file IP optimizations
+#IPO="-ip"
+# multi-file IP optimizations
+IPO="-ipo -ipo_obj"
+CFLAGS="$CFLAGS $IPO"
+CORE_LINK="$CORE_LINK $IPO"
+CORE_LINK="$CORE_LINK -opt_report_file=$OBJS/opt_report_file"
+
+case $CPU in
+    pentium)
+        # optimize for Pentium and Athlon
+        CPU_OPT="-march=pentium"
+    ;;
+
+    pentiumpro)
+        # optimize for Pentium Pro, Pentium II and Pentium III
+        CPU_OPT="-mcpu=pentiumpro -march=pentiumpro"
+    ;;
+
+    pentium4)
+        # optimize for Pentium 4, default
+        CPU_OPT="-march=pentium4"
+    ;;
+esac
+
+CFLAGS="$CFLAGS $CPU_OPT"
+
+if [ ".$PCRE_OPT" = "." ]; then
+    PCRE_OPT="-O $IPO $CPU_OPT"
+fi
+
+if [ ".$MD5_OPT" = "." ]; then
+    MD5_OPT="-O $IPO $CPU_OPT"
+fi
+
+if [ ".$ZLIB_OPT" = "." ]; then
+    ZLIB_OPT="-O $IPO $CPU_OPT"
+fi
+
+
+# warnings
+
+CFLAGS="$CFLAGS -w1"
+#CFLAGS="$CFLAGS -w2"
+
+# stop on warning
+CFLAGS="$CFLAGS -Werror"
+
+# debug
+CFLAGS="$CFLAGS -g"
+
+LINK="\$(CC)"
+
+
+CC_STRONG="-w1 -Werror"
new file mode 100644
--- /dev/null
+++ b/auto/cc/msvc
@@ -0,0 +1,81 @@
+
+# Copyright (C) Igor Sysoev
+
+
+# MSVC 6.0 SP2
+
+# optimizations
+
+# maximize speed
+CFLAGS="$CFLAGS -O2"
+# enable global optimization
+CFLAGS="$CFLAGS -Og"
+# enable intrinsic functions
+CFLAGS="$CFLAGS -Oi"
+# inline expansion
+CFLAGS="$CFLAGS -Ob1"
+# enable frame pointer omission
+CFLAGS="$CFLAGS -Oy"
+# disable stack checking calls
+CFLAGS="$CFLAGS -Gs"
+
+case $CPU in
+    pentium)
+        # optimize for Pentium and Athlon
+        CPU_OPT="-G5"
+    ;;
+
+    pentiumpro)
+        # optimize for Pentium Pro, Pentium II and Pentium III
+        CPU_OPT="-G6"
+    ;;
+
+    pentium4)
+        # optimize for Pentium 4
+        #CPU_OPT="-G7"
+    ;;
+esac
+
+CFLAGS="$CFLAGS $CPU_OPT"
+
+
+# warnings
+
+#CFLAGS="$CFLAGS -W3"
+CFLAGS="$CFLAGS -W4"
+
+# stop on warning
+CFLAGS="$CFLAGS -WX"
+
+# link with libcmt.lib, multithreaded
+#LIBC="-MT"
+# link with msvcrt.dll
+LIBC="-MD"
+
+CFLAGS="$CFLAGS $LIBC"
+
+# disable logo
+CFLAGS="$CFLAGS -nologo"
+
+LINK="\$(CC)"
+
+# link flags
+CORE_LINK="$CORE_LINK -link -verbose:lib"
+
+# debug
+CFLAGS="$CFLAGS -Yd"
+CORE_LINK="$CORE_LINK -debug -debugtype:coff"
+
+# precompiled headers
+CORE_DEPS="$CORE_DEPS $OBJS/ngx_config.pch"
+NGX_PCH="$OBJS/ngx_config.pch"
+NGX_BUILD_PCH="-Ycngx_config.h -Fp$OBJS/ngx_config.pch"
+NGX_USE_PCH="-Yungx_config.h -Fp$OBJS/ngx_config.pch"
+
+ngx_objout="-Fo"
+ngx_binout="-Fe"
+ngx_objext="obj"
+ngx_binext=".exe"
+
+#ngx_regex_dirsep='\\'
+#ngx_dirsep="\\"
new file mode 100644
--- /dev/null
+++ b/auto/cc/name
@@ -0,0 +1,51 @@
+
+# Copyright (C) Igor Sysoev
+
+
+echo $ngx_n "checking for C compiler ...$ngx_c"
+
+
+if [ $CC = cl ]; then
+    NGX_CC_NAME=msvc
+    echo " using Microsoft Visual C compiler"
+
+else
+if [ $CC = wcl386 ]; then
+    NGX_CC_NAME=owc
+    echo " using Open Watcom C compiler"
+
+else
+if [ $CC = bcc32 ]; then
+    NGX_CC_NAME=bcc
+    echo " using Borland C++ compiler"
+
+else
+if `$CC -v 2>&1 | grep '^gcc version' 2>&1 >/dev/null`; then
+    NGX_CC_NAME=gcc
+    echo " using GNU C compiler"
+
+else
+if `$CC -V 2>&1 | grep '^Intel(R) C++ Compiler' 2>&1 >/dev/null`; then
+    NGX_CC_NAME=icc
+    echo " using Intel C++ compiler"
+
+else
+if `$CC -V 2>&1 | grep '^Compaq C' 2>&1 >/dev/null`; then
+    NGX_CC_NAME=ccc
+    echo " using Compaq C compiler"
+
+else
+if `$CC -V 2>&1 | grep '^aCC: ' 2>&1 >/dev/null`; then
+    NGX_CC_NAME=acc
+    echo " using HP aC++ compiler"
+
+else
+    echo " unknown"
+
+fi # acc
+fi # ccc
+fi # icc
+fi # gcc
+fi # bcc
+fi # owc
+fi # msvc
new file mode 100644
--- /dev/null
+++ b/auto/cc/owc
@@ -0,0 +1,82 @@
+
+# Copyright (C) Igor Sysoev
+
+
+# Open Watcom C 1.0, 1.2
+
+# optimizations
+
+# maximize speed
+CFLAGS="$CFLAGS -ot"
+# reorder instructions for best pipeline usage 
+CFLAGS="$CFLAGS -op"
+# inline intrinsic functions
+CFLAGS="$CFLAGS -oi"
+# inline expansion
+CFLAGS="$CFLAGS -oe"
+# disable stack checking calls
+CFLAGS="$CFLAGS -s"
+
+case $CPU in
+    pentium)
+        # optimize for Pentium and Athlon
+        # register-based arguments passing conventions
+        CPU_OPT="-5r"
+        # stack-based arguments passing conventions
+        #CPU_OPT="-5s"
+    ;; 
+
+    pentiumpro)
+        # optimize for Pentium Pro, Pentium II and Pentium III
+        # register-based arguments passing conventions
+        CPU_OPT="-6r"
+        # stack-based arguments passing conventions
+        #CPU_OPT="-6s"
+    ;;
+esac
+
+CFLAGS="$CFLAGS $CPU_OPT"
+
+
+# warnings
+
+#CFLAGS="$CFLAGS -w3"
+CFLAGS="$CFLAGS -wx"
+
+# stop on warning
+CFLAGS="$CFLAGS -we"
+
+# built target is NT
+CFLAGS="$CFLAGS -bt=nt"
+
+# multithreaded
+CFLAGS="$CFLAGS -bm"
+
+# debug
+CFLAGS="$CFLAGS -d2"
+
+# quiet
+CFLAGS="$CFLAGS -zq"
+
+# Open Watcom C 1.2
+#have=HAVE_C99_VARIADIC_MACROS . auto/have
+
+# precompiled headers 
+CORE_DEPS="$CORE_DEPS $OBJS/ngx_config.pch"
+NGX_PCH="$OBJS/ngx_config.pch"
+NGX_BUILD_PCH="-fhq=$OBJS/ngx_config.pch"
+NGX_USE_PCH="-fh=$OBJS/ngx_config.pch"
+
+LINK="\$(CC)"
+
+# link flags
+CORE_LINK="$CORE_LINK -l=nt"
+
+ngx_include_opt="-i="
+ngx_objout="-fo"
+ngx_binout="-fe="
+ngx_objext="obj"
+ngx_binext=".exe"
+
+ngx_regex_dirsep='\\'
+ngx_dirsep="\\"
--- a/auto/endianess
+++ b/auto/endianess
@@ -2,7 +2,7 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for system endianess ..." $ngx_c
+echo $ngx_n "checking for system endianess ...$ngx_c"
 echo >> $NGX_ERR
 echo "checking for system endianess" >> $NGX_ERR
 
--- a/auto/feature
+++ b/auto/feature
@@ -2,19 +2,28 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_feature ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_feature" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_feature ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_feature
+
+END
 
 ngx_found=no
 
-feature=`echo $ngx_feature_name | tr '[a-z]' '[A-Z]'`
+if test -n "$ngx_feature_name"; then
+    ngx_have_feature=`echo NGX_$ngx_feature_name | tr '[a-z]' '[A-Z]'`
+    # STUB
+    ngx_have_feature0=`echo HAVE_$ngx_feature_name | tr '[a-z]' '[A-Z]'`
+fi
 
 cat << END > $NGX_AUTOTEST.c
 
 #include <sys/types.h>
-$NGX_UNISTD_H
-$ngx_feature_inc
+$NGX_INCLUDE_UNISTD_H
+$ngx_feature_incs
 
 int main() {
     $ngx_feature_test;
@@ -23,34 +32,56 @@ int main() {
 
 END
 
-test="$CC_WARN $CC_TEST_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c \
-      $ngx_feature_libs"
-eval "$test >> $NGX_ERR 2>&1"
+
+ngx_test="$CC $CC_TEST_FLAGS $CC_WARN $CC_AUX_FLAGS \
+          -o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs"
+eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
+
 
 if [ -x $NGX_AUTOTEST ]; then
 
     if [ $ngx_feature_run = yes ]; then
+
        if $NGX_AUTOTEST 2>&1 > /dev/null; then
            echo " found"
-           have=HAVE_$feature . auto/have
            ngx_found=yes
+
+           if test -n "$ngx_feature_name"; then
+               have=$ngx_have_feature . auto/have
+           fi
+
+           #STUB
+           if test -n "$ngx_feature_name"; then
+               have=$ngx_have_feature0 . auto/have
+           fi
+
        else
            echo " found but is not working"
        fi
 
     else
        echo " found"
-       have=HAVE_$feature . auto/have
        ngx_found=yes
+
+       if test -n "$ngx_feature_name"; then
+           have=$ngx_have_feature . auto/have
+       fi
+
+       #STUB
+       if test -n "$ngx_feature_name"; then
+           have=$ngx_have_feature0 . auto/have
+       fi
+
     fi
 
 else
     echo " not found"
-    echo "---------" >> $NGX_ERR
-    cat $NGX_AUTOTEST.c >> $NGX_ERR
-    echo "---------" >> $NGX_ERR
-    echo $test >> $NGX_ERR
-    echo "---------" >> $NGX_ERR
+
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    echo $ngx_test       >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
 fi
 
 rm $NGX_AUTOTEST*
--- a/auto/fmt/fmt
+++ b/auto/fmt/fmt
@@ -2,14 +2,21 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_type printf() format ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_type printf() format" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_type printf() format ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_type printf() format
 
-ngx_fmt=no
-comma=
+END
+
 
-for fmt in $ngx_formats
+ngx_format=no
+ngx_comma=
+
+
+for ngx_fmt in $ngx_formats
 do
 
     cat << END > $NGX_AUTOTEST.c
@@ -18,52 +25,63 @@ do
 #include <sys/time.h>
 #include <stdio.h>
 #include <sys/resource.h>
-$NGX_INTTYPES_H
-$NGX_AUTO_CONFIG
+$NGX_INCLUDE_INTTYPES_H
+$NGX_INCLUDE_AUTO_CONFIG_H
 
 int main() {
-    printf("$fmt", ($ngx_type) $ngx_max_value);
+    printf("$ngx_fmt", ($ngx_type) $ngx_max_value);
     return 0;
 }
 
 END
 
-    eval "$CC_WARN $CC_TEST_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c \
-          >> $NGX_ERR 2>&1"
+    ngx_test="$CC $CC_TEST_FLAGS $CC_WARN $CC_AUX_FLAGS \
+              -o $NGX_AUTOTEST $NGX_AUTOTEST.c"
+    eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
 
-    max_value=`echo $ngx_max_value | sed -e "s/L*\$//"`
+    ngx_max_val=`echo $ngx_max_value | sed -e "s/L*\$//"`
 
     if [ -x $NGX_AUTOTEST ]; then
-        if [ "`$NGX_AUTOTEST`" = $max_value ]; then
+
+        if [ "`$NGX_AUTOTEST`" = $ngx_max_val ]; then
+
             if [ $ngx_fmt_collect = yes ]; then
-                echo $ngx_n "$comma \"${fmt}\" is appropriate" $ngx_c
+                echo $ngx_n "$ngx_comma \"${ngx_fmt}\" is appropriate$ngx_c"
             else
-                echo $ngx_n "$comma \"${fmt}\" used" $ngx_c
+                echo $ngx_n "$ngx_comma \"${ngx_fmt}\" used$ngx_c"
             fi
-            ngx_fmt=$fmt
+
+            ngx_format=$ngx_fmt
         fi
     fi
 
-    rm $NGX_AUTOTEST*
+    rm -f $NGX_AUTOTEST
 
-    if [ $ngx_fmt != no ]; then
+    if [ $ngx_format != no ]; then
         if [ $ngx_fmt_collect = yes ]; then
-            eval "ngx_${ngx_size}_fmt=\"\${ngx_${ngx_size}_fmt} \$ngx_fmt\""
-            comma=","
+            eval "ngx_${ngx_size}_fmt=\"\${ngx_${ngx_size}_fmt} \$ngx_format\""
+            ngx_comma=","
             continue
         else
             break
         fi
     fi
 
-    echo $ngx_n "$comma \"${fmt}\" is not appropriate" $ngx_c
-    comma=","
+    echo $ngx_n "$ngx_comma \"${ngx_fmt}\" is not appropriate$ngx_c"
+    ngx_comma=","
+
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    echo $ngx_test       >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
 done
 
 echo
 
-if [ $ngx_fmt = no ]; then
+if [ $ngx_format = no ]; then
     echo "$0: error: printf() $ngx_type format not found"
+
     exit 1
 fi
 
@@ -72,7 +90,7 @@ if [ $ngx_fmt_collect = no ]; then
     cat << END >> $NGX_AUTO_CONFIG_H
 
 #ifndef $ngx_fmt_name
-#define $ngx_fmt_name  "$ngx_fmt"
+#define $ngx_fmt_name  "$ngx_format"
 #endif
 
 END
--- a/auto/fmt/ptrfmt
+++ b/auto/fmt/ptrfmt
@@ -2,71 +2,85 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_type printf() format ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_type printf() format" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_type printf() format ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_type printf() format
 
-ngx_fmt=no
-comma=
-fmtX=
+END
+
 
-for fmt in $ngx_formats
+ngx_format=no
+ngx_comma=
+ngx_fmt_x=
+
+for ngx_fmt in $ngx_formats
 do
 
     cat << END > $NGX_AUTOTEST.c
 
 int main() {
-    printf("$fmt", ($ngx_type) $ngx_max_value);
+    printf("$ngx_fmt", ($ngx_type) $ngx_max_value);
     return 0;
 }
 
 END
 
-    eval "$CC_WARN $CC_TEST_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c \
-          >> $NGX_ERR 2>&1"
+    ngx_test="$CC $CC_TEST_FLAGS $CC_WARN $CC_AUX_FLAGS \
+              -o $NGX_AUTOTEST $NGX_AUTOTEST.c"
+    eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
 
-    max_value=`echo $ngx_max_value | sed -e "s/L*\$//"`
+    ngx_max_val=`echo $ngx_max_value | sed -e "s/L*\$//"`
 
     if [ -x $NGX_AUTOTEST ]; then
-        if [ "`$NGX_AUTOTEST`" = $max_value ]; then
-            ngx_fmt=$fmt
+        if [ "`$NGX_AUTOTEST`" = $ngx_max_val ]; then
+            ngx_format=$ngx_fmt
         fi
     fi
 
-    rm $NGX_AUTOTEST*
+    rm $NGX_AUTOTEST
 
-    if [ $ngx_fmt != no ]; then
+    if [ $ngx_format != no ]; then
         break
     fi
 
-    fmtX=`echo $fmt | sed -e "s/d/X/"`
+    ngx_fmt_x=`echo $ngx_fmt | sed -e "s/d/X/"`
+
+    echo $ngx_n "$ngx_comma \"${ngx_fmt_x}\" is not appropriate$ngx_c"
+    ngx_comma=","
 
-    echo $ngx_n "$comma \"${fmtX}\" is not appropriate" $ngx_c
-    comma=","
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    echo $ngx_test       >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
 done
 
 
-if [ $ngx_fmt = no ]; then
+if [ $ngx_format = no ]; then
     echo "$0: error: printf() $ngx_type format not found"
+
     exit 1
 fi
 
 
 if [ $ngx_ptr_size = 4 ]; then
-    fmtX="%0`expr 2 \* $ngx_ptr_size`"
+    ngx_fmt_x="%0`expr 2 \* $ngx_ptr_size`"
 else
-    fmtX="%"
+    ngx_fmt_x="%"
 fi
 
-ngx_fmt=`echo $ngx_fmt | sed -e "s/d/X/" -e "s/^%/$fmtX/"`
+ngx_format=`echo $ngx_format | sed -e "s/d/X/" -e "s/^%/$ngx_fmt_x/"`
 
-echo "$comma \"${ngx_fmt}\" used"
+echo "$ngx_comma \"${ngx_format}\" used"
 
 
 cat << END >> $NGX_AUTO_CONFIG_H
 
 #ifndef $ngx_fmt_name
-#define $ngx_fmt_name  "$ngx_fmt"
+#define $ngx_fmt_name  "$ngx_format"
 #endif
 
 END
--- a/auto/headers
+++ b/auto/headers
@@ -2,5 +2,7 @@
 # Copyright (C) Igor Sysoev
 
 
-ngx_inc="unistd.h"; . auto/inc
-ngx_inc="inttypes.h"; . auto/inc
+ngx_include="unistd.h";    . auto/include
+ngx_include="inttypes.h";  . auto/include
+ngx_include="limits.h";    . auto/include
+ngx_include="sys/filio.h"; . auto/include
deleted file mode 100644
--- a/auto/inc
+++ /dev/null
@@ -1,35 +0,0 @@
-
-# Copyright (C) Igor Sysoev
-
-
-echo $ngx_n "checking for $ngx_inc ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_inc" >> $NGX_ERR
-
-ngx_found=no
-
-inc=`echo $ngx_inc | sed -e 's/\./_/' | sed -e 's/\//_/' | tr '[a-z]' '[A-Z]'`
-
-cat << END > $NGX_AUTOTEST.c
-
-#include <$ngx_inc>
-
-int main() {
-    return 0;
-}
-
-END
-
-eval "${CC} -o $NGX_AUTOTEST $NGX_AUTOTEST.c >> $NGX_ERR 2>&1"
-
-if [ -x $NGX_AUTOTEST ]; then
-    echo " found"
-    have=HAVE_$inc . auto/have
-    eval "NGX_$inc='#include <$ngx_inc>'"
-    ngx_found=yes
-
-else
-    echo " not found"
-fi
-
-rm $NGX_AUTOTEST*
new file mode 100644
--- /dev/null
+++ b/auto/include
@@ -0,0 +1,66 @@
+
+# Copyright (C) Igor Sysoev
+
+
+echo $ngx_n "checking for $ngx_include ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_include
+
+END
+
+
+ngx_found=no
+
+cat << END > $NGX_AUTOTEST.c
+
+#include <$ngx_include>
+
+int main() {
+    return 0;
+}
+
+END
+
+
+ngx_test="$CC -o $NGX_AUTOTEST $NGX_AUTOTEST.c"
+
+eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
+
+if [ -x $NGX_AUTOTEST ]; then
+
+    ngx_found=yes
+
+    echo " found"
+
+    ngx_name=`echo $ngx_include | sed -e 's/\./_/' -e 's/\//_/' \
+                  |  tr '[a-z]' '[A-Z]'`
+
+
+    cat << END >> $NGX_AUTO_HEADERS_H
+
+#ifndef NGX_HAVE_$ngx_name
+#define NGX_HAVE_$ngx_name  1
+#endif
+
+END
+
+
+    eval "NGX_INCLUDE_$ngx_name='#include <$ngx_include>'"
+
+    #STUB 
+    eval "NGX_$ngx_name='#include <$ngx_include>'"
+
+else
+    echo " not found"
+
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+    echo $ngx_test       >> $NGX_AUTOCONF_ERR
+    echo "----------"    >> $NGX_AUTOCONF_ERR
+fi
+
+rm $NGX_AUTOTEST*
--- a/auto/init
+++ b/auto/init
@@ -2,31 +2,33 @@
 # Copyright (C) Igor Sysoev
 
 
+NGX_MAKEFILE=$NGX_OBJS/Makefile
+NGX_MODULES_C=$NGX_OBJS/ngx_modules.c
+
+NGX_AUTO_HEADERS_H=$NGX_OBJS/ngx_auto_headers.h
+NGX_AUTO_CONFIG_H=$NGX_OBJS/ngx_auto_config.h
+
+NGX_AUTOTEST=$NGX_OBJS/autotest
+NGX_AUTOCONF_ERR=$NGX_OBJS/autoconf.err
+
+# STUBs
+OBJS=$NGX_OBJS
+NGX_ERR=$OBJS/autoconf.err
 MAKEFILE=$OBJS/Makefile
 
-NGX_AUTO_CONFIG_H=$OBJS/ngx_auto_config.h
-NGX_MODULES_C=$OBJS/ngx_modules.c
 
-NGX_AUTOTEST=$OBJS/autotest
-NGX_ERR=$OBJS/autoconf.err
-
-CC_WARN=$CC
-
-PCH=NO
-USEPCH=
-
-OBJEXT=
-BINEXT=
-DIRSEP='\/'
-MAKE_SL=NO
+NGX_PCH=
+NGX_USE_PCH=
 
 
-# checking echo's "-n" option and "\c" capabilties
+# check the echo's "-n" option and "\c" capability
 
 if echo "test\c" | grep c >/dev/null; then
+
     if echo -n test | grep n >/dev/null; then
         ngx_n=
         ngx_c=
+
     else
         ngx_n=-n
         ngx_c=
@@ -38,19 +40,18 @@ else
 fi
 
 
+# create Makefile
 
-if test ! -f Makefile; then
-
-    cat << END > Makefile
+cat << END > Makefile
 
 build:
-	\$(MAKE) -f $OBJS/Makefile
+	\$(MAKE) -f $NGX_MAKEFILE
 
 install:
-	\$(MAKE) -f $OBJS/Makefile install
+	\$(MAKE) -f $NGX_MAKEFILE install
 
 clean:
-	rm -rf Makefile $OBJS
+	rm -rf Makefile $NGX_OBJS
 
 upgrade:
 	$SBIN_PATH -t
@@ -58,7 +59,4 @@ upgrade:
 	sleep 1
 	test -f $PID_PATH.newbin
 	kill -WINCH \`cat $PID_PATH\`
-
 END
-
-fi
--- a/auto/install
+++ b/auto/install
@@ -8,7 +8,7 @@ install:
 
 	test -d `dirname $SBIN_PATH` || mkdir -p `dirname $SBIN_PATH`
 	test ! -f $SBIN_PATH || mv $SBIN_PATH $SBIN_PATH.old
-	cp nginx $SBIN_PATH
+	cp $OBJS/nginx $SBIN_PATH
 
 	test -d `dirname $CONF_PATH` || mkdir -p `dirname $CONF_PATH`
 
--- a/auto/lib/md5/conf
+++ b/auto/lib/md5/conf
@@ -15,14 +15,14 @@ if [ $MD5 != NONE ]; then
 
     CORE_INCS="$CORE_INCS $MD5"
 
-    case "$CC" in
+    case "$NGX_CC_NAME" in
 
-        cl | wcl386 | bcc32)
+        msvc | owc | bcc)
             LINK_DEPS="$LINK_DEPS $MD5/md5.lib"
             CORE_LIBS="$CORE_LIBS $MD5/md5.lib"
         ;;
 
-        *icc)
+        icc)
             LINK_DEPS="$LINK_DEPS $MD5/libmd5.a"
 
             # to allow -ipo optimization we link with the *.o but not library
@@ -45,20 +45,19 @@ else
 
     if [ $PLATFORM != win32 ]; then
         MD5=NO
-        ngx_lib_cflags=
 
         # Solaris 8/9
 
-        ngx_lib_inc="#include <sys/types.h>
-#include <md5.h>"
-        ngx_lib="rsaref md5 library"
-        ngx_lib_test="MD5_CTX md5; MD5Init(&md5)"
-        ngx_libs=-lmd5
-        . auto/lib/test
-
+        ngx_feature="rsaref md5 library"
+        ngx_feature_name="rsaref_md5"
+        ngx_feature_run=no
+        ngx_feature_incs="#include <md5.h>"
+        ngx_feature_libs="-lmd5"
+        ngx_feature_test="MD5_CTX md5; MD5Init(&md5)"
+        . auto/feature
 
         if [ $ngx_found = yes ]; then
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             MD5=YES
             MD5_LIB=md5
             ngx_found=no
@@ -66,15 +65,18 @@ else
         else
             # FreeBSD
 
-            ngx_lib="rsaref md library"
-            ngx_lib_test="MD5_CTX md5; MD5Init(&md5)"
-            ngx_libs=-lmd
-            . auto/lib/test
+            ngx_feature="rsaref md library"
+            ngx_feature_name="rsaref_md5"
+            ngx_feature_run=no
+            ngx_feature_incs="#include <md5.h>"
+            ngx_feature_libs="-lmd"
+            ngx_feature_test="MD5_CTX md5; MD5Init(&md5)"
+            . auto/feature
         fi
 
 
         if [ $ngx_found = yes ]; then
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             MD5=YES
             MD5_LIB=md
             ngx_found=no
@@ -84,19 +86,20 @@ else
 
                # OpenSSL crypto library
 
-               ngx_lib_inc="#include <openssl/md5.h>"
-               ngx_lib="OpenSSL md5 crypto library"
-               ngx_lib_test="MD5_CTX md5; MD5_Init(&md5)"
-               ngx_libs=-lcrypto
-               . auto/lib/test
+               ngx_feature="OpenSSL md5 crypto library"
+               ngx_feature_name="OpenSSL_md5"
+               ngx_feature_run=no
+               ngx_feature_incs="#include <openssl/md5.h>"
+               ngx_feature_libs="-lcrypto"
+               ngx_feature_test="MD5_CTX md5; MD5_Init(&md5)"
+               . auto/feature
             fi
         fi
 
 
         if [ $ngx_found = yes ]; then
-            have=HAVE_OPENSSL_MD5 . auto/have
             have=HAVE_OPENSSL_MD5_H . auto/have
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             MD5=YES
             MD5_LIB=crypto
         fi
--- a/auto/lib/md5/make
+++ b/auto/lib/md5/make
@@ -25,7 +25,7 @@ esac
 case $PLATFORM in
 
     win32)
-        line=`echo $MD5/md5.lib: | sed -e "s/\//$DIRSEP/g"`
+        line=`echo $MD5/md5.lib: | sed -e "s/\//$ngx_regex_dirsep/g"`
         echo "$line"                                              >> $MAKEFILE
     ;;
 
@@ -42,7 +42,7 @@ done=NO
 case $PLATFORM in
 
     win32)
-        md5=`echo $MD5 | sed -e "s/\//$DIRSEP/g"`
+        md5=`echo $MD5 | sed -e "s/\//$ngx_regex_dirsep/g"`
 
         cp auto/lib/md5/$makefile $MD5
         echo "	cd $md5"                                          >> $MAKEFILE
--- a/auto/lib/openssl/conf
+++ b/auto/lib/openssl/conf
@@ -19,23 +19,22 @@ if [ $OPENSSL != NONE ]; then
 else
 
     if [ $PLATFORM != win32 ]; then
-            OPENSSL=NO
-            ngx_lib_cflags=
+        OPENSSL=NO
 
-            ngx_lib_inc="#include <openssl/ssl.h>"
-            ngx_lib="OpenSSL library"
-            ngx_lib_test="SSL_library_init()"
-            ngx_libs="-lssl -lcrypto"
-            . auto/lib/test
-
+        ngx_feature="OpenSSL library"
+        ngx_feature_name="OpenSSL"
+        ngx_feature_run=no
+        ngx_feature_incs="#include <openssl/ssl.h>"
+        ngx_feature_libs="-lssl -lcrypto"
+        ngx_feature_test="SSL_library_init()"
+        . auto/feature
 
         if [ $ngx_found = yes ]; then
             have=NGX_OPENSSL . auto/have
             CORE_DEPS="$CORE_DEPS $OPENSSL_DEPS"
             CORE_SRCS="$CORE_SRCS $OPENSSL_SRCS"
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             OPENSSL=YES
-            ngx_found=no
         fi
 
     fi
--- a/auto/lib/pcre/conf
+++ b/auto/lib/pcre/conf
@@ -7,18 +7,18 @@ if [ $PCRE != NONE ]; then
     CORE_DEPS="$CORE_DEPS $REGEX_DEPS"
     CORE_SRCS="$CORE_SRCS $REGEX_SRCS"
 
-    case "$CC" in
+    case "$NGX_CC_NAME" in
 
-        cl | wcl386 | bcc32)
-            have=HAVE_PCRE . auto/have
+        msvc | owc | bcc)
+            have=NGX_PCRE . auto/have
             have=PCRE_STATIC . auto/have
             CORE_DEPS="$CORE_DEPS $PCRE/pcre.h"
             LINK_DEPS="$LINK_DEPS $PCRE/pcre.lib"
             CORE_LIBS="$CORE_LIBS $PCRE/pcre.lib"
         ;;
 
-        *icc)
-            have=HAVE_PCRE . auto/have
+        icc)
+            have=NGX_PCRE . auto/have
             CORE_DEPS="$CORE_DEPS $PCRE/pcre.h"
 
             LINK_DEPS="$LINK_DEPS $PCRE/.libs/libpcre.a"
@@ -31,7 +31,7 @@ if [ $PCRE != NONE ]; then
         ;;
 
         *)
-            have=HAVE_PCRE . auto/have
+            have=NGX_PCRE . auto/have
             CORE_DEPS="$CORE_DEPS $PCRE/pcre.h"
             LINK_DEPS="$LINK_DEPS $PCRE/.libs/libpcre.a"
             CORE_LIBS="$CORE_LIBS $PCRE/.libs/libpcre.a"
@@ -43,44 +43,25 @@ if [ $PCRE != NONE ]; then
 else
 
     if [ $PLATFORM != win32 ]; then
-            PCRE=NO
-            ngx_lib_cflags=
-
-            # Linux
+        PCRE=NO
 
-            ngx_lib_inc="#include <pcre.h>"
-            ngx_lib="PCRE library"
-            ngx_lib_test="pcre *re; re = pcre_compile(NULL, 0, NULL, 0, NULL)"
-            ngx_libs="-lpcre"
-            . auto/lib/test
-
+        # FreeBSD PCRE port requires --with-cc-opt="-I /usr/local/include"
+        #                            --with-ld-opt="-L /usr/local/lib"
+        
+        ngx_feature="PCRE library"
+        ngx_feature_name="PCRE"
+        ngx_feature_run=no
+        ngx_feature_incs="#include <pcre.h>"
+        ngx_feature_libs="-lpcre"
+        ngx_feature_test="pcre *re; re = pcre_compile(NULL, 0, NULL, 0, NULL)"
+        . auto/feature
 
         if [ $ngx_found = yes ]; then
-            have=HAVE_PCRE . auto/have
             CORE_DEPS="$CORE_DEPS $REGEX_DEPS"
             CORE_SRCS="$CORE_SRCS $REGEX_SRCS"
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             PCRE=YES
             ngx_found=no
-
-        else
-            # FreeBSD PCRE port.
-
-            ngx_lib="PCRE library in /usr/local/"
-            ngx_lib_cflags="-I /usr/local/include"
-            ngx_libs="-L /usr/local/lib -lpcre"
-            . auto/lib/test
-        fi
-
-
-        if [ $ngx_found = yes ]; then
-            have=HAVE_PCRE . auto/have
-            CORE_DEPS="$CORE_DEPS $REGEX_DEPS"
-            CORE_INCS="$CORE_INCS /usr/local/include"
-            CORE_SRCS="$CORE_SRCS $REGEX_SRCS"
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
-            PCRE=YES
         fi
     fi
-
 fi
--- a/auto/lib/pcre/make
+++ b/auto/lib/pcre/make
@@ -29,8 +29,8 @@ case $PLATFORM in
         cp auto/lib/pcre/patch.config.in $PCRE
         cp auto/lib/pcre/$makefile $PCRE
 
-        pcre=`echo $PCRE | sed -e "s/\//$DIRSEP/g"`
-        line=`echo $PCRE/pcre.h: | sed -e "s/\//$DIRSEP/g"`
+        pcre=`echo $PCRE | sed -e "s/\//$ngx_regex_dirsep/g"`
+        line=`echo $PCRE/pcre.h: | sed -e "s/\//$ngx_regex_dirsep/g"`
 
         echo "$line"                                              >> $MAKEFILE
         echo "	cd $pcre"                                         >> $MAKEFILE
@@ -39,7 +39,7 @@ case $PLATFORM in
         echo                                                      >> $MAKEFILE
 
         line="$PCRE/pcre.lib:	$PCRE/pcre.h"
-        line=`echo $line | sed -e "s/\//$DIRSEP/g"`
+        line=`echo $line | sed -e "s/\//$ngx_regex_dirsep/g"`
 
         echo "$line"                                              >> $MAKEFILE
         echo "	cd $pcre"                                         >> $MAKEFILE
--- a/auto/lib/test
+++ b/auto/lib/test
@@ -2,14 +2,26 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_lib ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_lib library" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_lib ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_lib
+
+END
 
 ngx_found=no
 
-echo "$ngx_lib_inc" > $NGX_AUTOTEST.c
-echo "int main() { $ngx_lib_test; return 0; }" >> $NGX_AUTOTEST.c
+cat << END > $NGX_AUTOTEST.c
+
+$ngx_lib_incs
+
+int main() {
+    $ngx_lib_test;
+    return 0;
+}
+
 
 eval "$CC $cc_test_flags $ngx_lib_cflags \
      -o $NGX_AUTOTEST $NGX_AUTOTEST.c $ngx_libs \
--- a/auto/lib/zlib/conf
+++ b/auto/lib/zlib/conf
@@ -5,14 +5,14 @@
 if [ $ZLIB != NONE ]; then
     CORE_INCS="$CORE_INCS $ZLIB"
 
-    case "$CC" in
+    case "$NGX_CC_NAME" in
 
-        cl | wcl386 | bcc32)
+        msvc | owc | bcc)
             LINK_DEPS="$LINK_DEPS $ZLIB/zlib.lib"
             CORE_LIBS="$CORE_LIBS $ZLIB/zlib.lib"
         ;;
 
-        *icc)
+        icc)
             LINK_DEPS="$LINK_DEPS $ZLIB/libz.a"
 
             # to allow -ipo optimization we link with the *.o but not library
@@ -38,22 +38,23 @@ if [ $ZLIB != NONE ]; then
 else
 
     if [ $PLATFORM != win32 ]; then
+        ZLIB=NO
 
         # FreeBSD, Solaris, Linux
 
-        ngx_lib_cflags=
-        ngx_lib_inc="#include <zlib.h>"
-        ngx_lib="zlib library"
-        ngx_lib_test="z_stream z; deflate(&z, Z_NO_FLUSH)"
-        ngx_libs=-lz
-        . auto/lib/test
+        ngx_feature="zlib library"
+        ngx_feature_name="zlib"
+        ngx_feature_run=no
+        ngx_feature_incs="#include <zlib.h>"
+        ngx_feature_libs="-lz"
+        ngx_feature_test="z_stream z; deflate(&z, Z_NO_FLUSH)"
+        . auto/feature
 
 
         if [ $ngx_found = yes ]; then
-            CORE_LIBS="$CORE_LIBS $ngx_libs"
+            CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
             ZLIB=YES
-        else
-            ZLIB=NO
+            ngx_found=no
         fi
     fi
 
--- a/auto/lib/zlib/make
+++ b/auto/lib/zlib/make
@@ -26,7 +26,7 @@ esac
 case $PLATFORM in
 
     win32)
-        line=`echo $ZLIB/zlib.lib: | sed -e "s/\//$DIRSEP/g"`
+        line=`echo $ZLIB/zlib.lib: | sed -e "s/\//$ngx_regex_dirsep/g"`
         echo "$line"                                              >> $MAKEFILE
     ;;
 
@@ -43,7 +43,7 @@ done=NO
 case $PLATFORM in
 
     win32)
-        zlib=`echo $ZLIB | sed -e "s/\//$DIRSEP/g"`
+        zlib=`echo $ZLIB | sed -e "s/\//$ngx_regex_dirsep/g"`
 
         cp auto/lib/zlib/$makefile $ZLIB
         echo "	cd $zlib"                                         >> $MAKEFILE
--- a/auto/make
+++ b/auto/make
@@ -8,353 +8,271 @@ mkdir -p $OBJS/src/core $OBJS/src/event 
          $OBJS/src/imap
 
 
-echo "CC = $CC"                                                   > $MAKEFILE
-echo "CPP = $CPP"                                                 >> $MAKEFILE
-echo "LINK = $LINK"                                               >> $MAKEFILE
-if [ "$CC" = wcl386 ]; then
-    echo MAKE = wmake                                             >> $MAKEFILE
-fi
-echo "CFLAGS = $CFLAGS"                                           >> $MAKEFILE
-echo                                                              >> $MAKEFILE
-
-
-if [ $MAKE_SL = YES ]; then
-    echo                                                          >> $MAKEFILE
-fi
-
-all_inc="$CORE_INCS $OBJS $HTTP_INCS $IMAP_INCS"
-all_inc=`echo " $all_inc" | sed -e "s/ \([^ ]\)/ $INCOPT\1/g"`
-all_inc=`echo $all_inc | sed -e "s/\//$DIRSEP/g"`
-
-echo "ALL_INCS = $all_inc"                                        >> $MAKEFILE
-echo                                                              >> $MAKEFILE
-
-all_srcs="$CORE_SRCS"
-
-
-# CORE_DEPS
-
-if [ $MAKE_SL = YES ]; then
-    echo $ngx_n "CORE_DEPS =" $ngx_c                              >> $MAKEFILE
-else
-    echo "CORE_DEPS = \\"                                         >> $MAKEFILE
-fi
-
-for dep in $CORE_DEPS
-do
-    dep=`echo $dep | sed -e "s/\//$DIRSEP/g"`
-
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n " $dep" $ngx_c                                >> $MAKEFILE
-    else
-        echo "	$dep \\"                                          >> $MAKEFILE
-    fi
-done
-echo                                                              >> $MAKEFILE
+ngx_objs_dir=$NGX_OBJS$ngx_regex_dirsep
+ngx_use_pch=`echo $NGX_USE_PCH | sed -e "s/\//$ngx_regex_dirsep/g"`
 
 
-# CORE_INCS
-
-if [ $MAKE_SL = YES ]; then
-    echo                                                          >> $MAKEFILE
-fi
-
-inc="$CORE_INCS $OBJS"
-inc=`echo " $inc" | sed -e "s/ \([^ ]\)/ $INCOPT\1/g" -e "s/\//$DIRSEP/g"`
-
-echo "CORE_INCS = $inc"                                           >> $MAKEFILE
-echo                                                              >> $MAKEFILE
+cat << END                                                    >> $NGX_MAKEFILE
 
-
-if [ $HTTP = YES ]; then
-
-    all_srcs="$all_srcs $HTTP_SRCS"
-
-    # HTTP_DEPS
-
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n "HTTP_DEPS =" $ngx_c                          >> $MAKEFILE
-    else
-        echo "HTTP_DEPS = \\"                                     >> $MAKEFILE
-    fi
+CC =	$CC
+CFLAGS = $CFLAGS
+CPP =	$CPP
+LINK =	$LINK
 
-    for dep in $HTTP_DEPS
-    do
-        dep=`echo $dep | sed -e "s/\//$DIRSEP/g"`
-
-        if [ $MAKE_SL = YES ]; then
-            echo $ngx_n " $dep" $ngx_c                            >> $MAKEFILE
-        else
-            echo "	$dep \\"                                  >> $MAKEFILE
-        fi
-    done
-    echo                                                          >> $MAKEFILE
+END
 
-
-    # HTTP_INCS
+if [ "$CC" = wcl386 ]; then
+    echo MAKE = wmake                                         >> $NGX_MAKEFILE
 
-    if [ $MAKE_SL = YES ]; then
-        echo                                                      >> $MAKEFILE
-    fi
-
-    inc="$HTTP_INCS $OBJS"
-    inc=`echo " $inc" | sed -e "s/ \([^ ]\)/ $INCOPT\1/g" -e "s/\//$DIRSEP/g"`
-
-    echo "HTTP_INCS = $inc"                                       >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
+    ngx_regex_cont=' '
+    ngx_cont=' '
+    ngx_tab=' '
 
 fi
 
 
+# ALL_INCS, required by OpenWatcom C precompiled headers
+
+ngx_incs=`echo $CORE_INCS $OBJS $HTTP_INCS $IMAP_INCS\
+    | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
+          -e "s/\//$ngx_regex_dirsep/g"`
+
+cat << END                                                    >> $NGX_MAKEFILE
+
+ALL_INCS = $ngx_include_opt$ngx_incs
+
+END
+
+
+ngx_all_srcs="$CORE_SRCS"
+
+
+# the core dependences and include pathes
+
+ngx_deps=`echo $CORE_DEPS $NGX_PCH \
+    | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
+          -e "s/\//$ngx_regex_dirsep/g"`
+
+ngx_incs=`echo $CORE_INCS $OBJS \
+    | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
+          -e "s/\//$ngx_regex_dirsep/g"`
+
+cat << END                                                    >> $NGX_MAKEFILE
+
+CORE_DEPS = $ngx_deps
+
+
+CORE_INCS = $ngx_include_opt$ngx_incs
+
+END
+
+
+# the http dependences and include pathes
+
+if [ $HTTP = YES ]; then
+
+    ngx_all_srcs="$ngx_all_srcs $HTTP_SRCS"
+
+    ngx_deps=`echo $HTTP_DEPS \
+        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
+              -e "s/\//$ngx_regex_dirsep/g"`
+
+    ngx_incs=`echo $HTTP_INCS \
+        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
+              -e "s/\//$ngx_regex_dirsep/g"`
+
+    cat << END                                                >> $NGX_MAKEFILE
+
+HTTP_DEPS = $ngx_deps
+
+
+HTTP_INCS = $ngx_include_opt$ngx_incs
+
+END
+
+fi
+
+
+# the imap dependences and include pathes
+
 if [ $IMAP = YES ]; then
 
-    all_srcs="$all_srcs $IMAP_SRCS"
-
-    # IMAP_DEPS
+    ngx_all_srcs="$ngx_all_srcs $IMAP_SRCS"
 
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n "IMAP_DEPS =" $ngx_c                          >> $MAKEFILE
-    else
-        echo "IMAP_DEPS = \\"                                     >> $MAKEFILE
-    fi
+    ngx_deps=`echo $IMAP_DEPS \
+        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
+              -e "s/\//$ngx_regex_dirsep/g"`
 
-    for dep in $IMAP_DEPS
-    do
-        dep=`echo $dep | sed -e "s/\//$DIRSEP/g"`
+    ngx_incs=`echo $IMAP_INCS \
+        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
+              -e "s/\//$ngx_regex_dirsep/g"`
 
-        if [ $MAKE_SL = YES ]; then
-            echo $ngx_n " $dep" $ngx_c                            >> $MAKEFILE
-        else
-            echo "	$dep \\"                                  >> $MAKEFILE
-        fi
-    done
-    echo                                                          >> $MAKEFILE
+    cat << END                                                >> $NGX_MAKEFILE
+
+IMAP_DEPS = $ngx_deps
 
 
-    # IMAP_INCS
-
-    if [ $MAKE_SL = YES ]; then
-        echo                                                      >> $MAKEFILE
-    fi
+IMAP_INCS = $ngx_include_opt$ngx_incs
 
-    inc="$IMAP_INCS $OBJS"
-    inc=`echo " $inc" | sed -e "s/ \([^ ]\)/ $INCOPT\1/g" -e "s/\//$DIRSEP/g"`
-
-    echo "IMAP_INCS = $inc"                                       >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
+END
 
 fi
 
 
 # nginx
 
-if [ $MAKE_SL = YES ]; then
-    echo $ngx_n "nginx$BINEXT:	" $ngx_c                          >> $MAKEFILE
-else
-    echo "nginx$BINEXT: \\"                                       >> $MAKEFILE
-fi
-
-
-# nginx deps
-
-for src in $all_srcs
-do
-    obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\.S\$/.$OBJEXT/"`
-    obj=`echo $OBJS/$obj | sed -e "s/\//$DIRSEP/g"`
+ngx_all_srcs=`echo $ngx_all_srcs | sed -e "s/\//$ngx_regex_dirsep/g"`
+ngx_modules_c=`echo $NGX_MODULES_C | sed -e "s/\//$ngx_regex_dirsep/g"`
 
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n " $obj" $ngx_c                                >> $MAKEFILE
-    else
-        echo "	$obj \\"                                          >> $MAKEFILE
-    fi
-done
+ngx_all_objs=`echo $ngx_all_srcs \
+    | sed -e "s/\([^ ]*\.\)cpp/$NGX_OBJS\/\1$ngx_objext/g" \
+          -e "s/\([^ ]*\.\)cc/$NGX_OBJS\/\1$ngx_objext/g" \
+          -e "s/\([^ ]*\.\)c/$NGX_OBJS\/\1$ngx_objext/g" \
+          -e "s/\([^ ]*\.\)S/$NGX_OBJS\/\1$ngx_objext/g"`
 
-for src in $NGX_MODULES_C $LINK_DEPS
-do
-    obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/"`
-    obj=`echo $obj | sed -e "s/\//$DIRSEP/g"`
+ngx_modules_obj=`echo $ngx_modules_c | sed -e "s/\(.*\.\)c/\1$ngx_objext/"`
 
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n " $obj" $ngx_c                                >> $MAKEFILE
-    else
-        echo "	$obj \\"                                          >> $MAKEFILE
-    fi
-done
-echo                                                              >> $MAKEFILE
-
-
-# nginx build
+ngx_deps=`echo $ngx_all_objs $ngx_modules_obj $LINK_DEPS \
+    | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
+          -e "s/\//$ngx_regex_dirsep/g"`
 
-if [ $MAKE_SL = YES ]; then
-    echo $ngx_n "	\$(LINK) ${BINOUT}nginx" $ngx_c           >> $MAKEFILE
-else
-    echo "	\$(LINK) ${BINOUT}nginx \\"                       >> $MAKEFILE
-fi
-
+ngx_objs=`echo $ngx_all_objs $ngx_modules_obj \
+    | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
+          -e "s/\//$ngx_regex_dirsep/g"`
 
-# nginx build sources
-
-for src in $all_srcs
-do
-    obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\.S\$/.$OBJEXT/"`
-    obj=`echo $OBJS/$obj | sed -e "s/\//$DIRSEP/g"`
+ngx_libs=${CORE_LIBS:+`echo $NGX_LD_OPT $CORE_LIBS \
+    | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_regex_cont/"`}
 
-    if [ $MAKE_SL = YES ]; then
-        echo $ngx_n " $obj" $ngx_c                                >> $MAKEFILE
-    else
-        echo "	$obj \\"                                          >> $MAKEFILE
-    fi
-done
+ngx_link=${CORE_LINK:+`echo $CORE_LINK \
+    | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_regex_cont/"`}
 
-
-# nginx build ngx_modules.c and libs
+cat << END                                                    >> $NGX_MAKEFILE
 
-obj=`echo $NGX_MODULES_C | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\//$DIRSEP/g"`
-src=`echo $NGX_MODULES_C | sed -e "s/\//$DIRSEP/g"`
-libs=`echo $CORE_LIBS | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\//$DIRSEP/g"`
-link=`echo $CORE_LINK | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\//$DIRSEP/g"`
+$NGX_OBJS${ngx_dirsep}nginx${ngx_binext}:	$ngx_deps$ngx_spacer
+	\$(LINK) ${ngx_binout}$NGX_OBJS${ngx_dirsep}nginx$ngx_cont$ngx_objs$ngx_libs$ngx_link
 
-if [ $MAKE_SL = YES ]; then
-    echo " $obj $libs $CORE_LINK"                                 >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
-else
-    echo "	$obj \\"                                          >> $MAKEFILE
-    echo "	$libs \\"                                         >> $MAKEFILE
-    echo "	$link"                                            >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
-fi
+END
 
 
 # ngx_modules.c
 
-deps="\$(CORE_DEPS)"
-
-if [ $PCH != NO ]; then
-    args="\$(CFLAGS) $USEPCH \$(ALL_INCS)"
+if test -n "$NGX_PCH"; then
+    ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
 else
-    args="\$(CFLAGS) $USEPCH \$(CORE_INCS)"
-fi
-
-if [ $MAKE_SL = YES ]; then
-    echo "$obj: $NGX_MODULES_C $deps"                             >> $MAKEFILE
-    echo $ngx_n "	\$(CC) $COMPOPT $args" $ngx_c             >> $MAKEFILE
-    echo " $OBJOUT$obj $src"                                      >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
-else
-    echo "$obj: \\"                                               >> $MAKEFILE
-    echo "	$NGX_MODULES_C $deps"                             >> $MAKEFILE
-    echo "	\$(CC) $COMPOPT $args \\"                         >> $MAKEFILE
-    echo "		$OBJOUT$obj \\"                           >> $MAKEFILE
-    echo "		$src"                                     >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
+    ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS)"
 fi
 
+cat << END                                                    >> $NGX_MAKEFILE
 
-# core sources
+$ngx_modules_obj:	\$(CORE_DEPS)$ngx_cont$ngx_modules_c
+	$ngx_cc$ngx_tab$ngx_objout$ngx_modules_obj$ngx_tab$ngx_modules_c
 
-for src in $CORE_SRCS
+END
+
+
+# the core sources
+
+for ngx_src in $CORE_SRCS
 do
-    obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/" -e "s/\.S\$/.$OBJEXT/"`
-    obj=`echo $OBJS/$obj | sed -e "s/\//$DIRSEP/g"`
-    src=`echo $src | sed -e "s/\//$DIRSEP/g"`
 
-    if [ $MAKE_SL = YES ]; then
-        echo "$obj: $src $deps"                                   >> $MAKEFILE
-        echo "	\$(CC) $COMPOPT $args $OBJOUT$obj $src"           >> $MAKEFILE
-        echo                                                      >> $MAKEFILE
-    else
-        echo "$obj: \\"                                           >> $MAKEFILE
-        echo "	$src $deps"                                       >> $MAKEFILE
-        echo "	\$(CC) $COMPOPT $args \\"                         >> $MAKEFILE
-        echo "		$OBJOUT$obj \\"                           >> $MAKEFILE
-        echo "		$src"                                     >> $MAKEFILE
-        echo                                                      >> $MAKEFILE
-    fi
+    ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
+    ngx_obj=`echo $ngx_src \
+        | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
+              -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
+              -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
+              -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+
+    cat << END                                                >> $NGX_MAKEFILE
+
+$ngx_obj:	\$(CORE_DEPS)$ngx_cont$ngx_src
+	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src
+
+END
+
 done
 
 
-# http sources
+# the http sources
 
 if [ $HTTP = YES ]; then
 
-    deps="\$(CORE_DEPS) \$(HTTP_DEPS)"
-
-    if [ $PCH != NO ]; then
-        args="\$(CFLAGS) $USEPCH \$(ALL_INCS)"
+    if test -n "$NGX_PCH"; then
+        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
     else
-        args="\$(CFLAGS) $USEPCH \$(CORE_INCS) \$(HTTP_INCS)"
+        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(HTTP_INCS)"
     fi
 
-    for src in $HTTP_SRCS
+    for ngx_src in $HTTP_SRCS
     do
-        obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/"`
-        obj=`echo $OBJS/$obj | sed -e "s/\//$DIRSEP/g"`
-        src=`echo $src | sed -e "s/\//$DIRSEP/g"`
 
-        if [ $MAKE_SL = YES ]; then
-            echo "$obj: $src $deps"                               >> $MAKEFILE
-            echo "	\$(CC) $COMPOPT $args $OBJOUT$obj $src"   >> $MAKEFILE
-            echo                                                  >> $MAKEFILE
-        else
-            echo "$obj: \\"                                       >> $MAKEFILE
-            echo "	$src $deps"                               >> $MAKEFILE
-            echo "	\$(CC) $COMPOPT $args \\"                 >> $MAKEFILE
-            echo "		$OBJOUT$obj \\"                   >> $MAKEFILE
-            echo "		$src"                             >> $MAKEFILE
-            echo                                                  >> $MAKEFILE
-        fi
-    done
+        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
+        ngx_obj=`echo $ngx_src \
+            | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+
+        cat << END                                            >> $NGX_MAKEFILE
+
+$ngx_obj:	\$(CORE_DEPS) \$(HTTP_DEPS)$ngx_cont$ngx_src
+	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src
+
+END
+
+     done
 
 fi
 
 
-# imap sources
+# the imap sources
 
 if [ $IMAP = YES ]; then
 
-    deps="\$(CORE_DEPS) \$(IMAP_DEPS)"
-
-    if [ $PCH != NO ]; then
-        args="\$(CFLAGS) $USEPCH \$(ALL_INCS)"
+    if test -n "$NGX_PCH"; then
+        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
     else
-        args="\$(CFLAGS) $USEPCH \$(CORE_INCS) \$(IMAP_INCS)"
+        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(IMAP_INCS)"
     fi
 
-    for src in $IMAP_SRCS
+    for ngx_src in $IMAP_SRCS
     do
-        obj=`echo $src | sed -e "s/\.c\$/.$OBJEXT/"`
-        obj=`echo $OBJS/$obj | sed -e "s/\//$DIRSEP/g"`
-        src=`echo $src | sed -e "s/\//$DIRSEP/g"`
 
-        if [ $MAKE_SL = YES ]; then
-            echo "$obj: $src $deps"                               >> $MAKEFILE
-            echo "	\$(CC) $COMPOPT $args $OBJOUT$obj $src"   >> $MAKEFILE
-            echo                                                  >> $MAKEFILE
-        else
-            echo "$obj: \\"                                       >> $MAKEFILE
-            echo "	$src $deps"                               >> $MAKEFILE
-            echo "	\$(CC) $COMPOPT $args \\"                 >> $MAKEFILE
-            echo "		$OBJOUT$obj \\"                   >> $MAKEFILE
-            echo "		$src"                             >> $MAKEFILE
-            echo                                                  >> $MAKEFILE
-        fi
-    done
+        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
+        ngx_obj=`echo $ngx_src \
+            | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
+                  -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+
+        cat << END                                            >> $NGX_MAKEFILE
+
+$ngx_obj:	\$(CORE_DEPS) \$(IMAP_DEPS)$ngx_cont$ngx_src
+	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src
+
+END
+
+     done
 
 fi
 
 
-# precompiled headers
+# the precompiled headers
 
-if [ $PCH != NO ]; then
-    echo "#include <ngx_config.h>" > $OBJS/pch.c
+if test -n "$NGX_PCH"; then
+    echo "#include <ngx_config.h>" > $OBJS/ngx_pch.c
+
+    ngx_pch="src/core/ngx_config.h $OS_CONFIG $OBJS/ngx_auto_config.h"
+    ngx_pch=`echo "$NGX_PCH:	$ngx_pch" | sed -e "s/\//$ngx_regex_dirsep/g"`
 
-    pch="$PCH:	src/core/ngx_config.h $OS_CONFIG $OBJS/ngx_auto_config.h"
-    pch=`echo $pch | sed -e "s/\//$DIRSEP/g"`
-    src="\$(CC) \$(CFLAGS) $BUILDPCH $COMPOPT \$(ALL_INCS)"
-    src="$src $OBJOUT$OBJS/pch.obj $OBJS/pch.c"
-    src=`echo $src | sed -e "s/\//$DIRSEP/g"`
+    ngx_src="\$(CC) \$(CFLAGS) $NGX_BUILD_PCH $ngx_compile_opt \$(ALL_INCS)"
+    ngx_src="$ngx_src $ngx_objout$OBJS/ngx_pch.obj $OBJS/ngx_pch.c"
+    ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
+
+    cat << END                                                >> $NGX_MAKEFILE
 
-    echo "$pch"                                                   >> $MAKEFILE
-    echo "	$src"                                             >> $MAKEFILE
-    echo                                                          >> $MAKEFILE
+$ngx_pch
+	$ngx_src
+
+END
+
 fi
--- a/auto/options
+++ b/auto/options
@@ -1,21 +1,25 @@
 
 # Copyright (C) Igor Sysoev
 
+
 help=no
 
 PREFIX=
 SBIN_PATH=
 CONF_PATH=
-HTTP_LOG_PATH=
 ERROR_LOG_PATH=
 PID_PATH=
+NGX_USER=
+NGX_GROUP=
+HTTP_LOG_PATH=
 
-CC=gcc
+CC=${CC:-gcc}
 CPP=
-OBJS=objs
+NGX_OBJS=objs
 
 DEBUG=NO
-CC_OPT=
+NGX_CC_OPT=
+NGX_LD_OPT=
 CPU=NO
 
 TEST_BUILD_DEVPOLL=NO
@@ -77,10 +81,12 @@ do
         --conf-path=*)                   CONF_PATH="$value"         ;;
         --error-log-path=*)              ERROR_LOG_PATH="$value"    ;;
         --pid-path=*)                    PID_PATH="$value"          ;;
+        --user=*)                        NGX_USER="$value"          ;;
+        --group=*)                       NGX_GROUP="$value"         ;;
 
         --crossbuild=*)                  PLATFORM="$value"          ;;
 
-        --builddir=*)                    OBJS="$value"              ;;
+        --builddir=*)                    NGX_OBJS="$value"          ;;
 
         --with-rtsig_module)             EVENT_RTSIG=YES            ;;
         --with-select_module)            EVENT_SELECT=YES           ;;
@@ -109,7 +115,8 @@ do
 
         --with-cc=*)                     CC="$value"                ;;
         --with-cpp=*)                    CPP="$value"               ;;
-        --with-cc-opt=*)                 CC_OPT="$value"            ;;
+        --with-cc-opt=*)                 NGX_CC_OPT="$value"        ;;
+        --with-ld-opt=*)                 NGX_LD_OPT="$value"        ;;
         --with-cpu-opt=*)                CPU="$value"               ;;
         --with-debug)                    DEBUG=YES                  ;;
 
@@ -140,24 +147,35 @@ done
 
 
 if [ $help = yes ]; then
-    echo
-    echo "  --help                         this message"
-    echo
+
+cat << END
+
+  --help                           this message
 
-    echo "  --without-select_module        disable select_module"
-    echo "  --without-poll_module          disable poll_module"
+  --user=USER                      set non-privilege user
+                                   for the worker processes
+  --group=GROUP                    set non-privilege group
+                                   for the worker processes
+
+  --with-select_module             enable select module
+  --without-select_module          disable select module
+  --with-poll_module               enable poll module
+  --without-poll_module            disable poll module
 
-    echo "  --without-http_rewrite_module  disable http_rewrite_module"
-    echo "  --without-http_gzip_module     disable http_gzip_module"
-    echo "  --without-http_proxy_module    disable http_proxy_module"
+  --without-http_charset_module    disable ngx_http_charset_module
+  --without-http_rewrite_module    disable ngx_http_rewrite_module
+  --without-http_gzip_module       disable ngx_http_gzip_module
+  --without-http_proxy_module      disable ngx_http_proxy_module
 
-    echo "  --with-cc=NAME                 name of or path to C compiler"
-    echo
+  --with-cc-opt=OPTIONS            additional options for compiler
+  --with-ld-opt=OPTIONS            additional options for linker
 
-    echo "  --with-pcre=DIR                path to PCRE library"
-    echo "  --with-md5=DIR                 path to md5 library"
-    echo "  --with-zlib=DIR                path to zlib library"
-    echo
+  --with-pcre=DIR                  path to PCRE library
+  --with-md5=DIR                   path to md5 library
+  --with-zlib=DIR                  path to zlib library
+  --with-openssl=DIR               path to OpenSSL library
+
+END
 
     exit 1
 fi
@@ -181,11 +199,22 @@ if [ ".$PLATFORM" = ".win32" -a $EVENT_P
 fi
 
 
-if [ ".$PREFIX" = "." ]; then
+if test -z "$PREFIX"; then
     PREFIX=/usr/local/nginx
 fi
 
 
+if test -z "$NGX_GROUP"; then
+    NGX_GROUP=NGX_USER
+fi
+
+
+if test -z "$NGX_USER"; then
+    NGX_USER=nobody
+    NGX_GROUP=nobody
+fi
+
+
 case ".$SBIN_PATH" in
     ./*)
     ;;
--- a/auto/os/conf
+++ b/auto/os/conf
@@ -31,21 +31,7 @@ case $PLATFORM in
     ;;
 
     win32)
-        CORE_INCS="$WIN32_INCS"
-        CORE_DEPS="$WIN32_DEPS"
-        CORE_SRCS="$WIN32_SRCS $IOCP_SRCS"
-        OS_CONFIG="$WIN32_CONFIG"
-        EVENT_MODULES="$EVENT_MODULES $IOCP_MODULE"
-        EVENT_FOUND=YES
-
-        if [ $EVENT_SELECT = NO ]; then
-            CORE_SRCS="$CORE_SRCS $SELECT_SRCS"
-            EVENT_MODULES="$EVENT_MODULES $SELECT_MODULE"
-        fi
-
-        have=HAVE_AIO . auto/have
-        have=HAVE_IOCP . auto/have
-        CORE_LIBS="$CORE_LIBS ws2_32.lib"
+        . auto/os/win32
     ;;
 
     *)
@@ -55,3 +41,56 @@ case $PLATFORM in
     ;;
 
 esac
+
+
+if [ $PLATFORM != win32 ]; then
+
+    ngx_feature="/dev/poll"
+    ngx_feature_name="devpoll"
+    ngx_feature_run=no
+    ngx_feature_incs="#include <sys/devpoll.h>"
+    ngx_feature_libs=
+    ngx_feature_test="int  n, dp; struct dvpoll  dvp;
+                      dp = 0;
+                      dvp.dp_fds = NULL;
+                      dvp.dp_nfds = 0;
+                      dvp.dp_timeout = 0;
+                      n = ioctl(dp, DP_POLL, &dvp)"
+    . auto/feature
+
+    if [ $ngx_found = yes ]; then
+        have=HAVE_DEVPOLL . auto/have
+        CORE_SRCS="$CORE_SRCS $DEVPOLL_SRCS"
+        EVENT_MODULES="$EVENT_MODULES $DEVPOLL_MODULE"
+        EVENT_FOUND=YES
+    fi
+
+
+    if test -z "$NGX_KQUEUE_CHECKED"; then
+        ngx_feature="kqueue"
+        ngx_feature_name="kqueue"
+        ngx_feature_run=no
+        ngx_feature_incs="#include <sys/event.h>"
+        ngx_feature_libs=
+        ngx_feature_test="int kq; kq = kqueue()"
+        . auto/feature
+
+        if [ $ngx_found = yes ]; then
+
+            have=HAVE_KQUEUE . auto/have
+            have=HAVE_CLEAR_EVENT . auto/have
+            EVENT_MODULES="$EVENT_MODULES $KQUEUE_MODULE"
+            CORE_SRCS="$CORE_SRCS $KQUEUE_SRCS"
+            EVENT_FOUND=YES
+
+            ngx_feature="kqueue's NOTE_LOWAT"
+            ngx_feature_name="HAVE_LOWAT_EVENT"
+            ngx_feature_run=no
+            ngx_feature_incs="#include <sys/event.h>"
+            ngx_feature_libs=
+            ngx_feature_test="struct kevent  kev;
+                              kev.fflags = NOTE_LOWAT;"
+            . auto/feature
+        fi
+    fi
+fi
--- a/auto/os/freebsd
+++ b/auto/os/freebsd
@@ -2,19 +2,33 @@
 # Copyright (C) Igor Sysoev
 
 
+cat << END >> $NGX_AUTO_HEADERS_H
+
+#ifndef NGX_FREEBSD
+#define NGX_FREEBSD  1
+#endif
+
+END
+
+
 CORE_INCS="$UNIX_INCS"
 CORE_DEPS="$UNIX_DEPS $FREEBSD_DEPS"
 CORE_SRCS="$UNIX_SRCS $FREEBSD_SRCS"
 
-PIPE="-pipe"
+ngx_spacer='
+'
 
 
-# __FreeBSD_version is the best way to determine whether
-# some capability exists and is safe to use
+# __FreeBSD_version and sysctl kern.osreldate are the best ways
+# to determine whether some capability exists and is safe to use.
+# __FreeBSD_version is used for the testing of the build enviroment.
+# sysctl kern.osreldate is used for the testing of the kernel capabilities.
 
 version=`grep "#define __FreeBSD_version" /usr/include/osreldate.h \
          | sed -e 's/^.* \(.*\)$/\1/'`
 
+osreldate=`/sbin/sysctl -n kern.osreldate`
+
 
 # setproctitle() in libutil
 
@@ -28,7 +42,7 @@ fi
 
 # sendfile
 
-if [ $version -gt 300007 ]; then
+if [ $osreldate -gt 300007 ]; then
     echo " + using sendfile()"
 
     have=HAVE_SENDFILE . auto/have
@@ -38,8 +52,8 @@ fi
 
 # kqueue
 
-if [ \( $version -lt 500000 -a $version -ge 410000 \) \
-     -o $version -ge 500011 ]
+if [ \( $osreldate -lt 500000 -a $osreldate -ge 410000 \) \
+     -o $osreldate -ge 500011 ]
 then
     echo " + using kqueue"
 
@@ -50,6 +64,8 @@ then
     EVENT_FOUND=YES
 fi
 
+NGX_KQUEUE_CHECKED=YES
+
 
 # kqueue's NOTE_LAWAT
 
--- a/auto/os/linux
+++ b/auto/os/linux
@@ -2,15 +2,23 @@
 # Copyright (C) Igor Sysoev
 
 
+cat << END >> $NGX_AUTO_HEADERS_H
+
+#ifndef NGX_LINUX
+#define NGX_LINUX  1
+#endif
+
+END
+
 CORE_INCS="$UNIX_INCS"
 CORE_DEPS="$UNIX_DEPS $LINUX_DEPS"
 CORE_SRCS="$UNIX_SRCS $LINUX_SRCS"
 EVENT_MODULES="$EVENT_MODULES"
 
-PIPE="-pipe"
+ngx_spacer='
+'
 
-
-CC_TEST_FLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"
+CC_AUX_FLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64"
 
 # Linux kernel version
 
@@ -31,14 +39,18 @@ fi
 
 # epoll, EPOLLET version
 
-ngx_func="epoll";
-ngx_func_inc="#include <sys/epoll.h>"
-ngx_func_test="int efd = 0, fd = 1, n;
-               struct epoll_event ee;
-               ee.events = EPOLLIN|EPOLLOUT|EPOLLET;
-               ee.data.ptr = NULL;
-               n = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ee)"
-. auto/func
+ngx_feature="epoll"
+ngx_feature_name="epoll"
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/epoll.h>"
+ngx_feature_libs=
+ngx_feature_test="int efd = 0, fd = 1, n;
+                  struct epoll_event ee;
+                  ee.events = EPOLLIN|EPOLLOUT|EPOLLET;
+                  ee.data.ptr = NULL;
+                  efd = epoll_create(100);
+                  if (efd == -1) return 1;"
+. auto/feature
 
 if [ $ngx_found = yes ]; then
     have=HAVE_EPOLL . auto/have
@@ -51,13 +63,16 @@ fi
 
 # sendfile()
 
-CC_TEST_FLAGS="-D_GNU_SOURCE"
-ngx_func="sendfile()";
-ngx_func_inc="#include <sys/sendfile.h>"
-ngx_func_test="int s = 0, fd = 1;
-               ssize_t n; off_t off = 0;
-               n = sendfile(s, fd, &off, 1)"
-. auto/func
+CC_AUX_FLAGS="-D_GNU_SOURCE"
+ngx_feature="sendfile()"
+ngx_feature_name="sendfile"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/sendfile.h>"
+ngx_feature_libs=
+ngx_feature_test="int s = 0, fd = 1;
+                  ssize_t n; off_t off = 0;
+                  n = sendfile(s, fd, &off, 1)"
+. auto/feature
 
 if [ $ngx_found = yes ]; then
     CORE_SRCS="$CORE_SRCS $LINUX_SENDFILE_SRCS"
@@ -66,17 +81,26 @@ fi
 
 # sendfile64()
 
-CC_TEST_FLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"
-ngx_func="sendfile64()"; . auto/func
+CC_AUX_FLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64"
+ngx_feature="sendfile64()"
+ngx_feature_name="HAVE_SENDFILE64"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/sendfile.h>"
+ngx_feature_libs=
+ngx_feature_test="int s = 0, fd = 1;
+                  ssize_t n; off_t off = 0;
+                  n = sendfile(s, fd, &off, 1)"
+. auto/feature
 
 
+ngx_include="sys/prctl.h"; . auto/include
+
 # prctl(PR_SET_DUMPABLE)
 
-ngx_func="prctl()";
-ngx_func_inc="#include <sys/prctl.h>"
-ngx_func_test="prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)"
-. auto/func
-
-if [ $ngx_found = yes ]; then
-    have=HAVE_PR_SET_DUMPABLE . auto/have
-fi
+ngx_feature="prctl(PR_SET_DUMPABLE)"
+ngx_feature_name="HAVE_PR_SET_DUMPABLE"
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/prctl.h>"
+ngx_feature_libs=
+ngx_feature_test="if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) return 1"
+. auto/feature
--- a/auto/os/solaris
+++ b/auto/os/solaris
@@ -2,6 +2,14 @@
 # Copyright (C) Igor Sysoev
 
 
+cat << END >> $NGX_AUTO_HEADERS_H
+
+#ifndef NGX_SOLARIS
+#define NGX_SOLARIS  1
+#endif
+
+END
+
 CORE_INCS="$UNIX_INCS"
 CORE_DEPS="$UNIX_DEPS $SOLARIS_DEPS"
 CORE_SRCS="$UNIX_SRCS $SOLARIS_SRCS "
@@ -9,40 +17,17 @@ EVENT_MODULES="$EVENT_MODULES"
 
 CORE_LIBS="$CORE_LIBS -lsocket -lnsl -lrt"
 
-# the Solaris's make support
-MAKE_SL=YES
-
-
-CC_TEST_FLAGS="-D_FILE_OFFSET_BITS=64"
-
-case $PLATFORM in
+# Solaris's make does not support a blank line between target and rules
+ngx_spacer=
 
-    SunOS:5.[89]:* | SunOS:5.10:*)
-        PIPE="-pipe"
-    ;;
-
-    *)
-        # Solaris 7's /usr/ccs/bin/as does not support "-pipe"
-    ;;
-
-esac
+CC_AUX_FLAGS="-D_FILE_OFFSET_BITS=64 -lrt"
 
 
 case $PLATFORM in
 
     *:sun4u)
+        # "-mcpu=v9" enables the "casa" assembler instruction
         CFLAGS="$CFLAGS -mcpu=v9"
-
-        if [ ".$CPU" = ".sparc64" ]; then
-            CFLAGS="$CFLAGS -m64"
-            CPU_OPT="-m64"
-            CORE_LINK="$CORE_LINK -m64"
-
-            CC_TEST_FLAGS="$CC_TEST_FLAGS -mcpu=v9 -m64"
-        fi
-    ;;
-
-    *)
     ;;
 
 esac
@@ -57,23 +42,15 @@ if [ $ZLIB_ASM != NO ]; then
 fi
 
 
-ngx_inc="sys/devpoll.h"; . auto/inc
-
-if [ $ngx_found = yes ]; then
-    have=HAVE_DEVPOLL . auto/have
-    CORE_SRCS="$CORE_SRCS $DEVPOLL_SRCS"
-    EVENT_MODULES="$EVENT_MODULES $DEVPOLL_MODULE"
-    EVENT_FOUND=YES
-fi
-
-
-ngx_func="sendfilev()";
-ngx_func_inc="#include <sys/sendfile.h>"
-ngx_func_libs="-lsendfile"
-ngx_func_test="int fd = 1; sendfilevec_t vec[1];
-               size_t sent; ssize_t n;
-               n = sendfilev(fd, vec, 1, &sent)"
-. auto/func
+ngx_feature="sendfilev()"
+ngx_feature_name="sendfile"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/sendfile.h>"
+ngx_feature_libs="-lsendfile"
+ngx_feature_test="int fd = 1; sendfilevec_t vec[1];
+                  size_t sent; ssize_t n;
+                  n = sendfilev(fd, vec, 1, &sent)"
+. auto/feature
 
 
 if [ $ngx_found = yes ]; then
new file mode 100644
--- /dev/null
+++ b/auto/os/win32
@@ -0,0 +1,29 @@
+
+# Copyright (C) Igor Sysoev
+
+
+cat << END >> $NGX_AUTO_HEADERS_H
+
+#ifndef NGX_WIN32
+#define NGX_WIN32  1
+#endif
+
+END
+
+
+CORE_INCS="$WIN32_INCS"
+CORE_DEPS="$WIN32_DEPS"
+CORE_SRCS="$WIN32_SRCS $IOCP_SRCS"
+OS_CONFIG="$WIN32_CONFIG"
+CORE_LIBS="$CORE_LIBS ws2_32.lib"
+
+EVENT_MODULES="$EVENT_MODULES $IOCP_MODULE"
+EVENT_FOUND=YES
+
+if [ $EVENT_SELECT = NO ]; then
+    CORE_SRCS="$CORE_SRCS $SELECT_SRCS"
+    EVENT_MODULES="$EVENT_MODULES $SELECT_MODULE"
+fi
+
+have=HAVE_AIO . auto/have
+have=HAVE_IOCP . auto/have
--- a/auto/sources
+++ b/auto/sources
@@ -303,6 +303,6 @@ IMAP_DEPS="src/imap/ngx_imap.h"
 
 IMAP_MODULE=ngx_imap_module
 IMAP_SRCS="src/imap/ngx_imap.c \
-	   src/imap/ngx_imap_handler.c \
-	   src/imap/ngx_imap_parse.c \
-	   src/imap/ngx_imap_proxy.c"
+           src/imap/ngx_imap_handler.c \
+           src/imap/ngx_imap_parse.c \
+           src/imap/ngx_imap_proxy.c"
new file mode 100644
--- /dev/null
+++ b/auto/stubs
@@ -0,0 +1,8 @@
+
+# Copyright (C) Igor Sysoev
+
+
+have=NGX_USE_HTTP_FILE_CACHE_UNIQ . auto/have
+have=NGX_SUPPRESS_WARN . auto/have
+
+have=NGX_SMP . auto/have
--- a/auto/types/sizeof
+++ b/auto/types/sizeof
@@ -2,9 +2,14 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_type size ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_type size" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_type size ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for $ngx_type size
+
+END
 
 ngx_size=
 
@@ -12,11 +17,11 @@ cat << END > $NGX_AUTOTEST.c
 
 #include <sys/types.h>
 #include <sys/time.h>
-$NGX_UNISTD_H
+$NGX_INCLUDE_UNISTD_H
 #include <signal.h>
 #include <sys/resource.h>
-$NGX_INTTYPES_H
-$NGX_AUTO_CONFIG
+$NGX_INCLUDE_INTTYPES_H
+$NGX_INCLUDE_AUTO_CONFIG_H
 
 int main() {
     printf("%d", sizeof($ngx_type));
@@ -25,14 +30,19 @@ int main() {
 
 END
 
-eval "$CC $CC_TEST_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c >> $NGX_ERR 2>&1"
+
+ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c"
+eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
+
 
 if [ -x $NGX_AUTOTEST ]; then
     ngx_size=`$NGX_AUTOTEST`
     echo " $ngx_size bytes"
 fi
 
-rm $NGX_AUTOTEST*
+
+rm -f $NGX_AUTOTEST
+
 
 case $ngx_size in
     4)
@@ -58,5 +68,13 @@ case $ngx_size in
     *)
         echo
         echo "$0: error: can not detect $ngx_type size"
+
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+        cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+        echo $ngx_test       >> $NGX_AUTOCONF_ERR
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+
         exit 1
 esac
+
--- a/auto/types/typedef
+++ b/auto/types/typedef
@@ -2,13 +2,18 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for $ngx_type ..." $ngx_c
-echo >> $NGX_ERR
-echo "checking for $ngx_type" >> $NGX_ERR
+echo $ngx_n "checking for $ngx_type ...$ngx_c"
+
+cat << END >> $NGX_AUTOCONF_ERR
 
-found=no
+----------------------------------------
+checking for $ngx_type
 
-for type in $ngx_type $ngx_types
+END
+
+ngx_found=no
+
+for ngx_try in $ngx_type $ngx_types
 do
 
     cat << END > $NGX_AUTOTEST.c
@@ -19,42 +24,51 @@ do
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <netinet/in.h>
-$NGX_INTTYPES_H
+$NGX_INCLUDE_INTTYPES_H
 
 int main() {
-    $type i = 0;
+    $ngx_try i = 0;
     return 0;
 }
 
 END
 
-    eval "$CC -o $NGX_AUTOTEST $NGX_AUTOTEST.c >> $NGX_ERR 2>&1"
+    ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS -o $NGX_AUTOTEST $NGX_AUTOTEST.c"
+    eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
 
     if [ -x $NGX_AUTOTEST ]; then
-        if [ $type = $ngx_type ]; then
+        if [ $ngx_try = $ngx_type ]; then
             echo " found"
-            found=yes
+            ngx_found=yes
         else
-            echo ", $type used"
-            found=$type
+            echo ", $ngx_try used"
+            ngx_found=$ngx_try
         fi
     fi
 
-    rm $NGX_AUTOTEST*
+    rm -f $NGX_AUTOTEST
+
+    if [ $ngx_found = no ]; then
+        echo $ngx_n " $ngx_try not found$ngx_c"
 
-    if [ $found = no ]; then
-        echo $ngx_n " $type not found" $ngx_c
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+        cat $NGX_AUTOTEST.c  >> $NGX_AUTOCONF_ERR
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+        echo $ngx_test       >> $NGX_AUTOCONF_ERR
+        echo "----------"    >> $NGX_AUTOCONF_ERR
+
     else
         break
     fi
 done
 
-if [ $found = no ]; then
+if [ $ngx_found = no ]; then
     echo
     echo "$0: error: can not define $ngx_type"
+
     exit 1
 fi
 
-if [ $found != yes ]; then
-    echo "typedef $found  $ngx_type;"   >> $NGX_AUTO_CONFIG_H
+if [ $ngx_found != yes ]; then
+    echo "typedef $ngx_found  $ngx_type;"   >> $NGX_AUTO_CONFIG_H
 fi
--- a/auto/types/uintptr_t
+++ b/auto/types/uintptr_t
@@ -2,7 +2,7 @@
 # Copyright (C) Igor Sysoev
 
 
-echo $ngx_n "checking for uintptr_t ... " $ngx_c
+echo $ngx_n "checking for uintptr_t ...$ngx_c"
 echo >> $NGX_ERR
 echo "checking for uintptr_t" >> $NGX_ERR
 
--- a/auto/unix
+++ b/auto/unix
@@ -2,7 +2,7 @@
 # Copyright (C) Igor Sysoev
 
 
-CC_WARN=$CC
+CC_WARN=
 ngx_fmt_collect=yes
 
 # C types
@@ -23,7 +23,7 @@ eval ngx_formats=\${ngx_${ngx_ptr_size}_
 
 # POSIX types
 
-NGX_AUTO_CONFIG="#include \"../$NGX_AUTO_CONFIG_H\""
+NGX_INCLUDE_AUTO_CONFIG_H="#include \"../$NGX_AUTO_CONFIG_H\""
 
 ngx_type="uint64_t"; ngx_types="u_int64_t"; . auto/types/typedef
 
@@ -72,75 +72,98 @@ eval ngx_formats=\${ngx_${ngx_size}_fmt}
 
 # syscalls, libc calls and some features
 
+
+ngx_feature="pread()"
+ngx_feature_name="pread"
+ngx_feature_run=no
+ngx_feature_incs=
 ngx_feature_libs=
-ngx_func_libs=
-
-
-ngx_func="pread()"
-ngx_func_inc=
-ngx_func_test="char buf[1]; ssize_t n; n = pread(0, buf, 1, 0)"
-. auto/func
+ngx_feature_test="char buf[1]; ssize_t n; n = pread(0, buf, 1, 0)"
+. auto/feature
 
 
-ngx_func="pwrite()"
-ngx_func_inc=
-ngx_func_test="char buf[1]; ssize_t n; n = pwrite(1, buf, 1, 0)"
-. auto/func
+ngx_feature="pwrite()"
+ngx_feature_name="pwrite"
+ngx_feature_run=no
+ngx_feature_incs=
+ngx_feature_libs=
+ngx_feature_test="char buf[1]; ssize_t n; n = pwrite(1, buf, 1, 0)"
+. auto/feature
 
 
-#ngx_func="strsignal()"
-#ngx_func_inc="#include <string.h>"
-#ngx_func_test="char *s = strsignal(1)"
-#. auto/func
+ngx_feature="strerror_r()"
+ngx_feature_name="strerror_r"
+ngx_feature_run=no
+ngx_feature_incs="#include <string.h>"
+ngx_feature_libs=
+ngx_feature_test="char buf[20]; int n; n = strerror_r(1, buf, 20)"
+. auto/feature
 
 
-ngx_func="strerror_r()"
-ngx_func_inc="#include <string.h>"
-ngx_func_test="char buf[20]; int n; n = strerror_r(1, buf, 20)"
-. auto/func
+ngx_feature="gnu_strerror_r()"
+ngx_feature_name="gnu_strerror_r"
+ngx_feature_run=no
+ngx_feature_incs="#include <string.h>"
+ngx_feature_libs=
+ngx_feature_test="char buf[20], *str; str = strerror_r(1, buf, 20)"
+. auto/feature
 
 
-ngx_func="gnu_strerror_r()"
-ngx_func_inc="#include <string.h>"
-ngx_func_test="char buf[20], *str; str = strerror_r(1, buf, 20)"
-. auto/func
+ngx_feature="localtime_r()"
+ngx_feature_name="localtime_r"
+ngx_feature_run=no
+ngx_feature_incs="#include <time.h>"
+ngx_feature_libs=
+ngx_feature_test="struct tm t; time_t c=0; localtime_r(&c, &t)"
+. auto/feature
 
 
-ngx_func="localtime_r()"
-ngx_func_inc="#include <time.h>"
-ngx_func_test="struct tm t; time_t c=0; localtime_r(&c, &t)"
-. auto/func
+ngx_feature="posix_memalign()"
+ngx_feature_name="posix_memalign"
+ngx_feature_run=no
+ngx_feature_incs="#include <stdlib.h>"
+ngx_feature_libs=
+ngx_feature_test="void *p; int n; n = posix_memalign(&p, 4096, 4096)"
+. auto/feature
 
 
-ngx_func="posix_memalign()"
-ngx_func_inc="#include <stdlib.h>"
-ngx_func_test="void *p; int n; n = posix_memalign(&p, 4096, 4096)"
-. auto/func
+ngx_feature="memalign()"
+ngx_feature_name="memalign"
+ngx_feature_run=no
+ngx_feature_incs="#include <stdlib.h>"
+ngx_feature_libs=
+ngx_feature_test="void *p; p = memalign(4096, 4096)"
+. auto/feature
 
 
-ngx_func="memalign()"
-ngx_func_inc="#include <stdlib.h>"
-ngx_func_test="void *p; p = memalign(4096, 4096)"
-. auto/func
-
+ngx_feature="sched_yield()"
+ngx_feature_name="HAVE_SCHED_YIELD"
+ngx_feature_run=no
+ngx_feature_incs="#include <sched.h>"
+ngx_feature_libs=
+ngx_feature_test="sched_yield()"
+. auto/feature
 
 
 ngx_feature="mmap(MAP_ANON|MAP_SHARED)"
 ngx_feature_name="MAP_ANON"
-ngx_feature_inc="#include <sys/mman.h>"
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/mman.h>"
+ngx_feature_libs=
 ngx_feature_test="void *p;
                   p = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
                            MAP_ANON|MAP_SHARED, -1, 0);
                   if (p == MAP_FAILED) return 1;"
-ngx_feature_run=yes
 . auto/feature
 
 
 ngx_feature='mmap("/dev/zero", MAP_SHARED)'
 ngx_feature_name="MAP_DEVZERO"
-ngx_feature_inc="#include <sys/mman.h>
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/mman.h>
 #include <sys/stat.h>
 #include <fcntl.h>"
+ngx_feature_libs=
 ngx_feature_test='void *p; int  fd;
                   fd = open("/dev/zero", O_RDWR);
                   p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
@@ -150,8 +173,10 @@ ngx_feature_test='void *p; int  fd;
 
 ngx_feature="System V shared memory"
 ngx_feature_name="SYSVSHM"
-ngx_feature_inc="#include <sys/ipc.h>
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/ipc.h>
 #include <sys/shm.h>"
+ngx_feature_libs=
 ngx_feature_test="int  id;
                   id = shmget(IPC_PRIVATE, 4096, (SHM_R|SHM_W|IPC_CREAT));
                   if (id == -1) return 1;
@@ -159,41 +184,39 @@ ngx_feature_test="int  id;
 . auto/feature
 
 
-
 ngx_feature="struct sockaddr_in.sin_len"
 ngx_feature_name="sin_len"
-ngx_feature_inc="#include <sys/socket.h>
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/socket.h>
 #include <netinet/in.h>"
+ngx_feature_libs=
 ngx_feature_test="struct sockaddr_in sa; sa.sin_len = 5"
-ngx_feature_run=no
 . auto/feature
 
 
 ngx_feature="struct msghdr.msg_control"
 ngx_feature_name="msghdr_msg_control"
-ngx_feature_inc="#include <sys/socket.h>"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/socket.h>"
+ngx_feature_libs=
 ngx_feature_test="struct msghdr  msg; msg.msg_control = NULL"
 . auto/feature
 
 
-case $PLATFORM in
-    Linux:*)
-        ngx_feature_inc="#include <sys/ioctl.h>"
-    ;;
-
-    *)
-        ngx_feature_inc="#include <sys/filio.h>"
-    ;;
-esac
-
 ngx_feature="ioctl(FIONBIO)"
 ngx_feature_name="FIONBIO"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/ioctl.h>
+$NGX_INCLUDE_SYS_FILIO_H"
+ngx_feature_libs=
 ngx_feature_test="int i; i = FIONBIO"
 . auto/feature
 
 
 ngx_feature="struct tm.tm_gmtoff"
 ngx_feature_name="gmtoff"
-ngx_feature_inc="#include <time.h>"
+ngx_feature_run=no
+ngx_feature_incs="#include <time.h>"
+ngx_feature_libs=
 ngx_feature_test="struct tm  tm; tm.tm_gmtoff = 0"
 . auto/feature
--- a/conf/nginx.conf
+++ b/conf/nginx.conf
@@ -1,5 +1,5 @@
 
-user  nobody;
+#user  nobody;
 worker_processes  3;
 
 #error_log  logs/error.log;
--- a/configure
+++ b/configure
@@ -8,24 +8,22 @@
 . auto/sources
 
 test -d $OBJS || mkdir $OBJS
+
+echo > $NGX_AUTO_HEADERS_H
 echo > $NGX_AUTO_CONFIG_H
 
 if [ $DEBUG = YES ]; then
     have=NGX_DEBUG . auto/have
 fi
 
-have=NGX_USE_HTTP_FILE_CACHE_UNIQ . auto/have
-have=NGX_SUPPRESS_WARN . auto/have
-
 
 if [ "$PLATFORM" != win32 ]; then
     . auto/headers
 fi
 
+. auto/cc/conf
 . auto/os/conf
 . auto/modules
-
-. auto/cc
 . auto/lib/conf
 
 if [ "$PLATFORM" != win32 ]; then
@@ -40,7 +38,8 @@ if [ "$PLATFORM" != win32 ]; then
     . auto/unix
 fi
 
-have=NGX_SMP . auto/have
+# STUB
+. auto/stubs
 
 have=NGX_PREFIX value="\"$PREFIX/\"" . auto/define
 have=NGX_SBIN_PATH value="\"$SBIN_PATH\"" . auto/define
@@ -51,4 +50,7 @@ if [ ".$ERROR_LOG_PATH" != "." ]; then
 fi
 have=NGX_HTTP_LOG_PATH value="\"$HTTP_LOG_PATH\"" . auto/define
 
+have=NGX_USER value="\"$NGX_USER\"" . auto/define
+have=NGX_GROUP value="\"$NGX_GROUP\"" . auto/define
+
 . auto/summary
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -104,7 +104,7 @@ int main(int argc, char *const *argv, ch
     ngx_cycle_t      *cycle, init_cycle;
     ngx_core_conf_t  *ccf;
 
-#if defined __FreeBSD__
+#if (NGX_FREEBSD)
     ngx_debug_init();
 #endif
 
@@ -112,7 +112,7 @@ int main(int argc, char *const *argv, ch
 
     ngx_time_init();
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_regex_init();
 #endif
 
@@ -136,11 +136,11 @@ int main(int argc, char *const *argv, ch
         return 1;
     }
 
-    if (ngx_getopt(&init_cycle, argc, argv) == NGX_ERROR) {
+    if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
         return 1;
     }
 
-    if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
+    if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) {
         return 1;
     }
 
@@ -175,7 +175,7 @@ int main(int argc, char *const *argv, ch
     if (ngx_test_config) {
         ngx_log_error(NGX_LOG_INFO, log, 0,
                       "the configuration file %s was tested successfully",
-                      init_cycle.conf_file.data);
+                      cycle->conf_file.data);
         return 0;
     }
 
@@ -387,7 +387,7 @@ static ngx_int_t ngx_save_argv(ngx_cycle
 
     ngx_argc = argc;
 
-#if __FreeBSD__
+#if (NGX_FREEBSD)
 
     ngx_argv = (char **) argv;
 
@@ -462,28 +462,26 @@ static char *ngx_core_module_init_conf(n
 
 #if !(WIN32)
 
-#if 0
     if (ccf->user == (uid_t) NGX_CONF_UNSET) {
 
-        pwd = getpwnam("nobody");
+        pwd = getpwnam(NGX_USER);
         if (pwd == NULL) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getpwnam(\"nobody\") failed");
+                          "getpwnam(\"" NGX_USER "\") failed");
             return NGX_CONF_ERROR;
         }
 
         ccf->user = pwd->pw_uid;
 
-        grp = getgrnam("nobody");
+        grp = getgrnam(NGX_GROUP);
         if (grp == NULL) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getgrnam(\"nobody\") failed");
+                          "getgrnam(\"" NGX_GROUP "\") failed");
             return NGX_CONF_ERROR;
         }
 
         ccf->group = grp->gr_gid;
     }
-#endif
 
     if (ccf->pid.len == 0) {
         ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
@@ -522,6 +520,7 @@ static char *ngx_set_user(ngx_conf_t *cf
 
     ngx_core_conf_t  *ccf = conf;
 
+    char             *group;
     struct passwd    *pwd;
     struct group     *grp;
     ngx_str_t        *value;
@@ -530,6 +529,14 @@ static char *ngx_set_user(ngx_conf_t *cf
         return "is duplicate";
     }
 
+    if (geteuid() != 0) {
+        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+                           "the \"user\" directive makes sense only "
+                           "if the master process runs "
+                           "with super-user privileges, ignored");
+        return NGX_CONF_OK;
+    }
+
     value = (ngx_str_t *) cf->args->elts;
 
     pwd = getpwnam((const char *) value[1].data);
@@ -541,14 +548,12 @@ static char *ngx_set_user(ngx_conf_t *cf
 
     ccf->user = pwd->pw_uid;
 
-    if (cf->args->nelts == 2) {
-        return NGX_CONF_OK;
-    }
+    group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
 
-    grp = getgrnam((const char *) value[2].data);
+    grp = getgrnam(group);
     if (grp == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
-                           "getgrnam(\"%s\") failed", value[2].data);
+                           "getgrnam(\"%s\") failed", group);
         return NGX_CONF_ERROR;
     }
 
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.1.1"
+#define NGINX_VER          "nginx/0.1.2"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_NEWPID_EXT     ".newbin"
--- a/src/core/ngx_buf.c
+++ b/src/core/ngx_buf.c
@@ -126,12 +126,11 @@ void ngx_chain_update_chains(ngx_chain_t
         *busy = *out;
 
     } else {
-        for (tl = *busy; /* void */ ; tl = tl->next) {
-            if (tl->next == NULL) {
-                tl->next = *out;
-                break;
-            }
+        for (tl = *busy; tl->next; tl = tl->next) {
+            /* void */;
         }
+
+        tl->next = *out;
     }
 
     *out = NULL;
--- a/src/core/ngx_buf.h
+++ b/src/core/ngx_buf.h
@@ -22,7 +22,6 @@ struct ngx_buf_s {
     off_t            file_pos;
     off_t            file_last;
 
-    int              type;
     u_char          *start;         /* start of buffer */
     u_char          *end;           /* end of buffer */
     ngx_buf_tag_t    tag;
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -579,8 +579,18 @@ ngx_int_t ngx_conf_full_name(ngx_cycle_t
 
     name->len = cycle->root.len + old.len;
 
-    if (!(name->data = ngx_palloc(cycle->pool, name->len + 1))) {
-        return  NGX_ERROR;
+    if (cycle->connections) {
+        if (!(name->data = ngx_palloc(cycle->pool, name->len + 1))) {
+            return  NGX_ERROR;
+        }
+
+    } else {
+
+        /* the init_cycle */
+
+        if (!(name->data = ngx_alloc(name->len + 1, cycle->log))) {
+            return  NGX_ERROR;
+        }
     }
 
     p = ngx_cpymem(name->data, cycle->root.data, cycle->root.len),
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -8,30 +8,32 @@
 #define _NGX_CONFIG_H_INCLUDED_
 
 
+#include <ngx_auto_headers.h>
+
+
 #if defined __DragonFly__ && !defined __FreeBSD__
 #define __FreeBSD__        4
 #define __FreeBSD_version  480101
 #endif
 
 
-#if defined __FreeBSD__
+#if (NGX_FREEBSD)
 #include <ngx_freebsd_config.h>
 
 
-#elif defined __linux__
+#elif (NGX_LINUX)
 #include <ngx_linux_config.h>
 
 
-       /* Solaris */
-#elif defined sun && (defined __svr4__ || defined __SVR4)
+#elif (NGX_SOLARIS)
 #include <ngx_solaris_config.h>
 
 
-#elif defined _WIN32
+#elif (NGX_WIN32)
 #include <ngx_win32_config.h>
 
 
-#else /* posix */
+#else /* POSIX */
 #include <ngx_posix_config.h>
 
 #endif
@@ -89,8 +91,10 @@ typedef long               ngx_flag_t;
 #define NGX_INT64_LEN      sizeof("-9223372036854775808") - 1
 #define NGX_OFF_T_LEN      sizeof("-9223372036854775808") - 1
 
+#define NGX_MAX_INT_LEN    (sizeof("-9223372036854775808") - 1)
 
-#if (SOLARIS)
+
+#if (NGX_SOLARIS)
 
 /* TODO: auto_conf */
 #define NGX_ALIGN       (_MAX_ALIGNMENT - 1)         /* platform word */
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -40,7 +40,7 @@ typedef struct {
     time_t            post_accept_timeout;     /* should be here because
                                                   of the deferred accept */
 
-    unsigned          new:1;
+    unsigned          open:1;
     unsigned          remain:1;
     unsigned          ignore:1;
 
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -54,7 +54,7 @@ typedef void (*ngx_event_handler_pt)(ngx
 #include <ngx_file.h>
 #include <ngx_files.h>
 #include <ngx_crc.h>
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 #include <ngx_regex.h>
 #endif
 #include <ngx_rbtree.h>
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -158,6 +158,9 @@ ngx_cycle_t *ngx_init_cycle(ngx_cycle_t 
     conf.module_type = NGX_CORE_MODULE;
     conf.cmd_type = NGX_MAIN_CONF;
 
+#if 0
+    log->log_level = NGX_LOG_DEBUG_ALL;
+#endif
 
     if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
         ngx_destroy_pool(pool);
@@ -223,9 +226,6 @@ ngx_cycle_t *ngx_init_cycle(ngx_cycle_t 
                                        NGX_FILE_RDWR,
                                        NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
 
-#if 0
-            log->log_level = NGX_LOG_DEBUG_ALL;
-#endif
             ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
                            "log: %0X %d \"%s\"",
                            &file[i], file[i].fd, file[i].name.data);
@@ -310,14 +310,14 @@ ngx_cycle_t *ngx_init_cycle(ngx_cycle_t 
                 }
 
                 if (nls[n].fd == -1) {
-                    nls[n].new = 1;
+                    nls[n].open = 1;
                 }
             }
 
         } else {
             ls = cycle->listening.elts;
             for (i = 0; i < cycle->listening.nelts; i++) {
-                ls[i].new = 1;
+                ls[i].open = 1;
             }
         }
 
@@ -366,7 +366,7 @@ ngx_cycle_t *ngx_init_cycle(ngx_cycle_t 
 
         ls = cycle->listening.elts;
         for (i = 0; i < cycle->listening.nelts; i++) {
-            if (ls[i].fd == -1 || !ls[i].new) {
+            if (ls[i].fd == -1 || !ls[i].open) {
                 continue;
             }
 
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -57,7 +57,7 @@ static const char *debug_levels[] = {
 };
 
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                         const char *fmt, ...)
 #else
@@ -67,7 +67,7 @@ void ngx_log_error_core(ngx_uint_t level
 {
     char      errstr[MAX_ERROR_STR];
     size_t    len, max;
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
     va_list   args;
 #endif
 
@@ -97,7 +97,7 @@ void ngx_log_error_core(ngx_uint_t level
                             "*%u ", *(u_int *) log->data);
     }
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 
     va_start(args, fmt);
     len += ngx_vsnprintf(errstr + len, max - len, fmt, args);
@@ -187,7 +187,7 @@ static void ngx_log_write(ngx_log_t *log
 }
 
 
-#if !(HAVE_VARIADIC_MACROS)
+#if !(NGX_HAVE_VARIADIC_MACROS)
 
 void ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                    const char *fmt, ...)
@@ -253,7 +253,7 @@ ngx_log_t *ngx_log_init_stderr()
 #endif
 
     ngx_log.file = &ngx_stderr;
-    ngx_log.log_level = NGX_LOG_ERR;
+    ngx_log.log_level = NGX_LOG_NOTICE;
 
     return &ngx_log;
 }
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -55,9 +55,9 @@ struct ngx_log_s {
 
 /*********************************/
 
-#if (HAVE_GCC_VARIADIC_MACROS)
+#if (NGX_HAVE_GCC_VARIADIC_MACROS)
 
-#define HAVE_VARIADIC_MACROS  1
+#define NGX_HAVE_VARIADIC_MACROS  1
 
 #define ngx_log_error(level, log, args...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, args)
@@ -67,9 +67,9 @@ void ngx_log_error_core(ngx_uint_t level
 
 /*********************************/
 
-#elif (HAVE_C99_VARIADIC_MACROS)
+#elif (NGX_HAVE_C99_VARIADIC_MACROS)
 
-#define HAVE_VARIADIC_MACROS  1
+#define NGX_HAVE_VARIADIC_MACROS  1
 
 #define ngx_log_error(level, log, ...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__)
@@ -81,7 +81,7 @@ void ngx_log_error_core(ngx_uint_t level
 
 #else /* NO VARIADIC MACROS */
 
-#define HAVE_VARIADIC_MACROS  0
+#define NGX_HAVE_VARIADIC_MACROS  0
 
 void ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                    const char *fmt, ...);
@@ -98,7 +98,7 @@ void ngx_assert_core(ngx_log_t *log, con
 
 #if (NGX_DEBUG)
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 
 #define ngx_log_debug0(level, log, err, fmt) \
     if (log->log_level & level) \
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -24,13 +24,13 @@ ngx_int_t ngx_output_chain(ngx_output_ch
     size_t        size, bsize;
     ngx_chain_t  *cl, *out, **last_out;
 
-    /*
-     * the short path for the case when the ctx->in chain is empty
-     * and the incoming chain is empty too or it has the single buf
-     * that does not require the copy
-     */
+    if (ctx->in == NULL && ctx->busy == NULL) {
 
-    if (ctx->in == NULL) {
+       /*
+        * the short path for the case when the ctx->in and ctx->busy chains
+        * are empty, the incoming chain is empty too or has the single buf
+        * that does not require the copy
+        */
 
         if (in == NULL) {
             return ctx->output_filter(ctx->filter_ctx, in);
@@ -192,6 +192,7 @@ ngx_inline static ngx_int_t
     }
 
     if (!ctx->sendfile) {
+
         if (!ngx_buf_in_memory(buf)) {
             return 1;
         }
@@ -228,12 +229,19 @@ static ngx_int_t ngx_output_chain_copy_b
         src->pos += size;
         dst->last += size;
 
-        if (src->in_file && sendfile) {
-            dst->in_file = 1;
-            dst->file = src->file;
-            dst->file_pos = src->file_pos;
+        if (src->in_file) {
+
+            if (sendfile) {
+                dst->in_file = 1;
+                dst->file = src->file;
+                dst->file_pos = src->file_pos;
+                dst->file_last = src->file_pos + size;
+
+            } else {
+                dst->in_file = 0;
+            }
+
             src->file_pos += size;
-            dst->file_last = src->file_pos;
 
         } else {
             dst->in_file = 0;
@@ -271,14 +279,14 @@ static ngx_int_t ngx_output_chain_copy_b
             dst->in_file = 1;
             dst->file = src->file;
             dst->file_pos = src->file_pos;
-            src->file_pos += size;
-            dst->file_last = src->file_pos;
+            dst->file_last = src->file_pos + n;
 
         } else {
             dst->in_file = 0;
-            src->file_pos += n;
         }
 
+        src->file_pos += n;
+
         if (src->last_buf && src->file_pos == src->file_last) {
             dst->last_buf = 1;
         }
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -28,6 +28,238 @@ u_char *ngx_cpystrn(u_char *dst, u_char 
 }
 
 
+/*
+ * supported formats:
+ *    %[0][width]O     off_t
+ *    %[0][width]T     time_t
+ *    %[0][width]S     ssize_t
+ *    %[0][width]uS    size_t
+ *    %[0][width]uxS   size_t in hex
+ *    %[0][width]l     long
+ *    %[0][width]d     int
+ *    %[0][width]i     ngx_int_t
+ *    %[0][width]ui    ngx_uint_t
+ *    %[0][width]uxi   ngx_uint_t in hex
+ *    %s               null-terminated string
+ *    %%               %
+ *
+ */
+
+u_char *ngx_sprintf(u_char *buf, char *fmt, ...)
+{
+    u_char        *p, c, temp[NGX_MAX_INT_LEN];
+    int            d;
+    long           l;
+    off_t          offset;
+    size_t         size, len;
+    ssize_t        ssize;
+    time_t         sec;
+    va_list        arg;
+    ngx_int_t      i;
+    ngx_uint_t     ui, zero, width, sign, hexadecimal;
+    static u_char  hex[] = "0123456789abcdef";
+
+    va_start(arg, fmt);
+
+    while (*fmt) {
+        if (*fmt == '%') {
+
+            zero = (*++fmt == '0') ? 1 : 0;
+            width = 0;
+            sign = 1;
+            hexadecimal = 0;
+
+            p = temp + NGX_MAX_INT_LEN;
+
+            while (*fmt >= '0' && *fmt <= '9') {
+                width = width * 10 + *fmt++ - '0';
+            }
+
+
+            for ( ;; ) {
+                switch (*fmt) {
+
+                case 'u':
+                    sign = 0;
+                    fmt++;
+                    continue;
+
+                case 'x':
+                    hexadecimal = 1;
+                    fmt++;
+                    continue;
+
+                default:
+                    break;
+                }
+
+                break;
+            }
+
+
+            switch (*fmt) {
+
+            case 'O':
+                offset = va_arg(arg, off_t);
+
+                if (offset < 0) {
+                    *buf++ = '-';
+                    offset = -offset;
+                }
+
+                do {
+                    *--p = (u_char) (offset % 10 + '0');
+                } while (offset /= 10);
+
+                break;
+
+            case 'T':
+                sec = va_arg(arg, time_t);
+
+                if (sec < 0) {
+                    *buf++ = '-';
+                    sec = -sec;
+                }
+
+                do {
+                    *--p = (u_char) (sec % 10 + '0');
+                } while (sec /= 10);
+
+                break;
+
+            case 'S':
+                if (sign) {
+                    ssize = va_arg(arg, ssize_t);
+
+                    if (ssize < 0) {
+                        *buf++ = '-';
+                        size = (size_t) -ssize;
+
+                    } else {
+                        size = (size_t) ssize;
+                    }
+
+                } else {
+                    size = va_arg(arg, size_t);
+                }
+
+                if (hexadecimal) {
+                    do {
+                        *--p = hex[size & 0xf];
+                    } while (size >>= 4);
+
+                } else {
+                    do {
+                        *--p = (u_char) (size % 10 + '0');
+                    } while (size /= 10);
+                }
+
+                break;
+
+            case 'l':
+                l = va_arg(arg, long);
+
+                if (l < 0) {
+                    *buf++ = '-';
+                    l = -l;
+                }
+
+                do {
+                    *--p = (u_char) (l % 10 + '0');
+                } while (l /= 10);
+
+                break;
+
+            case 'd':
+                d = va_arg(arg, int);
+
+                if (d < 0) {
+                    *buf++ = '-';
+                    d = -d;
+                }
+
+                do {
+                    *--p = (u_char) (d % 10 + '0');
+                } while (d /= 10);
+
+                break;
+
+            case 'i':
+                if (sign) {
+                    i = va_arg(arg, ngx_int_t);
+
+                    if (i < 0) {
+                        *buf++ = '-';
+                        ui = (ngx_uint_t) -i;
+
+                    } else {
+                        ui = (ngx_uint_t) i;
+                    }
+
+                } else {
+                    ui = va_arg(arg, ngx_uint_t);
+                }
+
+                if (hexadecimal) {
+                    do {
+                        *--p = hex[ui & 0xf];
+                    } while (ui >>= 4);
+
+                } else {
+                    do {
+                        *--p = (u_char) (ui % 10 + '0');
+                    } while (ui /= 10);
+                }
+
+                break;
+
+            case 's':
+                p = va_arg(arg, u_char *);
+
+                while (*p) {
+                    *buf++ = *p++;
+                }
+                fmt++;
+
+                continue;
+
+            case '%':
+                *buf++ = '%';
+                fmt++;
+
+                continue;
+
+            default:
+                *buf++ = *fmt++;
+
+                continue;
+            }
+
+            len = (temp + NGX_MAX_INT_LEN) - p;
+
+            c = (u_char) (zero ? '0' : ' ');
+
+            while (len++ < width) {
+                *buf++ = c;
+            }
+
+            buf = ngx_cpymem(buf, p, ((temp + NGX_MAX_INT_LEN) - p));
+
+            fmt++;
+
+        } else {
+            *buf++ = *fmt++;
+        }
+    }
+
+    va_end(arg);
+
+    *buf = '\0';
+
+    return buf;
+}
+
+
 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
 {
     if (n == 0) {
@@ -50,6 +282,40 @@ ngx_int_t ngx_rstrncmp(u_char *s1, u_cha
 }
 
 
+ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
+{
+    u_char  c1, c2;
+
+    if (n == 0) {
+        return 0;
+    }
+
+    n--;
+
+    for ( ;; ) {
+        c1 = s1[n];
+        if (c1 >= 'a' && c1 <= 'z') {
+            c1 -= 'a' - 'A';
+        }
+
+        c2 = s2[n];
+        if (c2 >= 'a' && c2 <= 'z') {
+            c2 -= 'a' - 'A';
+        }
+
+        if (c1 != c2) {
+            return c1 - c2;
+        }
+
+        if (n == 0) {
+            return 0;
+        }
+
+        n--;
+    }
+}
+
+
 ngx_int_t ngx_atoi(u_char *line, size_t n)
 {
     ngx_int_t  value;
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -70,7 +70,10 @@ typedef struct {
 #define ngx_memcmp                memcmp
 
 u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n);
+u_char *ngx_sprintf(u_char *buf, char *fmt, ...);
+
 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
+ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
 
 ngx_int_t ngx_atoi(u_char *line, size_t n);
 ngx_int_t ngx_hextoi(u_char *line, size_t n);
--- a/src/core/ngx_times.c
+++ b/src/core/ngx_times.c
@@ -212,12 +212,25 @@ void ngx_time_update(time_t s)
 }
 
 
+u_char *ngx_http_time(u_char *buf, time_t t)
+#if 0
 size_t ngx_http_time(u_char *buf, time_t t)
+#endif
 {
     ngx_tm_t  tm;
 
     ngx_gmtime(t, &tm);
 
+    return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
+                       week[tm.ngx_tm_wday],
+                       tm.ngx_tm_mday,
+                       months[tm.ngx_tm_mon - 1],
+                       tm.ngx_tm_year,
+                       tm.ngx_tm_hour,
+                       tm.ngx_tm_min,
+                       tm.ngx_tm_sec);
+
+#if 0
     return ngx_snprintf((char *) buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
                                       "%s, %02d %s %4d %02d:%02d:%02d GMT",
                                       week[tm.ngx_tm_wday],
@@ -227,10 +240,14 @@ size_t ngx_http_time(u_char *buf, time_t
                                       tm.ngx_tm_hour,
                                       tm.ngx_tm_min,
                                       tm.ngx_tm_sec);
+#endif
 }
 
 
+u_char *ngx_http_cookie_time(u_char *buf, time_t t)
+#if 0
 size_t ngx_http_cookie_time(u_char *buf, time_t t)
+#endif
 {
     ngx_tm_t  tm;
 
@@ -241,6 +258,20 @@ size_t ngx_http_cookie_time(u_char *buf,
      * 2-digit years more than "37"
      */
 
+    return ngx_sprintf(buf,
+                       (tm.ngx_tm_year > 2037) ?
+                                         "%s, %02d-%s-%d %02d:%02d:%02d GMT":
+                                         "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
+                       week[tm.ngx_tm_wday],
+                       tm.ngx_tm_mday,
+                       months[tm.ngx_tm_mon - 1],
+                       (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
+                                                 tm.ngx_tm_year % 100,
+                       tm.ngx_tm_hour,
+                       tm.ngx_tm_min,
+                       tm.ngx_tm_sec);
+
+#if 0
     if (tm.ngx_tm_year > 2037) {
         return ngx_snprintf((char *) buf,
                                       sizeof("Mon, 28-Sep-1970 06:00:00 GMT"),
@@ -264,6 +295,7 @@ size_t ngx_http_cookie_time(u_char *buf,
                                       tm.ngx_tm_min,
                                       tm.ngx_tm_sec);
     }
+#endif
 }
 
 
--- a/src/core/ngx_times.h
+++ b/src/core/ngx_times.h
@@ -14,8 +14,8 @@
 
 void ngx_time_init();
 void ngx_time_update(time_t s);
-size_t ngx_http_time(u_char *buf, time_t t);
-size_t ngx_http_cookie_time(u_char *buf, time_t t);
+u_char *ngx_http_time(u_char *buf, time_t t);
+u_char *ngx_http_cookie_time(u_char *buf, time_t t);
 void ngx_gmtime(time_t t, ngx_tm_t *tp);
 
 #if (NGX_THREADS)
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -173,7 +173,7 @@ static int ngx_epoll_init(ngx_cycle_t *c
 #else
     ngx_event_flags = NGX_USE_LEVEL_EVENT
 #endif
-                      |NGX_HAVE_GREEDY_EVENT
+                      |NGX_USE_GREEDY_EVENT
                       |NGX_USE_EPOLL_EVENT;
 
     return NGX_OK;
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -194,10 +194,10 @@ static ngx_int_t ngx_kqueue_init(ngx_cyc
 #else
                      |NGX_USE_LEVEL_EVENT
 #endif
-#if (HAVE_LOWAT_EVENT)
-                     |NGX_HAVE_LOWAT_EVENT
+#if (NGX_HAVE_LOWAT_EVENT)
+                     |NGX_USE_LOWAT_EVENT
 #endif
-                     |NGX_HAVE_KQUEUE_EVENT;
+                     |NGX_USE_KQUEUE_EVENT;
 
     return NGX_OK;
 }
@@ -245,6 +245,8 @@ static ngx_int_t ngx_kqueue_add_event(ng
         return NGX_ERROR;
     }
 
+#if 1
+
     if (nchanges > 0
         && ev->index < (u_int) nchanges
         && ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
@@ -262,7 +264,8 @@ static ngx_int_t ngx_kqueue_add_event(ng
                            ngx_event_ident(ev->data), event);
 
             if (ev->index < (u_int) --nchanges) {
-                e = (ngx_event_t *) change_list[nchanges].udata;
+                e = (ngx_event_t *)
+                    ((uintptr_t) change_list[nchanges].udata & (uintptr_t) ~1);
                 change_list[ev->index] = change_list[nchanges];
                 e->index = ev->index;
             }
@@ -282,6 +285,8 @@ static ngx_int_t ngx_kqueue_add_event(ng
         return NGX_ERROR;
     }
 
+#endif
+
     rc = ngx_kqueue_set_event(ev, event, EV_ADD|EV_ENABLE|flags);
 
     ngx_mutex_unlock(list_mutex);
@@ -302,6 +307,8 @@ static ngx_int_t ngx_kqueue_del_event(ng
         return NGX_ERROR;
     }
 
+#if 1
+
     if (nchanges > 0
         && ev->index < (u_int) nchanges
         && ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
@@ -314,7 +321,8 @@ static ngx_int_t ngx_kqueue_del_event(ng
         /* if the event is still not passed to a kernel we will not pass it */
 
         if (ev->index < (u_int) --nchanges) {
-            e = (ngx_event_t *) change_list[nchanges].udata;
+            e = (ngx_event_t *)
+                    ((uintptr_t) change_list[nchanges].udata & (uintptr_t) ~1);
             change_list[ev->index] = change_list[nchanges];
             e->index = ev->index;
         }
@@ -324,6 +332,8 @@ static ngx_int_t ngx_kqueue_del_event(ng
         return NGX_OK;
     }
 
+#endif
+
     /*
      * when the file descriptor is closed the kqueue automatically deletes
      * its filters so we do not need to delete explicity the event
@@ -393,7 +403,7 @@ static ngx_int_t ngx_kqueue_set_event(ng
         kev->data = 0;
 
     } else {
-#if (HAVE_LOWAT_EVENT)
+#if (NGX_HAVE_LOWAT_EVENT)
         if (flags & NGX_LOWAT_EVENT) {
             kev->fflags = NOTE_LOWAT;
             kev->data = ev->available;
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -158,7 +158,7 @@ static ngx_int_t ngx_rtsig_init(ngx_cycl
 
     ngx_event_actions = ngx_rtsig_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_HAVE_GREEDY_EVENT;
+    ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_USE_GREEDY_EVENT;
 
     return NGX_OK;
 }
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -465,9 +465,9 @@ ngx_int_t ngx_send_lowat(ngx_connection_
 {
     int  sndlowat;
 
-#if (HAVE_LOWAT_EVENT)
+#if (NGX_HAVE_LOWAT_EVENT)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         c->write->available = lowat;
         return NGX_OK;
     }
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -240,19 +240,19 @@ extern ngx_event_actions_t   ngx_event_a
  * The event filter has kqueue features - the eof flag, errno,
  * available data, etc.
  */
-#define NGX_HAVE_KQUEUE_EVENT    0x00000008
+#define NGX_USE_KQUEUE_EVENT    0x00000008
 
 /*
  * The event filter supports low water mark - kqueue's NOTE_LOWAT.
  * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
  */
-#define NGX_HAVE_LOWAT_EVENT     0x00000010
+#define NGX_USE_LOWAT_EVENT     0x00000010
 
 /*
  * The event filter requires to do i/o operation until EAGAIN -
  * epoll, rt signals.
  */
-#define NGX_HAVE_GREEDY_EVENT    0x00000020
+#define NGX_USE_GREEDY_EVENT    0x00000020
 
 /*
  * The event filter is epoll,
@@ -571,7 +571,7 @@ ngx_inline static int ngx_handle_write_e
     ngx_connection_t  *c;
 
     if (lowat) {
-        c = wev->data;
+        c = (ngx_connection_t *) wev->data;
 
         if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
             return NGX_ERROR;
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -39,7 +39,7 @@ void ngx_event_accept(ngx_event_t *ev)
     if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
         ev->available = 1;
 
-    } else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
+    } else if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
         ev->available = ecf->multi_accept;
     }
 
@@ -118,7 +118,7 @@ void ngx_event_accept(ngx_event_t *ev)
                           ls->listening->addr_text.data);
 
             if (err == NGX_ECONNABORTED) {
-                if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+                if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                     ev->available--;
                 }
 
@@ -342,7 +342,7 @@ void ngx_event_accept(ngx_event_t *ev)
 
         ls->listening->handler(c);
 
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             ev->available--;
         }
 
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -296,6 +296,9 @@ int ngx_event_connect_peer(ngx_peer_conn
 
     if (ngx_event_flags & NGX_USE_AIO_EVENT) {
 
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, ngx_socket_errno,
+                       "connect(): %d", rc);
+
         /* aio, iocp */
 
         if (ngx_blocking(s) == -1) {
@@ -311,8 +314,7 @@ int ngx_event_connect_peer(ngx_peer_conn
         }
 
         /*
-         * aio allows to post operation on non-connected socket
-         * at least in FreeBSD.
+         * FreeBSD aio allows to post operation on non-connected socket.
          * NT does not support it.
          * 
          * TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -86,6 +86,7 @@ ngx_int_t ngx_ssl_recv(ngx_connection_t 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
 
     if (sslerr == SSL_ERROR_WANT_READ) {
+        c->read->ready = 0;
         return NGX_AGAIN;
     }
 
--- a/src/http/modules/ngx_http_charset_filter.c
+++ b/src/http/modules/ngx_http_charset_filter.c
@@ -12,7 +12,7 @@
 typedef struct {
     char       **tables;
     ngx_str_t    name;
-    unsigned     server;
+    ngx_uint_t   server;  /* unsigned     server:1; */
 } ngx_http_charset_t;
 
 
@@ -45,7 +45,7 @@ typedef struct {
 } ngx_http_charset_ctx_t;
 
 
-static void ngx_charset_recode(ngx_buf_t *b, char *table);
+static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table);
 
 static char *ngx_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd,
                                    void *conf);
@@ -232,14 +232,31 @@ static ngx_int_t ngx_http_charset_body_f
 }
 
 
-static void ngx_charset_recode(ngx_buf_t *b, char *table)
+static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table)
 {
-    u_char  *p, c;
+    u_char      *p;
+    ngx_uint_t   change;
+
+    change = 0;
 
     for (p = b->pos; p < b->last; p++) {
-        c = *p;
-        *p = table[c];
+        if (*p != table[*p]) {
+            change = 1;
+            break;
+        }
     }
+
+    if (change) {
+
+        while (p < b->last) {
+            *p = table[*p];
+            p++;
+        }
+
+        b->in_file = 0;
+    }
+
+    return change;
 }
 
 
@@ -419,7 +436,9 @@ static ngx_int_t ngx_http_add_charset(ng
         return NGX_ERROR;
     }
 
+    c->tables = NULL;
     c->name = *name;
+    c->server = 0;
 
     return i;
 }
--- a/src/http/modules/ngx_http_chunked_filter.c
+++ b/src/http/modules/ngx_http_chunked_filter.c
@@ -63,7 +63,7 @@ static ngx_int_t ngx_http_chunked_body_f
                                               ngx_chain_t *in)
 {
     u_char       *chunk;
-    size_t        size, len;
+    size_t        size;
     ngx_buf_t    *b;
     ngx_chain_t   out, tail, *cl, *tl, **ll;
 
@@ -98,6 +98,20 @@ static ngx_int_t ngx_http_chunked_body_f
     }
 
     if (size) {
+        if (!(b = ngx_calloc_buf(r->pool))) {
+            return NGX_ERROR;
+        }
+
+        if (!(chunk = ngx_palloc(r->pool, 11))) {
+            return NGX_ERROR;
+        }
+
+        b->temporary = 1;
+        b->pos = chunk;
+        b->last = ngx_sprintf(chunk, "%uxS" CRLF, size);
+
+        out.buf = b;
+#if 0
         ngx_test_null(chunk, ngx_palloc(r->pool, 11), NGX_ERROR);
         len = ngx_snprintf((char *) chunk, 11, SIZE_T_X_FMT CRLF, size);
 
@@ -107,6 +121,7 @@ static ngx_int_t ngx_http_chunked_body_f
         b->last = chunk + len;
 
         out.buf = b;
+#endif
     }
 
     if (cl->buf->last_buf) {
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -837,26 +837,30 @@ static u_char *ngx_http_gzip_log_ratio(n
         return buf + 1;
     }
 
-#if 0
-    return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4, "%.2f",
-                              (float) ctx->zin / ctx->zout);
-#endif
-
     /* we prefer do not use the FPU */
 
     zint = (ngx_uint_t) (ctx->zin / ctx->zout);
     zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
 
-    if ((ctx->zin * 1000 / ctx->zout) %10 > 4) {
-        if (++zfrac > 99) {
+    if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
+
+        /* the rounding, e.g., 2.125 to 2.13 */
+
+        zfrac++;
+
+        if (zfrac > 99) {
             zint++;
             zfrac = 0;
         }
     }
 
+    return ngx_sprintf(buf, "%ui.%02ui", zint, zfrac);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4,
                               "%" NGX_UINT_T_FMT ".%02" NGX_UINT_T_FMT,
                               zint, zfrac);
+#endif
 }
 
 
--- a/src/http/modules/ngx_http_headers_filter.c
+++ b/src/http/modules/ngx_http_headers_filter.c
@@ -134,10 +134,16 @@ static ngx_int_t ngx_http_headers_filter
                         return NGX_ERROR;
                     }
 
+                    cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T",
+                                                conf->expires)
+                                    - cc->value.data;
+
+#if 0
                     cc->value.len = ngx_snprintf((char *) cc->value.data,
                                                sizeof("max-age=") + TIME_T_LEN,
                                                "max-age=" TIME_T_FMT,
                                                conf->expires);
+#endif
                 }
             }
         }
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -264,9 +264,14 @@ static ngx_int_t ngx_http_range_header_f
         }
 
         r->headers_out.content_range->value.len =
+                ngx_sprintf(r->headers_out.content_range->value.data,
+                            "bytes */%O", r->headers_out.content_length_n)
+                - r->headers_out.content_range->value.data;
+#if 0
                 ngx_snprintf((char *) r->headers_out.content_range->value.data,
                              8 + 20 + 1, "bytes */" OFF_T_FMT,
                              r->headers_out.content_length_n);
+#endif
 
         r->headers_out.content_length_n = -1;
         if (r->headers_out.content_length) {
@@ -297,12 +302,20 @@ static ngx_int_t ngx_http_range_header_f
             /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
 
             r->headers_out.content_range->value.len =
+                         ngx_sprintf(r->headers_out.content_range->value.data,
+                                     "bytes %O-%O/%O",
+                                     range->start, range->end - 1,
+                                     r->headers_out.content_length_n)
+                         - r->headers_out.content_range->value.data;
+
+#if 0
                    ngx_snprintf((char *)
                                 r->headers_out.content_range->value.data,
                                 6 + 20 + 1 + 20 + 1 + 20 + 1,
                                 "bytes " OFF_T_FMT "-" OFF_T_FMT "/" OFF_T_FMT,
                                 range->start, range->end - 1,
                                 r->headers_out.content_length_n);
+#endif
 
             r->headers_out.content_length_n = range->end - range->start;
 
@@ -343,6 +356,15 @@ static ngx_int_t ngx_http_range_header_f
 
             if (r->headers_out.charset.len) {
                 ctx->boundary_header.len =
+                          ngx_sprintf(ctx->boundary_header.data,
+                                      CRLF "--%010ui" CRLF
+                                      "Content-Type: %s; charset=%s" CRLF
+                                      "Content-Range: bytes ",
+                                      boundary,
+                                      r->headers_out.content_type->value.data,
+                                      r->headers_out.charset.data)
+                          - ctx->boundary_header.data;
+#if 0
                          ngx_snprintf((char *) ctx->boundary_header.data, len,
                                       CRLF "--%010" NGX_UINT_T_FMT CRLF
                                       "Content-Type: %s; charset=%s" CRLF
@@ -350,17 +372,29 @@ static ngx_int_t ngx_http_range_header_f
                                       boundary,
                                       r->headers_out.content_type->value.data,
                                       r->headers_out.charset.data);
+#endif
 
                 r->headers_out.charset.len = 0;
 
             } else {
                 ctx->boundary_header.len =
+                          ngx_sprintf(ctx->boundary_header.data,
+                                      CRLF "--%010ui" CRLF
+                                      "Content-Type: %s" CRLF
+                                      "Content-Range: bytes ",
+                                      boundary,
+                                      r->headers_out.content_type->value.data)
+                          - ctx->boundary_header.data;
+
+#if 0
                          ngx_snprintf((char *) ctx->boundary_header.data, len,
                                       CRLF "--%010" NGX_UINT_T_FMT CRLF
                                       "Content-Type: %s" CRLF
                                       "Content-Range: bytes ",
                                       boundary,
                                       r->headers_out.content_type->value.data);
+
+#endif
             }
 
             ngx_test_null(r->headers_out.content_type->value.data,
@@ -370,12 +404,18 @@ static ngx_int_t ngx_http_range_header_f
             /* "Content-Type: multipart/byteranges; boundary=0123456789" */
 
             r->headers_out.content_type->value.len =
+                       ngx_sprintf(r->headers_out.content_type->value.data,
+                                   "multipart/byteranges; boundary=%010ui",
+                                   boundary)
+                       - r->headers_out.content_type->value.data;
+#if 0
                       ngx_snprintf((char *)
                                    r->headers_out.content_type->value.data,
                                    31 + 10 + 1,
                                    "multipart/byteranges; boundary=%010"
                                    NGX_UINT_T_FMT,
                                    boundary);
+#endif
 
             /* the size of the last boundary CRLF "--0123456789--" CRLF */
             len = 4 + 10 + 4;
@@ -389,11 +429,18 @@ static ngx_int_t ngx_http_range_header_f
                 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
 
                 range[i].content_range.len =
+                                 ngx_sprintf(range[i].content_range.data,
+                                             "%O-%O/%O" CRLF CRLF,
+                                             range[i].start, range[i].end - 1,
+                                             r->headers_out.content_length_n)
+                                 - range[i].content_range.data;
+#if 0
                   ngx_snprintf((char *) range[i].content_range.data,
                                20 + 1 + 20 + 1 + 20 + 5,
                                OFF_T_FMT "-" OFF_T_FMT "/" OFF_T_FMT CRLF CRLF,
                                range[i].start, range[i].end - 1,
                                r->headers_out.content_length_n);
+#endif
 
                 len += ctx->boundary_header.len + range[i].content_range.len
                                     + (size_t) (range[i].end - range[i].start);
--- a/src/http/modules/ngx_http_userid_filter.c
+++ b/src/http/modules/ngx_http_userid_filter.c
@@ -367,7 +367,7 @@ static ngx_int_t ngx_http_userid_set_uid
 
     } else if (conf->expires) {
         p = ngx_cpymem(p, expires, sizeof("; expires=") - 1);
-        p += ngx_http_cookie_time(p, ngx_time() + conf->expires);
+        p = ngx_http_cookie_time(p, ngx_time() + conf->expires);
     }
 
     if (conf->domain.len > 1) {
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -289,6 +289,8 @@ ngx_http_header_t ngx_http_proxy_headers
                          offsetof(ngx_http_proxy_headers_in_t, content_type) },
     { ngx_string("Content-Length"),
                        offsetof(ngx_http_proxy_headers_in_t, content_length) },
+    { ngx_string("Content-Encoding"),
+                     offsetof(ngx_http_proxy_headers_in_t, content_encoding) },
     { ngx_string("Last-Modified"),
                         offsetof(ngx_http_proxy_headers_in_t, last_modified) },
     { ngx_string("Location"),
@@ -400,7 +402,7 @@ void ngx_http_proxy_check_broken_connect
 
 #if (HAVE_KQUEUE)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
 
         if (!ev->pending_eof) {
             return;
@@ -1310,9 +1312,9 @@ static char *ngx_http_proxy_parse_upstre
 
 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
 {
-#if __FreeBSD__
+    ssize_t *np = data;
 
-    ssize_t *np = data;
+#if (NGX_FREEBSD)
 
     if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
@@ -1323,15 +1325,12 @@ static char *ngx_http_proxy_lowat_check(
         return NGX_CONF_ERROR;
     }
 
+#elif !(HAVE_SO_SNDLOWAT)
 
-#else
-
-#if 0
     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                        "\"proxy_send_lowat\" is not supported, ignored");
 
     *np = 0;
-#endif
 
 #endif
 
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -130,6 +130,7 @@ typedef struct {
     ngx_table_elt_t                 *connection;
     ngx_table_elt_t                 *content_type;
     ngx_table_elt_t                 *content_length;
+    ngx_table_elt_t                 *content_encoding;
     ngx_table_elt_t                 *last_modified;
     ngx_table_elt_t                 *location;
     ngx_table_elt_t                 *accept_ranges;
--- a/src/http/modules/proxy/ngx_http_proxy_header.c
+++ b/src/http/modules/proxy/ngx_http_proxy_header.c
@@ -113,6 +113,11 @@ int ngx_http_proxy_copy_header(ngx_http_
             continue;
         }
 
+        if (&h[i] == headers_in->content_encoding) {
+            r->headers_out.content_encoding = ho;
+            continue;
+        }
+
         if (&h[i] == headers_in->last_modified) {
             r->headers_out.last_modified = ho;
             /* TODO: update r->headers_out.last_modified_time */
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -692,12 +692,14 @@ static void ngx_http_proxy_connect(ngx_h
 
     /* rc == NGX_OK */
 
-#if 1 /* test only, see below about "post aio operation" */
+#if 0 /* test only, see below about "post aio operation" */
 
     if (c->read->ready) {
         /* post aio operation */
         ngx_http_proxy_process_upstream_status_line(c->read);
+#if 0
         return;
+#endif
     }
 
 #endif
@@ -718,7 +720,7 @@ static void ngx_http_proxy_send_request(
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
         && !p->request_sent
         && c->write->pending_eof)
     {
@@ -776,7 +778,7 @@ static void ngx_http_proxy_send_request(
 
     ngx_add_timer(c->read, p->lcf->read_timeout);
 
-#if 0
+#if 1
     if (c->read->ready) {
 
         /* post aio operation */
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -11,6 +11,14 @@
 
 
 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
+                                      ngx_http_in_port_t *in_port,
+                                      ngx_http_listen_t *lscf,
+                                      ngx_http_core_srv_conf_t *cscf);
+static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
+                                    ngx_http_in_addr_t *in_addr,
+                                    ngx_http_core_srv_conf_t *cscf);
+
 static char *ngx_http_merge_locations(ngx_conf_t *cf,
                                       ngx_array_t *locations,
                                       void **loc_conf,
@@ -79,6 +87,11 @@ static char *ngx_http_block(ngx_conf_t *
     ngx_iocp_conf_t             *iocpcf;
 #endif
 
+#if (NGX_SUPPRESS_WARN)
+    /* MSVC thinks 'in_ports' may be used without having been initialized */
+    ngx_memzero(&in_ports, sizeof(ngx_array_t));
+#endif
+
     /* the main http context */
     ngx_test_null(ctx,
                   ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
@@ -274,18 +287,23 @@ static char *ngx_http_block(ngx_conf_t *
 
 
     /*
-     * create the lists of the ports, the addresses and the server names
-     * to allow quickly find the server core module configuration at run-time
+     * create the lists of ports, addresses and server names
+     * to quickly find the server core module configuration at run-time
      */
 
-    ngx_init_array(in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t),
-                   NGX_CONF_ERROR);
+    if (ngx_array_init(&in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t))
+                                                                  == NGX_ERROR)
+    {
+        return NGX_CONF_ERROR;
+    }
 
     /* "server" directives */
+
     cscfp = cmcf->servers.elts;
     for (s = 0; s < cmcf->servers.nelts; s++) {
 
         /* "listen" directives */
+
         lscf = cscfp[s]->listen.elts;
         for (l = 0; l < cscfp[s]->listen.nelts; l++) {
 
@@ -308,38 +326,26 @@ static char *ngx_http_block(ngx_conf_t *
 
                         if (lscf[l].addr == in_addr[a].addr) {
 
-                            /* the address is already bound to this port */
-
-                            /* "server_name" directives */
-                            s_name = cscfp[s]->server_names.elts;
-                            for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
+                            /* the address is already in the address list */
 
-                                /*
-                                 * add the server name and server core module
-                                 * configuration to the address:port
-                                 */
-
-                                /* TODO: duplicate names can be checked here */
-
-                                ngx_test_null(name,
-                                              ngx_push_array(&in_addr[a].names),
-                                              NGX_CONF_ERROR);
-
-                                name->name = s_name[n].name;
-                                name->core_srv_conf = s_name[n].core_srv_conf;
+                            if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
+                                                                  == NGX_ERROR)
+                            {
+                                return NGX_CONF_ERROR;
                             }
 
                             /*
-                             * check duplicate "default" server that
-                             * serves this address:port
+                             * check the duplicate "default" server 
+                             * for this address:port
                              */
 
                             if (lscf[l].default_server) {
+
                                 if (in_addr[a].default_server) {
                                     ngx_log_error(NGX_LOG_ERR, cf->log, 0,
-                                           "duplicate default server in %s:%d",
-                                           lscf[l].file_name.data,
-                                           lscf[l].line);
+                                        "the duplicate default server in %s:%d",
+                                        lscf[l].file_name.data,
+                                        lscf[l].line);
 
                                     return NGX_CONF_ERROR;
                                 }
@@ -354,31 +360,31 @@ static char *ngx_http_block(ngx_conf_t *
 
                         } else if (in_addr[a].addr == INADDR_ANY) {
 
+                            /* the INADDR_ANY is always the last address */
+
+                            if (!(inaddr = ngx_array_push(&in_port[p].addrs))) {
+                                return NGX_CONF_ERROR;
+                            }
+
                             /*
-                             * "*:port" must be the last resort so move it
-                             * to the end of the address list and add
-                             * the new address at its place
+                             * the INADDR_ANY must be the last resort
+                             * so we move it to the end of the address list
+                             * and put the new address in its place
                              */
 
-                            ngx_test_null(inaddr,
-                                          ngx_push_array(&in_port[p].addrs),
-                                          NGX_CONF_ERROR);
-
                             ngx_memcpy(inaddr, &in_addr[a],
                                        sizeof(ngx_http_in_addr_t));
 
                             in_addr[a].addr = lscf[l].addr;
+                            in_addr[a].names.elts = NULL;
                             in_addr[a].default_server = lscf[l].default_server;
                             in_addr[a].core_srv_conf = cscfp[s];
 
-                            /*
-                             * create the empty list of the server names that
-                             * can be served on this address:port
-                             */
-
-                            ngx_init_array(inaddr->names, cf->pool, 10,
-                                           sizeof(ngx_http_server_name_t),
-                                           NGX_CONF_ERROR);
+                            if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
+                                                                  == NGX_ERROR)
+                            {
+                                return NGX_CONF_ERROR;
+                            }
 
                             addr_found = 1;
 
@@ -393,22 +399,11 @@ static char *ngx_http_block(ngx_conf_t *
                          * bound to this port
                          */
 
-                        ngx_test_null(inaddr,
-                                      ngx_push_array(&in_port[p].addrs),
-                                      NGX_CONF_ERROR);
-
-                        inaddr->addr = lscf[l].addr;
-                        inaddr->default_server = lscf[l].default_server;
-                        inaddr->core_srv_conf = cscfp[s];
-
-                        /*
-                         * create the empty list of the server names that
-                         * can be served on this address:port
-                         */
-
-                        ngx_init_array(inaddr->names, cf->pool, 10,
-                                       sizeof(ngx_http_server_name_t),
-                                       NGX_CONF_ERROR);
+                        if (ngx_http_add_address(cf, &in_port[p], &lscf[l],
+                                                        cscfp[s]) == NGX_ERROR)
+                        {
+                            return NGX_CONF_ERROR;
+                        }
                     }
                 }
             }
@@ -417,54 +412,42 @@ static char *ngx_http_block(ngx_conf_t *
 
                 /* add the port to the in_port list */
 
-                ngx_test_null(in_port,
-                              ngx_push_array(&in_ports),
-                              NGX_CONF_ERROR);
+                if (!(in_port = ngx_array_push(&in_ports))) {
+                    return NGX_CONF_ERROR;
+                }
 
                 in_port->port = lscf[l].port;
+                in_port->addrs.elts = NULL;
 
-                ngx_test_null(in_port->port_text.data, ngx_palloc(cf->pool, 7),
-                              NGX_CONF_ERROR);
-                in_port->port_text.len = ngx_snprintf((char *)
-                                                      in_port->port_text.data,
-                                                      7, ":%d",
-                                                      in_port->port);
-
-                /* create list of the addresses that bound to this port ... */
-
-                ngx_init_array(in_port->addrs, cf->pool, 10,
-                               sizeof(ngx_http_in_addr_t),
-                               NGX_CONF_ERROR);
+                if (!(in_port->port_text.data = ngx_palloc(cf->pool, 7))) {
+                    return NGX_CONF_ERROR;
+                }
 
-                ngx_test_null(inaddr, ngx_push_array(&in_port->addrs),
-                              NGX_CONF_ERROR);
-
-                /* ... and add the address to this list */
-
-                inaddr->addr = lscf[l].addr;
-                inaddr->default_server = lscf[l].default_server;
-                inaddr->core_srv_conf = cscfp[s];
+                in_port->port_text.len = ngx_sprintf(in_port->port_text.data,
+                                                     ":%d", in_port->port)
+                                         - in_port->port_text.data;
 
-                /*
-                 * create the empty list of the server names that
-                 * can be served on this address:port
-                 */
-
-                ngx_init_array(inaddr->names, cf->pool, 10,
-                               sizeof(ngx_http_server_name_t),
-                               NGX_CONF_ERROR);
+                if (ngx_http_add_address(cf, in_port, &lscf[l], cscfp[s])
+                                                                  == NGX_ERROR)
+                {
+                    return NGX_CONF_ERROR;
+                }
             }
         }
     }
 
-    /* optimize the lists of the ports, the addresses and the server names */
+
+    /* optimize the lists of ports, addresses and server names */
 
     /* AF_INET only */
 
     in_port = in_ports.elts;
     for (p = 0; p < in_ports.nelts; p++) {
 
-        /* check whether the all server names point to the same server */
+        /*
+         * check whether all name-based servers have the same configuraiton
+         * as the default server, or some servers restrict the host names
+         */
 
         in_addr = in_port[p].addrs.elts;
         for (a = 0; a < in_port[p].addrs.nelts; a++) {
@@ -473,15 +456,19 @@ static char *ngx_http_block(ngx_conf_t *
 
             name = in_addr[a].names.elts;
             for (n = 0; n < in_addr[a].names.nelts; n++) {
-                if (in_addr[a].core_srv_conf != name[n].core_srv_conf) {
+                if (in_addr[a].core_srv_conf != name[n].core_srv_conf
+                    || name[n].core_srv_conf->restrict_host_names
+                                                 != NGX_HTTP_RESTRICT_HOST_OFF)
+                {
                     virtual_names = 1;
                     break;
                 }
             }
 
             /*
-             * if the all server names point to the same server
-             * then we do not need to check them at run-time
+             * if all name-based servers have the same configuration
+             * as the default server, and no servers restrict the host names
+             * then we do not need to check them at run-time at all
              */
 
             if (!virtual_names) {
@@ -588,30 +575,117 @@ static char *ngx_http_block(ngx_conf_t *
     }
 
 #if (NGX_DEBUG)
+    {
+    u_char      address[20];
+    ngx_uint_t  p, a, n;
+
     in_port = in_ports.elts;
     for (p = 0; p < in_ports.nelts; p++) {
         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
                       "port: %d %08x", in_port[p].port, &in_port[p]);
         in_addr = in_port[p].addrs.elts;
         for (a = 0; a < in_port[p].addrs.nelts; a++) {
-            u_char ip[20];
-            ngx_inet_ntop(AF_INET, &in_addr[a].addr, ip, 20);
-            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
-                           "%s %08x", ip, in_addr[a].core_srv_conf);
+            ngx_inet_ntop(AF_INET, &in_addr[a].addr, address, 20);
+            ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                           "%s:%d %08x",
+                           address, in_port[p].port, in_addr[a].core_srv_conf);
             s_name = in_addr[a].names.elts;
             for (n = 0; n < in_addr[a].names.nelts; n++) {
-                 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
-                                "%s %08x", s_name[n].name.data,
+                 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                                "%s:%d %s %08x",
+                                address, in_port[p].port, s_name[n].name.data,
                                 s_name[n].core_srv_conf);
             }
         }
     }
+    }
 #endif
 
     return NGX_CONF_OK;
 }
 
 
+/*
+ * add the server address, the server names and the server core module
+ * configurations to the port (in_port)
+ */
+
+static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
+                                      ngx_http_in_port_t *in_port,
+                                      ngx_http_listen_t *lscf,
+                                      ngx_http_core_srv_conf_t *cscf)
+{
+    ngx_http_in_addr_t  *in_addr;
+
+    if (in_port->addrs.elts == NULL) {
+        if (ngx_array_init(&in_port->addrs, cf->pool, 10,
+                                      sizeof(ngx_http_in_addr_t)) == NGX_ERROR)
+        {
+            return NGX_ERROR;
+        }
+    }
+
+    if (!(in_addr = ngx_array_push(&in_port->addrs))) {
+        return NGX_ERROR;
+    }
+
+    in_addr->addr = lscf->addr;
+    in_addr->names.elts = NULL;
+    in_addr->default_server = lscf->default_server;
+    in_addr->core_srv_conf = cscf;
+
+#if (NGX_DEBUG)
+    {
+    u_char text[20];
+    ngx_inet_ntop(AF_INET, &in_addr->addr, text, 20);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0, "address: %s:%d",
+                   text, in_port->port);
+    }
+#endif
+
+    return ngx_http_add_names(cf, in_addr, cscf);
+}
+
+
+/*
+ * add the server names and the server core module
+ * configurations to the address:port (in_addr)
+ */
+
+static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
+                                    ngx_http_in_addr_t *in_addr,
+                                    ngx_http_core_srv_conf_t *cscf)
+{
+    ngx_uint_t               i;
+    ngx_http_server_name_t  *server_names, *name;
+
+    if (in_addr->names.elts == NULL) {
+        if (ngx_array_init(&in_addr->names, cf->pool, 10,
+                                  sizeof(ngx_http_server_name_t)) == NGX_ERROR)
+        {
+            return NGX_ERROR;
+        }
+    }
+
+    server_names = cscf->server_names.elts;
+    for (i = 0; i < cscf->server_names.nelts; i++) {
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                       "name: %s", server_names[i].name.data);
+
+        /* TODO: duplicate names can be checked here */
+
+        if (!(name = ngx_array_push(&in_addr->names))) {
+            return NGX_ERROR;
+        }
+
+        *name = server_names[i];
+    }
+
+    return NGX_OK;
+}
+
+
 static char *ngx_http_merge_locations(ngx_conf_t *cf,
                                       ngx_array_t *locations,
                                       void **loc_conf,
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -36,7 +36,7 @@ static char *ngx_location_block(ngx_conf
                                 void *dummy);
 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_set_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
-static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+static char *ngx_http_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_set_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
                                  void *conf);
 static char *ngx_set_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -126,9 +126,9 @@ static ngx_command_t  ngx_http_core_comm
 #if 0
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
 #else
-      NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
+      NGX_HTTP_SRV_CONF|NGX_CONF_TAKE12,
 #endif
-      ngx_set_listen,
+      ngx_http_listen,
       NGX_HTTP_SRV_CONF_OFFSET,
       0,
       NULL },
@@ -576,7 +576,7 @@ static ngx_int_t ngx_http_find_location(
     clcfp = locations->elts;
     for (i = 0; i < locations->nelts; i++) {
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
         if (clcfp[i]->regex) {
             break;
         }
@@ -638,7 +638,7 @@ static ngx_int_t ngx_http_find_location(
         }
     }
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 
     /* regex matches */
 
@@ -673,7 +673,7 @@ static ngx_int_t ngx_http_find_location(
         return NGX_HTTP_LOCATION_REGEX;
     }
 
-#endif /* HAVE_PCRE */
+#endif /* NGX_PCRE */
 
     return NGX_OK;
 }
@@ -991,7 +991,7 @@ static int ngx_cmp_locations(const void 
     first = *(ngx_http_core_loc_conf_t **) one;
     second = *(ngx_http_core_loc_conf_t **) two;
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 
     if (first->regex && !second->regex) {
         /* shift the regex matches to the end */
@@ -1026,7 +1026,7 @@ static char *ngx_location_block(ngx_conf
     ngx_http_conf_ctx_t       *ctx, *pctx;
     ngx_http_core_srv_conf_t  *cscf;
     ngx_http_core_loc_conf_t  *clcf, *pclcf, **clcfp;
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_str_t                  err;
     u_char                     errstr[NGX_MAX_CONF_ERRSTR];
 #endif
@@ -1076,7 +1076,7 @@ static char *ngx_location_block(ngx_conf
                        && value[1].data[0] == '~'
                        && value[1].data[1] == '*'))
         {
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
             err.len = NGX_MAX_CONF_ERRSTR;
             err.data = errstr;
 
@@ -1129,7 +1129,7 @@ static char *ngx_location_block(ngx_conf
             return NGX_CONF_ERROR;
         }
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
         if (clcf->regex == NULL
             && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
                                                                          != 0)
@@ -1323,12 +1323,8 @@ static char *ngx_http_core_merge_srv_con
 
         n->name.len = ngx_strlen(n->name.data);
         n->core_srv_conf = conf;
+        n->wildcard = 0;
 
-#if 0
-        ctx = (ngx_http_conf_ctx_t *)
-                                    cf->cycle->conf_ctx[ngx_http_module.index];
-        cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
-#endif
         cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
         if (cmcf->max_server_name_len < n->name.len) {
@@ -1512,7 +1508,7 @@ static char *ngx_http_core_merge_loc_con
 }
 
 
-static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+static char *ngx_http_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_core_srv_conf_t *scf = conf;
 
@@ -1607,13 +1603,8 @@ static char *ngx_set_server_name(ngx_con
     ngx_http_server_name_t     *sn;
     ngx_http_core_main_conf_t  *cmcf;
 
-    /* TODO: several names */
     /* TODO: warn about duplicate 'server_name' directives */
 
-#if 0
-    ctx = (ngx_http_conf_ctx_t *) cf->cycle->conf_ctx[ngx_http_module.index];
-    cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
-#endif
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
     value = cf->args->elts;
@@ -1627,12 +1618,23 @@ static char *ngx_set_server_name(ngx_con
             return NGX_CONF_ERROR;
         }
 
-        ngx_test_null(sn, ngx_push_array(&scf->server_names), NGX_CONF_ERROR);
+        if (!(sn = ngx_array_push(&scf->server_names))) {
+            return NGX_CONF_ERROR;
+        }
 
         sn->name.len = value[i].len;
         sn->name.data = value[i].data;
         sn->core_srv_conf = scf;
 
+        if (sn->name.data[0] == '*') {
+            sn->name.len--;
+            sn->name.data++;
+            sn->wildcard = 1;
+
+        } else {
+            sn->wildcard = 0;
+        }
+
         if (cmcf->max_server_name_len < sn->name.len) {
             cmcf->max_server_name_len = sn->name.len;
         }
@@ -1806,7 +1808,7 @@ static char *ngx_http_lowat_check(ngx_co
 {
     ssize_t *np = data;
 
-#if __FreeBSD__
+#if (NGX_FREEBSD)
 
     if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -96,13 +96,15 @@ typedef struct {
     ngx_http_core_srv_conf_t  *core_srv_conf;  /* default server conf
                                                   for this address:port */
 
-    unsigned                   default_server:1;
+    ngx_uint_t                 default_server; /* unsigned  default_server:1; */
 } ngx_http_in_addr_t;
 
 
 typedef struct {
     ngx_str_t                  name;
     ngx_http_core_srv_conf_t  *core_srv_conf; /* virtual name server conf */
+
+    ngx_uint_t                 wildcard;  /*unsigned  wildcard:1; */
 } ngx_http_server_name_t;
 
 
@@ -135,7 +137,7 @@ typedef struct ngx_http_core_loc_conf_s 
 struct ngx_http_core_loc_conf_s {
     ngx_str_t     name;          /* location name */
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_regex_t  *regex;
 #endif
 
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -317,10 +317,15 @@ static ngx_int_t ngx_http_header_filter(
 
     if (r->headers_out.content_length == NULL) {
         if (r->headers_out.content_length_n >= 0) {
+            b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
+                                  r->headers_out.content_length_n);
+
+#if 0
             b->last += ngx_snprintf((char *) b->last,
                                 sizeof("Content-Length: ") + NGX_OFF_T_LEN + 2,
                                 "Content-Length: " OFF_T_FMT CRLF,
                                 r->headers_out.content_length_n);
+#endif
         }
     }
 
@@ -372,7 +377,7 @@ static ngx_int_t ngx_http_header_filter(
     {
         b->last = ngx_cpymem(b->last, "Last-Modified: ",
                              sizeof("Last-Modified: ") - 1);
-        b->last += ngx_http_time(b->last, r->headers_out.last_modified_time);
+        b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
 
         *(b->last++) = CR; *(b->last++) = LF;
     }
@@ -389,10 +394,14 @@ static ngx_int_t ngx_http_header_filter(
         if (clcf->keepalive_header
             && (r->headers_in.gecko || r->headers_in.konqueror))
         {
+            b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
+                                  clcf->keepalive_header);
+#if 0
             b->last += ngx_snprintf((char *) b->last,
                             sizeof("Keep-Alive: timeout=") + TIME_T_LEN + 2,
                             "Keep-Alive: timeout=" TIME_T_FMT CRLF,
                             clcf->keepalive_header);
+#endif
         }
 
     } else {
--- a/src/http/ngx_http_log_handler.c
+++ b/src/http/ngx_http_log_handler.c
@@ -210,9 +210,13 @@ static u_char *ngx_http_log_addr(ngx_htt
 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
                                      uintptr_t data)
 {
+    return ngx_sprintf(buf, "%ui", r->connection->number);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_INT_T_LEN + 1,
                               "%" NGX_UINT_T_FMT,
                               r->connection->number);
+#endif
 }
 
 
@@ -244,8 +248,12 @@ static u_char *ngx_http_log_msec(ngx_htt
 
     ngx_gettimeofday(&tv);
 
+    return ngx_sprintf(buf, "%l.%03l", tv.tv_sec, tv.tv_usec / 1000);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, TIME_T_LEN + 5, "%ld.%03ld",
                               tv.tv_sec, tv.tv_usec / 1000);
+#endif
 }
 
 
@@ -264,24 +272,36 @@ static u_char *ngx_http_log_request(ngx_
 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
                                    uintptr_t data)
 {
+    return ngx_sprintf(buf, "%ui",
+                       r->err_status ? r->err_status : r->headers_out.status);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, 4, "%" NGX_UINT_T_FMT,
                         r->err_status ? r->err_status : r->headers_out.status);
+#endif
 }
 
 
 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
                                    uintptr_t data)
 {
+    return ngx_sprintf(buf, "%O", r->connection->sent);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
                               r->connection->sent);
+#endif
 }
 
 
 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
                                           uintptr_t data)
 {
+    return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
                               r->connection->sent - r->header_size);
+#endif
 }
 
 
@@ -467,8 +487,7 @@ static u_char *ngx_http_log_header_out(n
                     return (u_char *)
                                    sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
                 }
-                return buf + ngx_http_time(buf,
-                                           r->headers_out.last_modified_time);
+                return ngx_http_time(buf, r->headers_out.last_modified_time);
             }
 
             if (buf) {
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1091,27 +1091,47 @@ static ngx_int_t ngx_http_process_reques
 
         name = r->virtual_names->elts;
         for (i = 0; i < r->virtual_names->nelts; i++) {
-            if (r->headers_in.host_name_len != name[i].name.len) {
-                continue;
+
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "server name: %s", name[i].name.data);
+
+            if (name[i].wildcard) {
+                if (r->headers_in.host_name_len <= name[i].name.len) {
+                    continue;
+                }
+
+                if (ngx_rstrncasecmp(r->headers_in.host->value.data,
+                                     name[i].name.data,
+                                     name[i].name.len) == 0)
+                {
+                    continue;
+                }
+
+            } else {
+                if (r->headers_in.host_name_len != name[i].name.len) {
+                    continue;
+                }
+
+                if (ngx_strncasecmp(r->headers_in.host->value.data,
+                                    name[i].name.data,
+                                    name[i].name.len) != 0)
+                {
+                    continue;
+                }
             }
 
-            if (ngx_strncasecmp(r->headers_in.host->value.data,
-                                name[i].name.data,
-                                r->headers_in.host_name_len) == 0)
-            {
-                r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
-                r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
-                r->server_name = &name[i].name;
-
-                clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-                r->connection->log->file = clcf->err_log->file;
-                if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION))
-                {
-                    r->connection->log->log_level = clcf->err_log->log_level;
-                }
-
-                break;
+            r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
+            r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
+            r->server_name = &name[i].name;
+
+            clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+            r->connection->log->file = clcf->err_log->file;
+
+            if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
+                r->connection->log->log_level = clcf->err_log->log_level;
             }
+
+            break;
         }
 
         if (i == r->virtual_names->nelts) {
@@ -1562,7 +1582,13 @@ static void ngx_http_set_keepalive(ngx_h
 
         if (b != c->buffer) {
 
-            /* move the large header buffers to the free list */
+            /*
+             * If the large header buffers were allocated while the previous
+             * request processing then we do not use c->buffer for
+             * the pipelined request (see ngx_http_init_request()).
+             * 
+             * Now we would move the large header buffers to the free list.
+             */
 
             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
 
@@ -1614,6 +1640,14 @@ static void ngx_http_set_keepalive(ngx_h
 
     hc->pipeline = 0;
 
+    /*
+     * To keep a memory footprint as small as possible for an idle
+     * keepalive connection we try to free the ngx_http_request_t and
+     * c->buffer's memory if they were allocated outside the c->pool.
+     * The large header buffers are always allocated outside the c->pool and
+     * are freed too.
+     */
+
     if (ngx_pfree(c->pool, r) == NGX_OK) {
         hc->request = NULL;
     }
@@ -1621,6 +1655,12 @@ static void ngx_http_set_keepalive(ngx_h
     b = c->buffer;
 
     if (ngx_pfree(c->pool, b->start) == NGX_OK) {
+
+        /*
+         * the special note for ngx_http_keepalive_handler() that
+         * c->buffer's memory was freed
+         */
+
         b->pos = NULL;
 
     } else {
@@ -1655,7 +1695,7 @@ static void ngx_http_set_keepalive(ngx_h
     rev->event_handler = ngx_http_keepalive_handler;
 
     if (wev->active) {
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
                                                                   == NGX_ERROR)
             {
@@ -1702,7 +1742,7 @@ static void ngx_http_set_keepalive(ngx_h
     }
 
 #if 0
-    /* if "keepalive_buffers off" then we need some other place */
+    /* if ngx_http_request_t was freed then we need some other place */
     r->http_state = NGX_HTTP_KEEPALIVE_STATE;
 #endif
 
@@ -1734,7 +1774,7 @@ static void ngx_http_keepalive_handler(n
 
 #if (HAVE_KQUEUE)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         if (rev->pending_eof) {
             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
                           "kevent() reported that client %s closed "
@@ -1751,6 +1791,13 @@ static void ngx_http_keepalive_handler(n
     size = b->end - b->start;
 
     if (b->pos == NULL) {
+
+        /*
+         * The c->buffer's memory was freed by ngx_http_set_keepalive().
+         * However, the c->buffer->start and c->buffer->end were not changed
+         * to keep the buffer size.
+         */
+
         if (!(b->pos = ngx_palloc(c->pool, size))) {
             ngx_http_close_connection(c);
             return;
@@ -1824,7 +1871,7 @@ static void ngx_http_set_lingering_close
     wev->event_handler = ngx_http_empty_handler;
 
     if (wev->active) {
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
                                                                   == NGX_ERROR)
             {
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -38,11 +38,11 @@
 #define NGX_HTTP_PARSE_HEADER_ERROR        14
 #define NGX_HTTP_PARSE_INVALID_HEADER      14
 #define NGX_HTTP_PARSE_TOO_LONG_HEADER     15
-#define NGX_HTTP_PARSE_NO_HOST_HEADER      17
-#define NGX_HTTP_PARSE_INVALID_CL_HEADER   18
-#define NGX_HTTP_PARSE_POST_WO_CL_HEADER   19
-#define NGX_HTTP_PARSE_HTTP_TO_HTTPS       20
-#define NGX_HTTP_PARSE_INVALID_HOST        21
+#define NGX_HTTP_PARSE_NO_HOST_HEADER      16
+#define NGX_HTTP_PARSE_INVALID_CL_HEADER   17
+#define NGX_HTTP_PARSE_POST_WO_CL_HEADER   18
+#define NGX_HTTP_PARSE_HTTP_TO_HTTPS       19
+#define NGX_HTTP_PARSE_INVALID_HOST        20
 
 
 #define NGX_HTTP_OK                        200
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -125,6 +125,7 @@ ngx_int_t ngx_http_write_filter(ngx_http
         if (!last) {
             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                           "the http output chain is empty");
+            return NGX_ERROR;
         }
         return NGX_OK;
     }
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -210,59 +210,3 @@ int ngx_open_dir(ngx_str_t *name, ngx_di
 
     return NGX_OK;
 }
-
-
-#if 0
-
-ssize_t ngx_read_file(ngx_file_t *file, char *buf, size_t size, off_t offset)
-{
-    if (!file->read->ready) {
-
-        ngx_memzero(&file->iocb, sizeof(iocb));
-        file->iocb.aio_fildes = file->fd;
-        file->iocb.aio_buf = buf;
-        file->iocb.aio_nbytes = size;
-        file->iocb.aio_offset = offset;
-#if (USE_AIO_KQUEUE)
-        file->iocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
-        file->iocb.aio_sigevent.sigev_notify_kqueue = tid->kq;
-        file->iocb.aio_sigevent.sigev_value = (union sigval) file;
-#endif
-#if (USE_AIO_SIGNAL)
-        file->iocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
-        file->iocb.aio_sigevent.sigev_signo = NGX_SIGAIO;
-#ifndef __FreeBSD__
-        file->iocb.aio_sigevent.sigev_value.sival_ptr = file;
-#endif
-#endif
-
-        if (aio_read(&file->iocb) == -1) {
-            ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                          "aio_read() failed");
-            return NGX_ERROR;
-
-        n = aio_error(&file->iocb);
-        if (n == EINPROGRESS)
-            return NGX_AGAIN;
-
-        if (n == -1) {
-            ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                          "aio_read() failed");
-            return NGX_ERROR;
-        }
-    }
-
-    ngx_assert(file->iocb.aio_buf == buf), return NGX_ERROR,
-               "ngx_aio_read_file: another buffer is passed");
-
-    n = aio_return(&file->iocb);
-    if (n == -1) {
-        ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                      "aio_read() failed");
-        return NGX_ERROR;
-    }
-
-    return n;
-}
-
-#endif
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -99,7 +99,7 @@ int ngx_open_dir(ngx_str_t *name, ngx_di
 
 
 #define ngx_de_name(dir)         (dir)->de->d_name
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 #define ngx_de_namelen(dir)      (dir)->de->d_namlen
 #else
 #define ngx_de_namelen(dir)      ngx_strlen((dir)->de->d_name)
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -22,8 +22,8 @@
 #include <grp.h>
 #include <dirent.h>
 
+#include <sys/filio.h>          /* FIONBIO */
 #include <sys/uio.h>
-#include <sys/filio.h>          /* FIONBIO */
 #include <sys/stat.h>
 #include <fcntl.h>
 
@@ -41,12 +41,11 @@
 #include <libutil.h>            /* setproctitle() before 4.1 */
 #include <osreldate.h>
 #include <sys/sysctl.h>
+#include <sys/param.h>          /* ALIGN() */
 
 
 #if __FreeBSD_version < 400017
 
-#include <sys/param.h>          /* ALIGN() */
-
 /* FreeBSD 3.x has no CMSG_SPACE() at all and has the broken CMSG_DATA() */
 
 #undef  CMSG_SPACE
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -57,7 +57,7 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");
 
@@ -131,7 +131,6 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
             send += size;
         }
 
-        /* get the file buf */
 
         if (cl && cl->buf->in_file && send < limit) {
             file = cl->buf;
@@ -164,17 +163,18 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
                      && fprev == cl->buf->file_pos);
         }
 
+
         if (file) {
+
             /* create the tailer iovec and coalesce the neighbouring bufs */
 
             prev = NULL;
             iov = NULL;
 
-            for (/* void */;
-                 cl && header.nelts < IOV_MAX && send < limit;
-                 cl = cl->next)
-            {
+            while (cl && header.nelts < IOV_MAX && send < limit) {
+
                 if (ngx_buf_special(cl->buf)) {
+                    cl = cl->next;
                     continue;
                 }
 
@@ -202,6 +202,7 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
 
                 prev = cl->buf->pos + size;
                 send += size;
+                cl = cl->next;
             }
         }
 
@@ -210,7 +211,6 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
             if (ngx_freebsd_use_tcp_nopush
                 && c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
             {
-
                 if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
                     err = ngx_errno;
 
@@ -275,6 +275,20 @@ ngx_chain_t *ngx_freebsd_sendfile_chain(
                 }
             }
 
+            if (rc == 0 && sent == 0) {
+
+                /*
+                 * rc and sent are equals to zero when someone has truncated
+                 * the file, so the offset became beyond the end of the file
+                 */
+
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "sendfile() reported that \"%s\" was truncated",
+                              file->file->name.data);
+
+                return NGX_CHAIN_ERROR;
+            }
+
             ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
                            "sendfile: %d, @" OFF_T_FMT " " OFF_T_FMT ":%d",
                            rc, file->file_pos, sent, fsize + hsize);
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -8,11 +8,11 @@
 #define _NGX_LINUX_CONFIG_H_INCLUDED_
 
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE             /* pread(), pwrite(), gethostname() */
+#endif
 
 #define _FILE_OFFSET_BITS  64
-#define _LARGEFILE_SOURCE
-
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -51,11 +51,11 @@
 #include <ngx_auto_config.h>
 
 
-#if (HAVE_PRCTL)
+#if (NGX_HAVE_SYS_PRCTL_H)
 #include <sys/prctl.h>
 #endif
 
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
 #include <sys/sendfile.h>
 #else
 extern ssize_t sendfile(int s, int fd, int32_t *offset, size_t size);
--- a/src/os/unix/ngx_linux_sendfile_chain.c
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -37,7 +37,7 @@ ngx_chain_t *ngx_linux_sendfile_chain(ng
     ngx_event_t     *wev;
     ngx_chain_t     *cl;
     struct iovec    *iov, headers[NGX_HEADERS];
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
     off_t            offset;
 #else
     int32_t          offset;
@@ -167,7 +167,7 @@ ngx_chain_t *ngx_linux_sendfile_chain(ng
         }
 
         if (file) {
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
             offset = file->file_pos;
 #else
             offset = (int32_t) file->file_pos;
--- a/src/os/unix/ngx_os.h
+++ b/src/os/unix/ngx_os.h
@@ -66,17 +66,15 @@ extern ngx_int_t    ngx_inherited_nonblo
 
 #define ngx_stderr_fileno  STDERR_FILENO
 
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 #include <ngx_freebsd.h>
-#endif
 
 
-#ifdef __linux__
+#elif (NGX_LINUX)
 #include <ngx_linux.h>
-#endif
 
 
-#ifdef SOLARIS
+#elif (NGX_SOLARIS)
 #include <ngx_solaris.h>
 #endif
 
--- a/src/os/unix/ngx_posix_config.h
+++ b/src/os/unix/ngx_posix_config.h
@@ -8,9 +8,20 @@
 #define _NGX_POSIX_CONFIG_H_INCLUDED_
 
 
+#if 0
+#define _XOPEN_SOURCE
+#define _XOPEN_SOURCE_EXTENDED  1
+#endif
+
+
 #include <sys/types.h>
 #include <sys/time.h>
+#if (NGX_HAVE_UNISTD_H)
 #include <unistd.h>
+#endif
+#if (NGX_HAVE_INTTYPES_H)
+#include <inttypes.h>
+#endif
 #include <stdarg.h>
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
@@ -22,8 +33,12 @@
 #include <grp.h>
 #include <dirent.h>
 
+#if (NGX_HAVE_SYS_FILIO_H)
+#include <sys/filio.h>          /* FIONBIO */
+#endif
+#include <sys/ioctl.h>          /* FIONBIO */
+
 #include <sys/uio.h>
-#include <sys/filio.h>          /* FIONBIO */
 #include <sys/stat.h>
 #include <fcntl.h>
 
@@ -34,9 +49,19 @@
 
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>        /* TCP_NODELAY */
 #include <arpa/inet.h>
 #include <netdb.h>
 
+#if (NGX_HAVE_LIMITS_H)
+#include <limits.h>             /* IOV_MAX */
+#endif
+
+#ifndef IOV_MAX
+#define IOV_MAX   16
+#endif
+
+
 #include <ngx_auto_config.h>
 
 
@@ -53,6 +78,32 @@
 #endif
 
 
+#if (HAVE_KQUEUE)
+#include <sys/event.h>
+#endif
+
+
+#if (HAVE_DEVPOLL)
+#include <sys/ioctl.h>
+#include <sys/devpoll.h>
+#endif
+
+
+#if (__FreeBSD__) && (__FreeBSD_version < 400017)
+
+#include <sys/param.h>          /* ALIGN() */
+
+/* FreeBSD 3.x has no CMSG_SPACE() at all and has the broken CMSG_DATA() */
+
+#undef  CMSG_SPACE
+#define CMSG_SPACE(l)       (ALIGN(sizeof(struct cmsghdr)) + ALIGN(l))
+
+#undef  CMSG_DATA
+#define CMSG_DATA(cmsg)     ((u_char *)(cmsg) + ALIGN(sizeof(struct cmsghdr)))
+
+#endif
+
+
 #define ngx_setproctitle(title)
 
 
--- a/src/os/unix/ngx_posix_init.c
+++ b/src/os/unix/ngx_posix_init.c
@@ -33,6 +33,12 @@ int ngx_os_init(ngx_log_t *log)
 }
 
 
+void ngx_os_status(ngx_log_t *log)
+{
+    ngx_posix_status(log);
+}
+
+
 #endif
 
 
--- a/src/os/unix/ngx_process.c
+++ b/src/os/unix/ngx_process.c
@@ -236,7 +236,7 @@ void ngx_process_get_status()
                 return;
             }
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
             /*
              * Solaris always calls the signal handler for each exited process
--- a/src/os/unix/ngx_process.h
+++ b/src/os/unix/ngx_process.h
@@ -54,7 +54,13 @@ ngx_pid_t ngx_spawn_process(ngx_cycle_t 
 ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx);
 void ngx_process_get_status(void);
 
+
+#if (NGX_HAVE_SCHED_YIELD)
 #define ngx_sched_yield()  sched_yield()
+#else
+#define ngx_sched_yield()  usleep(1)
+#endif
+
 
 extern int            ngx_argc;
 extern char         **ngx_argv;
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -175,11 +175,14 @@ void ngx_master_process_cycle(ngx_cycle_
 
         if (ngx_timer) {
             ngx_timer = 0;
-            ngx_start_worker_processes(cycle, ccf->worker_processes,
-                                       NGX_PROCESS_JUST_RESPAWN);
-            live = 1;
-            ngx_signal_worker_processes(cycle,
+
+            if (!ngx_noaccepting) {
+                ngx_start_worker_processes(cycle, ccf->worker_processes,
+                                           NGX_PROCESS_JUST_RESPAWN);
+                live = 1;
+                ngx_signal_worker_processes(cycle,
                                         ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
+            }
         }
 
         if (ngx_reconfigure) {
@@ -578,16 +581,14 @@ static void ngx_worker_process_cycle(ngx
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    if (ccf->group != (gid_t) NGX_CONF_UNSET) {
+    if (geteuid() == 0) {
         if (setgid(ccf->group) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                           "setgid(%d) failed", ccf->group);
             /* fatal */
             exit(2);
         }
-    }
 
-    if (ccf->user != (uid_t) NGX_CONF_UNSET) {
         if (setuid(ccf->user) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                           "setuid(%d) failed", ccf->user);
@@ -596,7 +597,7 @@ static void ngx_worker_process_cycle(ngx
         }
     }
 
-#if (HAVE_PR_SET_DUMPABLE)
+#if (NGX_HAVE_PR_SET_DUMPABLE)
 
     /* allow coredump after setuid() in Linux 2.4.x */
 
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -22,7 +22,7 @@ ssize_t ngx_readv_chain(ngx_connection_t
 
     rev = c->read; 
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
                        "readv: eof:%d, avail:%d, err:%d",
                        rev->pending_eof, rev->available, rev->kq_errno);
@@ -81,7 +81,7 @@ ssize_t ngx_readv_chain(ngx_connection_t
         n = readv(c->fd, (struct iovec *) io.elts, io.nelts);
 
         if (n >= 0) {
-            if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                 rev->available -= n;
 
                 /*
@@ -186,7 +186,7 @@ ssize_t ngx_readv_chain(ngx_connection_t
 
         } else if (n > 0) {
 
-            if (n < size && !(ngx_event_flags & NGX_HAVE_GREEDY_EVENT)) {
+            if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
                 rev->ready = 0;
             }
 
--- a/src/os/unix/ngx_recv.c
+++ b/src/os/unix/ngx_recv.c
@@ -19,7 +19,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
 
     rev = c->read;
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
                        "recv: eof:%d, avail:%d, err:%d",
                        rev->pending_eof, rev->available, rev->kq_errno);
@@ -60,7 +60,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
                        "recv: fd:%d %d of %d", c->fd, n, size);
 
         if (n >= 0) {
-            if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                 rev->available -= n;
 
                 /*
@@ -139,7 +139,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
         } else if (n > 0) {
 
             if ((size_t) n < size
-                && !(ngx_event_flags & NGX_HAVE_GREEDY_EVENT))
+                && !(ngx_event_flags & NGX_USE_GREEDY_EVENT))
             {
                 rev->ready = 0;
             }
--- a/src/os/unix/ngx_send.c
+++ b/src/os/unix/ngx_send.c
@@ -19,7 +19,7 @@ ssize_t ngx_unix_send(ngx_connection_t *
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");
 
--- a/src/os/unix/ngx_setproctitle.c
+++ b/src/os/unix/ngx_setproctitle.c
@@ -76,7 +76,7 @@ void ngx_setproctitle(char *title)
 {
     u_char     *p;
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
     ngx_int_t   i;
     size_t      size;
@@ -90,7 +90,7 @@ void ngx_setproctitle(char *title)
 
     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
     size = 0;
 
--- a/src/os/unix/ngx_socket.c
+++ b/src/os/unix/ngx_socket.c
@@ -44,7 +44,7 @@ int ngx_blocking(ngx_socket_t s)
 #endif
 
 
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 
 int ngx_tcp_nopush(ngx_socket_t s)
 {
@@ -67,7 +67,7 @@ int ngx_tcp_push(ngx_socket_t s)
                       (const void *) &tcp_nopush, sizeof(int));
 }
 
-#elif __linux__
+#elif (NGX_LINUX)
 
 int ngx_tcp_nopush(ngx_socket_t s)
 {
--- a/src/os/unix/ngx_socket.h
+++ b/src/os/unix/ngx_socket.h
@@ -37,7 +37,7 @@ int ngx_blocking(ngx_socket_t s);
 int ngx_tcp_nopush(ngx_socket_t s);
 int ngx_tcp_push(ngx_socket_t s);
 
-#ifdef __linux__
+#if (NGX_LINUX)
 
 #define ngx_tcp_nopush_n   "setsockopt(TCP_CORK)"
 #define ngx_tcp_push_n     "setsockopt(!TCP_CORK)"
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -8,8 +8,6 @@
 #define _NGX_SOLARIS_CONFIG_H_INCLUDED_
 
 
-#define SOLARIS  1
-
 #define _REENTRANT
 
 #define _FILE_OFFSET_BITS  64   /* must be before <sys/types.h> */
@@ -28,8 +26,8 @@
 #include <grp.h>
 #include <dirent.h>
 
+#include <sys/filio.h>          /* FIONBIO */
 #include <sys/uio.h>
-#include <sys/filio.h>          /* FIONBIO */
 #include <sys/stat.h>
 #include <fcntl.h>
 
--- a/src/os/unix/ngx_thread.h
+++ b/src/os/unix/ngx_thread.h
@@ -28,7 +28,7 @@ typedef pthread_t                    ngx
 #define ngx_thread_self()            pthread_self()
 #define ngx_log_tid                  (int) ngx_thread_self()
 
-#if defined(__FreeBSD__) && !defined(NGX_LINUXTHREADS)
+#if (NGX_FREEBSD) && !(NGX_LINUXTHREADS)
 #define TID_T_FMT                    PTR_FMT
 #else
 #define TID_T_FMT                    "%d"
--- a/src/os/unix/ngx_time.h
+++ b/src/os/unix/ngx_time.h
@@ -42,8 +42,14 @@ typedef struct tm      ngx_tm_t;
 #endif
 
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
+
 #define ngx_timezone(isdst) (- (isdst ? altzone : timezone) / 60)
+
+#else
+
+#define ngx_timezone(isdst) (- (isdst ? timezone + 3600 : timezone) / 60)
+
 #endif
 
 
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -32,7 +32,7 @@ ngx_chain_t *ngx_writev_chain(ngx_connec
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");