Imported Upstream version 3.2 upstream/3.2
authorJinWang An <jinwang.an@samsung.com>
Tue, 3 Aug 2021 07:16:19 +0000 (16:16 +0900)
committerJinWang An <jinwang.an@samsung.com>
Tue, 3 Aug 2021 07:16:19 +0000 (16:16 +0900)
89 files changed:
AUTHORS.html
AUTHORS.txt
HACKING.txt [new file with mode: 0644]
INSTALL.html
INSTALL.txt
LICENSE.html
LICENSE.txt
MANUAL.html
MANUAL.txt
Makefile.in
NEWS.html
NEWS.txt
README.html
args.c
autogen.sh [new file with mode: 0755]
ccache.1
ccache.c
ccache.h
cleanup.c
compopt.c
compopt.h
conf.c [new file with mode: 0644]
conf.h [new file with mode: 0644]
config.guess [new file with mode: 0755]
config.h.in
config.sub [new file with mode: 0755]
configure
configure.ac [new file with mode: 0644]
confitems.gperf [new file with mode: 0644]
confitems_lookup.c [new file with mode: 0644]
dev.mk.in [new file with mode: 0644]
dev_mode_disabled [new file with mode: 0644]
envtoconfitems.gperf [new file with mode: 0644]
envtoconfitems_lookup.c [new file with mode: 0644]
execute.c
exitfn.c
hash.c
hashutil.c
hashutil.h
language.c
lockfile.c
manifest.c
manifest.h
mdfour.c
stats.c
system.h
test.sh
test/framework.c
test/framework.h
test/main.c
test/suites.h
test/test_args.c
test/test_argument_processing.c
test/test_compopt.c
test/test_conf.c [new file with mode: 0644]
test/test_counters.c
test/test_hashutil.c
test/test_lockfile.c
test/test_stats.c
test/test_util.c
test/util.c
unify.c
util.c
version.c
zlib/adler32.c
zlib/compress.c [deleted file]
zlib/crc32.c
zlib/crc32.h
zlib/deflate.c
zlib/deflate.h
zlib/gzclose.c [new file with mode: 0644]
zlib/gzguts.h [new file with mode: 0644]
zlib/gzio.c [deleted file]
zlib/gzlib.c [new file with mode: 0644]
zlib/gzread.c [new file with mode: 0644]
zlib/gzwrite.c [new file with mode: 0644]
zlib/inffast.c
zlib/inffast.h
zlib/inffixed.h
zlib/inflate.c
zlib/inflate.h
zlib/inftrees.c
zlib/inftrees.h
zlib/trees.c
zlib/trees.h
zlib/zconf.h
zlib/zlib.h
zlib/zutil.c
zlib/zutil.h

index afb5688c9736479e7bd4c25f33f905e577a8e625..0ddc35c5dcaa54b5b98f4de178d7f99c2e447895 100644 (file)
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>ccache authors</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -759,6 +759,11 @@ Andrea Bittau &lt;<a href="mailto:a.bittau@cs.ucl.ac.uk">a.bittau@cs.ucl.ac.uk</
 </li>\r
 <li>\r
 <p>\r
+Andrew P Boie &lt;<a href="mailto:andrew.p.boie@intel.com">andrew.p.boie@intel.com</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Andrew Stubbs &lt;<a href="mailto:ams@codesourcery.com">ams@codesourcery.com</a>&gt;\r
 </p>\r
 </li>\r
@@ -784,11 +789,21 @@ Bo Rydberg &lt;<a href="mailto:bolry@hotmail.com">bolry@hotmail.com</a>&gt;
 </li>\r
 <li>\r
 <p>\r
+Chris AtLee &lt;<a href="mailto:chris@atlee.ca">chris@atlee.ca</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Clemens Rabe &lt;<a href="mailto:crabe@gmx.de">crabe@gmx.de</a>&gt;\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
+David Givone &lt;<a href="mailto:david@givone.net">david@givone.net</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Eric Blau &lt;<a href="mailto:Eric.Blau@tekelec.com">Eric.Blau@tekelec.com</a>&gt;\r
 </p>\r
 </li>\r
@@ -799,6 +814,11 @@ Francois Marier &lt;<a href="mailto:francois@debian.org">francois@debian.org</a>
 </li>\r
 <li>\r
 <p>\r
+Hongli Lai &lt;<a href="mailto:hongli@phusion.nl">hongli@phusion.nl</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Joel Rosdahl &lt;<a href="mailto:joel@rosdahl.net">joel@rosdahl.net</a>&gt;\r
 </p>\r
 </li>\r
@@ -809,6 +829,21 @@ John Coiner &lt;<a href="mailto:john.coiner@amd.com">john.coiner@amd.com</a>&gt;
 </li>\r
 <li>\r
 <p>\r
+Jon Bernard &lt;<a href="mailto:jbernard@tuxion.com">jbernard@tuxion.com</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Justin Lebar &lt;<a href="mailto:justin.lebar@gmail.com">justin.lebar@gmail.com</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Jørgen P. Tjernø &lt;<a href="mailto:jorgen@valvesoftware.com">jorgen@valvesoftware.com</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Karl Chen &lt;<a href="mailto:quarl@cs.berkeley.edu">quarl@cs.berkeley.edu</a>&gt;\r
 </p>\r
 </li>\r
@@ -829,7 +864,12 @@ Lars Gustäbel &lt;<a href="mailto:lars@gustaebel.de">lars@gustaebel.de</a>&gt;
 </li>\r
 <li>\r
 <p>\r
-Lubos Lunak &lt;<a href="mailto:l.lunak@suse.cz">l.lunak@suse.cz</a>&gt;\r
+LuboÅ¡ Luňák &lt;<a href="mailto:l.lunak@suse.cz">l.lunak@suse.cz</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Mark Starovoytov &lt;<a href="mailto:starovoytov.mark@googlemail.com">starovoytov.mark@googlemail.com</a>&gt;\r
 </p>\r
 </li>\r
 <li>\r
@@ -849,6 +889,11 @@ Mike Frysinger &lt;<a href="mailto:vapier@gentoo.org">vapier@gentoo.org</a>&gt;
 </li>\r
 <li>\r
 <p>\r
+Orgad Shaneh &lt;<a href="mailto:orgad.shaneh@audiocodes.com">orgad.shaneh@audiocodes.com</a>&gt;\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 Owen Mann &lt;<a href="mailto:owen@mann.org">owen@mann.org</a>&gt;\r
 </p>\r
 </li>\r
@@ -915,9 +960,8 @@ Yiding Jia &lt;<a href="mailto:yiding@fb.com">yiding@fb.com</a>&gt;
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2014-11-17 19:55:54 CET\r
 </div>\r
 </div>\r
 </body>\r
index 7ce3afaea6ea49113f1a0a2168c07f66d59cd36c..391000b38b55591a69efcdb2092f1a1d05d0c238 100644 (file)
@@ -8,24 +8,33 @@ ccache is a collective work with contributions from many people, including:
 
 * Alfred Landrum <alfred.landrum@riverbed.com>
 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
+* Andrew P Boie <andrew.p.boie@intel.com>
 * Andrew Stubbs <ams@codesourcery.com>
 * Andrew Tridgell <tridge@samba.org>
 * Bernhard Bauer <bauerb@chromium.org>
 * Björn Jacke <bj@sernet.de>
 * Bo Rydberg <bolry@hotmail.com>
+* Chris AtLee <chris@atlee.ca>
 * Clemens Rabe <crabe@gmx.de>
+* David Givone <david@givone.net>
 * Eric Blau <Eric.Blau@tekelec.com>
 * Francois Marier <francois@debian.org>
+* Hongli Lai <hongli@phusion.nl>
 * Joel Rosdahl <joel@rosdahl.net>
 * John Coiner <john.coiner@amd.com>
+* Jon Bernard <jbernard@tuxion.com>
+* Justin Lebar <justin.lebar@gmail.com>
+* Jørgen P. Tjernø <jorgen@valvesoftware.com>
 * Karl Chen <quarl@cs.berkeley.edu>
 * Kovarththanan Rajaratnam <kovarththanan.rajaratnam@gmail.com>
 * Lalit Chhabra <lchhabra@linuxmail.org>
 * Lars Gustäbel <lars@gustaebel.de>
-* Lubos Lunak <l.lunak@suse.cz>
+* LuboÅ¡ Luňák <l.lunak@suse.cz>
+* Mark Starovoytov <starovoytov.mark@googlemail.com>
 * Martin Pool <mbp@sourcefrog.net>
 * Michael Meeks <michael.meeks@suse.com>
 * Mike Frysinger <vapier@gentoo.org>
+* Orgad Shaneh <orgad.shaneh@audiocodes.com>
 * Owen Mann <owen@mann.org>
 * Patrick von Reth <vonreth@kde.org>
 * Paul Griffith <paulg@cse.yorku.ca>
diff --git a/HACKING.txt b/HACKING.txt
new file mode 100644 (file)
index 0000000..36de458
--- /dev/null
@@ -0,0 +1,59 @@
+Hacking ccache
+==============
+
+Code formatting
+---------------
+
+* Use tabs for indenting and spaces for aligning C code. See
+  <http://www.emacswiki.org/emacs/SmartTabs>.
+* Use 4 spaces for indenting other code (and spaces for aligning).
+* Put the opening curly brace on a new line when defining a function, otherwise
+  at the end of the same line.
+* Put no space between function name and the following parenthesis.
+* Put one space between if/switch/for/while/do and opening curly brace.
+* If possible, keep lines at most 80 character wide for a 2 character tab
+  width.
+* Use only lowercase names for functions and variables.
+* Use only uppercase names for enum items and (with some exceptions) macros.
+* Don't use typedefs for structs and enums.
+
+Idioms
+------
+
+* Use NULL to initialize null pointers.
+* Don't use NULL when comparing pointers.
+* Use format(), x_malloc() and friends instead of checking for memory
+  allocation failure explicitly.
+* Use str_eq() instead of strcmp() when testing for string (in)equality.
+* Consider using str_startswith() instead of strncmp().
+* Use bool, true and false for boolean values.
+* Use tmp_unlink() or x_unlink() instead of unlink().
+* Use x_rename() instead of rename().
+
+Other
+-----
+
+* Strive to minimize use of global variables.
+* Write test cases for new code.
+
+Commit messages
+---------------
+
+* Write a short description on the first line. If wanted, leave the second line
+  empty and write a longer description on line three and below.
+* Start the short description with a capital letter. Optional: prefix the short
+  description with a context followed by a colon.
+* The short description should be in "command form" (see examples below).
+* Don't put a final period after the short description.
+* Keep lines in the message at most 75 characters wide.
+
+Example 1:
+
+    Hash a delimiter string between parts to separate them
+
+    Previously, "gcc -I-O2 -c file.c" and "gcc -I -O2 -c file.c" would hash to
+    the same sum.
+
+Example 2:
+
+    win32: Add a space between filename and error string in x_fmmap()
index 30e10ade3c7294c9a93d127c144b8eefeb3d2c5a..3f29e9fb729c7f0eb3cb84231e981ec28100ae6a 100644 (file)
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>ccache installation</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -821,6 +821,16 @@ AsciiDoc (<a href="http://www.methods.co.nz/asciidoc/">http://www.methods.co.nz/
 Autoconf (<a href="http://www.gnu.org/software/autoconf/">http://www.gnu.org/software/autoconf/</a>)\r
 </p>\r
 </li>\r
+<li>\r
+<p>\r
+gperf (<a href="http://www.gnu.org/software/gperf/">http://www.gnu.org/software/gperf/</a>)\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+xsltproc (<a href="http://xmlsoft.org/XSLT/xsltproc2.html">http://xmlsoft.org/XSLT/xsltproc2.html</a>)\r
+</p>\r
+</li>\r
 </ul></div>\r
 <div class="paragraph"><p>To debug and run the performance test suite you&#8217;ll also need:</p></div>\r
 <div class="ulist"><ul>\r
@@ -843,9 +853,8 @@ above.</p></div>
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2014-11-15 16:04:58 CET\r
 </div>\r
 </div>\r
 </body>\r
index 1968d2bc8d3cc25e8b500a42818140b77a41b7f2..256beabc4130ca7c692dc8cc5b6007fe0c016354 100644 (file)
@@ -62,6 +62,8 @@ In addition to the prerequisites mentioned above, you also need:
 
 - AsciiDoc (http://www.methods.co.nz/asciidoc/) to build the documentation.
 - Autoconf (http://www.gnu.org/software/autoconf/)
+- gperf (http://www.gnu.org/software/gperf/)
+- xsltproc (http://xmlsoft.org/XSLT/xsltproc2.html)
 
 To debug and run the performance test suite you'll also need:
 
index 4aa1086d1dd931d2db0c6341a3a9febe5ab53abb..aea9be7ab7935f915fd6b7b3fa61bb99b576f917 100644 (file)
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>ccache copyright and license</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -776,7 +776,7 @@ copyrights on their portions of the work.</p></div>
 <div class="listingblock">\r
 <div class="content">\r
 <pre><code>  Copyright (C) 2002-2007 Andrew Tridgell\r
-  Copyright (C) 2009-2016 Joel Rosdahl</code></pre>\r
+  Copyright (C) 2009-2011 Joel Rosdahl</code></pre>\r
 </div></div>\r
 </div>\r
 </div>\r
@@ -1173,11 +1173,11 @@ Appleby. See <a href="http://murmurhash.googlepages.com">http://murmurhash.googl
 </div>\r
 <div class="sect2">\r
 <h3 id="_zlib_hc">zlib/*.[hc]</h3>\r
-<div class="paragraph"><p>This is a bundled subset of zlib 1.2.3 from <a href="http://www.zlib.net">http://www.zlib.net</a> with the\r
+<div class="paragraph"><p>This is a bundled subset of zlib 1.2.5 from <a href="http://zlib.net">http://zlib.net</a> with the\r
 following license:</p></div>\r
 <div class="listingblock">\r
 <div class="content">\r
-<pre><code>  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler\r
+<pre><code>  Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler\r
 \r
   This software is provided 'as-is', without any express or implied\r
   warranty.  In no event will the authors be held liable for any damages\r
@@ -1196,12 +1196,7 @@ following license:</p></div>
   3. This notice may not be removed or altered from any source distribution.\r
 \r
   Jean-loup Gailly        Mark Adler\r
-  jloup@gzip.org          madler@alumni.caltech.edu\r
-\r
-\r
-  The data format used by the zlib library is described by RFCs (Request for\r
-  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt\r
-  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).</code></pre>\r
+  jloup@gzip.org          madler@alumni.caltech.edu</code></pre>\r
 </div></div>\r
 </div>\r
 </div>\r
@@ -1210,9 +1205,8 @@ following license:</p></div>
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2014-11-15 16:04:58 CET\r
 </div>\r
 </div>\r
 </body>\r
index 60bbce8bbf165e56c25408167726cd7a804a645f..7186d5a41c419be5289f950678a005f67d15a403 100644 (file)
@@ -38,7 +38,7 @@ The copyright for ccache as a whole is as follows:
 
 -------------------------------------------------------------------------------
   Copyright (C) 2002-2007 Andrew Tridgell
-  Copyright (C) 2009-2016 Joel Rosdahl
+  Copyright (C) 2009-2011 Joel Rosdahl
 -------------------------------------------------------------------------------
 
 
@@ -448,11 +448,11 @@ http://www.jhweiss.de/software/snprintf.html and has the following license:
 zlib/*.[hc]
 ~~~~~~~~~~~
 
-This is a bundled subset of zlib 1.2.3 from <http://www.zlib.net> with the
+This is a bundled subset of zlib 1.2.5 from <http://zlib.net> with the
 following license:
 
 -------------------------------------------------------------------------------
-  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
+  Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -472,9 +472,4 @@ following license:
 
   Jean-loup Gailly        Mark Adler
   jloup@gzip.org          madler@alumni.caltech.edu
-
-
-  The data format used by the zlib library is described by RFCs (Request for
-  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
-  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
 -------------------------------------------------------------------------------
index d17a142d38e43cc03ba74010aa41265a2b2f0f00..82707f69776e4f81f9cd0d8335d0311a05fde6a5 100644 (file)
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>CCACHE(1)</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -893,7 +893,7 @@ compiler options apply and you should refer to the compiler&#8217;s documentatio
 <p>\r
     Clean up the cache by removing old cached files until the specified file\r
     number and cache size limits are not exceeded. This also recalculates the\r
-    cache file count and size totals. Normally, it&#8217;s not needed to initiate\r
+    cache file count and size totals. Normally, there is no need to initiate\r
     cleanup manually as ccache keeps the cache below the specified limits at\r
     runtime and keeps statistics up to date on each compilation. Forcing a\r
     cleanup is mostly useful if you manually modify the cache contents or\r
@@ -905,7 +905,8 @@ compiler options apply and you should refer to the compiler&#8217;s documentatio
 </dt>\r
 <dd>\r
 <p>\r
-    Clear the entire cache, removing all cached files.\r
+    Clear the entire cache, removing all cached files, but keeping the\r
+    configuration file.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
@@ -913,10 +914,9 @@ compiler options apply and you should refer to the compiler&#8217;s documentatio
 </dt>\r
 <dd>\r
 <p>\r
-    Set the maximum number of files allowed in the cache. The value is stored\r
-    inside the cache directory and applies to all future compilations. Due to\r
-    the way the value is stored the actual value used is always rounded down to\r
-    the nearest multiple of 16.\r
+    Set the maximum number of files allowed in the cache. Use 0 for no limit.\r
+    The value is stored in a configuration file in the cache directory and\r
+    applies to all future compilations.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
@@ -932,10 +932,29 @@ compiler options apply and you should refer to the compiler&#8217;s documentatio
 </dt>\r
 <dd>\r
 <p>\r
-    Set the maximum size of the files stored in the cache. You can specify a\r
-    value in gigabytes, megabytes or kilobytes by appending a G, M or K to the\r
-    value. The default is gigabytes. The actual value stored is rounded down to\r
-    the nearest multiple of 16 kilobytes.\r
+    Set the maximum size of the files stored in the cache. <em>SIZE</em> should be a\r
+    number followed by an optional suffix: k, M, G, T (decimal), Ki, Mi, Gi or\r
+    Ti (binary). The default suffix is G. Use 0 for no limit. The value is\r
+    stored in a configuration file in the cache directory and applies to all\r
+    future compilations.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
+<strong>-o, --set-config</strong>=<em>KEY=VALUE</em>\r
+</dt>\r
+<dd>\r
+<p>\r
+    Set configuration <em>KEY</em> to <em>VALUE</em>. See <a href="#_configuration">CONFIGURATION</a>\r
+    for more information.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
+<strong>-p, --print-config</strong>\r
+</dt>\r
+<dd>\r
+<p>\r
+    Print current configuration options and from where they originate\r
+    (environment variable, configuration file or compile-time default).\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
@@ -959,7 +978,7 @@ compiler options apply and you should refer to the compiler&#8217;s documentatio
 </dt>\r
 <dd>\r
 <p>\r
-    Zero the cache statistics (but not the configured limits).\r
+    Zero the cache statistics (but not the configuration options).\r
 </p>\r
 </dd>\r
 </dl></div>\r
@@ -988,41 +1007,120 @@ compiler than what ccache thinks.</p></div>
 </div>\r
 </div>\r
 <div class="sect1">\r
-<h2 id="_environment_variables">Environment variables</h2>\r
+<h2 id="_configuration">Configuration</h2>\r
 <div class="sectionbody">\r
-<div class="paragraph"><p>ccache uses a number of environment variables to control operation. In most\r
-cases you won&#8217;t need any of these as the defaults will be fine.</p></div>\r
+<div class="paragraph"><p>ccache&#8217;s default behavior can be overridden by configuration file settings,\r
+which in turn can be overridden by environment variables with names starting\r
+with <strong>CCACHE_</strong>. ccache normally reads configuration from two files: first a\r
+system-level configuration file and secondly a cache-specific configuration\r
+file. The priority of configuration settings is as follows (where 1 is\r
+highest):</p></div>\r
+<div class="olist arabic"><ol class="arabic">\r
+<li>\r
+<p>\r
+Environment variables.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The cache-specific configuration file <strong>&lt;ccachedir&gt;/ccache.conf</strong> (typically\r
+   <strong>$HOME/.ccache/ccache.conf</strong>).\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The system-wide configuration file <strong>&lt;sysconfdir&gt;/ccache.conf</strong> (typically\r
+   <strong>/etc/ccache.conf</strong> or <strong>/usr/local/etc/ccache.conf</strong>).\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Compile-time defaults.\r
+</p>\r
+</li>\r
+</ol></div>\r
+<div class="paragraph"><p>As a special case, if the environment variable <strong>CCACHE_CONFIGPATH</strong> is set,\r
+ccache reads configuration from the specified path instead of the default\r
+paths.</p></div>\r
+<div class="sect2">\r
+<h3 id="_configuration_file_syntax">Configuration file syntax</h3>\r
+<div class="paragraph"><p>Configuration files are in a simple &#8220;key = value&#8221; format, one setting per\r
+line. Lines starting with a hash sign are comments. Blank lines are ignored, as\r
+is whitespace surrounding keys and values. Example:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><code># Set maximum cache size to 10 GB:\r
+max_size = 10G</code></pre>\r
+</div></div>\r
+</div>\r
+<div class="sect2">\r
+<h3 id="_boolean_values">Boolean values</h3>\r
+<div class="paragraph"><p>Some settings are boolean values (i.e. truth values). In a configuration file,\r
+such values must be set to the string <strong>true</strong> or <strong>false</strong>. For the corresponding\r
+environment variables, the semantics are a bit different: a set environment\r
+variable means &#8220;true&#8221; regardless of the value (even if set to the empty\r
+string), and an unset environment variable means &#8220;false&#8221;. Each boolean\r
+environment variable also has a negated form starting with <strong>CCACHE_NO</strong>. For\r
+example, <strong>CCACHE_COMPRESS</strong> can be set to force compression and\r
+<strong>CCACHE_NOCOMPRESS</strong> can be set to force no compression.</p></div>\r
+</div>\r
+<div class="sect2">\r
+<h3 id="_configuration_settings">Configuration settings</h3>\r
+<div class="paragraph"><p>Below is a list of available configuration settings. The corresponding\r
+environment variable name is indicated in parentheses after each configuration\r
+setting key. Boolean options are indicated with &#8220;[boolean]&#8221;</p></div>\r
 <div class="dlist"><dl>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_BASEDIR</strong>\r
+<strong>base_dir</strong> (<strong>CCACHE_BASEDIR</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_BASEDIR</strong> to an absolute path to\r
-    a directory, ccache rewrites absolute paths into relative paths before\r
-    computing the hash that identifies the compilation, but only for paths\r
-    under the specified directory. See the discussion under\r
+    This setting should be an absolute path to a directory. ccache then\r
+    rewrites absolute paths into relative paths before computing the hash that\r
+    identifies the compilation, but only for paths under the specified\r
+    directory. If set to the empty string (which is the default), no rewriting\r
+    is done. See also the discussion under\r
     <a href="#_compiling_in_different_directories">COMPILING IN DIFFERENT DIRECTORIES</a>.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_CC</strong>\r
+<strong>cache_dir</strong> (<strong>CCACHE_DIR</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    You can optionally set <strong>CCACHE_CC</strong> to force the name of the compiler to\r
-    use. If you don&#8217;t do this then ccache works it out from the command line.\r
+    This setting specifies where ccache will keep its cached compiler outputs.\r
+    It will only take effect if set in the system-wide configuration file or as\r
+    an environment variable. The default is <strong>$HOME/.ccache</strong>.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_COMPILERCHECK</strong>\r
+<strong>cache_dir_levels</strong> (<strong>CCACHE_NLEVELS</strong>)\r
+</dt>\r
+<dd>\r
+<p>\r
+    This setting allows you to choose the number of directory levels in the\r
+    cache directory. The default is 2. The minimum is 1 and the maximum is 8.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
+<strong>compiler</strong> (<strong>CCACHE_CC</strong>)\r
+</dt>\r
+<dd>\r
+<p>\r
+    This setting can be used to force the name of the compiler to use. If set\r
+    to the empty string (which is the default), ccache works it out from the\r
+    command line.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
+<strong>compiler_check</strong> (<strong>CCACHE_COMPILERCHECK</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
     By default, ccache includes the modification time (&#8220;mtime&#8221;) and size of\r
     the compiler in the hash to ensure that results retrieved from the cache\r
-    are accurate. The <strong>CCACHE_COMPILERCHECK</strong> environment variable can be used\r
-    to select another strategy. Possible values are:\r
+    are accurate. This setting can be used to select another strategy. Possible\r
+    values are:\r
 </p>\r
 <div class="openblock">\r
 <div class="content">\r
@@ -1066,7 +1164,7 @@ cases you won&#8217;t need any of these as the defaults will be fine.</p></div>
     Hash the standard output and standard error output of the specified\r
     command. The string will be split on whitespace to find out the command and\r
     arguments to run. No other interpretation of the command string will be\r
-    done, except that the special word &#8220;%compiler%&#8221; will be replaced with the\r
+    done, except that the special word <strong>%compiler%</strong> will be replaced with the\r
     path to the compiler. Several commands can be specified with semicolon as\r
     separator. Examples:\r
 </p>\r
@@ -1094,7 +1192,7 @@ method will hash the mtime and size of the other compiler wrapper, which means
 that ccache won&#8217;t be able to detect a compiler upgrade. Using a suitable\r
 command to identify the compiler is thus safer, but it&#8217;s also slower, so you\r
 should consider continue using the <strong>mtime</strong> method in combination with\r
-<strong>CCACHE_PREFIX</strong> if possible. See\r
+the <strong>prefix_command</strong> setting if possible. See\r
 <a href="#_using_ccache_with_other_compiler_wrappers">USING CCACHE WITH OTHER COMPILER WRAPPERS</a>.</p></div>\r
 </div></div>\r
 </dd>\r
@@ -1102,206 +1200,190 @@ should consider continue using the <strong>mtime</strong> method in combination
 </div></div>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_COMPRESS</strong>\r
-</dt>\r
-<dd>\r
-<p>\r
-    If you set the environment variable <strong>CCACHE_COMPRESS</strong> then ccache will\r
-    compress object files and other compiler output it puts in the cache.\r
-    However, this setting has no effect on how files are retrieved from the\r
-    cache; compressed and uncompressed results will still be usable regardless\r
-    of this setting.\r
-</p>\r
-</dd>\r
-<dt class="hdlist1">\r
-<strong>CCACHE_CPP2</strong>\r
+<strong>compression</strong> (<strong>CCACHE_COMPRESS</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_CPP2</strong> then ccache will not use\r
-    the optimisation of avoiding the second call to the preprocessor by\r
-    compiling the preprocessed output that was used for finding the hash in the\r
-    case of a cache miss. This is primarily a debugging option, although it is\r
-    possible that some unusual compilers will have problems with the\r
-    intermediate filename extensions used in this optimisation, in which case\r
-    this option could allow ccache to be used anyway.\r
+    If true, ccache will compress object files and other compiler output it\r
+    puts in the cache. However, this setting has no effect on how files are\r
+    retrieved from the cache; compressed and uncompressed results will still be\r
+    usable regardless of this setting. The default is false.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_DETECT_SHEBANG</strong>\r
+<strong>compression_level</strong> (<strong>CCACHE_COMPRESSLEVEL</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    The <strong>CCACHE_DETECT_SHEBANG</strong> environment variable only has meaning on\r
-    Windows. It instructs ccache to open the executable file to detect the\r
-    <strong>#!/bin/sh</strong> string, in which case ccache will search for <strong>sh.exe</strong> in\r
-    <strong>PATH</strong> and use that to launch the executable.\r
+    This setting determines the level at which ccache will compress object\r
+    files. It only has effect if <strong>compression</strong> is enabled. The value defaults\r
+    to 6, and must be no lower than 1 (fastest, worst compression) and no\r
+    higher than 9 (slowest, best compression).\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_DIR</strong>\r
+<strong>cpp_extension</strong> (<strong>CCACHE_EXTENSION</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    The <strong>CCACHE_DIR</strong> environment variable specifies where ccache will keep its\r
-    cached compiler output. The default is <strong>$HOME/.ccache</strong>.\r
+    This setting can be used to force a certain extension for the intermediate\r
+    preprocessed file. The default is to automatically determine the extension\r
+    to use for intermediate preprocessor files based on the type of file being\r
+    compiled, but that sometimes doesn&#8217;t work. For example, when using the\r
+    &#8220;aCC&#8221; compiler on HP-UX, set the cpp extension to <strong>i</strong>.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_DISABLE</strong>\r
+<strong>direct_mode</strong> (<strong>CCACHE_DIRECT</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_DISABLE</strong> then ccache will just\r
-    call the real compiler, bypassing the cache completely.\r
+    If true, the direct mode will be used. The default is true. See\r
+    <a href="#_the_direct_mode">THE DIRECT MODE</a>.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_EXTENSION</strong>\r
+<strong>disable</strong> (<strong>CCACHE_DISABLE</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    ccache tries to automatically determine the extension to use for\r
-    intermediate preprocessor files based on the type of file being compiled.\r
-    Unfortunately this sometimes doesn&#8217;t work, for example when using the\r
-    &#8220;aCC&#8221; compiler on HP-UX. On systems like this you can use the\r
-    <strong>CCACHE_EXTENSION</strong> option to override the default. On HP-UX set this\r
-    environment variable to <strong>i</strong> if you use the &#8220;aCC&#8221; compiler.\r
+    When true, ccache will just call the real compiler, bypassing the cache\r
+    completely. The default is false.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_EXTRAFILES</strong>\r
+<strong>extra_files_to_hash</strong> (<strong>CCACHE_EXTRAFILES</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_EXTRAFILES</strong> to a list of paths\r
-    then ccache will include the contents of those files when calculating the\r
-    hash sum. The list separator is semicolon in Windows systems and colon on\r
-    other systems.\r
+    This setting is a list of paths to files that ccache will include in the\r
+    the hash sum that idetifies the build. The list separator is semicolon on\r
+    Windows systems and colon on other systems.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_HARDLINK</strong>\r
+<strong>hard_link</strong> (<strong>CCACHE_HARDLINK</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_HARDLINK</strong> then ccache will\r
-    attempt to use hard links from the cache directory when creating the\r
-    compiler output rather than using a file copy. Using hard links may be\r
-    slightly faster in some situations, but can confuse programs like &#8220;make&#8221;\r
-    that rely on modification times. Another thing to keep in mind is that if\r
-    the resulting object file is modified in any way, this corrupts the cached\r
-    object file as well. Hard links are never made for compressed cache files.\r
-    This means that you should not set the <strong>CCACHE_COMPRESS</strong> variable if you\r
-    want to use hard links.\r
+    If true, ccache will attempt to use hard links from the cache directory\r
+    when creating the compiler output rather than using a file copy. Using hard\r
+    links may be slightly faster in some situations, but can confuse programs\r
+    like &#8220;make&#8221; that rely on modification times. Another thing to keep in\r
+    mind is that if the resulting object file is modified in any way, this\r
+    corrupts the cached object file as well. Hard links are never made for\r
+    compressed cache files. This means that you should not enable compression\r
+    if you want to use hard links. The default is false.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_HASHDIR</strong>\r
+<strong>hash_dir</strong> (<strong>CCACHE_HASHDIR</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    This tells ccache to hash the current working directory when calculating\r
-    the hash that is used to distinguish two compilations. This prevents a\r
-    problem with the storage of the current working directory in the debug info\r
-    of a object file, which can lead ccache to give a cached object file that\r
-    has the working directory in the debug info set incorrectly. This option is\r
-    off by default as the incorrect setting of this debug info rarely causes\r
-    problems. If you strike problems with GDB not using the correct directory\r
-    then enable this option.\r
+    If true, ccache will include the current working directory in the hash that\r
+    is used to distinguish two compilations. This prevents a problem with the\r
+    storage of the current working directory in the debug info of a object\r
+    file, which can lead ccache to give a cached object file that has the\r
+    working directory in the debug info set incorrectly. This option is off by\r
+    default as the incorrect setting of this debug info rarely causes problems.\r
+    If you strike problems with GDB not using the correct directory then enable\r
+    this option.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_LOGFILE</strong>\r
+<strong>log_file</strong> (<strong>CCACHE_LOGFILE</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the <strong>CCACHE_LOGFILE</strong> environment variable then ccache will write\r
-    information on what it is doing to the specified file. This is useful for\r
-    tracking down problems.\r
+    If set to a file path, ccache will write information on what it is doing to\r
+    the specified file. This is useful for tracking down problems.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_NLEVELS</strong>\r
+<strong>stats</strong> (<strong>CCACHE_STATS</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    The environment variable <strong>CCACHE_NLEVELS</strong> allows you to choose the number\r
-    of levels of hash in the cache directory. The default is 2. The minimum is\r
-    1 and the maximum is 8.\r
+    If true, ccache will update the statistics counters on each compilation.\r
+    The default is true.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_NODIRECT</strong>\r
+<strong>path</strong> (<strong>CCACHE_PATH</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_NODIRECT</strong> then ccache will not\r
-    use the direct mode.\r
+    If set, ccache will search directories in this list when looking for the\r
+    real compiler. The list separator is semicolon on Windows systems and colon\r
+    on other systems. If not set, ccache will look for the first executable\r
+    matching the compiler name in the normal <strong>PATH</strong> that isn&#8217;t a symbolic link\r
+    to ccache itself.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_NOSTATS</strong>\r
+<strong>prefix_command</strong> (<strong>CCACHE_PREFIX</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_NOSTATS</strong> then ccache will not\r
-    update the statistics files on each compilation.\r
+    This option adds a list of prefixes (separated by space) to the command\r
+    line that ccache uses when invoking the compiler. See also\r
+    <a href="#_using_ccache_with_other_compiler_wrappers">USING CCACHE WITH OTHER     COMPILER WRAPPERS</a>.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_PATH</strong>\r
+<strong>read_only</strong> (<strong>CCACHE_READONLY</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    You can optionally set <strong>CCACHE_PATH</strong> to a colon-separated path where ccache\r
-    will look for the real compilers. If you don&#8217;t do this then ccache will\r
-    look for the first executable matching the compiler name in the normal\r
-    <strong>PATH</strong> that isn&#8217;t a symbolic link to ccache itself.\r
+    If true, ccache will attempt to use existing cached object files, but it\r
+    will not to try to add anything new to the cache. If you are using this\r
+    because your ccache directory is read-only, then you need to set\r
+    <strong>temporary_dir</strong> as otherwise ccache will fail to create temporary files.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_PREFIX</strong>\r
+<strong>read_only_direct</strong> (<strong>CCACHE_READONLY_DIRECT</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    This option adds a prefix to the command line that ccache runs when\r
-    invoking the compiler. Also see the section below on using ccache with\r
-    &#8220;distcc&#8221;.\r
+    Just like <strong>read_only</strong> except that ccache will only try to retrieve results\r
+    from the cache using the direct mode, not the preprocessor mode. See\r
+    documentation for <strong>read_only</strong> regarding using a read-only ccache directory.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_READONLY</strong>\r
+<strong>reache</strong> (<strong>CCACHE_RECACHE</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    The <strong>CCACHE_READONLY</strong> environment variable tells ccache to attempt to use\r
-    existing cached object files, but not to try to add anything new to the\r
-    cache. If you are using this because your <strong>CCACHE_DIR</strong> is read-only, then\r
-    you may find that you also need to set <strong>CCACHE_TEMPDIR</strong> as otherwise ccache\r
-    will fail to create temporary files.\r
+    If true, ccache will not use any previously stored result. New results will\r
+    still be cached, possibly overwriting any pre-existing results.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_RECACHE</strong>\r
+<strong>run_second_cpp</strong> (<strong>CCACHE_CPP2</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    This forces ccache to not use any cached results, even if it finds them.\r
-    New results are still cached, but existing cache entries are ignored.\r
+    If true, ccache will not use the optimisation of avoiding the second call\r
+    to the preprocessor by compiling the preprocessed output that was used for\r
+    finding the hash in the case of a cache miss. This is primarily a debugging\r
+    option, although it is possible that some unusual compilers will have\r
+    problems with compiling the preprocessed output, in which case this option\r
+    could allow ccache to be used anyway.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_SLOPPINESS</strong>\r
+<strong>sloppiness</strong> (<strong>CCACHE_SLOPPINESS</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
     By default, ccache tries to give as few false cache hits as possible.\r
     However, in certain situations it&#8217;s possible that you know things that\r
-    ccache can&#8217;t take for granted. The <strong>CCACHE_SLOPPINESS</strong> environment variable\r
-    makes it possible to tell ccache to relax some checks in order to increase\r
-    the hit rate. The value should be a comma-separated string with options.\r
-    Available options are:\r
+    ccache can&#8217;t take for granted. This setting makes it possible to tell\r
+    ccache to relax some checks in order to increase the hit rate. The value\r
+    should be a comma-separated string with options. Available options are:\r
 </p>\r
 <div class="openblock">\r
 <div class="content">\r
@@ -1315,11 +1397,31 @@ should consider continue using the <strong>mtime</strong> method in combination
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
+<strong>file_stat_matches</strong>\r
+</dt>\r
+<dd>\r
+<p>\r
+    ccache normally examines a file&#8217;s contents to determine whether it matches\r
+    the cached version. With this option set, ccache will consider a file as\r
+    matching its cached version if the sizes, mtimes and ctimes match.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
+<strong>include_file_ctime</strong>\r
+</dt>\r
+<dd>\r
+<p>\r
+    By default, ccache also will not cache a file if it includes a header whose\r
+    ctime is too new. This option disables that check.\r
+</p>\r
+</dd>\r
+<dt class="hdlist1">\r
 <strong>include_file_mtime</strong>\r
 </dt>\r
 <dd>\r
 <p>\r
-    Don&#8217;t check the modification time of include files in the direct mode.\r
+    By default, ccache will not cache a file if it includes a header whose\r
+    mtime is too new. This option disables that check.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
@@ -1345,12 +1447,12 @@ should consider continue using the <strong>mtime</strong> method in combination
 information.</p></div>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_TEMPDIR</strong>\r
+<strong>temporary_dir</strong> (<strong>CCACHE_TEMPDIR</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    The <strong>CCACHE_TEMPDIR</strong> environment variable specifies where ccache will put\r
-    temporary files. The default is <strong>$CCACHE_DIR/tmp</strong>.\r
+    This setting specifies where ccache will put temporary files. The default\r
+    is <strong>&lt;cache_dir&gt;/tmp</strong>.\r
 </p>\r
 <div class="admonitionblock">\r
 <table><tr>\r
@@ -1364,41 +1466,41 @@ information.</p></div>
 </div>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_UMASK</strong>\r
+<strong>umask</strong> (<strong>CCACHE_UMASK</strong>)\r
 </dt>\r
 <dd>\r
 <p>\r
-    This sets the umask for ccache and all child processes (such as the\r
-    compiler). This is mostly useful when you wish to share your cache with\r
-    other users. Note that this also affects the file permissions set on the\r
-    object files created from your compilations.\r
+    This setting specifies the umask for ccache and all child processes (such\r
+    as the compiler). This is mostly useful when you wish to share your cache\r
+    with other users. Note that this also affects the file permissions set on\r
+    the object files created from your compilations.\r
 </p>\r
 </dd>\r
 <dt class="hdlist1">\r
-<strong>CCACHE_UNIFY</strong>\r
+<strong>unify</strong> (<strong>CCACHE_UNIFY</strong>) [boolean]\r
 </dt>\r
 <dd>\r
 <p>\r
-    If you set the environment variable <strong>CCACHE_UNIFY</strong> then ccache will use a\r
-    C/C++ unifier when hashing the preprocessor output if the <strong>-g</strong> option is\r
-    not used. The unifier is slower than a normal hash, so setting this\r
-    environment variable loses a little bit of speed, but it means that ccache\r
-    can take advantage of not recompiling when the changes to the source code\r
-    consist of reformatting only. Note that using <strong>CCACHE_UNIFY</strong> changes the\r
-    hash, so cached compilations with <strong>CCACHE_UNIFY</strong> set cannot be used when\r
-    <strong>CCACHE_UNIFY</strong> is not set and vice versa. The reason the unifier is off by\r
-    default is that it can give incorrect line number information in compiler\r
-    warning messages. Also note that enabling the unifier implies turning off\r
-    the direct mode.\r
+    If true, ccache will use a C/C++ unifier when hashing the preprocessor\r
+    output if the <strong>-g</strong> option is not used. The unifier is slower than a normal\r
+    hash, so setting this environment variable loses a little bit of speed, but\r
+    it means that ccache can take advantage of not recompiling when the changes\r
+    to the source code consist of reformatting only. Note that enabling the\r
+    unifier changes the hash, so cached compilations produced when the unifier\r
+    is enabled cannot be reused when the unifier is disabled, and vice versa.\r
+    Enabling the unifier may result in incorrect line number information in\r
+    compiler warning messages and expansions of the <strong>__LINE__</strong> macro. Also\r
+    note that enabling the unifier implies turning off the direct mode.\r
 </p>\r
 </dd>\r
 </dl></div>\r
 </div>\r
 </div>\r
+</div>\r
 <div class="sect1">\r
 <h2 id="_cache_size_management">Cache size management</h2>\r
 <div class="sectionbody">\r
-<div class="paragraph"><p>By default ccache has a one gigabyte limit on the total size of files in the\r
+<div class="paragraph"><p>By default, ccache has a five gigabyte limit on the total size of files in the\r
 cache and no maximum number of files. You can set different limits using the\r
 <strong>-M</strong>/<strong>--max-size</strong> and <strong>-F</strong>/<strong>--max-files</strong> options. Use <strong>ccache -s/--show-stats</strong>\r
 to see the cache size and the currently configured limits (in addition to other\r
@@ -1409,10 +1511,10 @@ various statistics).</p></div>
 <h2 id="_cache_compression">Cache compression</h2>\r
 <div class="sectionbody">\r
 <div class="paragraph"><p>ccache can optionally compress all files it puts into the cache using the\r
-compression library zlib. While this involves a negligible performance\r
-slowdown, it significantly increases the number of files that fit in the cache.\r
-You can turn on compression by setting the <strong>CCACHE_COMPRESS</strong> environment\r
-variable.</p></div>\r
+compression library zlib. While this may involve a tiny performance slowdown,\r
+it increases the number of files that fit in the cache. You can turn on\r
+compression with the <strong>compression</strong> configuration setting and you can also tweak\r
+the compression level with <strong>compression_level</strong>.</p></div>\r
 </div>\r
 </div>\r
 <div class="sect1">\r
@@ -1457,7 +1559,7 @@ the extension used by the compiler for a file with preprocessor output
 <li>\r
 <p>\r
 the compiler&#8217;s size and modification time (or other compiler-specific\r
-  information specified by <strong>CCACHE_COMPILERCHECK</strong>)\r
+  information specified by the <strong>compiler_check</strong> setting)\r
 </p>\r
 </li>\r
 <li>\r
@@ -1467,12 +1569,12 @@ the name of the compiler
 </li>\r
 <li>\r
 <p>\r
-the current directory (if <strong>CCACHE_HASHDIR</strong> is set)\r
+the current directory (if the <strong>hash_dir</strong> setting is enabled)\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
-contents of files specified by <strong>CCACHE_EXTRAFILES</strong> (if any)\r
+contents of files specified by the <strong>extra_files_to_hash</strong> setting (if any)\r
 </p>\r
 </li>\r
 </ul></div>\r
@@ -1521,11 +1623,17 @@ preprocessor. The output from the preprocessor is parsed to find the include
 files that were read. The paths and hash sums of those include files are then\r
 stored in the manifest along with information about the produced compilation\r
 result.</p></div>\r
+<div class="paragraph"><p>There is a catch with the direct mode: header files that were used by the\r
+compiler are recorded, but header files that were <strong>not</strong> used, but would have\r
+been used if they existed, are not. So, when ccache checks if a result can be\r
+taken from the cache, it currently can&#8217;t check if the existence of a new header\r
+file should invalidate the result. In practice, the direct mode is safe to use\r
+in the absolute majority of cases.</p></div>\r
 <div class="paragraph"><p>The direct mode will be disabled if any of the following holds:</p></div>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
-the environment variable <strong>CCACHE_NODIRECT</strong> is set\r
+the configuration setting <strong>direct_mode</strong> is false\r
 </p>\r
 </li>\r
 <li>\r
@@ -1536,7 +1644,7 @@ a modification time of one of the include files is too new (needed to avoid a
 </li>\r
 <li>\r
 <p>\r
-the unifier is enabled (the environment variable <strong>CCACHE_UNIFY</strong> is set)\r
+the unifier is enabled (the configuration setting <strong>unify</strong> is true)\r
 </p>\r
 </li>\r
 <li>\r
@@ -1559,8 +1667,7 @@ a <strong>-Wp,<em>X</em></strong> compiler option other than <strong>-Wp,-MD,<em
 </li>\r
 <li>\r
 <p>\r
-the string &#8220;__TIME__&#8221; is present outside comments and string literals in\r
-  the source code\r
+the string &#8220;__TIME__&#8221; is present in the source code\r
 </p>\r
 </li>\r
 </ul></div>\r
@@ -1622,15 +1729,15 @@ The source code file path may be absolute, and that path may substituted for
 <div class="paragraph"><p>This means that if you compile the same code in different locations, you can&#8217;t\r
 share compilation results between the different build directories since you get\r
 cache misses because of the absolute build directory paths that are part of the\r
-hash. To mitigate this problem, you can specify a &#8220;base directory&#8221; by setting\r
-the <strong>CCACHE_BASEDIR</strong> variable to an absolute path to the directory. ccache will\r
-then rewrite absolute paths that are under the base directory (i.e., paths that\r
-have the base directory as a prefix) to relative paths when constructing the\r
-hash. A typical path to use as the base directory is your home directory or\r
+hash. To mitigate this problem, you can specify a &#8220;base directory&#8221; in the\r
+configuration setting <strong>base_dir</strong> to an absolute path to the directory. ccache\r
+will then rewrite absolute paths that are under the base directory (i.e., paths\r
+that have the base directory as a prefix) to relative paths when constructing\r
+the hash. A typical path to use as the base directory is your home directory or\r
 another directory that is a parent of your build directories. (Don&#8217;t use <code>/</code> as\r
 the base directory since that will make ccache also rewrite paths to system\r
 header files, which doesn&#8217;t gain anything.)</p></div>\r
-<div class="paragraph"><p>The drawbacks of using <strong>CCACHE_BASEDIR</strong> are:</p></div>\r
+<div class="paragraph"><p>The drawbacks of using a base directory are:</p></div>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1658,10 +1765,10 @@ things to make it work properly:</p></div>
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
-You must set <strong>CCACHE_SLOPPINESS</strong> to <strong>pch_defines,time_macros</strong>. The reason is\r
-  that ccache can&#8217;t tell whether <strong>__TIME__</strong> or <strong>__DATE__</strong> is used when\r
-  using a precompiled header. Further, it can&#8217;t detect changes in #defines in\r
-  the source code because of how preprocessing works in combination with\r
+You must set <strong>sloppiness</strong> to <strong>pch_defines,time_macros</strong>. The reason is that\r
+  ccache can&#8217;t tell whether <strong>__TIME__</strong> or <strong>__DATE__</strong> is used when using a\r
+  precompiled header. Further, it can&#8217;t detect changes in #defines in the\r
+  source code because of how preprocessing works in combination with\r
   precompiled headers.\r
 </p>\r
 </li>\r
@@ -1680,6 +1787,12 @@ use the <strong>-include</strong> compiler option to include the precompiled hea
 </li>\r
 <li>\r
 <p>\r
+(for the Clang compiler) use the <strong>-include-pch</strong> compiler option to include\r
+   the PCH file generated from the precompiled header; or\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
 add the <strong>-fpch-preprocess</strong> compiler option when compiling.\r
 </p>\r
 </li>\r
@@ -1702,18 +1815,24 @@ conditions should to be met:</p></div>
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
-Use the same <strong>CCACHE_DIR</strong> environment variable setting.\r
+Use the same cache directory.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Make sure that the configuration setting <strong>hard_link</strong> is false (which is the\r
+  default).\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
-Unset the <strong>CCACHE_HARDLINK</strong> environment variable.\r
+Make sure that all users are in the same group.\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
-Make sure everyone sets the <strong>CCACHE_UMASK</strong> environment variable to 002. This\r
-  ensures that cached files are accessible to everyone in the group.\r
+Set the configuration setting <strong>umask</strong> to 002. This ensures that cached files\r
+  are accessible to everyone in the group.\r
 </p>\r
 </li>\r
 <li>\r
@@ -1737,8 +1856,8 @@ timestamp. This results in false dependencies to be triggered by
 timestamp-based build systems whenever another user links to an existing file.\r
 Typically, users will see that their libraries and binaries are relinked\r
 without reason.</p></div>\r
-<div class="paragraph"><p>You may also want to make sure that the developers have <strong>CCACHE_BASEDIR</strong> set\r
-appropriately, as discussed in the previous section.</p></div>\r
+<div class="paragraph"><p>You may also want to make sure that a base directory is set appropriately, as\r
+discussed in a previous section.</p></div>\r
 </div>\r
 </div>\r
 <div class="sect1">\r
@@ -1759,7 +1878,7 @@ ccache hasn&#8217;t been tested very thoroughly on NFS.
 </p>\r
 </li>\r
 </ul></div>\r
-<div class="paragraph"><p>A tip is to set <strong>CCACHE_TEMPDIR</strong> to a directory on the local host to avoid NFS\r
+<div class="paragraph"><p>A tip is to set <strong>temporary_dir</strong> to a directory on the local host to avoid NFS\r
 traffic for temporary files.</p></div>\r
 </div>\r
 </div>\r
@@ -1767,17 +1886,18 @@ traffic for temporary files.</p></div>
 <h2 id="_using_ccache_with_other_compiler_wrappers">Using ccache with other compiler wrappers</h2>\r
 <div class="sectionbody">\r
 <div class="paragraph"><p>The recommended way of combining ccache with another compiler wrapper (such as\r
-&#8220;distcc&#8221;) is by using the <strong>CCACHE_PREFIX</strong> option. You just need to set the\r
-environment variable <strong>CCACHE_PREFIX</strong> to the name of the wrapper (e.g. <strong>distcc</strong>)\r
-and ccache will prefix the command line with the specified command when running\r
-the compiler.</p></div>\r
-<div class="paragraph"><p>Unless you set <strong>CCACHE_COMPILERCHECK</strong> to a suitable command (see the\r
-description of that configuration option), it is not recommended to use the\r
-form <strong>ccache anotherwrapper compiler args</strong> as the compilation command. It&#8217;s\r
-also not recommended to use the masquerading technique for the other compiler\r
-wrapper. The reason is that by default, ccache will in both cases hash the\r
-mtime and size of the other wrapper instead of the real compiler, which means\r
-that:</p></div>\r
+&#8220;distcc&#8221;) is by letting ccache execute the compiler wrapper. This is\r
+accomplished by defining the configuration setting <strong>prefix_command</strong>, for\r
+example by setting the environment variable <strong>CCACHE_PREFIX</strong> to the name of the\r
+wrapper (e.g. <strong>distcc</strong>). ccache will then prefix the command line with the\r
+specified command when running the compiler. To specify several prefix\r
+commands, set <strong>prefix_command</strong> to a colon-separated list of commands.</p></div>\r
+<div class="paragraph"><p>Unless you set <strong>compiler_check</strong> to a suitable command (see the description of\r
+that configuration option), it is not recommended to use the form <strong>ccache\r
+anotherwrapper compiler args</strong> as the compilation command. It&#8217;s also not\r
+recommended to use the masquerading technique for the other compiler wrapper.\r
+The reason is that by default, ccache will in both cases hash the mtime and\r
+size of the other wrapper instead of the real compiler, which means that:</p></div>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1791,8 +1911,8 @@ The cached results will not be shared between compilations with and without
 </p>\r
 </li>\r
 </ul></div>\r
-<div class="paragraph"><p>Another minor thing is that if <strong>CCACHE_PREFIX</strong> is not used, ccache will\r
-needlessly invoke the other wrapper when running the preprocessor.</p></div>\r
+<div class="paragraph"><p>Another minor thing is that if <strong>prefix_command</strong> is used, ccache will not invoke\r
+the other wrapper when running the preprocessor, which increase performance.</p></div>\r
 </div>\r
 </div>\r
 <div class="sect1">\r
@@ -1805,8 +1925,14 @@ ccache doesn&#8217;t handle the GNU Assembler&#8217;s <strong>.incbin</strong> d
   directive can be embedded in the source code inside an <strong><em>asm</em></strong> statement in\r
   order to include a file verbatim in the object file. If the included file is\r
   modified, ccache doesn&#8217;t pick up the change since the inclusion isn&#8217;t done by\r
-  the preprocessor. A workaround of this problem is to set <strong>CCACHE_EXTRAFILES</strong>\r
-  to the path of the included file.\r
+  the preprocessor. A workaround of this problem is to set\r
+  <strong>extra_files_to_hash</strong> to the path of the included file.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The direct mode fails to pick up new header files in some rare scenarios. See\r
+  <a href="#_the_direct_mode">THE DIRECT MODE</a> above.\r
 </p>\r
 </li>\r
 </ul></div>\r
@@ -1818,7 +1944,7 @@ ccache doesn&#8217;t handle the GNU Assembler&#8217;s <strong>.incbin</strong> d
 <div class="sect2">\r
 <h3 id="_general">General</h3>\r
 <div class="paragraph"><p>A general tip for getting information about what ccache is doing is to enable\r
-debug logging by setting <strong>CCACHE_LOGFILE</strong>. The log contains executed commands,\r
+debug logging by setting <strong>log_file</strong>. The log contains executed commands,\r
 important decisions that ccache makes, read and written files, etc. Another way\r
 of keeping track of what is happening is to check the output of <strong>ccache -s</strong>.</p></div>\r
 </div>\r
@@ -1862,7 +1988,8 @@ The compiler option <strong>-Xpreprocessor</strong> or <strong>-Wp,<em>X</em></s
 </li>\r
 <li>\r
 <p>\r
-This was the first compilation with a new value of <strong>CCACHE_BASEDIR</strong>.\r
+This was the first compilation with a new value of the base directory\r
+   setting.\r
 </p>\r
 </li>\r
 <li>\r
@@ -1870,12 +1997,12 @@ This was the first compilation with a new value of <strong>CCACHE_BASEDIR</stron
 A modification time of one of the include files is too new (created the same\r
    second as the compilation is being done). This check is made to avoid a race\r
    condition. To fix this, create the include file earlier in the build\r
-   process, if possible, or set <strong>CCACHE_SLOPPINESS</strong> to <strong>include_file_mtime</strong> if\r
-   you are willing to take the risk. (The race condition consists of these\r
-   events: the preprocessor is run; an include file is modified by someone; the\r
-   new include file is hashed by ccache; the real compiler is run on the\r
-   preprocessor&#8217;s output, which contains data from the old header file; the\r
-   wrong object file is stored in the cache.)\r
+   process, if possible, or set <strong>sloppiness</strong> to <strong>include_file_mtime</strong> if you are\r
+   willing to take the risk. (The race condition consists of these events: the\r
+   preprocessor is run; an include file is modified by someone; the new include\r
+   file is hashed by ccache; the real compiler is run on the preprocessor&#8217;s\r
+   output, which contains data from the old header file; the wrong object file\r
+   is stored in the cache.)\r
 </p>\r
 </li>\r
 <li>\r
@@ -1887,7 +2014,7 @@ The <strong>__TIME__</strong> preprocessor macro is (potentially) being used. cc
    be sure, ccache would have to run the preprocessor, but the sole point of\r
    the direct mode is to avoid that.) If you know that <strong>__TIME__</strong> isn&#8217;t used\r
    in practise, or don&#8217;t care if ccache produces objects where <strong>__TIME__</strong> is\r
-   expanded to something in the past, you can set <strong>CCACHE_SLOPPINESS</strong> to\r
+   expanded to something in the past, you can set <strong>sloppiness</strong> to\r
    <strong>time_macros</strong>.\r
 </p>\r
 </li>\r
@@ -1900,7 +2027,7 @@ The <strong>__DATE__</strong> preprocessor macro is (potentially) being used and
    correct object file if the <strong>__DATE__</strong> macro affects the output. If you\r
    know that <strong>__DATE__</strong> isn&#8217;t used in practise, or don&#8217;t care if ccache\r
    produces objects where <strong>__DATE__</strong> is expanded to something in the past,\r
-   you can set <strong>CCACHE_SLOPPINESS</strong> to <strong>time_macros</strong>.\r
+   you can set <strong>sloppiness</strong> to <strong>time_macros</strong>.\r
 </p>\r
 </li>\r
 <li>\r
@@ -1911,8 +2038,8 @@ The <strong>__FILE__</strong> preprocessor macro is (potentially) being used and
    path in order to be able to produce the correct object file if the\r
    <strong>__FILE__</strong> macro affects the output. If you know that <strong>__FILE__</strong> isn&#8217;t\r
    used in practise, or don&#8217;t care if ccache produces objects where\r
-   <strong>__FILE__</strong> is expanded to the wrong path, you can set <strong>CCACHE_SLOPPINESS</strong>\r
-   to <strong>file_macro</strong>.\r
+   <strong>__FILE__</strong> is expanded to the wrong path, you can set <strong>sloppiness</strong> to\r
+   <strong>file_macro</strong>.\r
 </p>\r
 </li>\r
 </ul></div>\r
@@ -1959,8 +2086,8 @@ If &#8220;can&#8217;t use precompiled header&#8221; has been incremented, see
 <h3 id="_errors_when_compiling_with_ccache">Errors when compiling with ccache</h3>\r
 <div class="paragraph"><p>If compilation doesn&#8217;t work with ccache, but it works without it, one possible\r
 reason is that the compiler can&#8217;t compile preprocessed output correctly. A\r
-workaround that may work is to set <strong>CCACHE_CPP2</strong>. This will make cache misses\r
-slower, though, so it is better to find and fix the root cause.</p></div>\r
+workaround that may work is to enable <strong>run_second_cpp</strong>*. This will make cache\r
+misses slower, though, so it is better to find and fix the root cause.</p></div>\r
 </div>\r
 <div class="sect2">\r
 <h3 id="_corrupt_object_files">Corrupt object files</h3>\r
@@ -1968,8 +2095,8 @@ slower, though, so it is better to find and fix the root cause.</p></div>
 bad object file sneaks into the cache for some reason, it will of course stay\r
 bad. Some possible reasons for erroneous object files are bad hardware (disk\r
 drive, disk controller, memory, etc), buggy drivers or file systems, a bad\r
-<strong>CCACHE_PREFIX</strong> command or compiler wrapper. If this happens, the easiest way\r
-of fixing it is this:</p></div>\r
+<strong>prefix_command</strong> or compiler wrapper. If this happens, the easiest way of\r
+fixing it is this:</p></div>\r
 <div class="olist arabic"><ol class="arabic">\r
 <li>\r
 <p>\r
@@ -2014,9 +2141,8 @@ maintained by Joel Rosdahl. See AUTHORS.txt or AUTHORS.html and
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2014-11-15 16:04:58 CET\r
 </div>\r
 </div>\r
 </body>\r
index 5bc1ca3950943e87ded72f04b2d79b1be8685ed2..dd0c96908885fbea9a611488564b65aa01e8f372 100644 (file)
@@ -104,7 +104,7 @@ compiler options apply and you should refer to the compiler's documentation.
 
     Clean up the cache by removing old cached files until the specified file
     number and cache size limits are not exceeded. This also recalculates the
-    cache file count and size totals. Normally, it's not needed to initiate
+    cache file count and size totals. Normally, there is no need to initiate
     cleanup manually as ccache keeps the cache below the specified limits at
     runtime and keeps statistics up to date on each compilation. Forcing a
     cleanup is mostly useful if you manually modify the cache contents or
@@ -112,14 +112,14 @@ compiler options apply and you should refer to the compiler's documentation.
 
 *-C, --clear*::
 
-    Clear the entire cache, removing all cached files.
+    Clear the entire cache, removing all cached files, but keeping the
+    configuration file.
 
 *-F, --max-files*='N'::
 
-    Set the maximum number of files allowed in the cache. The value is stored
-    inside the cache directory and applies to all future compilations. Due to
-    the way the value is stored the actual value used is always rounded down to
-    the nearest multiple of 16.
+    Set the maximum number of files allowed in the cache. Use 0 for no limit.
+    The value is stored in a configuration file in the cache directory and
+    applies to all future compilations.
 
 *-h, --help*::
 
@@ -127,10 +127,21 @@ compiler options apply and you should refer to the compiler's documentation.
 
 *-M, --max-size*='SIZE'::
 
-    Set the maximum size of the files stored in the cache. You can specify a
-    value in gigabytes, megabytes or kilobytes by appending a G, M or K to the
-    value. The default is gigabytes. The actual value stored is rounded down to
-    the nearest multiple of 16 kilobytes.
+    Set the maximum size of the files stored in the cache. 'SIZE' should be a
+    number followed by an optional suffix: k, M, G, T (decimal), Ki, Mi, Gi or
+    Ti (binary). The default suffix is G. Use 0 for no limit. The value is
+    stored in a configuration file in the cache directory and applies to all
+    future compilations.
+
+*-o, --set-config*='KEY=VALUE'::
+
+    Set configuration 'KEY' to 'VALUE'. See <<_configuration,CONFIGURATION>>
+    for more information.
+
+*-p, --print-config*::
+
+    Print current configuration options and from where they originate
+    (environment variable, configuration file or compile-time default).
 
 *-s, --show-stats*::
 
@@ -142,7 +153,7 @@ compiler options apply and you should refer to the compiler's documentation.
 
 *-z, --zero-stats*::
 
-    Zero the cache statistics (but not the configured limits).
+    Zero the cache statistics (but not the configuration options).
 
 
 Extra options
@@ -169,31 +180,92 @@ option specially but shouldn't, since the option has another meaning for your
 compiler than what ccache thinks.
 
 
-Environment variables
----------------------
+Configuration
+-------------
+
+ccache's default behavior can be overridden by configuration file settings,
+which in turn can be overridden by environment variables with names starting
+with *CCACHE_*. ccache normally reads configuration from two files: first a
+system-level configuration file and secondly a cache-specific configuration
+file. The priority of configuration settings is as follows (where 1 is
+highest):
 
-ccache uses a number of environment variables to control operation. In most
-cases you won't need any of these as the defaults will be fine.
+1. Environment variables.
+2. The cache-specific configuration file *<ccachedir>/ccache.conf* (typically
+   *$HOME/.ccache/ccache.conf*).
+3. The system-wide configuration file *<sysconfdir>/ccache.conf* (typically
+   */etc/ccache.conf* or */usr/local/etc/ccache.conf*).
+4. Compile-time defaults.
 
-*CCACHE_BASEDIR*::
+As a special case, if the environment variable *CCACHE_CONFIGPATH* is set,
+ccache reads configuration from the specified path instead of the default
+paths.
+
+
+Configuration file syntax
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Configuration files are in a simple ``key = value'' format, one setting per
+line. Lines starting with a hash sign are comments. Blank lines are ignored, as
+is whitespace surrounding keys and values. Example:
+
+-------------------------------------------------------------------------------
+# Set maximum cache size to 10 GB:
+max_size = 10G
+-------------------------------------------------------------------------------
 
-    If you set the environment variable *CCACHE_BASEDIR* to an absolute path to
-    a directory, ccache rewrites absolute paths into relative paths before
-    computing the hash that identifies the compilation, but only for paths
-    under the specified directory. See the discussion under
+Boolean values
+~~~~~~~~~~~~~~
+
+Some settings are boolean values (i.e. truth values). In a configuration file,
+such values must be set to the string *true* or *false*. For the corresponding
+environment variables, the semantics are a bit different: a set environment
+variable means ``true'' regardless of the value (even if set to the empty
+string), and an unset environment variable means ``false''. Each boolean
+environment variable also has a negated form starting with *CCACHE_NO*. For
+example, *CCACHE_COMPRESS* can be set to force compression and
+*CCACHE_NOCOMPRESS* can be set to force no compression.
+
+
+Configuration settings
+~~~~~~~~~~~~~~~~~~~~~~
+
+Below is a list of available configuration settings. The corresponding
+environment variable name is indicated in parentheses after each configuration
+setting key. Boolean options are indicated with ``[boolean]''
+
+*base_dir* (*CCACHE_BASEDIR*)::
+
+    This setting should be an absolute path to a directory. ccache then
+    rewrites absolute paths into relative paths before computing the hash that
+    identifies the compilation, but only for paths under the specified
+    directory. If set to the empty string (which is the default), no rewriting
+    is done. See also the discussion under
     <<_compiling_in_different_directories,COMPILING IN DIFFERENT DIRECTORIES>>.
 
-*CCACHE_CC*::
+*cache_dir* (*CCACHE_DIR*)::
+
+    This setting specifies where ccache will keep its cached compiler outputs.
+    It will only take effect if set in the system-wide configuration file or as
+    an environment variable. The default is *$HOME/.ccache*.
 
-    You can optionally set *CCACHE_CC* to force the name of the compiler to
-    use. If you don't do this then ccache works it out from the command line.
+*cache_dir_levels* (*CCACHE_NLEVELS*)::
 
-*CCACHE_COMPILERCHECK*::
+    This setting allows you to choose the number of directory levels in the
+    cache directory. The default is 2. The minimum is 1 and the maximum is 8.
+
+*compiler* (*CCACHE_CC*)::
+
+    This setting can be used to force the name of the compiler to use. If set
+    to the empty string (which is the default), ccache works it out from the
+    command line.
+
+*compiler_check* (*CCACHE_COMPILERCHECK*)::
 
     By default, ccache includes the modification time (``mtime'') and size of
     the compiler in the hash to ensure that results retrieved from the cache
-    are accurate. The *CCACHE_COMPILERCHECK* environment variable can be used
-    to select another strategy. Possible values are:
+    are accurate. This setting can be used to select another strategy. Possible
+    values are:
 +
 --
 *content*::
@@ -213,7 +285,7 @@ _a command string_::
     Hash the standard output and standard error output of the specified
     command. The string will be split on whitespace to find out the command and
     arguments to run. No other interpretation of the command string will be
-    done, except that the special word ``%compiler%'' will be replaced with the
+    done, except that the special word *%compiler%* will be replaced with the
     path to the compiler. Several commands can be specified with semicolon as
     separator. Examples:
 +
@@ -233,148 +305,145 @@ method will hash the mtime and size of the other compiler wrapper, which means
 that ccache won't be able to detect a compiler upgrade. Using a suitable
 command to identify the compiler is thus safer, but it's also slower, so you
 should consider continue using the *mtime* method in combination with
-*CCACHE_PREFIX* if possible. See
+the *prefix_command* setting if possible. See
 <<_using_ccache_with_other_compiler_wrappers,USING CCACHE WITH OTHER COMPILER
 WRAPPERS>>.
 --
 --
 
-*CCACHE_COMPRESS*::
+*compression* (*CCACHE_COMPRESS*) [boolean]::
 
-    If you set the environment variable *CCACHE_COMPRESS* then ccache will
-    compress object files and other compiler output it puts in the cache.
-    However, this setting has no effect on how files are retrieved from the
-    cache; compressed and uncompressed results will still be usable regardless
-    of this setting.
+    If true, ccache will compress object files and other compiler output it
+    puts in the cache. However, this setting has no effect on how files are
+    retrieved from the cache; compressed and uncompressed results will still be
+    usable regardless of this setting. The default is false.
 
-*CCACHE_CPP2*::
+*compression_level* (*CCACHE_COMPRESSLEVEL*)::
 
-    If you set the environment variable *CCACHE_CPP2* then ccache will not use
-    the optimisation of avoiding the second call to the preprocessor by
-    compiling the preprocessed output that was used for finding the hash in the
-    case of a cache miss. This is primarily a debugging option, although it is
-    possible that some unusual compilers will have problems with the
-    intermediate filename extensions used in this optimisation, in which case
-    this option could allow ccache to be used anyway.
+    This setting determines the level at which ccache will compress object
+    files. It only has effect if *compression* is enabled. The value defaults
+    to 6, and must be no lower than 1 (fastest, worst compression) and no
+    higher than 9 (slowest, best compression).
 
-*CCACHE_DETECT_SHEBANG*::
+*cpp_extension* (*CCACHE_EXTENSION*)::
 
-    The *CCACHE_DETECT_SHEBANG* environment variable only has meaning on
-    Windows. It instructs ccache to open the executable file to detect the
-    *#!/bin/sh* string, in which case ccache will search for *sh.exe* in
-    *PATH* and use that to launch the executable.
+    This setting can be used to force a certain extension for the intermediate
+    preprocessed file. The default is to automatically determine the extension
+    to use for intermediate preprocessor files based on the type of file being
+    compiled, but that sometimes doesn't work. For example, when using the
+    ``aCC'' compiler on HP-UX, set the cpp extension to *i*.
 
-*CCACHE_DIR*::
+*direct_mode* (*CCACHE_DIRECT*) [boolean]::
 
-    The *CCACHE_DIR* environment variable specifies where ccache will keep its
-    cached compiler output. The default is *$HOME/.ccache*.
+    If true, the direct mode will be used. The default is true. See
+    <<_the_direct_mode,THE DIRECT MODE>>.
 
-*CCACHE_DISABLE*::
+*disable* (*CCACHE_DISABLE*) [boolean]::
 
-    If you set the environment variable *CCACHE_DISABLE* then ccache will just
-    call the real compiler, bypassing the cache completely.
+    When true, ccache will just call the real compiler, bypassing the cache
+    completely. The default is false.
 
-*CCACHE_EXTENSION*::
+*extra_files_to_hash* (*CCACHE_EXTRAFILES*)::
 
-    ccache tries to automatically determine the extension to use for
-    intermediate preprocessor files based on the type of file being compiled.
-    Unfortunately this sometimes doesn't work, for example when using the
-    ``aCC'' compiler on HP-UX. On systems like this you can use the
-    *CCACHE_EXTENSION* option to override the default. On HP-UX set this
-    environment variable to *i* if you use the ``aCC'' compiler.
+    This setting is a list of paths to files that ccache will include in the
+    the hash sum that idetifies the build. The list separator is semicolon on
+    Windows systems and colon on other systems.
 
-*CCACHE_EXTRAFILES*::
+*hard_link* (*CCACHE_HARDLINK*) [boolean]::
 
-    If you set the environment variable *CCACHE_EXTRAFILES* to a list of paths
-    then ccache will include the contents of those files when calculating the
-    hash sum. The list separator is semicolon in Windows systems and colon on
-    other systems.
+    If true, ccache will attempt to use hard links from the cache directory
+    when creating the compiler output rather than using a file copy. Using hard
+    links may be slightly faster in some situations, but can confuse programs
+    like ``make'' that rely on modification times. Another thing to keep in
+    mind is that if the resulting object file is modified in any way, this
+    corrupts the cached object file as well. Hard links are never made for
+    compressed cache files. This means that you should not enable compression
+    if you want to use hard links. The default is false.
 
-*CCACHE_HARDLINK*::
+*hash_dir* (*CCACHE_HASHDIR*) [boolean]::
 
-    If you set the environment variable *CCACHE_HARDLINK* then ccache will
-    attempt to use hard links from the cache directory when creating the
-    compiler output rather than using a file copy. Using hard links may be
-    slightly faster in some situations, but can confuse programs like ``make''
-    that rely on modification times. Another thing to keep in mind is that if
-    the resulting object file is modified in any way, this corrupts the cached
-    object file as well. Hard links are never made for compressed cache files.
-    This means that you should not set the *CCACHE_COMPRESS* variable if you
-    want to use hard links.
+    If true, ccache will include the current working directory in the hash that
+    is used to distinguish two compilations. This prevents a problem with the
+    storage of the current working directory in the debug info of a object
+    file, which can lead ccache to give a cached object file that has the
+    working directory in the debug info set incorrectly. This option is off by
+    default as the incorrect setting of this debug info rarely causes problems.
+    If you strike problems with GDB not using the correct directory then enable
+    this option.
 
-*CCACHE_HASHDIR*::
+*log_file* (*CCACHE_LOGFILE*)::
 
-    This tells ccache to hash the current working directory when calculating
-    the hash that is used to distinguish two compilations. This prevents a
-    problem with the storage of the current working directory in the debug info
-    of a object file, which can lead ccache to give a cached object file that
-    has the working directory in the debug info set incorrectly. This option is
-    off by default as the incorrect setting of this debug info rarely causes
-    problems. If you strike problems with GDB not using the correct directory
-    then enable this option.
+    If set to a file path, ccache will write information on what it is doing to
+    the specified file. This is useful for tracking down problems.
 
-*CCACHE_LOGFILE*::
+*stats* (*CCACHE_STATS*) [boolean]::
 
-    If you set the *CCACHE_LOGFILE* environment variable then ccache will write
-    information on what it is doing to the specified file. This is useful for
-    tracking down problems.
+    If true, ccache will update the statistics counters on each compilation.
+    The default is true.
 
-*CCACHE_NLEVELS*::
+*path* (*CCACHE_PATH*)::
 
-    The environment variable *CCACHE_NLEVELS* allows you to choose the number
-    of levels of hash in the cache directory. The default is 2. The minimum is
-    1 and the maximum is 8.
+    If set, ccache will search directories in this list when looking for the
+    real compiler. The list separator is semicolon on Windows systems and colon
+    on other systems. If not set, ccache will look for the first executable
+    matching the compiler name in the normal *PATH* that isn't a symbolic link
+    to ccache itself.
 
-*CCACHE_NODIRECT*::
+*prefix_command* (*CCACHE_PREFIX*)::
 
-    If you set the environment variable *CCACHE_NODIRECT* then ccache will not
-    use the direct mode.
+    This option adds a list of prefixes (separated by space) to the command
+    line that ccache uses when invoking the compiler. See also
+    <<_using_ccache_with_other_compiler_wrappers,USING CCACHE WITH OTHER
+    COMPILER WRAPPERS>>.
 
-*CCACHE_NOSTATS*::
+*read_only* (*CCACHE_READONLY*) [boolean]::
 
-    If you set the environment variable *CCACHE_NOSTATS* then ccache will not
-    update the statistics files on each compilation.
+    If true, ccache will attempt to use existing cached object files, but it
+    will not to try to add anything new to the cache. If you are using this
+    because your ccache directory is read-only, then you need to set
+    *temporary_dir* as otherwise ccache will fail to create temporary files.
 
-*CCACHE_PATH*::
+*read_only_direct* (*CCACHE_READONLY_DIRECT*) [boolean]::
 
-    You can optionally set *CCACHE_PATH* to a colon-separated path where ccache
-    will look for the real compilers. If you don't do this then ccache will
-    look for the first executable matching the compiler name in the normal
-    *PATH* that isn't a symbolic link to ccache itself.
+    Just like *read_only* except that ccache will only try to retrieve results
+    from the cache using the direct mode, not the preprocessor mode. See
+    documentation for *read_only* regarding using a read-only ccache directory.
 
-*CCACHE_PREFIX*::
+*reache* (*CCACHE_RECACHE*) [boolean]::
 
-    This option adds a prefix to the command line that ccache runs when
-    invoking the compiler. Also see the section below on using ccache with
-    ``distcc''.
+    If true, ccache will not use any previously stored result. New results will
+    still be cached, possibly overwriting any pre-existing results.
 
-*CCACHE_READONLY*::
+*run_second_cpp* (*CCACHE_CPP2*) [boolean]::
 
-    The *CCACHE_READONLY* environment variable tells ccache to attempt to use
-    existing cached object files, but not to try to add anything new to the
-    cache. If you are using this because your *CCACHE_DIR* is read-only, then
-    you may find that you also need to set *CCACHE_TEMPDIR* as otherwise ccache
-    will fail to create temporary files.
+    If true, ccache will not use the optimisation of avoiding the second call
+    to the preprocessor by compiling the preprocessed output that was used for
+    finding the hash in the case of a cache miss. This is primarily a debugging
+    option, although it is possible that some unusual compilers will have
+    problems with compiling the preprocessed output, in which case this option
+    could allow ccache to be used anyway.
 
-*CCACHE_RECACHE*::
-
-    This forces ccache to not use any cached results, even if it finds them.
-    New results are still cached, but existing cache entries are ignored.
-
-*CCACHE_SLOPPINESS*::
+*sloppiness* (*CCACHE_SLOPPINESS*)::
 
     By default, ccache tries to give as few false cache hits as possible.
     However, in certain situations it's possible that you know things that
-    ccache can't take for granted. The *CCACHE_SLOPPINESS* environment variable
-    makes it possible to tell ccache to relax some checks in order to increase
-    the hit rate. The value should be a comma-separated string with options.
-    Available options are:
+    ccache can't take for granted. This setting makes it possible to tell
+    ccache to relax some checks in order to increase the hit rate. The value
+    should be a comma-separated string with options. Available options are:
 +
 --
 *file_macro*::
     Ignore *\_\_FILE__* being present in the source.
+*file_stat_matches*::
+    ccache normally examines a file's contents to determine whether it matches
+    the cached version. With this option set, ccache will consider a file as
+    matching its cached version if the sizes, mtimes and ctimes match.
+*include_file_ctime*::
+    By default, ccache also will not cache a file if it includes a header whose
+    ctime is too new. This option disables that check.
 *include_file_mtime*::
-    Don't check the modification time of include files in the direct mode.
+    By default, ccache will not cache a file if it includes a header whose
+    mtime is too new. This option disables that check.
 *pch_defines*::
     Be sloppy about #defines when precompiling a header file. See
     <<_precompiled_headers,PRECOMPILED HEADERS>> for more information.
@@ -385,41 +454,40 @@ WRAPPERS>>.
 See the discussion under <<_troubleshooting,TROUBLESHOOTING>> for more
 information.
 
-*CCACHE_TEMPDIR*::
+*temporary_dir* (*CCACHE_TEMPDIR*)::
 
-    The *CCACHE_TEMPDIR* environment variable specifies where ccache will put
-    temporary files. The default is *$CCACHE_DIR/tmp*.
+    This setting specifies where ccache will put temporary files. The default
+    is *<cache_dir>/tmp*.
 +
 NOTE: In previous versions of ccache, *CCACHE_TEMPDIR* had to be on the same
     filesystem as the *CCACHE_DIR* path, but this requirement has been
     relaxed.)
 
-*CCACHE_UMASK*::
+*umask* (*CCACHE_UMASK*)::
 
-    This sets the umask for ccache and all child processes (such as the
-    compiler). This is mostly useful when you wish to share your cache with
-    other users. Note that this also affects the file permissions set on the
-    object files created from your compilations.
+    This setting specifies the umask for ccache and all child processes (such
+    as the compiler). This is mostly useful when you wish to share your cache
+    with other users. Note that this also affects the file permissions set on
+    the object files created from your compilations.
 
-*CCACHE_UNIFY*::
+*unify* (*CCACHE_UNIFY*) [boolean]::
 
-    If you set the environment variable *CCACHE_UNIFY* then ccache will use a
-    C/C++ unifier when hashing the preprocessor output if the *-g* option is
-    not used. The unifier is slower than a normal hash, so setting this
-    environment variable loses a little bit of speed, but it means that ccache
-    can take advantage of not recompiling when the changes to the source code
-    consist of reformatting only. Note that using *CCACHE_UNIFY* changes the
-    hash, so cached compilations with *CCACHE_UNIFY* set cannot be used when
-    *CCACHE_UNIFY* is not set and vice versa. The reason the unifier is off by
-    default is that it can give incorrect line number information in compiler
-    warning messages. Also note that enabling the unifier implies turning off
-    the direct mode.
+    If true, ccache will use a C/C++ unifier when hashing the preprocessor
+    output if the *-g* option is not used. The unifier is slower than a normal
+    hash, so setting this environment variable loses a little bit of speed, but
+    it means that ccache can take advantage of not recompiling when the changes
+    to the source code consist of reformatting only. Note that enabling the
+    unifier changes the hash, so cached compilations produced when the unifier
+    is enabled cannot be reused when the unifier is disabled, and vice versa.
+    Enabling the unifier may result in incorrect line number information in
+    compiler warning messages and expansions of the *\_\_LINE__* macro. Also
+    note that enabling the unifier implies turning off the direct mode.
 
 
 Cache size management
 ---------------------
 
-By default ccache has a one gigabyte limit on the total size of files in the
+By default, ccache has a five gigabyte limit on the total size of files in the
 cache and no maximum number of files. You can set different limits using the
 *-M*/*--max-size* and *-F*/*--max-files* options. Use *ccache -s/--show-stats*
 to see the cache size and the currently configured limits (in addition to other
@@ -430,10 +498,10 @@ Cache compression
 -----------------
 
 ccache can optionally compress all files it puts into the cache using the
-compression library zlib. While this involves a negligible performance
-slowdown, it significantly increases the number of files that fit in the cache.
-You can turn on compression by setting the *CCACHE_COMPRESS* environment
-variable.
+compression library zlib. While this may involve a tiny performance slowdown,
+it increases the number of files that fit in the cache. You can turn on
+compression with the *compression* configuration setting and you can also tweak
+the compression level with *compression_level*.
 
 
 How ccache works
@@ -468,10 +536,10 @@ For both modes, the following information is included in the hash:
 * the extension used by the compiler for a file with preprocessor output
   (normally *.i* for C code and *.ii* for C++ code)
 * the compiler's size and modification time (or other compiler-specific
-  information specified by *CCACHE_COMPILERCHECK*)
+  information specified by the *compiler_check* setting)
 * the name of the compiler
-* the current directory (if *CCACHE_HASHDIR* is set)
-* contents of files specified by *CCACHE_EXTRAFILES* (if any)
+* the current directory (if the *hash_dir* setting is enabled)
+* contents of files specified by the *extra_files_to_hash* setting (if any)
 
 
 The direct mode
@@ -500,18 +568,24 @@ files that were read. The paths and hash sums of those include files are then
 stored in the manifest along with information about the produced compilation
 result.
 
+There is a catch with the direct mode: header files that were used by the
+compiler are recorded, but header files that were *not* used, but would have
+been used if they existed, are not. So, when ccache checks if a result can be
+taken from the cache, it currently can't check if the existence of a new header
+file should invalidate the result. In practice, the direct mode is safe to use
+in the absolute majority of cases.
+
 The direct mode will be disabled if any of the following holds:
 
-* the environment variable *CCACHE_NODIRECT* is set
+* the configuration setting *direct_mode* is false
 * a modification time of one of the include files is too new (needed to avoid a
   race condition)
-* the unifier is enabled (the environment variable *CCACHE_UNIFY* is set)
+* the unifier is enabled (the configuration setting *unify* is true)
 * a compiler option not supported by the direct mode is used:
 ** a *-Wp,_X_* compiler option other than *-Wp,-MD,_path_* and
    *-Wp,-MMD,_path_*
 ** *-Xpreprocessor*
-* the string ``\_\_TIME__'' is present outside comments and string literals in
-  the source code
+* the string ``\_\_TIME__'' is present in the source code
 
 
 The preprocessor mode
@@ -547,16 +621,16 @@ contain absolute paths:
 This means that if you compile the same code in different locations, you can't
 share compilation results between the different build directories since you get
 cache misses because of the absolute build directory paths that are part of the
-hash. To mitigate this problem, you can specify a ``base directory'' by setting
-the *CCACHE_BASEDIR* variable to an absolute path to the directory. ccache will
-then rewrite absolute paths that are under the base directory (i.e., paths that
-have the base directory as a prefix) to relative paths when constructing the
-hash. A typical path to use as the base directory is your home directory or
+hash. To mitigate this problem, you can specify a ``base directory'' in the
+configuration setting *base_dir* to an absolute path to the directory. ccache
+will then rewrite absolute paths that are under the base directory (i.e., paths
+that have the base directory as a prefix) to relative paths when constructing
+the hash. A typical path to use as the base directory is your home directory or
 another directory that is a parent of your build directories. (Don't use +/+ as
 the base directory since that will make ccache also rewrite paths to system
 header files, which doesn't gain anything.)
 
-The drawbacks of using *CCACHE_BASEDIR* are:
+The drawbacks of using a base directory are:
 
 * If you specify an absolute path to the source code file, *\_\_FILE__* macros
   will be expanded to a relative path instead.
@@ -573,16 +647,18 @@ Precompiled headers
 ccache has support for GCC's precompiled headers. However, you have to do some
 things to make it work properly:
 
-* You must set *CCACHE_SLOPPINESS* to *pch_defines,time_macros*. The reason is
-  that ccache can't tell whether *\_\_TIME\__* or *\_\_DATE__* is used when
-  using a precompiled header. Further, it can't detect changes in #defines in
-  the source code because of how preprocessing works in combination with
+* You must set *sloppiness* to *pch_defines,time_macros*. The reason is that
+  ccache can't tell whether *\_\_TIME\__* or *\_\_DATE__* is used when using a
+  precompiled header. Further, it can't detect changes in #defines in the
+  source code because of how preprocessing works in combination with
   precompiled headers.
 * You must either:
 +
 --
 ** use the *-include* compiler option to include the precompiled header
    (i.e., don't use *#include* in the source code to include the header); or
+** (for the Clang compiler) use the *-include-pch* compiler option to include
+   the PCH file generated from the precompiled header; or
 ** add the *-fpch-preprocess* compiler option when compiling.
 
 If you don't do this, either the non-precompiled version of the header file
@@ -599,10 +675,12 @@ A group of developers can increase the cache hit rate by sharing a cache
 directory. To share a cache without unpleasant side effects, the following
 conditions should to be met:
 
-* Use the same *CCACHE_DIR* environment variable setting.
-* Unset the *CCACHE_HARDLINK* environment variable.
-* Make sure everyone sets the *CCACHE_UMASK* environment variable to 002. This
-  ensures that cached files are accessible to everyone in the group.
+* Use the same cache directory.
+* Make sure that the configuration setting *hard_link* is false (which is the
+  default).
+* Make sure that all users are in the same group.
+* Set the configuration setting *umask* to 002. This ensures that cached files
+  are accessible to everyone in the group.
 * Make sure that all users have write permission in the entire cache directory
   (and that you trust all users of the shared cache).
 * Make sure that the setgid bit is set on all directories in the cache. This
@@ -617,8 +695,8 @@ timestamp-based build systems whenever another user links to an existing file.
 Typically, users will see that their libraries and binaries are relinked
 without reason.
 
-You may also want to make sure that the developers have *CCACHE_BASEDIR* set
-appropriately, as discussed in the previous section.
+You may also want to make sure that a base directory is set appropriately, as
+discussed in a previous section.
 
 
 Sharing a cache on NFS
@@ -631,7 +709,7 @@ filesystems), but keep in mind that:
   benchmarking to see if it's worth it.
 * ccache hasn't been tested very thoroughly on NFS.
 
-A tip is to set *CCACHE_TEMPDIR* to a directory on the local host to avoid NFS
+A tip is to set *temporary_dir* to a directory on the local host to avoid NFS
 traffic for temporary files.
 
 
@@ -639,25 +717,26 @@ Using ccache with other compiler wrappers
 -----------------------------------------
 
 The recommended way of combining ccache with another compiler wrapper (such as
-``distcc'') is by using the *CCACHE_PREFIX* option. You just need to set the
-environment variable *CCACHE_PREFIX* to the name of the wrapper (e.g. *distcc*)
-and ccache will prefix the command line with the specified command when running
-the compiler.
-
-Unless you set *CCACHE_COMPILERCHECK* to a suitable command (see the
-description of that configuration option), it is not recommended to use the
-form *ccache anotherwrapper compiler args* as the compilation command. It's
-also not recommended to use the masquerading technique for the other compiler
-wrapper. The reason is that by default, ccache will in both cases hash the
-mtime and size of the other wrapper instead of the real compiler, which means
-that:
+``distcc'') is by letting ccache execute the compiler wrapper. This is
+accomplished by defining the configuration setting *prefix_command*, for
+example by setting the environment variable *CCACHE_PREFIX* to the name of the
+wrapper (e.g. *distcc*). ccache will then prefix the command line with the
+specified command when running the compiler. To specify several prefix
+commands, set *prefix_command* to a colon-separated list of commands.
+
+Unless you set *compiler_check* to a suitable command (see the description of
+that configuration option), it is not recommended to use the form *ccache
+anotherwrapper compiler args* as the compilation command. It's also not
+recommended to use the masquerading technique for the other compiler wrapper.
+The reason is that by default, ccache will in both cases hash the mtime and
+size of the other wrapper instead of the real compiler, which means that:
 
 * Compiler upgrades will not be detected properly.
 * The cached results will not be shared between compilations with and without
   the other wrapper.
 
-Another minor thing is that if *CCACHE_PREFIX* is not used, ccache will
-needlessly invoke the other wrapper when running the preprocessor.
+Another minor thing is that if *prefix_command* is used, ccache will not invoke
+the other wrapper when running the preprocessor, which increase performance.
 
 
 Bugs
@@ -667,9 +746,11 @@ Bugs
   directive can be embedded in the source code inside an *__asm__* statement in
   order to include a file verbatim in the object file. If the included file is
   modified, ccache doesn't pick up the change since the inclusion isn't done by
-  the preprocessor. A workaround of this problem is to set *CCACHE_EXTRAFILES*
-  to the path of the included file.
+  the preprocessor. A workaround of this problem is to set
+  *extra_files_to_hash* to the path of the included file.
 
+* The direct mode fails to pick up new header files in some rare scenarios. See
+  <<_the_direct_mode,THE DIRECT MODE>> above.
 
 
 Troubleshooting
@@ -679,7 +760,7 @@ General
 ~~~~~~~
 
 A general tip for getting information about what ccache is doing is to enable
-debug logging by setting *CCACHE_LOGFILE*. The log contains executed commands,
+debug logging by setting *log_file*. The log contains executed commands,
 important decisions that ccache makes, read and written files, etc. Another way
 of keeping track of what is happening is to check the output of *ccache -s*.
 
@@ -709,16 +790,17 @@ problems and what may be done to increase the hit rate:
    affect the preprocessor output.
 ** The compiler option *-Xpreprocessor* or *-Wp,_X_* (except *-Wp,-MD,_path_*
    and *Wp,-MMD,_path_*) is used.
-** This was the first compilation with a new value of *CCACHE_BASEDIR*.
+** This was the first compilation with a new value of the base directory
+   setting.
 ** A modification time of one of the include files is too new (created the same
    second as the compilation is being done). This check is made to avoid a race
    condition. To fix this, create the include file earlier in the build
-   process, if possible, or set *CCACHE_SLOPPINESS* to *include_file_mtime* if
-   you are willing to take the risk. (The race condition consists of these
-   events: the preprocessor is run; an include file is modified by someone; the
-   new include file is hashed by ccache; the real compiler is run on the
-   preprocessor's output, which contains data from the old header file; the
-   wrong object file is stored in the cache.)
+   process, if possible, or set *sloppiness* to *include_file_mtime* if you are
+   willing to take the risk. (The race condition consists of these events: the
+   preprocessor is run; an include file is modified by someone; the new include
+   file is hashed by ccache; the real compiler is run on the preprocessor's
+   output, which contains data from the old header file; the wrong object file
+   is stored in the cache.)
 ** The *\_\_TIME\__* preprocessor macro is (potentially) being used. ccache
    turns off direct mode if ``\_\_TIME\__'' is present in the source code
    outside comments and string literals. This is done as a safety measure since
@@ -726,7 +808,7 @@ problems and what may be done to increase the hit rate:
    be sure, ccache would have to run the preprocessor, but the sole point of
    the direct mode is to avoid that.) If you know that *\_\_TIME\__* isn't used
    in practise, or don't care if ccache produces objects where *\_\_TIME__* is
-   expanded to something in the past, you can set *CCACHE_SLOPPINESS* to
+   expanded to something in the past, you can set *sloppiness* to
    *time_macros*.
 ** The *\_\_DATE\__* preprocessor macro is (potentially) being used and the
    date has changed. This is similar to how *\_\_TIME\__* is handled. If
@@ -735,15 +817,15 @@ problems and what may be done to increase the hit rate:
    correct object file if the *\_\_DATE\__* macro affects the output. If you
    know that *\_\_DATE\__* isn't used in practise, or don't care if ccache
    produces objects where *\_\_DATE__* is expanded to something in the past,
-   you can set *CCACHE_SLOPPINESS* to *time_macros*.
+   you can set *sloppiness* to *time_macros*.
 ** The *\_\_FILE\__* preprocessor macro is (potentially) being used and the
    file path has changed. If ``\_\_FILE\__'' is present in the source code
    outside comments and string literals, ccache hashes the current input file
    path in order to be able to produce the correct object file if the
    *\_\_FILE\__* macro affects the output. If you know that *\_\_FILE\__* isn't
    used in practise, or don't care if ccache produces objects where
-   *\_\_FILE__* is expanded to the wrong path, you can set *CCACHE_SLOPPINESS*
-   to *file_macro*.
+   *\_\_FILE__* is expanded to the wrong path, you can set *sloppiness* to
+   *file_macro*.
 * If ``cache miss'' has been incremented even though the same code has been
   compiled and cached before, ccache has either detected that something has
   changed anyway or a cleanup has been performed (either explicitly or
@@ -768,8 +850,8 @@ Errors when compiling with ccache
 
 If compilation doesn't work with ccache, but it works without it, one possible
 reason is that the compiler can't compile preprocessed output correctly. A
-workaround that may work is to set *CCACHE_CPP2*. This will make cache misses
-slower, though, so it is better to find and fix the root cause.
+workaround that may work is to enable *run_second_cpp**. This will make cache
+misses slower, though, so it is better to find and fix the root cause.
 
 
 Corrupt object files
@@ -779,8 +861,8 @@ It should be noted that ccache is susceptible to general storage problems. If a
 bad object file sneaks into the cache for some reason, it will of course stay
 bad. Some possible reasons for erroneous object files are bad hardware (disk
 drive, disk controller, memory, etc), buggy drivers or file systems, a bad
-*CCACHE_PREFIX* command or compiler wrapper. If this happens, the easiest way
-of fixing it is this:
+*prefix_command* or compiler wrapper. If this happens, the easiest way of
+fixing it is this:
 
 1. Build so that the bad object file ends up in the build tree.
 2. Remove the bad object file from the build tree.
index 08c162fbdd6f054c8ebc3f62d558ab5557c717f0..e7515b1d9e84fa016e7a57cbb594f7560ba3645e 100644 (file)
@@ -6,31 +6,37 @@ exec_prefix = @exec_prefix@
 bindir = @bindir@
 mandir = @mandir@
 datarootdir = @datarootdir@
+sysconfdir = @sysconfdir@
 installcmd = @INSTALL@
 
 AR = @AR@
 CC = @CC@
 CFLAGS = @CFLAGS@
-CPPFLAGS = @DEFS@ @CPPFLAGS@ -I. -I$(srcdir)
-LDFLAGS = @LDFLAGS@
+CPPFLAGS = @CPPFLAGS@
 EXEEXT = @EXEEXT@
+LDFLAGS = @LDFLAGS@
+LIBS = @LIBS@
 RANLIB = @RANLIB@
 
-libs = @LIBS@ -lz
+all_cflags = $(CFLAGS)
+all_cppflags = @DEFS@ @extra_cppflags@ -DSYSCONFDIR=$(sysconfdir) -I. -I$(srcdir) $(CPPFLAGS)
+all_ldflags = @extra_ldflags@ $(LDFLAGS)
+extra_libs = @extra_libs@
 
 base_sources = \
     ccache.c mdfour.c hash.c execute.c util.c args.c stats.c version.c \
     cleanup.c snprintf.c unify.c manifest.c hashtable.c hashtable_itr.c \
     murmurhashneutral2.c hashutil.c getopt_long.c exitfn.c lockfile.c \
-    counters.c language.c compopt.c
+    counters.c language.c compopt.c conf.c
 base_objs = $(base_sources:.c=.o)
 
 ccache_sources = main.c $(base_sources)
 ccache_objs = $(ccache_sources:.c=.o)
 
 zlib_sources = \
-    zlib/adler32.c zlib/compress.c zlib/crc32.c zlib/deflate.c zlib/gzio.c \
-    zlib/inffast.c zlib/inflate.c zlib/inftrees.c zlib/trees.c zlib/zutil.c
+    zlib/adler32.c zlib/crc32.c zlib/deflate.c zlib/gzclose.c zlib/gzlib.c \
+    zlib/gzread.c zlib/gzwrite.c zlib/inffast.c zlib/inflate.c \
+    zlib/inftrees.c zlib/trees.c zlib/zutil.c
 zlib_objs = $(zlib_sources:.c=.o)
 
 test_suites = @test_suites@
@@ -46,8 +52,8 @@ files_to_distclean = Makefile config.h config.log config.status
 .PHONY: all
 all: ccache$(EXEEXT)
 
-ccache$(EXEEXT): $(ccache_objs) @extra_deps@
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(ccache_objs) $(libs)
+ccache$(EXEEXT): $(ccache_objs) $(extra_libs)
+       $(CC) $(all_cflags) -o $@ $(ccache_objs) $(all_ldflags) $(extra_libs) $(LIBS)
 
 .PHONY: install
 install: all
@@ -60,13 +66,17 @@ install: all
 clean:
        rm -f $(files_to_clean)
 
+conf.c: confitems_lookup.c envtoconfitems_lookup.c
+
+$(zlib_objs): CPPFLAGS += -include config.h
+
 zlib/libz.a: $(zlib_objs)
        $(AR) cr $@ $(zlib_objs)
        $(RANLIB) $@
 
 .PHONY: perf
 perf: ccache$(EXEEXT)
-       $(srcdir)/perf.py --ccache ccache$(EXEEXT) $(CC) $(CFLAGS) $(CPPFLAGS) $(srcdir)/ccache.c
+       $(srcdir)/perf.py --ccache ccache$(EXEEXT) $(CC) $(all_cppflags) $(all_cflags) $(srcdir)/ccache.c
 
 .PHONY: test
 test: ccache$(EXEEXT) test/main$(EXEEXT)
@@ -77,8 +87,8 @@ test: ccache$(EXEEXT) test/main$(EXEEXT)
 quicktest: test/main$(EXEEXT)
        test/main$(EXEEXT)
 
-test/main$(EXEEXT): $(base_objs) $(test_objs) @extra_deps@
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(base_objs) $(test_objs) $(libs)
+test/main$(EXEEXT): $(base_objs) $(test_objs) $(extra_libs)
+       $(CC) $(all_cflags) -o $@ $(base_objs) $(test_objs) $(all_ldflags) $(extra_libs) $(LIBS)
 
 test/main.o: test/suites.h
 
@@ -98,6 +108,6 @@ installcheck: ccache$(EXEEXT) test/main$(EXEEXT)
        CCACHE=$(bindir)/ccache CC='$(CC)' $(srcdir)/test.sh
 
 .c.o:
-       $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
+       $(CC) $(all_cppflags) $(all_cflags) -c -o $@ $<
 
 @include_dev_mk@
index 2ba0cfda38f17275e5e93ca7c3e6575b3cd4e7fa..f90da226d587ce88c198fd5b1084c3d5e5192078 100644 (file)
--- a/NEWS.html
+++ b/NEWS.html
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>ccache news</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -742,48 +742,221 @@ asciidoc.install(2);
 </div>\r
 <div id="content">\r
 <div class="sect1">\r
-<h2 id="_ccache_3_1_12">ccache 3.1.12</h2>\r
+<h2 id="_ccache_3_2">ccache 3.2</h2>\r
 <div class="sectionbody">\r
-<div class="paragraph"><p>Release date: 2016-07-12</p></div>\r
+<div class="paragraph"><p>Release date: 2014-11-17</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes">Bug fixes</h3>\r
+<h3 id="_new_features_and_improvements">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
-Fixed a bug where (due to ccache rewriting paths) the compiler could choose\r
-  incorrect include files if <code>CCACHE_BASEDIR</code> is used and the source file path\r
-  is absolute and is a symlink.\r
+Added support for configuring ccache via one or several configuration files\r
+  instead of via environment variables. Environment variables still have\r
+  priority but are no longer the recommended way of customizing ccache\r
+  behavior. See the manual for more information.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for compiler error/warning messages with color.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Made creation of temporary directories and cache directories smarter to avoid\r
+  unnecessary <code>stat</code> calls.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Improved efficiency of the algorithm that scans for <code>__DATE_</code> and <code>__TIME__</code>\r
+  tokens in the hashed source code.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for several binaries (separated by space) in <code>CCACHE_PREFIX</code>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The <code>-c</code> option is no longer passed to the preprocessor. This fixes problems\r
+  with clang and Solaris&#8217;s C++ compiler.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+ccache no longer passes preprocessor options like <code>-D</code> and <code>-I</code> to the\r
+  compiler when compiling preprocessed output. This fixes warnings emitted by\r
+  clang.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Compiler options <code>-fprofile-generate</code>, <code>-fprofile-arcs</code>, <code>-fprofile-use</code> and\r
+  <code>-fbranch-probabilities</code> are now handled without bailing.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for clang&#8217;s <code>--serialize-diagnostic</code> option, storing the\r
+  diagnostic file (<code>.dia</code>) in the cache.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for precompiled headers when using clang.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for clang <code>.pth</code> (pretokenized header) files.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Changed the <code>-x</code> language option to use the new objective C standard for GCC\r
+  and clang.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+On a cache miss, ccache now instructs the compiler to create the object file\r
+  at the real destination and then copies the file into the cache instead of\r
+  the other way around. This is needed to support compiler options like\r
+  <code>-fprofile-arcs</code> and <code>--serialize-diagnostics</code>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+ccache now checks that included files' ctimes aren&#8217;t too new. This check can\r
+  be turned off by adding <code>include_file_ctime</code> to the &#8220;ccache sloppiness&#8221;\r
+  setting.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added possibility to get cache hits based on filename, size, mtime and ctime\r
+  only. On other words, source code files are not even read, only stat-ed. This\r
+  operation mode is opt-in by adding <code>file_stat_matches</code> to the &#8220;ccache\r
+  sloppiness&#8221; setting.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The filename part of options like <code>-Wp,-MDfilename</code> is no longer included in\r
+  the hash since the filename doesn&#8217;t have any bearing on the result.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added a &#8220;read-only direct&#8221; configuration setting, which is like the\r
+  ordinary read-only setting except that ccache will only try to retrieve\r
+  results from the cache using the direct mode, not the preprocessor mode.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+The display and interpretation of cache size has been changed to use SI\r
+  units.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Default cache size is now 5 GB (was previously 1 GiB).\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added configuration option to set the compression level of compressed object\r
+  files in the cache.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added support for <code>@file</code> and <code>-@file</code> arguments (reading options from a\r
+  file).\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<code>-Wl,</code> options are no longer included in the hash since they don&#8217;t affect\r
+  compilation.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Bail out on too hard compiler option <code>-Wp,-P</code>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Optimized MD4 calculation code on little-endian systems.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Various improvements and fixes on win32.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Improved logging to the ccache log file.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added <code>--dump-manifest</code> command-line option for debugging purposes.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Added <code>--with-bundled-zlib</code> configure option.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Upgraded bundled zlib to version 1.2.8.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Improved <code>dev.mk</code> to be more platform independent.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Made the test suite work with clang and gcc-llvm on OS X.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Various other improvements of the test suite.\r
 </p>\r
 </li>\r
 </ul></div>\r
 </div>\r
-</div>\r
-</div>\r
-<div class="sect1">\r
-<h2 id="_ccache_3_1_11">ccache 3.1.11</h2>\r
-<div class="sectionbody">\r
-<div class="paragraph"><p>Release date: 2015-03-07</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_2">Bug fixes</h3>\r
+<h3 id="_bug_fixes">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
-Fixed bug which could result in false cache hits when source code contains\r
-  <code>'"'</code> followed by <code>" /*"</code> or <code>" //"</code> (with variations).\r
+Any previous <code>.stderr</code> is now removed from the cache when recaching.\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
-Made hash of cached result created with and without <code>CCACHE_CPP2</code> different.\r
-  This makes it possible to rebuild with <code>CCACHE_CPP2</code> set without having to\r
-  clear the cache to get new results.\r
+Fixed an issue when handling the <code>-arch</code> compiler option with an argument.\r
 </p>\r
 </li>\r
 <li>\r
 <p>\r
-Don&#8217;t try to reset a non-existing stats file. This avoids &#8220;No such file or\r
-  directory&#8221; messages in the ccache log when the cache directory doesn&#8217;t\r
-  exist.\r
+Fixed race condition when creating the initial cache directory.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Fixed test suite failures when <code>CC</code> is a ccache-wrapped compiler.\r
 </p>\r
 </li>\r
 </ul></div>\r
@@ -795,7 +968,7 @@ Don&#8217;t try to reset a non-existing stats file. This avoids &#8220;No such f
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2014-10-19</p></div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_2">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -847,7 +1020,7 @@ Stale files in the internal temporary directory (<code>&lt;ccache_dir&gt;/tmp</c
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_3">Bug fixes</h3>\r
+<h3 id="_bug_fixes_2">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -887,7 +1060,7 @@ Fixed problem with logging of current working directory.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2013-01-06</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_4">Bug fixes</h3>\r
+<h3 id="_bug_fixes_3">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -937,7 +1110,7 @@ Fixed test suite to work on ecryptfs.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2012-08-11</p></div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements_2">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_3">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -958,7 +1131,7 @@ Clang plugins are now hashed to catch plugin upgrades.
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_5">Bug fixes</h3>\r
+<h3 id="_bug_fixes_4">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1006,7 +1179,7 @@ Fixed <code>static_assert</code> macro definition clash with GCC 4.7.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2012-01-08</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_6">Bug fixes</h3>\r
+<h3 id="_bug_fixes_5">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1079,7 +1252,7 @@ Improved documentation on how to fix bad object files in the cache.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2011-08-21</p></div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements_3">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_4">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1089,7 +1262,7 @@ Rewrite argument to <code>--sysroot</code> if <code>CCACHE_BASEDIR</code> is use
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_7">Bug fixes</h3>\r
+<h3 id="_bug_fixes_6">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1110,7 +1283,7 @@ Fixed alignment of &#8220;called for preprocessing&#8221; counter.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2011-05-29</p></div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements_4">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_5">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1142,7 +1315,7 @@ Improved order of statistics counters in <code>ccache -s</code> output.
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_8">Bug fixes</h3>\r
+<h3 id="_bug_fixes_7">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1175,7 +1348,7 @@ Systems that lack (and don&#8217;t need to be linked with) libm are now supporte
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2011-01-09</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_9">Bug fixes</h3>\r
+<h3 id="_bug_fixes_8">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1208,7 +1381,7 @@ The file handle in now correctly closed on write error when trying to create
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2010-11-28</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_10">Bug fixes</h3>\r
+<h3 id="_bug_fixes_9">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1235,7 +1408,7 @@ Portability fixes for HP-UX 11.00 and other esoteric systems.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2010-11-21</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_11">Bug fixes</h3>\r
+<h3 id="_bug_fixes_10">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1283,7 +1456,7 @@ The test suite now also works on systems that lack a /dev/zero.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2010-11-07</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_12">Bug fixes</h3>\r
+<h3 id="_bug_fixes_11">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1337,7 +1510,7 @@ Minor debug log message improvements.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2010-09-16</p></div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements_5">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_6">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1405,7 +1578,7 @@ Added <code>-install_name</code> as an option known to take an argument. (This i
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_13">Bug fixes</h3>\r
+<h3 id="_bug_fixes_12">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1491,7 +1664,7 @@ New <code>HACKING.txt</code> file with some notes about ccache code conventions.
 <div class="sectionbody">\r
 <div class="paragraph"><p>Release date: 2010-07-15</p></div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_14">Bug fixes</h3>\r
+<h3 id="_bug_fixes_13">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1537,7 +1710,7 @@ The way the hashes are calculated has changed, so you won&#8217;t get cache hits
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_new_features_and_improvements_6">New features and improvements</h3>\r
+<h3 id="_new_features_and_improvements_7">New features and improvements</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1680,7 +1853,7 @@ The following options are no longer hashed in the preprocessor mode:
 </ul></div>\r
 </div>\r
 <div class="sect2">\r
-<h3 id="_bug_fixes_15">Bug fixes</h3>\r
+<h3 id="_bug_fixes_14">Bug fixes</h3>\r
 <div class="ulist"><ul>\r
 <li>\r
 <p>\r
@@ -1825,9 +1998,8 @@ Statistics counters are now correctly updated for -E option failures and
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2014-11-17 19:56:32 CET\r
 </div>\r
 </div>\r
 </body>\r
index 62cbaa3262aa32f0e3b134741e07ea59b4810ad3..c67a77d24f72b5e11a38a966f2028f33e7490500 100644 (file)
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -2,37 +2,115 @@ ccache news
 ===========
 
 
-ccache 3.1.12
--------------
-Release date: 2016-07-12
+ccache 3.2
+----------
+Release date: 2014-11-17
 
 
-Bug fixes
-~~~~~~~~~
+New features and improvements
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-- Fixed a bug where (due to ccache rewriting paths) the compiler could choose
-  incorrect include files if `CCACHE_BASEDIR` is used and the source file path
-  is absolute and is a symlink.
+- Added support for configuring ccache via one or several configuration files
+  instead of via environment variables. Environment variables still have
+  priority but are no longer the recommended way of customizing ccache
+  behavior. See the manual for more information.
 
+- Added support for compiler error/warning messages with color.
 
-ccache 3.1.11
--------------
-Release date: 2015-03-07
+- Made creation of temporary directories and cache directories smarter to avoid
+  unnecessary `stat` calls.
+
+- Improved efficiency of the algorithm that scans for `__DATE_` and `__TIME__`
+  tokens in the hashed source code.
+
+- Added support for several binaries (separated by space) in `CCACHE_PREFIX`.
+
+- The `-c` option is no longer passed to the preprocessor. This fixes problems
+  with clang and Solaris's C++ compiler.
+
+- ccache no longer passes preprocessor options like `-D` and `-I` to the
+  compiler when compiling preprocessed output. This fixes warnings emitted by
+  clang.
+
+- Compiler options `-fprofile-generate`, `-fprofile-arcs`, `-fprofile-use` and
+  `-fbranch-probabilities` are now handled without bailing.
+
+- Added support for clang's `--serialize-diagnostic` option, storing the
+  diagnostic file (`.dia`) in the cache.
+
+- Added support for precompiled headers when using clang.
+
+- Added support for clang `.pth` (pretokenized header) files.
+
+- Changed the `-x` language option to use the new objective C standard for GCC
+  and clang.
+
+- On a cache miss, ccache now instructs the compiler to create the object file
+  at the real destination and then copies the file into the cache instead of
+  the other way around. This is needed to support compiler options like
+  `-fprofile-arcs` and `--serialize-diagnostics`.
+
+- ccache now checks that included files' ctimes aren't too new. This check can
+  be turned off by adding `include_file_ctime` to the ``ccache sloppiness''
+  setting.
+
+- Added possibility to get cache hits based on filename, size, mtime and ctime
+  only. On other words, source code files are not even read, only stat-ed. This
+  operation mode is opt-in by adding `file_stat_matches` to the ``ccache
+  sloppiness'' setting.
+
+- The filename part of options like `-Wp,-MDfilename` is no longer included in
+  the hash since the filename doesn't have any bearing on the result.
+
+- Added a ``read-only direct'' configuration setting, which is like the
+  ordinary read-only setting except that ccache will only try to retrieve
+  results from the cache using the direct mode, not the preprocessor mode.
+
+- The display and interpretation of cache size has been changed to use SI
+  units.
+
+- Default cache size is now 5 GB (was previously 1 GiB).
+
+- Added configuration option to set the compression level of compressed object
+  files in the cache.
+
+- Added support for `@file` and `-@file` arguments (reading options from a
+  file).
+
+- `-Wl,` options are no longer included in the hash since they don't affect
+  compilation.
+
+- Bail out on too hard compiler option `-Wp,-P`.
+
+- Optimized MD4 calculation code on little-endian systems.
+
+- Various improvements and fixes on win32.
+
+- Improved logging to the ccache log file.
+
+- Added `--dump-manifest` command-line option for debugging purposes.
+
+- Added `--with-bundled-zlib` configure option.
+
+- Upgraded bundled zlib to version 1.2.8.
+
+- Improved `dev.mk` to be more platform independent.
+
+- Made the test suite work with clang and gcc-llvm on OS X.
+
+- Various other improvements of the test suite.
 
 
 Bug fixes
 ~~~~~~~~~
 
-- Fixed bug which could result in false cache hits when source code contains
-  `'"'` followed by `" /*"` or `" //"` (with variations).
+- Any previous `.stderr` is now removed from the cache when recaching.
 
-- Made hash of cached result created with and without `CCACHE_CPP2` different.
-  This makes it possible to rebuild with `CCACHE_CPP2` set without having to
-  clear the cache to get new results.
+- Fixed an issue when handling the `-arch` compiler option with an argument.
 
-- Don't try to reset a non-existing stats file. This avoids ``No such file or
-  directory'' messages in the ccache log when the cache directory doesn't
-  exist.
+- Fixed race condition when creating the initial cache directory.
+
+- Fixed test suite failures when `CC` is a ccache-wrapped compiler.
 
 
 ccache 3.1.10
index 4eebda3ea67a4e57c877ad0104835fcb4dc10dfb..6d06405e2315138d6a0de1aaaea328ca14a3fb66 100644 (file)
@@ -734,7 +734,7 @@ asciidoc.install(2);
 <body class="article">\r
 <div id="header">\r
 <h1>ccache README</h1>\r
-<span id="revnumber">version 3.1.12</span>\r
+<span id="revnumber">version 3.2</span>\r
 <div id="toc">
   <div id="toctitle">Table of Contents</div>
   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
@@ -830,9 +830,8 @@ Thiele&#8217;s &#8220;compilercache&#8221; (see <a href="http://www.erikyyy.de/c
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Version 3.1.12<br />\r
-Last updated\r
- 2016-07-12 21:34:27 CEST\r
+Version 3.2<br />\r
+Last updated 2012-11-07 19:42:42 CET\r
 </div>\r
 </div>\r
 </body>\r
diff --git a/args.c b/args.c
index 13a3d37c4d133aae402fad7210401b2fed532eaa..2b17779a15028bb45483f2c77fece95e46a0a747 100644 (file)
--- a/args.c
+++ b/args.c
@@ -51,17 +51,160 @@ args_init_from_string(const char *command)
        return args;
 }
 
+struct args *
+args_init_from_gcc_atfile(const char *filename)
+{
+       struct args *args;
+       char *pos, *argtext, *argpos, *argbuf;
+       char quoting;
+
+       /* Used to track quoting state; if \0, we're not
+        * inside quotes. Otherwise stores the quoting character
+        * that started it, for matching the end quote */
+       quoting = '\0';
+
+       if (!(argtext = read_text_file(filename, 0)))
+               return NULL;
+
+       args = args_init(0, NULL);
+       pos = argtext;
+       argbuf = x_malloc(strlen(argtext) + 1);
+       argpos = argbuf;
+
+       while (1) {
+               switch (*pos) {
+               case '\\':
+                       pos++;
+                       if (*pos == '\0') {
+                               continue;
+                       }
+                       break;
+
+               case '\"':
+               case '\'':
+                       if (quoting != '\0') {
+                               if (quoting == *pos) {
+                                       quoting = '\0';
+                                       pos++;
+                                       continue;
+                               } else {
+                                       break;
+                               }
+                       } else {
+                               quoting = *pos;
+                               pos++;
+                               continue;
+                       }
+
+               case '\n':
+               case '\r':
+               case '\t':
+               case ' ':
+                       if (quoting) {
+                               break;
+                       }
+                       /* Fall through */
+
+               case '\0':
+                       /* end of token */
+                       *argpos = '\0';
+                       if (argbuf[0] != '\0')
+                               args_add(args, argbuf);
+                       argpos = argbuf;
+                       if (*pos == '\0') {
+                               goto out;
+                       } else {
+                               pos++;
+                               continue;
+                       }
+               }
+
+               *argpos = *pos;
+               pos++;
+               argpos++;
+       }
+
+out:
+       free(argbuf);
+       free(argtext);
+       return args;
+}
+
 struct args *
 args_copy(struct args *args)
 {
        return args_init(args->argc, args->argv);
 }
 
+/* Insert all arguments in src into dest at position index.
+ * If replace is true, the element at dest->argv[index] is replaced
+ * with the contents of src and everything past it is shifted.
+ * Otherwise, dest->argv[index] is also shifted.
+ *
+ * src is consumed by this operation and should not be freed or used
+ * again by the caller */
+void
+args_insert(struct args *dest, int index, struct args *src, bool replace)
+{
+       int offset;
+       int i;
+
+       /* Adjustments made if we are replacing or shifting the element
+        * currently at dest->argv[index] */
+       offset = replace ? 1 : 0;
+
+       if (replace) {
+               free(dest->argv[index]);
+       }
+
+       if (src->argc == 0) {
+               if (replace) {
+                       /* Have to shift everything down by 1 since
+                        * we replaced with an empty list */
+                       for (i = index; i < dest->argc; i++) {
+                               dest->argv[i] = dest->argv[i + 1];
+                       }
+                       dest->argc--;
+               }
+               args_free(src);
+               return;
+       }
+
+       if (src->argc == 1 && replace) {
+               /* Trivial case; replace with 1 element */
+               dest->argv[index] = src->argv[0];
+               src->argc = 0;
+               args_free(src);
+               return;
+       }
+
+       dest->argv = (char **)x_realloc(
+               dest->argv,
+               (src->argc + dest->argc + 1 - offset) *
+               sizeof(char *));
+
+       /* Shift arguments over */
+       for (i = dest->argc; i >= index + offset; i--) {
+               dest->argv[i + src->argc - offset] = dest->argv[i];
+       }
+
+       /* Copy the new arguments into place */
+       for (i = 0; i < src->argc; i++) {
+               dest->argv[i + index] = src->argv[i];
+       }
+
+       dest->argc += src->argc - offset;
+       src->argc = 0;
+       args_free(src);
+}
+
 void
 args_free(struct args *args)
 {
        int i;
-       if (!args) return;
+       if (!args) {
+               return;
+       }
        for (i = 0; i < args->argc; ++i) {
                if (args->argv[i]) {
                        free(args->argv[i]);
@@ -74,7 +217,8 @@ args_free(struct args *args)
 void
 args_add(struct args *args, const char *s)
 {
-       args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
+       args->argv = (char **)x_realloc(args->argv,
+                                       (args->argc + 2) * sizeof(char *));
        args->argv[args->argc] = x_strdup(s);
        args->argc++;
        args->argv[args->argc] = NULL;
@@ -123,7 +267,8 @@ args_remove_first(struct args *args)
 void
 args_add_prefix(struct args *args, const char *s)
 {
-       args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
+       args->argv = (char **)x_realloc(args->argv,
+                                       (args->argc + 2) * sizeof(char *));
        memmove(&args->argv[1], &args->argv[0],
                (args->argc+1) * sizeof(args->argv[0]));
        args->argv[0] = x_strdup(s);
diff --git a/autogen.sh b/autogen.sh
new file mode 100755 (executable)
index 0000000..eb3d9ba
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+set -e
+
+rm -f dev_mode_disabled
+autoheader
+autoconf
+echo "Now run ./configure and make"
index b65c14ce6e185cc80ed24eb73ca04e2d23c3d7fa..186c50ac0c089ebd06a98d5b76c4dfd4e9ce5ee6 100644 (file)
--- a/ccache.1
+++ b/ccache.1
@@ -2,12 +2,12 @@
 .\"     Title: ccache
 .\"    Author: [see the "Author" section]
 .\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
-.\"      Date: 07/12/2016
+.\"      Date: 11/17/2014
 .\"    Manual: ccache Manual
-.\"    Source: ccache 3.1.12
+.\"    Source: ccache 3.2
 .\"  Language: English
 .\"
-.TH "CCACHE" "1" "07/12/2016" "ccache 3\&.1\&.12" "ccache Manual"
+.TH "CCACHE" "1" "11/17/2014" "ccache 3\&.2" "ccache Manual"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -214,17 +214,17 @@ These options only apply when you invoke ccache as \(lqccache\(rq\&. When invoke
 .PP
 \fB\-c, \-\-cleanup\fR
 .RS 4
-Clean up the cache by removing old cached files until the specified file number and cache size limits are not exceeded\&. This also recalculates the cache file count and size totals\&. Normally, it\(cqs not needed to initiate cleanup manually as ccache keeps the cache below the specified limits at runtime and keeps statistics up to date on each compilation\&. Forcing a cleanup is mostly useful if you manually modify the cache contents or believe that the cache size statistics may be inaccurate\&.
+Clean up the cache by removing old cached files until the specified file number and cache size limits are not exceeded\&. This also recalculates the cache file count and size totals\&. Normally, there is no need to initiate cleanup manually as ccache keeps the cache below the specified limits at runtime and keeps statistics up to date on each compilation\&. Forcing a cleanup is mostly useful if you manually modify the cache contents or believe that the cache size statistics may be inaccurate\&.
 .RE
 .PP
 \fB\-C, \-\-clear\fR
 .RS 4
-Clear the entire cache, removing all cached files\&.
+Clear the entire cache, removing all cached files, but keeping the configuration file\&.
 .RE
 .PP
 \fB\-F, \-\-max\-files\fR=\fIN\fR
 .RS 4
-Set the maximum number of files allowed in the cache\&. The value is stored inside the cache directory and applies to all future compilations\&. Due to the way the value is stored the actual value used is always rounded down to the nearest multiple of 16\&.
+Set the maximum number of files allowed in the cache\&. Use 0 for no limit\&. The value is stored in a configuration file in the cache directory and applies to all future compilations\&.
 .RE
 .PP
 \fB\-h, \-\-help\fR
@@ -234,7 +234,24 @@ Print an options summary page\&.
 .PP
 \fB\-M, \-\-max\-size\fR=\fISIZE\fR
 .RS 4
-Set the maximum size of the files stored in the cache\&. You can specify a value in gigabytes, megabytes or kilobytes by appending a G, M or K to the value\&. The default is gigabytes\&. The actual value stored is rounded down to the nearest multiple of 16 kilobytes\&.
+Set the maximum size of the files stored in the cache\&.
+\fISIZE\fR
+should be a number followed by an optional suffix: k, M, G, T (decimal), Ki, Mi, Gi or Ti (binary)\&. The default suffix is G\&. Use 0 for no limit\&. The value is stored in a configuration file in the cache directory and applies to all future compilations\&.
+.RE
+.PP
+\fB\-o, \-\-set\-config\fR=\fIKEY=VALUE\fR
+.RS 4
+Set configuration
+\fIKEY\fR
+to
+\fIVALUE\fR\&. See
+CONFIGURATION
+for more information\&.
+.RE
+.PP
+\fB\-p, \-\-print\-config\fR
+.RS 4
+Print current configuration options and from where they originate (environment variable, configuration file or compile\-time default)\&.
 .RE
 .PP
 \fB\-s, \-\-show\-stats\fR
@@ -249,7 +266,7 @@ Print version and copyright information\&.
 .PP
 \fB\-z, \-\-zero\-stats\fR
 .RS 4
-Zero the cache statistics (but not the configured limits)\&.
+Zero the cache statistics (but not the configuration options)\&.
 .RE
 .SH "EXTRA OPTIONS"
 .sp
@@ -258,30 +275,109 @@ When run as a compiler, ccache usually just takes the same command line options
 The reason this can be important is that ccache does need to parse the command line and determine what is an input filename and what is a compiler option, as it needs the input filename to determine the name of the resulting object file (among other things)\&. The heuristic ccache uses when parsing the command line is that any argument that exists as a file is treated as an input file name\&. By using \fB\-\-ccache\-skip\fR you can force an option to not be treated as an input file name and instead be passed along to the compiler as a command line option\&.
 .sp
 Another case where \fB\-\-ccache\-skip\fR can be useful is if ccache interprets an option specially but shouldn\(cqt, since the option has another meaning for your compiler than what ccache thinks\&.
-.SH "ENVIRONMENT VARIABLES"
+.SH "CONFIGURATION"
+.sp
+ccache\(cqs default behavior can be overridden by configuration file settings, which in turn can be overridden by environment variables with names starting with \fBCCACHE_\fR\&. ccache normally reads configuration from two files: first a system\-level configuration file and secondly a cache\-specific configuration file\&. The priority of configuration settings is as follows (where 1 is highest):
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 1.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP "  1." 4.2
+.\}
+Environment variables\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 2.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP "  2." 4.2
+.\}
+The cache\-specific configuration file
+\fB<ccachedir>/ccache\&.conf\fR
+(typically
+\fB$HOME/\&.ccache/ccache\&.conf\fR)\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 3.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP "  3." 4.2
+.\}
+The system\-wide configuration file
+\fB<sysconfdir>/ccache\&.conf\fR
+(typically
+\fB/etc/ccache\&.conf\fR
+or
+\fB/usr/local/etc/ccache\&.conf\fR)\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 4.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP "  4." 4.2
+.\}
+Compile\-time defaults\&.
+.RE
 .sp
-ccache uses a number of environment variables to control operation\&. In most cases you won\(cqt need any of these as the defaults will be fine\&.
+As a special case, if the environment variable \fBCCACHE_CONFIGPATH\fR is set, ccache reads configuration from the specified path instead of the default paths\&.
+.SS "Configuration file syntax"
+.sp
+Configuration files are in a simple \(lqkey = value\(rq format, one setting per line\&. Lines starting with a hash sign are comments\&. Blank lines are ignored, as is whitespace surrounding keys and values\&. Example:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+# Set maximum cache size to 10 GB:
+max_size = 10G
+.fi
+.if n \{\
+.RE
+.\}
+.SS "Boolean values"
+.sp
+Some settings are boolean values (i\&.e\&. truth values)\&. In a configuration file, such values must be set to the string \fBtrue\fR or \fBfalse\fR\&. For the corresponding environment variables, the semantics are a bit different: a set environment variable means \(lqtrue\(rq regardless of the value (even if set to the empty string), and an unset environment variable means \(lqfalse\(rq\&. Each boolean environment variable also has a negated form starting with \fBCCACHE_NO\fR\&. For example, \fBCCACHE_COMPRESS\fR can be set to force compression and \fBCCACHE_NOCOMPRESS\fR can be set to force no compression\&.
+.SS "Configuration settings"
+.sp
+Below is a list of available configuration settings\&. The corresponding environment variable name is indicated in parentheses after each configuration setting key\&. Boolean options are indicated with \(lq[boolean]\(rq
 .PP
-\fBCCACHE_BASEDIR\fR
+\fBbase_dir\fR (\fBCCACHE_BASEDIR\fR)
 .RS 4
-If you set the environment variable
-\fBCCACHE_BASEDIR\fR
-to an absolute path to a directory, ccache rewrites absolute paths into relative paths before computing the hash that identifies the compilation, but only for paths under the specified directory\&. See the discussion under
+This setting should be an absolute path to a directory\&. ccache then rewrites absolute paths into relative paths before computing the hash that identifies the compilation, but only for paths under the specified directory\&. If set to the empty string (which is the default), no rewriting is done\&. See also the discussion under
 COMPILING IN DIFFERENT DIRECTORIES\&.
 .RE
 .PP
-\fBCCACHE_CC\fR
+\fBcache_dir\fR (\fBCCACHE_DIR\fR)
+.RS 4
+This setting specifies where ccache will keep its cached compiler outputs\&. It will only take effect if set in the system\-wide configuration file or as an environment variable\&. The default is
+\fB$HOME/\&.ccache\fR\&.
+.RE
+.PP
+\fBcache_dir_levels\fR (\fBCCACHE_NLEVELS\fR)
 .RS 4
-You can optionally set
-\fBCCACHE_CC\fR
-to force the name of the compiler to use\&. If you don\(cqt do this then ccache works it out from the command line\&.
+This setting allows you to choose the number of directory levels in the cache directory\&. The default is 2\&. The minimum is 1 and the maximum is 8\&.
 .RE
 .PP
-\fBCCACHE_COMPILERCHECK\fR
+\fBcompiler\fR (\fBCCACHE_CC\fR)
 .RS 4
-By default, ccache includes the modification time (\(lqmtime\(rq) and size of the compiler in the hash to ensure that results retrieved from the cache are accurate\&. The
-\fBCCACHE_COMPILERCHECK\fR
-environment variable can be used to select another strategy\&. Possible values are:
+This setting can be used to force the name of the compiler to use\&. If set to the empty string (which is the default), ccache works it out from the command line\&.
+.RE
+.PP
+\fBcompiler_check\fR (\fBCCACHE_COMPILERCHECK\fR)
+.RS 4
+By default, ccache includes the modification time (\(lqmtime\(rq) and size of the compiler in the hash to ensure that results retrieved from the cache are accurate\&. This setting can be used to select another strategy\&. Possible values are:
 .PP
 \fBcontent\fR
 .RS 4
@@ -304,7 +400,9 @@ setting if you know what you are doing\&.
 .PP
 \fIa command string\fR
 .RS 4
-Hash the standard output and standard error output of the specified command\&. The string will be split on whitespace to find out the command and arguments to run\&. No other interpretation of the command string will be done, except that the special word \(lq%compiler%\(rq will be replaced with the path to the compiler\&. Several commands can be specified with semicolon as separator\&. Examples:
+Hash the standard output and standard error output of the specified command\&. The string will be split on whitespace to find out the command and arguments to run\&. No other interpretation of the command string will be done, except that the special word
+\fB%compiler%\fR
+will be replaced with the path to the compiler\&. Several commands can be specified with semicolon as separator\&. Examples:
 .sp
 .RS 4
 .ie n \{\
@@ -336,148 +434,109 @@ Another case is when the compiler (as seen by ccache) actually isn\(cqt the real
 \fBmtime\fR
 method will hash the mtime and size of the other compiler wrapper, which means that ccache won\(cqt be able to detect a compiler upgrade\&. Using a suitable command to identify the compiler is thus safer, but it\(cqs also slower, so you should consider continue using the
 \fBmtime\fR
-method in combination with
-\fBCCACHE_PREFIX\fR
-if possible\&. See
+method in combination with the
+\fBprefix_command\fR
+setting if possible\&. See
 USING CCACHE WITH OTHER COMPILER WRAPPERS\&.
 .RE
 .RE
 .PP
-\fBCCACHE_COMPRESS\fR
-.RS 4
-If you set the environment variable
-\fBCCACHE_COMPRESS\fR
-then ccache will compress object files and other compiler output it puts in the cache\&. However, this setting has no effect on how files are retrieved from the cache; compressed and uncompressed results will still be usable regardless of this setting\&.
-.RE
-.PP
-\fBCCACHE_CPP2\fR
+\fBcompression\fR (\fBCCACHE_COMPRESS\fR) [boolean]
 .RS 4
-If you set the environment variable
-\fBCCACHE_CPP2\fR
-then ccache will not use the optimisation of avoiding the second call to the preprocessor by compiling the preprocessed output that was used for finding the hash in the case of a cache miss\&. This is primarily a debugging option, although it is possible that some unusual compilers will have problems with the intermediate filename extensions used in this optimisation, in which case this option could allow ccache to be used anyway\&.
+If true, ccache will compress object files and other compiler output it puts in the cache\&. However, this setting has no effect on how files are retrieved from the cache; compressed and uncompressed results will still be usable regardless of this setting\&. The default is false\&.
 .RE
 .PP
-\fBCCACHE_DETECT_SHEBANG\fR
+\fBcompression_level\fR (\fBCCACHE_COMPRESSLEVEL\fR)
 .RS 4
-The
-\fBCCACHE_DETECT_SHEBANG\fR
-environment variable only has meaning on Windows\&. It instructs ccache to open the executable file to detect the
-\fB#!/bin/sh\fR
-string, in which case ccache will search for
-\fBsh\&.exe\fR
-in
-\fBPATH\fR
-and use that to launch the executable\&.
+This setting determines the level at which ccache will compress object files\&. It only has effect if
+\fBcompression\fR
+is enabled\&. The value defaults to 6, and must be no lower than 1 (fastest, worst compression) and no higher than 9 (slowest, best compression)\&.
 .RE
 .PP
-\fBCCACHE_DIR\fR
+\fBcpp_extension\fR (\fBCCACHE_EXTENSION\fR)
 .RS 4
-The
-\fBCCACHE_DIR\fR
-environment variable specifies where ccache will keep its cached compiler output\&. The default is
-\fB$HOME/\&.ccache\fR\&.
+This setting can be used to force a certain extension for the intermediate preprocessed file\&. The default is to automatically determine the extension to use for intermediate preprocessor files based on the type of file being compiled, but that sometimes doesn\(cqt work\&. For example, when using the \(lqaCC\(rq compiler on HP\-UX, set the cpp extension to
+\fBi\fR\&.
 .RE
 .PP
-\fBCCACHE_DISABLE\fR
+\fBdirect_mode\fR (\fBCCACHE_DIRECT\fR) [boolean]
 .RS 4
-If you set the environment variable
-\fBCCACHE_DISABLE\fR
-then ccache will just call the real compiler, bypassing the cache completely\&.
+If true, the direct mode will be used\&. The default is true\&. See
+THE DIRECT MODE\&.
 .RE
 .PP
-\fBCCACHE_EXTENSION\fR
+\fBdisable\fR (\fBCCACHE_DISABLE\fR) [boolean]
 .RS 4
-ccache tries to automatically determine the extension to use for intermediate preprocessor files based on the type of file being compiled\&. Unfortunately this sometimes doesn\(cqt work, for example when using the \(lqaCC\(rq compiler on HP\-UX\&. On systems like this you can use the
-\fBCCACHE_EXTENSION\fR
-option to override the default\&. On HP\-UX set this environment variable to
-\fBi\fR
-if you use the \(lqaCC\(rq compiler\&.
+When true, ccache will just call the real compiler, bypassing the cache completely\&. The default is false\&.
 .RE
 .PP
-\fBCCACHE_EXTRAFILES\fR
+\fBextra_files_to_hash\fR (\fBCCACHE_EXTRAFILES\fR)
 .RS 4
-If you set the environment variable
-\fBCCACHE_EXTRAFILES\fR
-to a list of paths then ccache will include the contents of those files when calculating the hash sum\&. The list separator is semicolon in Windows systems and colon on other systems\&.
+This setting is a list of paths to files that ccache will include in the the hash sum that idetifies the build\&. The list separator is semicolon on Windows systems and colon on other systems\&.
 .RE
 .PP
-\fBCCACHE_HARDLINK\fR
+\fBhard_link\fR (\fBCCACHE_HARDLINK\fR) [boolean]
 .RS 4
-If you set the environment variable
-\fBCCACHE_HARDLINK\fR
-then ccache will attempt to use hard links from the cache directory when creating the compiler output rather than using a file copy\&. Using hard links may be slightly faster in some situations, but can confuse programs like \(lqmake\(rq that rely on modification times\&. Another thing to keep in mind is that if the resulting object file is modified in any way, this corrupts the cached object file as well\&. Hard links are never made for compressed cache files\&. This means that you should not set the
-\fBCCACHE_COMPRESS\fR
-variable if you want to use hard links\&.
+If true, ccache will attempt to use hard links from the cache directory when creating the compiler output rather than using a file copy\&. Using hard links may be slightly faster in some situations, but can confuse programs like \(lqmake\(rq that rely on modification times\&. Another thing to keep in mind is that if the resulting object file is modified in any way, this corrupts the cached object file as well\&. Hard links are never made for compressed cache files\&. This means that you should not enable compression if you want to use hard links\&. The default is false\&.
 .RE
 .PP
-\fBCCACHE_HASHDIR\fR
+\fBhash_dir\fR (\fBCCACHE_HASHDIR\fR) [boolean]
 .RS 4
-This tells ccache to hash the current working directory when calculating the hash that is used to distinguish two compilations\&. This prevents a problem with the storage of the current working directory in the debug info of a object file, which can lead ccache to give a cached object file that has the working directory in the debug info set incorrectly\&. This option is off by default as the incorrect setting of this debug info rarely causes problems\&. If you strike problems with GDB not using the correct directory then enable this option\&.
+If true, ccache will include the current working directory in the hash that is used to distinguish two compilations\&. This prevents a problem with the storage of the current working directory in the debug info of a object file, which can lead ccache to give a cached object file that has the working directory in the debug info set incorrectly\&. This option is off by default as the incorrect setting of this debug info rarely causes problems\&. If you strike problems with GDB not using the correct directory then enable this option\&.
 .RE
 .PP
-\fBCCACHE_LOGFILE\fR
+\fBlog_file\fR (\fBCCACHE_LOGFILE\fR)
 .RS 4
-If you set the
-\fBCCACHE_LOGFILE\fR
-environment variable then ccache will write information on what it is doing to the specified file\&. This is useful for tracking down problems\&.
+If set to a file path, ccache will write information on what it is doing to the specified file\&. This is useful for tracking down problems\&.
 .RE
 .PP
-\fBCCACHE_NLEVELS\fR
+\fBstats\fR (\fBCCACHE_STATS\fR) [boolean]
 .RS 4
-The environment variable
-\fBCCACHE_NLEVELS\fR
-allows you to choose the number of levels of hash in the cache directory\&. The default is 2\&. The minimum is 1 and the maximum is 8\&.
+If true, ccache will update the statistics counters on each compilation\&. The default is true\&.
 .RE
 .PP
-\fBCCACHE_NODIRECT\fR
+\fBpath\fR (\fBCCACHE_PATH\fR)
 .RS 4
-If you set the environment variable
-\fBCCACHE_NODIRECT\fR
-then ccache will not use the direct mode\&.
+If set, ccache will search directories in this list when looking for the real compiler\&. The list separator is semicolon on Windows systems and colon on other systems\&. If not set, ccache will look for the first executable matching the compiler name in the normal
+\fBPATH\fR
+that isn\(cqt a symbolic link to ccache itself\&.
 .RE
 .PP
-\fBCCACHE_NOSTATS\fR
+\fBprefix_command\fR (\fBCCACHE_PREFIX\fR)
 .RS 4
-If you set the environment variable
-\fBCCACHE_NOSTATS\fR
-then ccache will not update the statistics files on each compilation\&.
+This option adds a list of prefixes (separated by space) to the command line that ccache uses when invoking the compiler\&. See also
+USING CCACHE WITH OTHER COMPILER WRAPPERS\&.
 .RE
 .PP
-\fBCCACHE_PATH\fR
+\fBread_only\fR (\fBCCACHE_READONLY\fR) [boolean]
 .RS 4
-You can optionally set
-\fBCCACHE_PATH\fR
-to a colon\-separated path where ccache will look for the real compilers\&. If you don\(cqt do this then ccache will look for the first executable matching the compiler name in the normal
-\fBPATH\fR
-that isn\(cqt a symbolic link to ccache itself\&.
+If true, ccache will attempt to use existing cached object files, but it will not to try to add anything new to the cache\&. If you are using this because your ccache directory is read\-only, then you need to set
+\fBtemporary_dir\fR
+as otherwise ccache will fail to create temporary files\&.
 .RE
 .PP
-\fBCCACHE_PREFIX\fR
+\fBread_only_direct\fR (\fBCCACHE_READONLY_DIRECT\fR) [boolean]
 .RS 4
-This option adds a prefix to the command line that ccache runs when invoking the compiler\&. Also see the section below on using ccache with \(lqdistcc\(rq\&.
+Just like
+\fBread_only\fR
+except that ccache will only try to retrieve results from the cache using the direct mode, not the preprocessor mode\&. See documentation for
+\fBread_only\fR
+regarding using a read\-only ccache directory\&.
 .RE
 .PP
-\fBCCACHE_READONLY\fR
+\fBreache\fR (\fBCCACHE_RECACHE\fR) [boolean]
 .RS 4
-The
-\fBCCACHE_READONLY\fR
-environment variable tells ccache to attempt to use existing cached object files, but not to try to add anything new to the cache\&. If you are using this because your
-\fBCCACHE_DIR\fR
-is read\-only, then you may find that you also need to set
-\fBCCACHE_TEMPDIR\fR
-as otherwise ccache will fail to create temporary files\&.
+If true, ccache will not use any previously stored result\&. New results will still be cached, possibly overwriting any pre\-existing results\&.
 .RE
 .PP
-\fBCCACHE_RECACHE\fR
+\fBrun_second_cpp\fR (\fBCCACHE_CPP2\fR) [boolean]
 .RS 4
-This forces ccache to not use any cached results, even if it finds them\&. New results are still cached, but existing cache entries are ignored\&.
+If true, ccache will not use the optimisation of avoiding the second call to the preprocessor by compiling the preprocessed output that was used for finding the hash in the case of a cache miss\&. This is primarily a debugging option, although it is possible that some unusual compilers will have problems with compiling the preprocessed output, in which case this option could allow ccache to be used anyway\&.
 .RE
 .PP
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR (\fBCCACHE_SLOPPINESS\fR)
 .RS 4
-By default, ccache tries to give as few false cache hits as possible\&. However, in certain situations it\(cqs possible that you know things that ccache can\(cqt take for granted\&. The
-\fBCCACHE_SLOPPINESS\fR
-environment variable makes it possible to tell ccache to relax some checks in order to increase the hit rate\&. The value should be a comma\-separated string with options\&. Available options are:
+By default, ccache tries to give as few false cache hits as possible\&. However, in certain situations it\(cqs possible that you know things that ccache can\(cqt take for granted\&. This setting makes it possible to tell ccache to relax some checks in order to increase the hit rate\&. The value should be a comma\-separated string with options\&. Available options are:
 .PP
 \fBfile_macro\fR
 .RS 4
@@ -486,9 +545,19 @@ Ignore
 being present in the source\&.
 .RE
 .PP
+\fBfile_stat_matches\fR
+.RS 4
+ccache normally examines a file\(cqs contents to determine whether it matches the cached version\&. With this option set, ccache will consider a file as matching its cached version if the sizes, mtimes and ctimes match\&.
+.RE
+.PP
+\fBinclude_file_ctime\fR
+.RS 4
+By default, ccache also will not cache a file if it includes a header whose ctime is too new\&. This option disables that check\&.
+.RE
+.PP
 \fBinclude_file_mtime\fR
 .RS 4
-Don\(cqt check the modification time of include files in the direct mode\&.
+By default, ccache will not cache a file if it includes a header whose mtime is too new\&. This option disables that check\&.
 .RE
 .PP
 \fBpch_defines\fR
@@ -512,12 +581,10 @@ TROUBLESHOOTING
 for more information\&.
 .RE
 .PP
-\fBCCACHE_TEMPDIR\fR
+\fBtemporary_dir\fR (\fBCCACHE_TEMPDIR\fR)
 .RS 4
-The
-\fBCCACHE_TEMPDIR\fR
-environment variable specifies where ccache will put temporary files\&. The default is
-\fB$CCACHE_DIR/tmp\fR\&.
+This setting specifies where ccache will put temporary files\&. The default is
+\fB<cache_dir>/tmp\fR\&.
 .if n \{\
 .sp
 .\}
@@ -539,31 +606,25 @@ path, but this requirement has been relaxed\&.)
 .RE
 .RE
 .PP
-\fBCCACHE_UMASK\fR
+\fBumask\fR (\fBCCACHE_UMASK\fR)
 .RS 4
-This sets the umask for ccache and all child processes (such as the compiler)\&. This is mostly useful when you wish to share your cache with other users\&. Note that this also affects the file permissions set on the object files created from your compilations\&.
+This setting specifies the umask for ccache and all child processes (such as the compiler)\&. This is mostly useful when you wish to share your cache with other users\&. Note that this also affects the file permissions set on the object files created from your compilations\&.
 .RE
 .PP
-\fBCCACHE_UNIFY\fR
+\fBunify\fR (\fBCCACHE_UNIFY\fR) [boolean]
 .RS 4
-If you set the environment variable
-\fBCCACHE_UNIFY\fR
-then ccache will use a C/C++ unifier when hashing the preprocessor output if the
+If true, ccache will use a C/C++ unifier when hashing the preprocessor output if the
 \fB\-g\fR
-option is not used\&. The unifier is slower than a normal hash, so setting this environment variable loses a little bit of speed, but it means that ccache can take advantage of not recompiling when the changes to the source code consist of reformatting only\&. Note that using
-\fBCCACHE_UNIFY\fR
-changes the hash, so cached compilations with
-\fBCCACHE_UNIFY\fR
-set cannot be used when
-\fBCCACHE_UNIFY\fR
-is not set and vice versa\&. The reason the unifier is off by default is that it can give incorrect line number information in compiler warning messages\&. Also note that enabling the unifier implies turning off the direct mode\&.
+option is not used\&. The unifier is slower than a normal hash, so setting this environment variable loses a little bit of speed, but it means that ccache can take advantage of not recompiling when the changes to the source code consist of reformatting only\&. Note that enabling the unifier changes the hash, so cached compilations produced when the unifier is enabled cannot be reused when the unifier is disabled, and vice versa\&. Enabling the unifier may result in incorrect line number information in compiler warning messages and expansions of the
+\fB__LINE__\fR
+macro\&. Also note that enabling the unifier implies turning off the direct mode\&.
 .RE
 .SH "CACHE SIZE MANAGEMENT"
 .sp
-By default ccache has a one gigabyte limit on the total size of files in the cache and no maximum number of files\&. You can set different limits using the \fB\-M\fR/\fB\-\-max\-size\fR and \fB\-F\fR/\fB\-\-max\-files\fR options\&. Use \fBccache \-s/\-\-show\-stats\fR to see the cache size and the currently configured limits (in addition to other various statistics)\&.
+By default, ccache has a five gigabyte limit on the total size of files in the cache and no maximum number of files\&. You can set different limits using the \fB\-M\fR/\fB\-\-max\-size\fR and \fB\-F\fR/\fB\-\-max\-files\fR options\&. Use \fBccache \-s/\-\-show\-stats\fR to see the cache size and the currently configured limits (in addition to other various statistics)\&.
 .SH "CACHE COMPRESSION"
 .sp
-ccache can optionally compress all files it puts into the cache using the compression library zlib\&. While this involves a negligible performance slowdown, it significantly increases the number of files that fit in the cache\&. You can turn on compression by setting the \fBCCACHE_COMPRESS\fR environment variable\&.
+ccache can optionally compress all files it puts into the cache using the compression library zlib\&. While this may involve a tiny performance slowdown, it increases the number of files that fit in the cache\&. You can turn on compression with the \fBcompression\fR configuration setting and you can also tweak the compression level with \fBcompression_level\fR\&.
 .SH "HOW CCACHE WORKS"
 .sp
 The basic idea is to detect when you are compiling exactly the same code a second time and reuse the previously produced output\&. The detection is done by hashing different kinds of information that should be unique for the compilation and then using the hash sum to identify the cached output\&. ccache uses MD4, a very fast cryptographic hash algorithm, for the hashing\&. (MD4 is nowadays too weak to be useful in cryptographic contexts, but it should be safe enough to be used to identify recompilations\&.) On a cache hit, ccache is able to supply all of the correct compiler outputs (including all warnings, dependency file, etc) from the cache\&.
@@ -622,8 +683,9 @@ for C++ code)
 .sp -1
 .IP \(bu 2.3
 .\}
-the compiler\(cqs size and modification time (or other compiler\-specific information specified by
-\fBCCACHE_COMPILERCHECK\fR)
+the compiler\(cqs size and modification time (or other compiler\-specific information specified by the
+\fBcompiler_check\fR
+setting)
 .RE
 .sp
 .RS 4
@@ -645,9 +707,9 @@ the name of the compiler
 .sp -1
 .IP \(bu 2.3
 .\}
-the current directory (if
-\fBCCACHE_HASHDIR\fR
-is set)
+the current directory (if the
+\fBhash_dir\fR
+setting is enabled)
 .RE
 .sp
 .RS 4
@@ -658,9 +720,9 @@ is set)
 .sp -1
 .IP \(bu 2.3
 .\}
-contents of files specified by
-\fBCCACHE_EXTRAFILES\fR
-(if any)
+contents of files specified by the
+\fBextra_files_to_hash\fR
+setting (if any)
 .RE
 .SS "The direct mode"
 .sp
@@ -725,6 +787,8 @@ hash sums of the include files at the time the compilation results were stored i
 .sp
 The current contents of the include files are then hashed and compared to the information in the manifest\&. If there is a match, ccache knows the result of the compilation\&. If there is no match, ccache falls back to running the preprocessor\&. The output from the preprocessor is parsed to find the include files that were read\&. The paths and hash sums of those include files are then stored in the manifest along with information about the produced compilation result\&.
 .sp
+There is a catch with the direct mode: header files that were used by the compiler are recorded, but header files that were \fBnot\fR used, but would have been used if they existed, are not\&. So, when ccache checks if a result can be taken from the cache, it currently can\(cqt check if the existence of a new header file should invalidate the result\&. In practice, the direct mode is safe to use in the absolute majority of cases\&.
+.sp
 The direct mode will be disabled if any of the following holds:
 .sp
 .RS 4
@@ -735,9 +799,9 @@ The direct mode will be disabled if any of the following holds:
 .sp -1
 .IP \(bu 2.3
 .\}
-the environment variable
-\fBCCACHE_NODIRECT\fR
-is set
+the configuration setting
+\fBdirect_mode\fR
+is false
 .RE
 .sp
 .RS 4
@@ -759,9 +823,9 @@ a modification time of one of the include files is too new (needed to avoid a ra
 .sp -1
 .IP \(bu 2.3
 .\}
-the unifier is enabled (the environment variable
-\fBCCACHE_UNIFY\fR
-is set)
+the unifier is enabled (the configuration setting
+\fBunify\fR
+is true)
 .RE
 .sp
 .RS 4
@@ -810,7 +874,7 @@ and
 .sp -1
 .IP \(bu 2.3
 .\}
-the string \(lq__TIME__\(rq is present outside comments and string literals in the source code
+the string \(lq__TIME__\(rq is present in the source code
 .RE
 .SS "The preprocessor mode"
 .sp
@@ -898,9 +962,9 @@ The source code file path may be absolute, and that path may substituted for
 macros in the source code or included in warnings emitted to standard error by the preprocessor\&.
 .RE
 .sp
-This means that if you compile the same code in different locations, you can\(cqt share compilation results between the different build directories since you get cache misses because of the absolute build directory paths that are part of the hash\&. To mitigate this problem, you can specify a \(lqbase directory\(rq by setting the \fBCCACHE_BASEDIR\fR variable to an absolute path to the directory\&. ccache will then rewrite absolute paths that are under the base directory (i\&.e\&., paths that have the base directory as a prefix) to relative paths when constructing the hash\&. A typical path to use as the base directory is your home directory or another directory that is a parent of your build directories\&. (Don\(cqt use / as the base directory since that will make ccache also rewrite paths to system header files, which doesn\(cqt gain anything\&.)
+This means that if you compile the same code in different locations, you can\(cqt share compilation results between the different build directories since you get cache misses because of the absolute build directory paths that are part of the hash\&. To mitigate this problem, you can specify a \(lqbase directory\(rq in the configuration setting \fBbase_dir\fR to an absolute path to the directory\&. ccache will then rewrite absolute paths that are under the base directory (i\&.e\&., paths that have the base directory as a prefix) to relative paths when constructing the hash\&. A typical path to use as the base directory is your home directory or another directory that is a parent of your build directories\&. (Don\(cqt use / as the base directory since that will make ccache also rewrite paths to system header files, which doesn\(cqt gain anything\&.)
 .sp
-The drawbacks of using \fBCCACHE_BASEDIR\fR are:
+The drawbacks of using a base directory are:
 .sp
 .RS 4
 .ie n \{\
@@ -939,7 +1003,7 @@ ccache has support for GCC\(cqs precompiled headers\&. However, you have to do s
 .IP \(bu 2.3
 .\}
 You must set
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR
 to
 \fBpch_defines,time_macros\fR\&. The reason is that ccache can\(cqt tell whether
 \fB__TIME__\fR
@@ -981,6 +1045,19 @@ in the source code to include the header); or
 .sp -1
 .IP \(bu 2.3
 .\}
+(for the Clang compiler) use the
+\fB\-include\-pch\fR
+compiler option to include the PCH file generated from the precompiled header; or
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
 add the
 \fB\-fpch\-preprocess\fR
 compiler option when compiling\&.
@@ -1000,9 +1077,20 @@ A group of developers can increase the cache hit rate by sharing a cache directo
 .sp -1
 .IP \(bu 2.3
 .\}
-Use the same
-\fBCCACHE_DIR\fR
-environment variable setting\&.
+Use the same cache directory\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+Make sure that the configuration setting
+\fBhard_link\fR
+is false (which is the default)\&.
 .RE
 .sp
 .RS 4
@@ -1013,9 +1101,7 @@ environment variable setting\&.
 .sp -1
 .IP \(bu 2.3
 .\}
-Unset the
-\fBCCACHE_HARDLINK\fR
-environment variable\&.
+Make sure that all users are in the same group\&.
 .RE
 .sp
 .RS 4
@@ -1026,9 +1112,9 @@ environment variable\&.
 .sp -1
 .IP \(bu 2.3
 .\}
-Make sure everyone sets the
-\fBCCACHE_UMASK\fR
-environment variable to 002\&. This ensures that cached files are accessible to everyone in the group\&.
+Set the configuration setting
+\fBumask\fR
+to 002\&. This ensures that cached files are accessible to everyone in the group\&.
 .RE
 .sp
 .RS 4
@@ -1055,7 +1141,7 @@ Make sure that the setgid bit is set on all directories in the cache\&. This tel
 .sp
 The reason to avoid the hard link mode is that the hard links cause unwanted side effects, as all links to a cached file share the file\(cqs modification timestamp\&. This results in false dependencies to be triggered by timestamp\-based build systems whenever another user links to an existing file\&. Typically, users will see that their libraries and binaries are relinked without reason\&.
 .sp
-You may also want to make sure that the developers have \fBCCACHE_BASEDIR\fR set appropriately, as discussed in the previous section\&.
+You may also want to make sure that a base directory is set appropriately, as discussed in a previous section\&.
 .SH "SHARING A CACHE ON NFS"
 .sp
 It is possible to put the cache directory on an NFS filesystem (or similar filesystems), but keep in mind that:
@@ -1082,12 +1168,12 @@ Having the cache on NFS may slow down compilation\&. Make sure to do some benchm
 ccache hasn\(cqt been tested very thoroughly on NFS\&.
 .RE
 .sp
-A tip is to set \fBCCACHE_TEMPDIR\fR to a directory on the local host to avoid NFS traffic for temporary files\&.
+A tip is to set \fBtemporary_dir\fR to a directory on the local host to avoid NFS traffic for temporary files\&.
 .SH "USING CCACHE WITH OTHER COMPILER WRAPPERS"
 .sp
-The recommended way of combining ccache with another compiler wrapper (such as \(lqdistcc\(rq) is by using the \fBCCACHE_PREFIX\fR option\&. You just need to set the environment variable \fBCCACHE_PREFIX\fR to the name of the wrapper (e\&.g\&. \fBdistcc\fR) and ccache will prefix the command line with the specified command when running the compiler\&.
+The recommended way of combining ccache with another compiler wrapper (such as \(lqdistcc\(rq) is by letting ccache execute the compiler wrapper\&. This is accomplished by defining the configuration setting \fBprefix_command\fR, for example by setting the environment variable \fBCCACHE_PREFIX\fR to the name of the wrapper (e\&.g\&. \fBdistcc\fR)\&. ccache will then prefix the command line with the specified command when running the compiler\&. To specify several prefix commands, set \fBprefix_command\fR to a colon\-separated list of commands\&.
 .sp
-Unless you set \fBCCACHE_COMPILERCHECK\fR to a suitable command (see the description of that configuration option), it is not recommended to use the form \fBccache anotherwrapper compiler args\fR as the compilation command\&. It\(cqs also not recommended to use the masquerading technique for the other compiler wrapper\&. The reason is that by default, ccache will in both cases hash the mtime and size of the other wrapper instead of the real compiler, which means that:
+Unless you set \fBcompiler_check\fR to a suitable command (see the description of that configuration option), it is not recommended to use the form \fBccache anotherwrapper compiler args\fR as the compilation command\&. It\(cqs also not recommended to use the masquerading technique for the other compiler wrapper\&. The reason is that by default, ccache will in both cases hash the mtime and size of the other wrapper instead of the real compiler, which means that:
 .sp
 .RS 4
 .ie n \{\
@@ -1111,7 +1197,7 @@ Compiler upgrades will not be detected properly\&.
 The cached results will not be shared between compilations with and without the other wrapper\&.
 .RE
 .sp
-Another minor thing is that if \fBCCACHE_PREFIX\fR is not used, ccache will needlessly invoke the other wrapper when running the preprocessor\&.
+Another minor thing is that if \fBprefix_command\fR is used, ccache will not invoke the other wrapper when running the preprocessor, which increase performance\&.
 .SH "BUGS"
 .sp
 .RS 4
@@ -1127,13 +1213,26 @@ ccache doesn\(cqt handle the GNU Assembler\(cqs
 directive correctly\&. This directive can be embedded in the source code inside an
 \fB\fIasm\fR\fR
 statement in order to include a file verbatim in the object file\&. If the included file is modified, ccache doesn\(cqt pick up the change since the inclusion isn\(cqt done by the preprocessor\&. A workaround of this problem is to set
-\fBCCACHE_EXTRAFILES\fR
+\fBextra_files_to_hash\fR
 to the path of the included file\&.
 .RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+The direct mode fails to pick up new header files in some rare scenarios\&. See
+THE DIRECT MODE
+above\&.
+.RE
 .SH "TROUBLESHOOTING"
 .SS "General"
 .sp
-A general tip for getting information about what ccache is doing is to enable debug logging by setting \fBCCACHE_LOGFILE\fR\&. The log contains executed commands, important decisions that ccache makes, read and written files, etc\&. Another way of keeping track of what is happening is to check the output of \fBccache \-s\fR\&.
+A general tip for getting information about what ccache is doing is to enable debug logging by setting \fBlog_file\fR\&. The log contains executed commands, important decisions that ccache makes, read and written files, etc\&. Another way of keeping track of what is happening is to check the output of \fBccache \-s\fR\&.
 .SS "Performance"
 .sp
 ccache has been written to perform well out of the box, but sometimes you may have to do some adjustments of how you use the compiler and ccache in order to improve performance\&.
@@ -1202,8 +1301,7 @@ and
 .sp -1
 .IP \(bu 2.3
 .\}
-This was the first compilation with a new value of
-\fBCCACHE_BASEDIR\fR\&.
+This was the first compilation with a new value of the base directory setting\&.
 .RE
 .sp
 .RS 4
@@ -1215,7 +1313,7 @@ This was the first compilation with a new value of
 .IP \(bu 2.3
 .\}
 A modification time of one of the include files is too new (created the same second as the compilation is being done)\&. This check is made to avoid a race condition\&. To fix this, create the include file earlier in the build process, if possible, or set
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR
 to
 \fBinclude_file_mtime\fR
 if you are willing to take the risk\&. (The race condition consists of these events: the preprocessor is run; an include file is modified by someone; the new include file is hashed by ccache; the real compiler is run on the preprocessor\(cqs output, which contains data from the old header file; the wrong object file is stored in the cache\&.)
@@ -1240,7 +1338,7 @@ affect the output\&. (To be sure, ccache would have to run the preprocessor, but
 isn\(cqt used in practise, or don\(cqt care if ccache produces objects where
 \fB__TIME__\fR
 is expanded to something in the past, you can set
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR
 to
 \fBtime_macros\fR\&.
 .RE
@@ -1264,7 +1362,7 @@ macro affects the output\&. If you know that
 isn\(cqt used in practise, or don\(cqt care if ccache produces objects where
 \fB__DATE__\fR
 is expanded to something in the past, you can set
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR
 to
 \fBtime_macros\fR\&.
 .RE
@@ -1286,7 +1384,7 @@ macro affects the output\&. If you know that
 isn\(cqt used in practise, or don\(cqt care if ccache produces objects where
 \fB__FILE__\fR
 is expanded to the wrong path, you can set
-\fBCCACHE_SLOPPINESS\fR
+\fBsloppiness\fR
 to
 \fBfile_macro\fR\&.
 .RE
@@ -1355,10 +1453,10 @@ PRECOMPILED HEADERS\&.
 .RE
 .SS "Errors when compiling with ccache"
 .sp
-If compilation doesn\(cqt work with ccache, but it works without it, one possible reason is that the compiler can\(cqt compile preprocessed output correctly\&. A workaround that may work is to set \fBCCACHE_CPP2\fR\&. This will make cache misses slower, though, so it is better to find and fix the root cause\&.
+If compilation doesn\(cqt work with ccache, but it works without it, one possible reason is that the compiler can\(cqt compile preprocessed output correctly\&. A workaround that may work is to enable \fBrun_second_cpp\fR*\&. This will make cache misses slower, though, so it is better to find and fix the root cause\&.
 .SS "Corrupt object files"
 .sp
-It should be noted that ccache is susceptible to general storage problems\&. If a bad object file sneaks into the cache for some reason, it will of course stay bad\&. Some possible reasons for erroneous object files are bad hardware (disk drive, disk controller, memory, etc), buggy drivers or file systems, a bad \fBCCACHE_PREFIX\fR command or compiler wrapper\&. If this happens, the easiest way of fixing it is this:
+It should be noted that ccache is susceptible to general storage problems\&. If a bad object file sneaks into the cache for some reason, it will of course stay bad\&. Some possible reasons for erroneous object files are bad hardware (disk drive, disk controller, memory, etc), buggy drivers or file systems, a bad \fBprefix_command\fR or compiler wrapper\&. If this happens, the easiest way of fixing it is this:
 .sp
 .RS 4
 .ie n \{\
index ff0eb0dfdb30c1fa30b918b2fbea64bade4d3d22..f3a6a0383aeadf23886adfd2b8e44f68c6904a97 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -2,7 +2,7 @@
  * ccache -- a fast C/C++ compiler cache
  *
  * Copyright (C) 2002-2007 Andrew Tridgell
- * Copyright (C) 2009-2016 Joel Rosdahl
+ * Copyright (C) 2009-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
 #include "language.h"
 #include "manifest.h"
 
+#define STRINGIFY(x) #x
+#define TO_STRING(x) STRINGIFY(x)
+
 static const char VERSION_TEXT[] =
 MYNAME " version %s\n"
 "\n"
 "Copyright (C) 2002-2007 Andrew Tridgell\n"
-"Copyright (C) 2009-2016 Joel Rosdahl\n"
+"Copyright (C) 2009-2011 Joel Rosdahl\n"
 "\n"
 "This program is free software; you can redistribute it and/or modify it under\n"
 "the terms of the GNU General Public License as published by the Free Software\n"
@@ -52,12 +55,14 @@ static const char USAGE_TEXT[] =
 "Options:\n"
 "    -c, --cleanup         delete old files and recalculate size counters\n"
 "                          (normally not needed as this is done automatically)\n"
-"    -C, --clear           clear the cache completely\n"
+"    -C, --clear           clear the cache completely (except configuration)\n"
 "    -F, --max-files=N     set maximum number of files in cache to N (use 0 for\n"
 "                          no limit)\n"
 "    -M, --max-size=SIZE   set maximum size of cache to SIZE (use 0 for no\n"
-"                          limit; available suffixes: G, M and K; default\n"
-"                          suffix: G)\n"
+"                          limit); available suffixes: k, M, G, T (decimal) and\n"
+"                          Ki, Mi, Gi, Ti (binary); default suffix: G\n"
+"    -o, --set-config=K=V  set configuration key K to value V\n"
+"    -p, --print-config    print current configuration options\n"
 "    -s, --show-stats      show statistics summary\n"
 "    -z, --zero-stats      zero statistics counters\n"
 "\n"
@@ -66,20 +71,17 @@ static const char USAGE_TEXT[] =
 "\n"
 "See also <http://ccache.samba.org>.\n";
 
-/* current working directory taken from $PWD, or getcwd() if $PWD is bad */
-char *current_working_dir = NULL;
-
-/* the base cache directory */
-char *cache_dir = NULL;
+/* Global configuration data. */
+struct conf *conf = NULL;
 
-/* the directory for temporary files */
-static char *temp_dir;
+/* Where to write configuration changes. */
+char *primary_config_path = NULL;
 
-/* the debug logfile name, if set */
-char *cache_logfile = NULL;
+/* Secondary, read-only configuration file (if any). */
+char *secondary_config_path = NULL;
 
-/* base directory (from CCACHE_BASEDIR) */
-char *base_dir = NULL;
+/* current working directory taken from $PWD, or getcwd() if $PWD is bad */
+char *current_working_dir = NULL;
 
 /* the original argument list */
 static struct args *orig_args;
@@ -93,6 +95,9 @@ static char *output_obj;
 /* The path to the dependency file (implicit or specified with -MF). */
 static char *output_dep;
 
+/* Diagnostic generation information (clang). */
+static char *output_dia = NULL;
+
 /*
  * Name (represented as a struct file_hash) of the file containing the cached
  * object code.
@@ -117,6 +122,12 @@ static char *cached_stderr;
  */
 static char *cached_dep;
 
+/*
+ * Full path to the file containing the diagnostic information (for clang)
+ * (cachedir/a/b/cdef[...]-size.dia).
+ */
+static char *cached_dia;
+
 /*
  * Full path to the file containing the manifest
  * (cachedir/a/b/cdef[...]-size.manifest).
@@ -127,10 +138,7 @@ static char *manifest_path;
  * Time of compilation. Used to see if include files have changed after
  * compilation.
  */
-static time_t time_of_compilation;
-
-/* Bitmask of SLOPPY_*. */
-unsigned sloppiness = 0;
+time_t time_of_compilation;
 
 /*
  * Files included by the preprocessor and their hashes/sizes. Key: file path.
@@ -141,9 +149,6 @@ static struct hashtable *included_files;
 /* is gcc being asked to output dependencies? */
 static bool generating_dependencies;
 
-/* the extension of the file (without dot) after pre-processing */
-static const char *i_extension;
-
 /* the name of the temporary pre-processor file */
 static char *i_tmpfile;
 
@@ -155,38 +160,28 @@ static char *cpp_stderr;
 
 /*
  * Full path to the statistics file in the subdirectory where the cached result
- * belongs (CCACHE_DIR/X/stats).
+ * belongs (<cache_dir>/<x>/stats).
  */
 char *stats_file = NULL;
 
-/* can we safely use the unification hashing backend? */
-static bool enable_unify;
-
-/* should we use the direct mode? */
-static bool enable_direct = true;
-
-/*
- * Whether to enable compression of files stored in the cache. (Manifest files
- * are always compressed.)
- */
-static bool enable_compression = false;
+/* Whether the output is a precompiled header */
+static bool output_is_precompiled_header = false;
 
-/* number of levels (1 <= nlevels <= 8) */
-static int nlevels = 2;
+/* Profile generation / usage information */
+static char *profile_dir = NULL;
+static bool profile_use = false;
+static bool profile_generate = false;
 
 /*
- * Whether we should use the optimization of passing the already existing
- * preprocessed source code to the compiler.
+ * Whether we are using a precompiled header (either via -include, #include or
+ * clang's -include-pch or -include-pth).
  */
-static bool compile_preprocessed_source_code;
-
-/* Whether the output is a precompiled header */
-static bool output_is_precompiled_header = false;
+static bool using_precompiled_header = false;
 
 /*
- * Whether we are using a precompiled header (either via -include or #include).
+ * The .gch/.pch/.pth file used for compilation.
  */
-static bool using_precompiled_header = false;
+static char *included_pch_file = NULL;
 
 /* How long (in microseconds) to wait before breaking a stale lock. */
 unsigned lock_staleness_limit = 2000000;
@@ -214,28 +209,70 @@ static struct pending_tmp_file *pending_tmp_files = NULL;
  */
 static const char HASH_PREFIX[] = "3";
 
-/* Something went badly wrong - just execute the real compiler. */
 static void
-failed(void)
+add_prefix(struct args *args)
 {
        char *e;
+       char *tok, *saveptr = NULL;
+       struct args *prefix;
+       int i;
 
-       /* strip any local args */
-       args_strip(orig_args, "--ccache-");
+       if (str_eq(conf->prefix_command, "")) {
+               return;
+       }
+
+       prefix = args_init(0, NULL);
+       e = x_strdup(conf->prefix_command);
+       for (tok = strtok_r(e, " ", &saveptr);
+            tok;
+            tok = strtok_r(NULL, " ", &saveptr)) {
+               char *p;
 
-       if ((e = getenv("CCACHE_PREFIX"))) {
-               char *p = find_executable(e, MYNAME);
+               p = find_executable(tok, MYNAME);
                if (!p) {
-                       fatal("%s: %s", e, strerror(errno));
+                       fatal("%s: %s", tok, strerror(errno));
                }
-               args_add_prefix(orig_args, p);
+
+               args_add(prefix, p);
+               free(p);
+       }
+       free(e);
+
+       cc_log("Using command-line prefix %s", conf->prefix_command);
+       for (i = prefix->argc; i != 0; i--) {
+               args_add_prefix(args, prefix->argv[i-1]);
        }
+       args_free(prefix);
+}
+
+/* Something went badly wrong - just execute the real compiler. */
+static void
+failed(void)
+{
+       assert(orig_args);
+
+       args_strip(orig_args, "--ccache-");
+       add_prefix(orig_args);
 
        cc_log("Failed; falling back to running the real compiler");
        cc_log_argv("Executing ", orig_args->argv);
        exitfn_call();
        execv(orig_args->argv[0], orig_args->argv);
-       fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
+       fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
+}
+
+static const char *
+temp_dir()
+{
+       static char *path = NULL;
+       if (path) {
+               return path; /* Memoize */
+       }
+       path = conf->temporary_dir;
+       if (str_eq(path, "")) {
+               path = format("%s/tmp", conf->cache_dir);
+       }
+       return path;
 }
 
 static void
@@ -274,15 +311,15 @@ clean_up_internal_tempdir(void)
        struct stat st;
        time_t now = time(NULL);
 
-       stat(cache_dir, &st);
+       stat(conf->cache_dir, &st);
        if (st.st_mtime + 3600 >= now) {
                /* No cleanup needed. */
                return;
        }
 
-       update_mtime(cache_dir);
+       update_mtime(conf->cache_dir);
 
-       dir = opendir(temp_dir);
+       dir = opendir(temp_dir());
        if (!dir) {
                return;
        }
@@ -294,7 +331,7 @@ clean_up_internal_tempdir(void)
                        continue;
                }
 
-               path = format("%s/%s", temp_dir, entry->d_name);
+               path = format("%s/%s", temp_dir(), entry->d_name);
                if (lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
                        tmp_unlink(path);
                }
@@ -329,20 +366,18 @@ get_current_working_dir(void)
 static char *
 get_path_in_cache(const char *name, const char *suffix)
 {
-       int i;
+       unsigned i;
        char *path;
        char *result;
 
-       path = x_strdup(cache_dir);
-       for (i = 0; i < nlevels; ++i) {
+       path = x_strdup(conf->cache_dir);
+       for (i = 0; i < conf->cache_dir_levels; ++i) {
                char *p = format("%s/%c", path, name[i]);
                free(path);
                path = p;
-               if (!getenv("CCACHE_READONLY") && create_dir(path) != 0) {
-                       fatal("Failed to create %s: %s", path, strerror(errno));
-               }
        }
-       result = format("%s/%s%s", path, name + nlevels, suffix);
+
+       result = format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
        free(path);
        return result;
 }
@@ -353,14 +388,17 @@ get_path_in_cache(const char *name, const char *suffix)
  * also updated. Takes over ownership of path.
  */
 static void
-remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
+remember_include_file(char *path, struct mdfour *cpp_hash)
 {
+#ifdef _WIN32
+       DWORD attributes;
+#endif
        struct mdfour fhash;
        struct stat st;
        char *source = NULL;
        size_t size;
-       int result;
        bool is_pch;
+       size_t path_len = strlen(path);
 
        if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
                /* Typically <built-in> or <command-line>. */
@@ -377,6 +415,14 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
                goto ignore;
        }
 
+#ifdef _WIN32
+       /* stat fails on directories on win32 */
+       attributes = GetFileAttributes(path);
+       if (attributes != INVALID_FILE_ATTRIBUTES &&
+           attributes & FILE_ATTRIBUTE_DIRECTORY)
+               goto ignore;
+#endif
+
        if (stat(path, &st) != 0) {
                cc_log("Failed to stat include file %s: %s", path, strerror(errno));
                goto failure;
@@ -392,12 +438,18 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
        }
 
        /* Let's hash the include file. */
-       if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
+       if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
            && st.st_mtime >= time_of_compilation) {
                cc_log("Include file %s too new", path);
                goto failure;
        }
 
+       if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
+           && st.st_ctime >= time_of_compilation) {
+               cc_log("Include file %s ctime too new", path);
+               goto failure;
+       }
+
        hash_start(&fhash);
 
        is_pch = is_precompiled_header(path);
@@ -411,10 +463,12 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
                hash_delimiter(cpp_hash, "pch_hash");
                hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
        }
-       if (enable_direct) {
+       if (conf->direct_mode) {
                struct file_hash *h;
 
                if (!is_pch) { /* else: the file has already been hashed. */
+                       int result;
+
                        if (st.st_size > 0) {
                                if (!read_file(path, st.st_size, &source, &size)) {
                                        goto failure;
@@ -424,7 +478,7 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
                                size = 0;
                        }
 
-                       result = hash_source_code_string(&fhash, source, size, path);
+                       result = hash_source_code_string(conf, &fhash, source, size, path);
                        if (result & HASH_SOURCE_CODE_ERROR
                            || result & HASH_SOURCE_CODE_FOUND_TIME) {
                                goto failure;
@@ -444,7 +498,7 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
 
 failure:
        cc_log("Disabling direct mode");
-       enable_direct = false;
+       conf->direct_mode = false;
        /* Fall through. */
 ignore:
        free(path);
@@ -452,9 +506,8 @@ ignore:
 }
 
 /*
- * Make a relative path from current working directory to path if
- * CCACHE_BASEDIR is a prefix of path. Takes over ownership of path. Caller
- * frees.
+ * Make a relative path from current working directory to path if path is under
+ * the base directory. Takes over ownership of path. Caller frees.
  */
 static char *
 make_relative_path(char *path)
@@ -462,7 +515,7 @@ make_relative_path(char *path)
        char *relpath, *canon_path, *path_suffix = NULL;
        struct stat st;
 
-       if (!base_dir || !str_startswith(path, base_dir)) {
+       if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
                return path;
        }
 
@@ -509,8 +562,8 @@ make_relative_path(char *path)
  * This function reads and hashes a file. While doing this, it also does these
  * things:
  *
- * - Makes include file paths whose prefix is CCACHE_BASEDIR relative when
- *   computing the hash sum.
+ * - Makes include file paths for which the base directory is a prefix relative
+ *   when computing the hash sum.
  * - Stores the paths and hashes of included files in the global variable
  *   included_files.
  */
@@ -590,7 +643,7 @@ process_preprocessed_file(struct mdfour *hash, const char *path)
                        path = x_strndup(p, q - p);
                        path = make_relative_path(path);
                        hash_string(hash, path);
-                       remember_include_file(path, q - p, hash);
+                       remember_include_file(path, hash);
                        p = q;
                } else {
                        q++;
@@ -599,42 +652,123 @@ process_preprocessed_file(struct mdfour *hash, const char *path)
 
        hash_buffer(hash, p, (end - p));
        free(data);
+
+       /* Explicitly check the .gch/.pch/.pth file, Clang does not include any
+        * mention of it in the preprocessed output. */
+       if (included_pch_file) {
+               char *path = x_strdup(included_pch_file);
+               path = make_relative_path(path);
+               hash_string(hash, path);
+               remember_include_file(path, hash);
+       }
+
        return true;
 }
 
+/* Copy or link a file to the cache. */
+static void
+put_file_in_cache(const char *source, const char *dest)
+{
+       int ret;
+       struct stat st;
+       bool do_link = conf->hard_link && !conf->compression;
+
+       if (do_link) {
+               x_unlink(dest);
+               ret = link(source, dest);
+       } else {
+               ret = copy_file(source, dest, conf->compression);
+       }
+       if (ret != 0) {
+               cc_log("Failed to %s %s to %s: %s",
+                      do_link ? "link" : "copy",
+                      source,
+                      dest,
+                      strerror(errno));
+               stats_update(STATS_ERROR);
+               failed();
+       }
+       cc_log("Stored in cache: %s -> %s", source, dest);
+       if (stat(dest, &st) != 0) {
+               cc_log("Failed to stat %s: %s", dest, strerror(errno));
+               stats_update(STATS_ERROR);
+               failed();
+       }
+       stats_update_size(file_size(&st), 1);
+}
+
+/* Copy or link a file from the cache. */
+static void
+get_file_from_cache(const char *source, const char *dest)
+{
+       int ret;
+       bool do_link = conf->hard_link && !file_is_compressed(source);
+
+       if (do_link) {
+               x_unlink(dest);
+               ret = link(source, dest);
+       } else {
+               ret = copy_file(source, dest, 0);
+       }
+
+       if (ret == -1) {
+               if (errno == ENOENT) {
+                       /* Someone removed the file just before we began copying? */
+                       cc_log("Cache file %s just disappeared from cache", source);
+                       stats_update(STATS_MISSING);
+               } else {
+                       cc_log("Failed to %s %s to %s: %s",
+                              do_link ? "link" : "copy",
+                              source,
+                              dest,
+                              strerror(errno));
+                       stats_update(STATS_ERROR);
+               }
+               failed();
+       }
+
+       cc_log("Created from cache: %s -> %s", source, dest);
+}
+
 /* run the real compiler and put the result in cache */
 static void
 to_cache(struct args *args)
 {
-       char *tmp_stdout, *tmp_stderr, *tmp_obj;
+       char *tmp_stdout, *tmp_stderr, *tmp_dia;
        struct stat st;
-       int status;
-       size_t added_bytes = 0;
-       unsigned added_files = 0;
+       int status, tmp_stdout_fd, tmp_stderr_fd;
 
-       tmp_stdout = format("%s.tmp.stdout.%s", cached_obj, tmp_string());
-       tmp_stderr = format("%s.tmp.stderr.%s", cached_obj, tmp_string());
-       tmp_obj = format("%s.tmp.%s", cached_obj, tmp_string());
+       tmp_stdout = format("%s.tmp.stdout", cached_obj);
+       tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
+       tmp_stderr = format("%s.tmp.stderr", cached_obj);
+       tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
 
        args_add(args, "-o");
-       args_add(args, tmp_obj);
+       args_add(args, output_obj);
+
+       if (output_dia) {
+               tmp_dia = x_strdup(output_dia);
+               args_add(args, "--serialize-diagnostics");
+               args_add(args, tmp_dia);
+       } else {
+               tmp_dia = NULL;
+       }
 
        /* Turn off DEPENDENCIES_OUTPUT when running cc1, because
         * otherwise it will emit a line like
         *
         *  tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
-        *
-        * unsetenv() is on BSD and Linux but not portable. */
-       putenv("DEPENDENCIES_OUTPUT");
+        */
+       x_unsetenv("DEPENDENCIES_OUTPUT");
 
-       if (compile_preprocessed_source_code) {
-               args_add(args, i_tmpfile);
-       } else {
+       if (conf->run_second_cpp) {
                args_add(args, input_file);
+       } else {
+               args_add(args, i_tmpfile);
        }
 
        cc_log("Running real compiler");
-       status = execute(args->argv, tmp_stdout, tmp_stderr);
+       status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd);
        args_pop(args, 3);
 
        if (stat(tmp_stdout, &st) != 0) {
@@ -643,7 +777,6 @@ to_cache(struct args *args)
                stats_update(STATS_MISSING);
                tmp_unlink(tmp_stdout);
                tmp_unlink(tmp_stderr);
-               tmp_unlink(tmp_obj);
                failed();
        }
        if (st.st_size != 0) {
@@ -651,7 +784,9 @@ to_cache(struct args *args)
                stats_update(STATS_STDOUT);
                tmp_unlink(tmp_stdout);
                tmp_unlink(tmp_stderr);
-               tmp_unlink(tmp_obj);
+               if (tmp_dia) {
+                       tmp_unlink(tmp_dia);
+               }
                failed();
        }
        tmp_unlink(tmp_stdout);
@@ -666,7 +801,7 @@ to_cache(struct args *args)
                int fd_result;
                char *tmp_stderr2;
 
-               tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
+               tmp_stderr2 = format("%s.2", tmp_stderr);
                if (x_rename(tmp_stderr, tmp_stderr2)) {
                        cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
                               strerror(errno));
@@ -703,24 +838,47 @@ to_cache(struct args *args)
 
                fd = open(tmp_stderr, O_RDONLY | O_BINARY);
                if (fd != -1) {
-                       if (str_eq(output_obj, "/dev/null")
-                           || (access(tmp_obj, R_OK) == 0
-                               && move_file(tmp_obj, output_obj, 0) == 0)
-                           || errno == ENOENT) {
+                       if (str_eq(output_obj, "/dev/null") || errno == ENOENT) {
                                /* we can use a quick method of getting the failed output */
                                copy_fd(fd, 2);
                                close(fd);
                                tmp_unlink(tmp_stderr);
+
+                               if (output_dia) {
+                                       int ret;
+                                       x_unlink(output_dia);
+                                       /* only make a hardlink if the cache file is uncompressed */
+                                       ret = move_file(tmp_dia, output_dia, 0);
+
+                                       if (ret == -1) {
+                                               if (errno == ENOENT) {
+                                                       /* Someone removed the file just before we began copying? */
+                                                       cc_log("Diagnostic file %s just disappeared", output_dia);
+                                                       stats_update(STATS_MISSING);
+                                               } else {
+                                                       cc_log("Failed to move %s to %s: %s",
+                                                              tmp_dia, output_dia, strerror(errno));
+                                                       stats_update(STATS_ERROR);
+                                                       failed();
+                                               }
+                                               x_unlink(tmp_dia);
+                                       } else {
+                                               cc_log("Created %s from %s", output_dia, tmp_dia);
+                                       }
+                               }
+
                                exit(status);
                        }
                }
 
                tmp_unlink(tmp_stderr);
-               tmp_unlink(tmp_obj);
+               if (tmp_dia) {
+                       tmp_unlink(tmp_dia);
+               }
                failed();
        }
 
-       if (stat(tmp_obj, &st) != 0) {
+       if (stat(output_obj, &st) != 0) {
                cc_log("Compiler didn't produce an object file");
                stats_update(STATS_NOOUTPUT);
                failed();
@@ -737,48 +895,55 @@ to_cache(struct args *args)
                failed();
        }
        if (st.st_size > 0) {
-               if (move_uncompressed_file(tmp_stderr, cached_stderr,
-                                          enable_compression) != 0) {
+               if (move_uncompressed_file(
+                           tmp_stderr, cached_stderr,
+                           conf->compression ? conf->compression_level : 0) != 0) {
                        cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
                               strerror(errno));
                        stats_update(STATS_ERROR);
                        failed();
                }
                cc_log("Stored in cache: %s", cached_stderr);
-               if (enable_compression) {
+               if (conf->compression) {
                        stat(cached_stderr, &st);
                }
-               added_bytes += file_size(&st);
-               added_files += 1;
+               stats_update_size(file_size(&st), 1);
        } else {
                tmp_unlink(tmp_stderr);
+               if (conf->recache) {
+                       /* If recaching, we need to remove any previous .stderr. */
+                       x_unlink(cached_stderr);
+               }
        }
-       if (move_uncompressed_file(tmp_obj, cached_obj, enable_compression) != 0) {
-               cc_log("Failed to move %s to %s: %s", tmp_obj, cached_obj, strerror(errno));
-               stats_update(STATS_ERROR);
-               failed();
-       } else {
-               cc_log("Stored in cache: %s", cached_obj);
-               stat(cached_obj, &st);
-               added_bytes += file_size(&st);
-               added_files += 1;
+
+       if (tmp_dia) {
+               if (stat(tmp_dia, &st) != 0) {
+                       cc_log("Failed to stat %s: %s", tmp_dia, strerror(errno));
+                       stats_update(STATS_ERROR);
+                       failed();
+               }
+               if (st.st_size > 0) {
+                       put_file_in_cache(tmp_dia, cached_dia);
+               }
        }
 
-       /*
-        * Do an extra stat on the potentially compressed object file for the
-        * size statistics.
+       put_file_in_cache(output_obj, cached_obj);
+       stats_update(STATS_TOCACHE);
+
+       /* Make sure we have a CACHEDIR.TAG
+        * This can be almost anywhere, but might as well do it near the end
+        * as if we exit early we save the stat call
         */
-       if (stat(cached_obj, &st) != 0) {
-               cc_log("Failed to stat %s: %s", cached_obj, strerror(errno));
+       if (create_cachedirtag(conf->cache_dir) != 0) {
+               cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
+                      conf->cache_dir, strerror(errno));
                stats_update(STATS_ERROR);
                failed();
        }
 
-       stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files);
-
-       free(tmp_obj);
        free(tmp_stderr);
        free(tmp_stdout);
+       free(tmp_dia);
 }
 
 /*
@@ -791,7 +956,7 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
        char *input_base;
        char *tmp;
        char *path_stdout, *path_stderr;
-       int status;
+       int status, path_stderr_fd;
        struct file_hash *result;
 
        /* ~/hello.c -> tmp.hello.123.i
@@ -800,39 +965,36 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
           maximum filename length limits */
        input_base = basename(input_file);
        tmp = strchr(input_base, '.');
-       if (tmp != NULL) {
+       if (tmp) {
                *tmp = 0;
        }
        if (strlen(input_base) > 10) {
                input_base[10] = 0;
        }
 
-       path_stderr = format("%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
+       path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
+       path_stderr_fd = create_tmp_fd(&path_stderr);
        add_pending_tmp_file(path_stderr);
 
        time_of_compilation = time(NULL);
 
-       if (!direct_i_file) {
-               path_stdout = format("%s/%s.tmp.%s.%s",
-                                    temp_dir, input_base, tmp_string(), i_extension);
+       if (direct_i_file) {
+               /* We are compiling a .i or .ii file - that means we can skip the cpp stage
+                * and directly form the correct i_tmpfile. */
+               path_stdout = input_file;
+               status = 0;
+       } else {
+               /* Run cpp on the input file to obtain the .i. */
+               int path_stdout_fd;
+               path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
+               path_stdout_fd = create_tmp_fd(&path_stdout);
                add_pending_tmp_file(path_stdout);
 
-               /* run cpp on the input file to obtain the .i */
                args_add(args, "-E");
                args_add(args, input_file);
-               status = execute(args->argv, path_stdout, path_stderr);
+               cc_log("Running preprocessor");
+               status = execute(args->argv, path_stdout_fd, path_stderr_fd);
                args_pop(args, 2);
-       } else {
-               /* we are compiling a .i or .ii file - that means we
-                  can skip the cpp stage and directly form the
-                  correct i_tmpfile */
-               path_stdout = input_file;
-               if (create_empty_file(path_stderr) != 0) {
-                       cc_log("Failed to create %s: %s", path_stderr, strerror(errno));
-                       stats_update(STATS_ERROR);
-                       failed();
-               }
-               status = 0;
        }
 
        if (status != 0) {
@@ -841,11 +1003,9 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
                failed();
        }
 
-       if (enable_unify) {
-               /*
-                * When we are doing the unifying tricks we need to include the
-                * input file name in the hash to get the warnings right.
-                */
+       if (conf->unify) {
+               /* When we are doing the unifying tricks we need to include the input file
+                * name in the hash to get the warnings right. */
                hash_delimiter(hash, "unifyfilename");
                hash_string(hash, input_file);
 
@@ -868,19 +1028,25 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
                fatal("Failed to open %s: %s", path_stderr, strerror(errno));
        }
 
-       i_tmpfile = path_stdout;
+       if (direct_i_file) {
+               i_tmpfile = input_file;
+       } else {
+               /* i_tmpfile needs the proper cpp_extension for the compiler to do its
+                * thing correctly. */
+               i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
+               x_rename(path_stdout, i_tmpfile);
+               add_pending_tmp_file(i_tmpfile);
+       }
 
-       if (compile_preprocessed_source_code) {
+       if (conf->run_second_cpp) {
+               free(path_stderr);
+       } else {
                /*
                 * If we are using the CPP trick, we need to remember this
                 * stderr data and output it just before the main stderr from
                 * the compiler pass.
                 */
                cpp_stderr = path_stderr;
-       } else {
-               hash_delimiter(hash, "runsecondcpp");
-               hash_string(hash, "false");
-               free(path_stderr);
        }
 
        result = x_malloc(sizeof(*result));
@@ -893,13 +1059,13 @@ static void
 update_cached_result_globals(struct file_hash *hash)
 {
        char *object_name;
-
        object_name = format_hash_as_string(hash->hash, hash->size);
        cached_obj_hash = hash;
        cached_obj = get_path_in_cache(object_name, ".o");
        cached_stderr = get_path_in_cache(object_name, ".stderr");
        cached_dep = get_path_in_cache(object_name, ".d");
-       stats_file = format("%s/%c/stats", cache_dir, object_name[0]);
+       cached_dia = get_path_in_cache(object_name, ".dia");
+       stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
        free(object_name);
 }
 
@@ -911,28 +1077,46 @@ static void
 hash_compiler(struct mdfour *hash, struct stat *st, const char *path,
               bool allow_command)
 {
-       const char *compilercheck;
-
-       compilercheck = getenv("CCACHE_COMPILERCHECK");
-       if (!compilercheck) {
-               compilercheck = "mtime";
-       }
-       if (str_eq(compilercheck, "none")) {
+       if (str_eq(conf->compiler_check, "none")) {
                /* Do nothing. */
-       } else if (str_eq(compilercheck, "mtime")) {
+       } else if (str_eq(conf->compiler_check, "mtime")) {
                hash_delimiter(hash, "cc_mtime");
                hash_int(hash, st->st_size);
                hash_int(hash, st->st_mtime);
-       } else if (str_eq(compilercheck, "content") || !allow_command) {
+       } else if (str_eq(conf->compiler_check, "content") || !allow_command) {
                hash_delimiter(hash, "cc_content");
                hash_file(hash, path);
        } else { /* command string */
-               if (!hash_multicommand_output(hash, compilercheck, orig_args->argv[0])) {
-                       fatal("Failure running compiler check command: %s", compilercheck);
+               if (!hash_multicommand_output(
+                           hash, conf->compiler_check, orig_args->argv[0])) {
+                       fatal("Failure running compiler check command: %s", conf->compiler_check);
                }
        }
 }
 
+/*
+ * Note that these compiler checks are unreliable, so nothing should
+ * hard-depend on them.
+ */
+
+static bool
+compiler_is_clang(struct args *args)
+{
+       char* name = basename(args->argv[0]);
+       bool is = strstr(name, "clang");
+       free(name);
+       return is;
+}
+
+static bool
+compiler_is_gcc(struct args *args)
+{
+       char* name = basename(args->argv[0]);
+       bool is = strstr(name, "gcc") || strstr(name, "g++");
+       free(name);
+       return is;
+}
+
 /*
  * Update a hash sum with information common for the direct and preprocessor
  * modes.
@@ -950,7 +1134,7 @@ calculate_common_hash(struct args *args, struct mdfour *hash)
         * by the compiler as a .ii file.
         */
        hash_delimiter(hash, "ext");
-       hash_string(hash, i_extension);
+       hash_string(hash, conf->cpp_extension);
 
        if (stat(args->argv[0], &st) != 0) {
                cc_log("Couldn't stat compiler %s: %s", args->argv[0], strerror(errno));
@@ -973,7 +1157,7 @@ calculate_common_hash(struct args *args, struct mdfour *hash)
        free(p);
 
        /* Possibly hash the current working directory. */
-       if (getenv("CCACHE_HASHDIR")) {
+       if (conf->hash_dir) {
                char *cwd = gnu_getcwd();
                if (cwd) {
                        hash_delimiter(hash, "cwd");
@@ -982,10 +1166,9 @@ calculate_common_hash(struct args *args, struct mdfour *hash)
                }
        }
 
-       p = getenv("CCACHE_EXTRAFILES");
-       if (p) {
-               char *path, *q, *saveptr = NULL;
-               p = x_strdup(p);
+       if (!str_eq(conf->extra_files_to_hash, "")) {
+               char *path, *p, *q, *saveptr = NULL;
+               p = x_strdup(conf->extra_files_to_hash);
                q = p;
                while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
                        cc_log("Hashing extra file %s", path);
@@ -998,6 +1181,15 @@ calculate_common_hash(struct args *args, struct mdfour *hash)
                }
                free(p);
        }
+
+       /* Possibly hash GCC_COLORS (for color diagnostics). */
+       if (compiler_is_gcc(args)) {
+               const char *gcc_colors = getenv("GCC_COLORS");
+               if (gcc_colors) {
+                       hash_delimiter(hash, "gcccolors");
+                       hash_string(hash, gcc_colors);
+               }
+       }
 }
 
 /*
@@ -1015,6 +1207,11 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
        struct file_hash *object_hash = NULL;
        char *p;
 
+       if (direct_mode) {
+               hash_delimiter(hash, "manifest version");
+               hash_int(hash, MANIFEST_VERSION);
+       }
+
        /* first the arguments */
        for (i = 1; i < args->argc; i++) {
                /* -L doesn't affect compilation. */
@@ -1026,6 +1223,11 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                        continue;
                }
 
+               /* -Wl,... doesn't affect compilation. */
+               if (str_startswith(args->argv[i], "-Wl,")) {
+                       continue;
+               }
+
                /* The -fdebug-prefix-map option may be used in combination with
                   CCACHE_BASEDIR to reuse results across different directories. Skip it
                   from hashing. */
@@ -1048,6 +1250,36 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                        }
                }
 
+               /* If we're generating dependencies, we make sure to skip the
+                * filename of the dependency file, since it doesn't impact the
+                * output.
+                */
+               if (generating_dependencies) {
+                       if (str_startswith(args->argv[i], "-Wp,")) {
+                               if (str_startswith(args->argv[i], "-Wp,-MD,")
+                                               && !strchr(args->argv[i] + 8, ',')) {
+                                       hash_string_length(hash, args->argv[i], 8);
+                                       continue;
+                               } else if (str_startswith(args->argv[i], "-Wp,-MMD,")
+                                               && !strchr(args->argv[i] + 9, ',')) {
+                                       hash_string_length(hash, args->argv[i], 9);
+                                       continue;
+                               }
+                       } else if (str_startswith(args->argv[i], "-MF")) {
+                               bool separate_argument = (strlen(args->argv[i]) == 3);
+
+                               /* In either case, hash the "-MF" part. */
+                               hash_string_length(hash, args->argv[i], 3);
+
+                               if (separate_argument) {
+                                       /* Next argument is dependency name, so
+                                        * skip it. */
+                                       i++;
+                               }
+                               continue;
+                       }
+               }
+
                p = NULL;
                if (str_startswith(args->argv[i], "-specs=")) {
                        p = args->argv[i] + 7;
@@ -1084,6 +1316,50 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                hash_string(hash, args->argv[i]);
        }
 
+       /*
+        * For profile generation (-fprofile-arcs, -fprofile-generate):
+        * - hash profile directory
+        * - output to the real file first
+        *
+        * For profile usage (-fprofile-use):
+        * - hash profile data
+        *
+        * -fbranch-probabilities and -fvpt usage is covered by
+        * -fprofile-generate/-fprofile-use.
+        *
+        * The profile directory can be specified as an argument to
+        * -fprofile-generate=, -fprofile-use=, or -fprofile-dir=.
+        */
+
+       /*
+        * We need to output to the real object first here, otherwise runtime
+        * artifacts will be produced in the wrong place.
+        */
+       if (profile_generate) {
+               if (!profile_dir) {
+                       profile_dir = get_cwd();
+               }
+               cc_log("Adding profile directory %s to our hash", profile_dir);
+               hash_delimiter(hash, "-fprofile-dir");
+               hash_string(hash, profile_dir);
+       }
+       if (profile_use) {
+               /* Calculate gcda name */
+               char *gcda_name;
+               char *base_name;
+               base_name = remove_extension(output_obj);
+               if (!profile_dir) {
+                       profile_dir = get_cwd();
+               }
+               gcda_name = format("%s/%s.gcda", profile_dir, base_name);
+               cc_log("Adding profile data %s to our hash", gcda_name);
+               /* Add the gcda to our hash */
+               hash_delimiter(hash, "-fprofile-use");
+               hash_file(hash, gcda_name);
+               free(base_name);
+               free(gcda_name);
+       }
+
        if (direct_mode) {
                /* Hash environment variables that affect the preprocessor output. */
                const char **p;
@@ -1095,7 +1371,7 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                        "OBJCPLUS_INCLUDE_PATH", /* clang */
                        NULL
                };
-               for (p = envvars; *p != NULL ; ++p) {
+               for (p = envvars; *p; ++p) {
                        char *v = getenv(*p);
                        if (v) {
                                hash_delimiter(hash, *p);
@@ -1103,7 +1379,7 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                        }
                }
 
-               if (!(sloppiness & SLOPPY_FILE_MACRO)) {
+               if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
                        /*
                         * The source code file or an include file may contain
                         * __FILE__, so make sure that the hash is unique for
@@ -1114,20 +1390,20 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
                }
 
                hash_delimiter(hash, "sourcecode");
-               result = hash_source_code_file(hash, input_file);
+               result = hash_source_code_file(conf, hash, input_file);
                if (result & HASH_SOURCE_CODE_ERROR) {
                        failed();
                }
                if (result & HASH_SOURCE_CODE_FOUND_TIME) {
                        cc_log("Disabling direct mode");
-                       enable_direct = false;
+                       conf->direct_mode = false;
                        return NULL;
                }
                manifest_name = hash_result(hash);
                manifest_path = get_path_in_cache(manifest_name, ".manifest");
                free(manifest_name);
                cc_log("Looking for object file hash in %s", manifest_path);
-               object_hash = manifest_get(manifest_path);
+               object_hash = manifest_get(conf, manifest_path);
                if (object_hash) {
                        cc_log("Got object file hash from manifest");
                } else {
@@ -1152,12 +1428,11 @@ static void
 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
 {
        int fd_stderr;
-       int ret;
        struct stat st;
        bool produce_dep_file;
 
        /* the user might be disabling cache hits */
-       if (mode != FROMCACHE_COMPILED_MODE && getenv("CCACHE_RECACHE")) {
+       if (mode != FROMCACHE_COMPILED_MODE && conf->recache) {
                return;
        }
 
@@ -1167,6 +1442,12 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
                return;
        }
 
+       /* Check if the diagnostic file is there. */
+       if (output_dia && stat(cached_dia, &st) != 0) {
+               cc_log("Diagnostic file %s not in cache", cached_dia);
+               return;
+       }
+
        /*
         * Occasionally, e.g. on hard reset, our cache ends up as just filesystem
         * meta-data with no content catch an easy case of this.
@@ -1189,69 +1470,14 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
                return;
        }
 
-       if (str_eq(output_obj, "/dev/null")) {
-               ret = 0;
-       } else {
-               x_unlink(output_obj);
-               /* only make a hardlink if the cache file is uncompressed */
-               if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_obj)) {
-                       ret = link(cached_obj, output_obj);
-               } else {
-                       ret = copy_file(cached_obj, output_obj, 0);
-               }
+       if (!str_eq(output_obj, "/dev/null")) {
+               get_file_from_cache(cached_obj, output_obj);
        }
-
-       if (ret == -1) {
-               if (errno == ENOENT) {
-                       /* Someone removed the file just before we began copying? */
-                       cc_log("Object file %s just disappeared from cache", cached_obj);
-                       stats_update(STATS_MISSING);
-               } else {
-                       cc_log("Failed to copy/link %s to %s: %s",
-                              cached_obj, output_obj, strerror(errno));
-                       stats_update(STATS_ERROR);
-                       failed();
-               }
-               x_unlink(output_obj);
-               x_unlink(cached_stderr);
-               x_unlink(cached_obj);
-               x_unlink(cached_dep);
-               return;
-       } else {
-               cc_log("Created %s from %s", output_obj, cached_obj);
-       }
-
        if (produce_dep_file) {
-               x_unlink(output_dep);
-               /* only make a hardlink if the cache file is uncompressed */
-               if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_dep)) {
-                       ret = link(cached_dep, output_dep);
-               } else {
-                       ret = copy_file(cached_dep, output_dep, 0);
-               }
-               if (ret == -1) {
-                       if (errno == ENOENT) {
-                               /*
-                                * Someone removed the file just before we
-                                * began copying?
-                                */
-                               cc_log("Dependency file %s just disappeared from cache", output_obj);
-                               stats_update(STATS_MISSING);
-                       } else {
-                               cc_log("Failed to copy/link %s to %s: %s",
-                                      cached_dep, output_dep, strerror(errno));
-                               stats_update(STATS_ERROR);
-                               failed();
-                       }
-                       x_unlink(output_obj);
-                       x_unlink(output_dep);
-                       x_unlink(cached_stderr);
-                       x_unlink(cached_obj);
-                       x_unlink(cached_dep);
-                       return;
-               } else {
-                       cc_log("Created %s from %s", output_dep, cached_dep);
-               }
+               get_file_from_cache(cached_dep, output_dep);
+       }
+       if (output_dia) {
+               get_file_from_cache(cached_dia, output_dia);
        }
 
        /* Update modification timestamps to save files from LRU cleanup.
@@ -1261,19 +1487,12 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
        if (produce_dep_file) {
                update_mtime(cached_dep);
        }
+       if (output_dia) {
+               update_mtime(cached_dia);
+       }
 
        if (generating_dependencies && mode != FROMCACHE_DIRECT_MODE) {
-               /* Store the dependency file in the cache. */
-               ret = copy_file(output_dep, cached_dep, enable_compression);
-               if (ret == -1) {
-                       cc_log("Failed to copy %s to %s: %s", output_dep, cached_dep,
-                              strerror(errno));
-                       /* Continue despite the error. */
-               } else {
-                       cc_log("Stored in cache: %s", cached_dep);
-                       stat(cached_dep, &st);
-                       stats_update_size(STATS_NONE, file_size(&st) / 1024, 1);
-               }
+               put_file_in_cache(output_dep, cached_dep);
        }
 
        /* Send the stderr, if any. */
@@ -1284,10 +1503,11 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
        }
 
        /* Create or update the manifest file. */
-       if (enable_direct
+       if (conf->direct_mode
            && put_object_in_manifest
            && included_files
-           && !getenv("CCACHE_READONLY")) {
+           && !conf->read_only
+           && !conf->read_only_direct) {
                struct stat st;
                size_t old_size = 0; /* in bytes */
                if (stat(manifest_path, &st) == 0) {
@@ -1297,9 +1517,7 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
                        cc_log("Added object file hash to %s", manifest_path);
                        update_mtime(manifest_path);
                        stat(manifest_path, &st);
-                       stats_update_size(STATS_NONE,
-                                         (file_size(&st) - old_size) / 1024,
-                                         old_size == 0 ? 1 : 0);
+                       stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
                } else {
                        cc_log("Failed to add object file hash to %s", manifest_path);
                }
@@ -1329,30 +1547,27 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
 /* find the real compiler. We just search the PATH to find a executable of the
    same name that isn't a link to ourselves */
 static void
-find_compiler(int argc, char **argv)
+find_compiler(char **argv)
 {
        char *base;
-       char *path;
        char *compiler;
 
-       orig_args = args_init(argc, argv);
-
        base = basename(argv[0]);
 
        /* we might be being invoked like "ccache gcc -c foo.c" */
        if (same_executable_name(base, MYNAME)) {
                args_remove_first(orig_args);
                free(base);
-               if (is_full_path(argv[1])) {
+               if (is_full_path(orig_args->argv[0])) {
                        /* a full path was given */
                        return;
                }
-               base = basename(argv[1]);
+               base = basename(orig_args->argv[0]);
        }
 
        /* support user override of the compiler */
-       if ((path = getenv("CCACHE_CC"))) {
-               base = x_strdup(path);
+       if (!str_eq(conf->compiler, "")) {
+               base = conf->compiler;
        }
 
        compiler = find_executable(base, MYNAME);
@@ -1372,7 +1587,16 @@ find_compiler(int argc, char **argv)
 bool
 is_precompiled_header(const char *path)
 {
-       return str_eq(get_extension(path), ".gch");
+       return str_eq(get_extension(path), ".gch")
+              || str_eq(get_extension(path), ".pch")
+              || str_eq(get_extension(path), ".pth");
+}
+
+static bool
+color_output_possible(void)
+{
+       const char *term_env = getenv("TERM");
+       return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
 }
 
 /*
@@ -1381,7 +1605,7 @@ is_precompiled_header(const char *path)
  * -E; this is added later. Returns true on success, otherwise false.
  */
 bool
-cc_process_args(struct args *orig_args, struct args **preprocessor_args,
+cc_process_args(struct args *args, struct args **preprocessor_args,
                 struct args **compiler_args)
 {
        int i;
@@ -1399,13 +1623,19 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
        bool dependency_filename_specified = false;
        /* is the dependency makefile target name specified with -MT or -MQ? */
        bool dependency_target_specified = false;
-       struct args *stripped_args = NULL, *dep_args = NULL;
-       int argc = orig_args->argc;
-       char **argv = orig_args->argv;
+       struct args *expanded_args, *stripped_args, *dep_args, *cpp_args;
+       int argc;
+       char **argv;
        bool result = true;
+       bool found_color_diagnostics = false;
 
+       expanded_args = args_copy(args);
        stripped_args = args_init(0, NULL);
        dep_args = args_init(0, NULL);
+       cpp_args = args_init(0, NULL);
+
+       argc = expanded_args->argc;
+       argv = expanded_args->argv;
 
        args_add(stripped_args, argv[0]);
 
@@ -1429,9 +1659,31 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        goto out;
                }
 
+               /* Handle "@file" argument. */
+               if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
+                       char *argpath = argv[i] + 1;
+                       struct args *file_args;
+
+                       if (argpath[-1] == '-') {
+                               ++argpath;
+                       }
+                       file_args = args_init_from_gcc_atfile(argpath);
+                       if (!file_args) {
+                               cc_log("Couldn't read arg file %s", argpath);
+                               stats_update(STATS_ARGS);
+                               result = false;
+                               goto out;
+                       }
+
+                       args_insert(expanded_args, i, file_args, true);
+                       argc = expanded_args->argc;
+                       argv = expanded_args->argv;
+                       i--;
+                       continue;
+               }
+
                /* These are always too hard. */
                if (compopt_too_hard(argv[i])
-                   || str_startswith(argv[i], "@")
                    || str_startswith(argv[i], "-fdump-")) {
                        cc_log("Compiler option %s is unsupported", argv[i]);
                        stats_update(STATS_UNSUPPORTED);
@@ -1440,10 +1692,10 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                }
 
                /* These are too hard in direct mode. */
-               if (enable_direct) {
+               if (conf->direct_mode) {
                        if (compopt_too_hard_for_direct_mode(argv[i])) {
                                cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
-                               enable_direct = false;
+                               conf->direct_mode = false;
                        }
                }
 
@@ -1459,13 +1711,14 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        }
                }
 
-               if (str_eq(argv[i], "-fpch-preprocess")) {
+               if (str_eq(argv[i], "-fpch-preprocess")
+                   || str_eq(argv[i], "-emit-pch")
+                   || str_eq(argv[i], "-emit-pth")) {
                        found_fpch_preprocess = true;
                }
 
                /* we must have -c */
                if (str_eq(argv[i], "-c")) {
-                       args_add(stripped_args, argv[i]);
                        found_c_opt = true;
                        continue;
                }
@@ -1525,9 +1778,9 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                */
                if (str_startswith(argv[i], "-g")) {
                        args_add(stripped_args, argv[i]);
-                       if (enable_unify && !str_eq(argv[i], "-g0")) {
+                       if (conf->unify && !str_eq(argv[i], "-g0")) {
                                cc_log("%s used; disabling unify mode", argv[i]);
-                               enable_unify = false;
+                               conf->unify = false;
                        }
                        if (str_eq(argv[i], "-g3")) {
                                /*
@@ -1535,7 +1788,7 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                                 * have non-zero lineno when using -g3").
                                 */
                                cc_log("%s used; not compiling preprocessed code", argv[i]);
-                               compile_preprocessed_source_code = false;
+                               conf->run_second_cpp = true;
                        }
                        continue;
                }
@@ -1617,7 +1870,15 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        continue;
                }
                if (str_startswith(argv[i], "-Wp,")) {
-                       if (str_startswith(argv[i], "-Wp,-MD,") && !strchr(argv[i] + 8, ',')) {
+                       if (str_eq(argv[i], "-Wp,-P") || strstr(argv[i], ",-P,")) {
+                               /* -P removes preprocessor information in such a way that the object
+                                * file from compiling the preprocessed file will not be equal to the
+                                * object file produced when compiling without ccache. */
+                               cc_log("Too hard option -Wp,-P detected");
+                               stats_update(STATS_UNSUPPORTED);
+                               failed();
+                       } else if (str_startswith(argv[i], "-Wp,-MD,")
+                                  && !strchr(argv[i] + 8, ',')) {
                                generating_dependencies = true;
                                dependency_filename_specified = true;
                                free(output_dep);
@@ -1632,14 +1893,14 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                                output_dep = make_relative_path(x_strdup(argv[i] + 9));
                                args_add(dep_args, argv[i]);
                                continue;
-                       } else if (enable_direct) {
+                       } else if (conf->direct_mode) {
                                /*
                                 * -Wp, can be used to pass too hard options to
                                 * the preprocessor. Hence, disable direct
                                 * mode.
                                 */
                                cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
-                               enable_direct = false;
+                               conf->direct_mode = false;
                        }
                }
                if (str_eq(argv[i], "-MP")) {
@@ -1653,15 +1914,104 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        continue;
                }
 
+               if (str_eq(argv[i], "--serialize-diagnostics")) {
+                       if (i >= argc - 1) {
+                               cc_log("Missing argument to %s", argv[i]);
+                               stats_update(STATS_ARGS);
+                               result = false;
+                               goto out;
+                       }
+                       output_dia = make_relative_path(x_strdup(argv[i+1]));
+                       i++;
+                       continue;
+               }
+
+               if (str_startswith(argv[i], "-fprofile-")) {
+                       const char *arg_profile_dir = strchr(argv[i], '=');
+                       char *arg = x_strdup(argv[i]);
+                       bool supported_profile_option = false;
+
+                       if (arg_profile_dir) {
+                               char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
+                               char *dir;
+
+                               /* Convert to absolute path. */
+                               dir = x_realpath(arg_profile_dir + 1);
+                               if (!dir) {
+                                       /* Directory doesn't exist. */
+                                       dir = x_strdup(arg_profile_dir + 1);
+                               }
+
+                               /* We can get a better hit rate by using the real path here. */
+                               free(arg);
+                               arg = format("%s=%s", option, dir);
+                               cc_log("Rewriting %s to %s", argv[i], arg);
+                               free(option);
+                               free(dir);
+                       }
+
+                       if (str_startswith(argv[i], "-fprofile-generate")
+                           || str_eq(argv[i], "-fprofile-arcs")) {
+                               profile_generate = true;
+                               supported_profile_option = true;
+                       } else if (str_startswith(argv[i], "-fprofile-use")
+                                  || str_eq(argv[i], "-fbranch-probabilities")) {
+                               profile_use = true;
+                               supported_profile_option = true;
+                       } else if (str_eq(argv[i], "-fprofile-dir")) {
+                               supported_profile_option = true;
+                       }
+
+                       if (supported_profile_option) {
+                               args_add(stripped_args, arg);
+                               free(arg);
+
+                               /*
+                                * If the profile directory has already been set, give up... Hard to
+                                * know what the user means, and what the compiler will do.
+                                */
+                               if (arg_profile_dir && profile_dir) {
+                                       cc_log("Profile directory already set; giving up");
+                                       result = false;
+                                       goto out;
+                               } else if (arg_profile_dir) {
+                                       cc_log("Setting profile directory to %s", profile_dir);
+                                       profile_dir = x_strdup(arg_profile_dir);
+                               }
+                               continue;
+                       }
+                       cc_log("Unknown profile option: %s", argv[i]);
+                       free(arg);
+               }
+
+               if (str_eq(argv[i], "-fcolor-diagnostics")
+                   || str_eq(argv[i], "-fno-color-diagnostics")
+                   || str_eq(argv[i], "-fdiagnostics-color")
+                   || str_eq(argv[i], "-fdiagnostics-color=always")
+                   || str_eq(argv[i], "-fno-diagnostics-color")
+                   || str_eq(argv[i], "-fdiagnostics-color=never")) {
+                       args_add(stripped_args, argv[i]);
+                       found_color_diagnostics = true;
+                       continue;
+               }
+               if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
+                       if (color_output_possible()) {
+                               /* Output is redirected, so color output must be forced. */
+                               args_add(stripped_args, "-fdiagnostics-color=always");
+                               cc_log("Automatically forcing colors");
+                       }
+                       found_color_diagnostics = true;
+                       continue;
+               }
+
                /*
-                * Options taking an argument that that we may want to rewrite
-                * to relative paths to get better hit rate. A secondary effect
-                * is that paths in the standard error output produced by the
-                * compiler will be normalized.
+                * Options taking an argument that we may want to rewrite to relative paths
+                * to get better hit rate. A secondary effect is that paths in the standard
+                * error output produced by the compiler will be normalized.
                 */
                if (compopt_takes_path(argv[i])) {
                        char *relpath;
-                       char *pchpath;
+                       char *pch_file = NULL;
                        if (i == argc-1) {
                                cc_log("Missing argument to %s", argv[i]);
                                stats_update(STATS_ARGS);
@@ -1669,18 +2019,61 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                                goto out;
                        }
 
-                       args_add(stripped_args, argv[i]);
                        relpath = make_relative_path(x_strdup(argv[i+1]));
-                       args_add(stripped_args, relpath);
+                       if (compopt_affects_cpp(argv[i])) {
+                               args_add(cpp_args, argv[i]);
+                               args_add(cpp_args, relpath);
+                       } else {
+                               args_add(stripped_args, argv[i]);
+                               args_add(stripped_args, relpath);
+                       }
 
                        /* Try to be smart about detecting precompiled headers */
-                       pchpath = format("%s.gch", argv[i+1]);
-                       if (stat(pchpath, &st) == 0) {
-                               cc_log("Detected use of precompiled header: %s", pchpath);
-                               found_pch = true;
+                       if (str_eq(argv[i], "-include-pch")
+                           || str_eq(argv[i], "-include-pth")) {
+                               if (stat(argv[i+1], &st) == 0) {
+                                       cc_log("Detected use of precompiled header: %s", argv[i+1]);
+                                       found_pch = true;
+                                       pch_file = x_strdup(argv[i+1]);
+                               }
+                       } else {
+                               char *gchpath = format("%s.gch", argv[i+1]);
+                               if (stat(gchpath, &st) == 0) {
+                                       cc_log("Detected use of precompiled header: %s", gchpath);
+                                       found_pch = true;
+                                       pch_file = x_strdup(gchpath);
+                               } else {
+                                       char *pchpath = format("%s.pch", argv[i+1]);
+                                       if (stat(pchpath, &st) == 0) {
+                                               cc_log("Detected use of precompiled header: %s", pchpath);
+                                               found_pch = true;
+                                               pch_file = x_strdup(pchpath);
+                                       } else {
+                                               /* clang may use pretokenized headers */
+                                               char *pthpath = format("%s.pth", argv[i+1]);
+                                               if (stat(pthpath, &st) == 0) {
+                                                       cc_log("Detected use of pretokenized header: %s", pthpath);
+                                                       found_pch = true;
+                                                       pch_file = x_strdup(pthpath);
+                                               }
+                                               free(pthpath);
+                                       }
+                                       free(pchpath);
+                               }
+                               free(gchpath);
+                       }
+
+                       if (pch_file) {
+                               if (included_pch_file) {
+                                       cc_log("Multiple precompiled headers used: %s and %s\n",
+                                              included_pch_file, pch_file);
+                                       stats_update(STATS_ARGS);
+                                       result = false;
+                                       goto out;
+                               }
+                               included_pch_file = pch_file;
                        }
 
-                       free(pchpath);
                        free(relpath);
                        i++;
                        continue;
@@ -1692,7 +2085,13 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        char *option;
                        relpath = make_relative_path(x_strdup(argv[i] + 2));
                        option = format("-%c%s", argv[i][1], relpath);
-                       args_add(stripped_args, option);
+
+                       if (compopt_short(compopt_affects_cpp, argv[i])) {
+                               args_add(cpp_args, option);
+                       } else {
+                               args_add(stripped_args, option);
+                       }
+
                        free(relpath);
                        free(option);
                        continue;
@@ -1706,15 +2105,27 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                                result = false;
                                goto out;
                        }
-                       args_add(stripped_args, argv[i]);
-                       args_add(stripped_args, argv[i+1]);
+
+                       if (compopt_affects_cpp(argv[i])) {
+                               args_add(cpp_args, argv[i]);
+                               args_add(cpp_args, argv[i+1]);
+                       } else {
+                               args_add(stripped_args, argv[i]);
+                               args_add(stripped_args, argv[i+1]);
+                       }
+
                        i++;
                        continue;
                }
 
                /* other options */
                if (argv[i][0] == '-') {
-                       args_add(stripped_args, argv[i]);
+                       if (compopt_affects_cpp(argv[i])
+                           || compopt_prefix_affects_cpp(argv[i])) {
+                               args_add(cpp_args, argv[i]);
+                       } else {
+                               args_add(stripped_args, argv[i]);
+                       }
                        continue;
                }
 
@@ -1747,17 +2158,8 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        goto out;
                }
 
-               lstat(argv[i], &st);
-               if (S_ISLNK(st.st_mode)) {
-                       /* Don't rewrite source file path if it's a symlink since
-                          make_relative_path resolves symlinks using realpath(3) and this leads
-                          to potentially choosing incorrect relative header files. See the
-                          "symlink to source file" test. */
-                       input_file = x_strdup(argv[i]);
-               } else {
-                       /* Rewrite to relative to increase hit rate. */
-                       input_file = make_relative_path(x_strdup(argv[i]));
-               }
+               /* Rewrite to relative to increase hit rate. */
+               input_file = make_relative_path(x_strdup(argv[i]));
        }
 
        if (!input_file) {
@@ -1769,9 +2171,9 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
 
        if (found_pch || found_fpch_preprocess) {
                using_precompiled_header = true;
-               if (!(sloppiness & SLOPPY_TIME_MACROS)) {
-                       cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
-                              " using precompiled headers to get direct hits");
+               if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
+                       cc_log("You have to specify \"time_macros\" sloppiness when using"
+                              " precompiled headers to get direct hits");
                        cc_log("Disabling direct mode");
                        stats_update(STATS_CANTUSEPCH);
                        result = false;
@@ -1796,9 +2198,9 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
        }
 
        output_is_precompiled_header =
-               actual_language && strstr(actual_language, "-header") != NULL;
+               actual_language && strstr(actual_language, "-header");
 
-       if (output_is_precompiled_header && !(sloppiness & SLOPPY_PCH_DEFINES)) {
+       if (output_is_precompiled_header && !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
                cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
                       " creating precompiled headers");
                stats_update(STATS_CANTUSEPCH);
@@ -1835,13 +2237,13 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
        if (output_is_precompiled_header) {
                /* It doesn't work to create the .gch from preprocessed source. */
                cc_log("Creating precompiled header; not compiling preprocessed code");
-               compile_preprocessed_source_code = false;
+               conf->run_second_cpp = true;
        }
 
-       i_extension = getenv("CCACHE_EXTENSION");
-       if (!i_extension) {
+       if (str_eq(conf->cpp_extension, "")) {
                const char *p_language = p_language_for_language(actual_language);
-               i_extension = extension_for_language(p_language) + 1;
+               free(conf->cpp_extension);
+               conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
        }
 
        /* don't try to second guess the compilers heuristics for stdout handling */
@@ -1857,10 +2259,7 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        output_obj = format("%s.gch", input_file);
                } else {
                        char *p;
-                       output_obj = x_strdup(input_file);
-                       if ((p = strrchr(output_obj, '/'))) {
-                               output_obj = p+1;
-                       }
+                       output_obj = basename(input_file);
                        p = strrchr(output_obj, '.');
                        if (!p || !p[1]) {
                                cc_log("Badly formed object filename");
@@ -1890,16 +2289,37 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
         * -finput-charset=XXX (otherwise conversion happens twice)
         * -x XXX (otherwise the wrong language is selected)
         */
-       *preprocessor_args = args_copy(stripped_args);
        if (input_charset) {
-               args_add(*preprocessor_args, input_charset);
+               args_add(cpp_args, input_charset);
        }
        if (found_pch) {
-               args_add(*preprocessor_args, "-fpch-preprocess");
+               args_add(cpp_args, "-fpch-preprocess");
        }
        if (explicit_language) {
-               args_add(*preprocessor_args, "-x");
-               args_add(*preprocessor_args, explicit_language);
+               args_add(cpp_args, "-x");
+               args_add(cpp_args, explicit_language);
+       }
+
+       /*
+        * Since output is redirected, compilers will not color their output by
+        * default, so force it explicitly if it would be otherwise done.
+        */
+       if (!found_color_diagnostics && color_output_possible()) {
+               if (compiler_is_clang(args)) {
+                       args_add(stripped_args, "-fcolor-diagnostics");
+                       cc_log("Automatically enabling colors");
+               } else if (compiler_is_gcc(args)) {
+                       /*
+                        * GCC has it since 4.9, but that'd require detecting what GCC version is
+                        * used for the actual compile. However it requires also GCC_COLORS to be
+                        * set (and not empty), so use that for detecting if GCC would use
+                        * colors.
+                        */
+                       if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
+                               args_add(stripped_args, "-fdiagnostics-color");
+                               cc_log("Automatically enabling colors");
+                       }
+               }
        }
 
        /*
@@ -1924,8 +2344,10 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                }
        }
 
-       if (compile_preprocessed_source_code) {
-               *compiler_args = args_copy(stripped_args);
+       *compiler_args = args_copy(stripped_args);
+       if (conf->run_second_cpp) {
+               args_extend(*compiler_args, cpp_args);
+       } else {
                if (explicit_language) {
                        /*
                         * Workaround for a bug in Apple's patched distcc -- it doesn't properly
@@ -1935,8 +2357,10 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        args_add(*compiler_args, "-x");
                        args_add(*compiler_args, p_language_for_language(explicit_language));
                }
-       } else {
-               *compiler_args = args_copy(*preprocessor_args);
+       }
+
+       if (found_c_opt) {
+               args_add(*compiler_args, "-c");
        }
 
        /*
@@ -1944,82 +2368,167 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
         * compiler doesn't produce a correct .d file when compiling preprocessed
         * source.
         */
-       args_extend(*preprocessor_args, dep_args);
+       args_extend(cpp_args, dep_args);
+
+       *preprocessor_args = args_copy(stripped_args);
+       args_extend(*preprocessor_args, cpp_args);
 
 out:
+       args_free(expanded_args);
        args_free(stripped_args);
        args_free(dep_args);
+       args_free(cpp_args);
        return result;
 }
 
+static void
+create_initial_config_file(struct conf *conf, const char *path)
+{
+       unsigned max_files;
+       uint64_t max_size;
+       char *stats_dir;
+       FILE *f;
+       struct stat st;
+
+       if (create_parent_dirs(path) != 0) {
+               return;
+       }
+
+       stats_dir = format("%s/0", conf->cache_dir);
+       if (stat(stats_dir, &st) == 0) {
+               stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
+               /* STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory. */
+               max_files *= 16;
+               max_size *= 16;
+       } else {
+               max_files = 0;
+               max_size = conf->max_size;
+       }
+       free(stats_dir);
+
+       f = fopen(path, "w");
+       if (!f) {
+               return;
+       }
+       if (max_files != 0) {
+               fprintf(f, "max_files = %u\n", max_files);
+               conf->max_files = max_files;
+       }
+       if (max_size != 0) {
+               char *size = format_parsable_size_with_suffix(max_size);
+               fprintf(f, "max_size = %s\n", size);
+               free(size);
+               conf->max_size = max_size;
+       }
+       fclose(f);
+}
+
+/*
+ * Read config file(s), populate variables, create configuration file in cache
+ * directory if missing, etc.
+ */
+static void
+initialize(void)
+{
+       char *errmsg;
+       char *p;
+       struct stat st;
+       bool should_create_initial_config = false;
+
+       conf_free(conf);
+       conf = conf_create();
+
+       p = getenv("CCACHE_CONFIGPATH");
+       if (p) {
+               primary_config_path = x_strdup(p);
+       } else {
+               secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
+               if (!conf_read(conf, secondary_config_path, &errmsg)) {
+                       if (stat(secondary_config_path, &st) == 0) {
+                               fatal("%s", errmsg);
+                       }
+                       /* Missing config file in SYSCONFDIR is OK. */
+                       free(errmsg);
+               }
+
+               if (str_eq(conf->cache_dir, "")) {
+                       fatal("configuration setting \"cache_dir\" must not be the empty string");
+               }
+               if ((p = getenv("CCACHE_DIR"))) {
+                       free(conf->cache_dir);
+                       conf->cache_dir = strdup(p);
+               }
+               if (str_eq(conf->cache_dir, "")) {
+                       fatal("CCACHE_DIR must not be the empty string");
+               }
+
+               primary_config_path = format("%s/ccache.conf", conf->cache_dir);
+       }
+
+       if (!conf_read(conf, primary_config_path, &errmsg)) {
+               if (stat(primary_config_path, &st) == 0) {
+                       fatal("%s", errmsg);
+               }
+               should_create_initial_config = true;
+       }
+
+       if (!conf_update_from_environment(conf, &errmsg)) {
+               fatal("%s", errmsg);
+       }
+
+       if (conf->disable) {
+               should_create_initial_config = false;
+       }
+
+       if (should_create_initial_config) {
+               create_initial_config_file(conf, primary_config_path);
+       }
+
+       exitfn_init();
+       exitfn_add_nullary(stats_flush);
+       exitfn_add_nullary(clean_up_pending_tmp_files);
+
+       cc_log("=== CCACHE %s STARTED =========================================",
+              CCACHE_VERSION);
+
+       if (conf->umask != UINT_MAX) {
+               umask(conf->umask);
+       }
+}
+
 /* Reset the global state. Used by the test suite. */
 void
 cc_reset(void)
 {
+       conf_free(conf); conf = NULL;
+       free(primary_config_path); primary_config_path = NULL;
+       free(secondary_config_path); secondary_config_path = NULL;
        free(current_working_dir); current_working_dir = NULL;
-       free(cache_dir); cache_dir = NULL;
-       cache_logfile = NULL;
-       base_dir = NULL;
+       free(profile_dir); profile_dir = NULL;
+       free(included_pch_file); included_pch_file = NULL;
        args_free(orig_args); orig_args = NULL;
        free(input_file); input_file = NULL;
        free(output_obj); output_obj = NULL;
        free(output_dep); output_dep = NULL;
+       free(output_dia); output_dia = NULL;
        free(cached_obj_hash); cached_obj_hash = NULL;
        free(cached_obj); cached_obj = NULL;
        free(cached_stderr); cached_stderr = NULL;
        free(cached_dep); cached_dep = NULL;
+       free(cached_dia); cached_dia = NULL;
        free(manifest_path); manifest_path = NULL;
        time_of_compilation = 0;
-       sloppiness = false;
        if (included_files) {
                hashtable_destroy(included_files, 1); included_files = NULL;
        }
        generating_dependencies = false;
-       i_extension = NULL;
        i_tmpfile = NULL;
        direct_i_file = false;
        free(cpp_stderr); cpp_stderr = NULL;
        free(stats_file); stats_file = NULL;
-       enable_unify = false;
-       enable_direct = true;
-       enable_compression = false;
-       nlevels = 2;
-       compile_preprocessed_source_code = false;
        output_is_precompiled_header = false;
-}
-
-static unsigned
-parse_sloppiness(char *p)
-{
-       unsigned result = 0;
-       char *word, *q, *saveptr = NULL;
 
-       if (!p) {
-               return result;
-       }
-       p = x_strdup(p);
-       q = p;
-       while ((word = strtok_r(q, ", ", &saveptr))) {
-               if (str_eq(word, "file_macro")) {
-                       cc_log("Being sloppy about __FILE__");
-                       result |= SLOPPY_FILE_MACRO;
-               }
-               if (str_eq(word, "include_file_mtime")) {
-                       cc_log("Being sloppy about include file mtime");
-                       result |= SLOPPY_INCLUDE_FILE_MTIME;
-               }
-               if (str_eq(word, "time_macros")) {
-                       cc_log("Being sloppy about __DATE__ and __TIME__");
-                       result |= SLOPPY_TIME_MACROS;
-               }
-               if (str_eq(word, "pch_defines")) {
-                       cc_log("Being sloppy about PCH #defines");
-                       result |= SLOPPY_PCH_DEFINES;
-               }
-               q = NULL;
-       }
-       free(p);
-       return result;
+       conf = conf_create();
 }
 
 /* Make a copy of stderr that will not be cached, so things like
@@ -2045,14 +2554,20 @@ setup_uncached_err(void)
        }
 }
 
+static void
+configuration_logger(const char *descr, const char *origin, void *context)
+{
+       (void)context;
+       cc_bulklog("Config: (%s) %s", origin, descr);
+}
+
 /* the main ccache driver function */
 static void
-ccache(char *argv[])
+ccache(int argc, char *argv[])
 {
        bool put_object_in_manifest = false;
        struct file_hash *object_hash;
        struct file_hash *object_hash_from_manifest = NULL;
-       char *env;
        struct mdfour common_hash;
        struct mdfour direct_hash;
        struct mdfour cpp_hash;
@@ -2063,45 +2578,39 @@ ccache(char *argv[])
        /* Arguments to send to the real compiler. */
        struct args *compiler_args;
 
-       setup_uncached_err();
-
-       if (!getenv("CCACHE_READONLY")) {
-               if (create_cachedirtag(cache_dir) != 0) {
-                       cc_log("failed to create %s/CACHEDIR.TAG (%s)\n",
-                               cache_dir, strerror(errno));
-                       failed();
-               }
-       }
+       orig_args = args_init(argc, argv);
 
-       sloppiness = parse_sloppiness(getenv("CCACHE_SLOPPINESS"));
+       initialize();
+       find_compiler(argv);
 
-       cc_log_argv("Command line: ", argv);
-       cc_log("Hostname: %s", get_hostname());
-       cc_log("Working directory: %s", get_current_working_dir());
+#ifndef _WIN32
+       signal(SIGHUP, signal_handler);
+#endif
+       signal(SIGINT, signal_handler);
+       signal(SIGTERM, signal_handler);
 
-       if (base_dir) {
-               cc_log("Base directory: %s", base_dir);
+       if (str_eq(conf->temporary_dir, "")) {
+               clean_up_internal_tempdir();
        }
 
-       if (getenv("CCACHE_UNIFY")) {
-               cc_log("Unify mode enabled");
-               enable_unify = true;
+       if (!str_eq(conf->log_file, "")) {
+               conf_print_items(conf, configuration_logger, NULL);
        }
 
-       if (getenv("CCACHE_NODIRECT") || enable_unify) {
-               cc_log("Direct mode disabled");
-               enable_direct = false;
+       if (conf->disable) {
+               cc_log("ccache is disabled");
+               failed();
        }
 
-       if (getenv("CCACHE_COMPRESS")) {
-               cc_log("Compression enabled");
-               enable_compression = true;
-       }
+       setup_uncached_err();
 
-       if ((env = getenv("CCACHE_NLEVELS"))) {
-               nlevels = atoi(env);
-               if (nlevels < 1) nlevels = 1;
-               if (nlevels > 8) nlevels = 8;
+       cc_log_argv("Command line: ", argv);
+       cc_log("Hostname: %s", get_hostname());
+       cc_log("Working directory: %s", get_current_working_dir());
+
+       if (conf->unify) {
+               cc_log("Direct mode disabled because unify mode is enabled");
+               conf->direct_mode = false;
        }
 
        if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
@@ -2112,6 +2621,9 @@ ccache(char *argv[])
        if (generating_dependencies) {
                cc_log("Dependency file: %s", output_dep);
        }
+       if (output_dia) {
+               cc_log("Diagnostic file: %s", output_dia);
+       }
        cc_log("Object file: %s", output_obj);
 
        hash_start(&common_hash);
@@ -2119,7 +2631,7 @@ ccache(char *argv[])
 
        /* try to find the hash using the manifest */
        direct_hash = common_hash;
-       if (enable_direct) {
+       if (conf->direct_mode) {
                cc_log("Trying direct lookup");
                object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
                if (object_hash) {
@@ -2145,12 +2657,16 @@ ccache(char *argv[])
                }
        }
 
+       if (conf->read_only_direct) {
+               cc_log("Read-only direct mode; running real compiler");
+               failed();
+       }
+
        /*
         * Find the hash using the preprocessed output. Also updates
         * included_files.
         */
        cpp_hash = common_hash;
-       cc_log("Running preprocessor");
        object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
        if (!object_hash) {
                fatal("internal error: object hash from cpp returned NULL");
@@ -2185,20 +2701,12 @@ ccache(char *argv[])
        /* if we can return from cache at this point then do */
        from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
 
-       if (getenv("CCACHE_READONLY")) {
+       if (conf->read_only) {
                cc_log("Read-only mode; running real compiler");
                failed();
        }
 
-       env = getenv("CCACHE_PREFIX");
-       if (env) {
-               char *p = find_executable(env, MYNAME);
-               if (!p) {
-                       fatal("%s: %s", env, strerror(errno));
-               }
-               cc_log("Using command-line prefix %s", env);
-               args_add_prefix(compiler_args, p);
-       }
+       add_prefix(compiler_args);
 
        /* run real compiler, sending output to cache */
        to_cache(compiler_args);
@@ -2213,11 +2721,10 @@ ccache(char *argv[])
 }
 
 static void
-check_cache_dir(void)
+configuration_printer(const char *descr, const char *origin, void *context)
 {
-       if (!cache_dir) {
-               fatal("Unable to determine cache directory");
-       }
+       assert(context);
+       fprintf(context, "(%s) %s\n", origin, descr);
 }
 
 /* the main program when not doing a compile */
@@ -2225,85 +2732,125 @@ static int
 ccache_main_options(int argc, char *argv[])
 {
        int c;
-       size_t v;
+       char *errmsg;
 
+       enum longopts {
+               DUMP_MANIFEST
+       };
        static const struct option options[] = {
-               {"show-stats", no_argument,       0, 's'},
-               {"zero-stats", no_argument,       0, 'z'},
-               {"cleanup",    no_argument,       0, 'c'},
-               {"clear",      no_argument,       0, 'C'},
-               {"max-files",  required_argument, 0, 'F'},
-               {"max-size",   required_argument, 0, 'M'},
-               {"help",       no_argument,       0, 'h'},
-               {"version",    no_argument,       0, 'V'},
+               {"cleanup",       no_argument,       0, 'c'},
+               {"clear",         no_argument,       0, 'C'},
+               {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
+               {"help",          no_argument,       0, 'h'},
+               {"max-files",     required_argument, 0, 'F'},
+               {"max-size",      required_argument, 0, 'M'},
+               {"set-config",    required_argument, 0, 'o'},
+               {"print-config",  no_argument,       0, 'p'},
+               {"show-stats",    no_argument,       0, 's'},
+               {"version",       no_argument,       0, 'V'},
+               {"zero-stats",    no_argument,       0, 'z'},
                {0, 0, 0, 0}
        };
 
-       while ((c = getopt_long(argc, argv, "hszcCF:M:V", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
                switch (c) {
-               case 'V':
-                       fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
-                       exit(0);
-
-               case 'h':
-                       fputs(USAGE_TEXT, stdout);
-                       exit(0);
-
-               case 's':
-                       check_cache_dir();
-                       stats_summary();
+               case DUMP_MANIFEST:
+                       manifest_dump(optarg, stdout);
                        break;
 
-               case 'c':
-                       check_cache_dir();
-                       cleanup_all(cache_dir);
+               case 'c': /* --cleanup */
+                       initialize();
+                       cleanup_all(conf);
                        printf("Cleaned cache\n");
                        break;
 
-               case 'C':
-                       check_cache_dir();
-                       wipe_all(cache_dir);
+               case 'C': /* --clear */
+                       initialize();
+                       wipe_all(conf);
                        printf("Cleared cache\n");
                        break;
 
-               case 'z':
-                       check_cache_dir();
-                       stats_zero();
-                       printf("Statistics cleared\n");
-                       break;
+               case 'h': /* --help */
+                       fputs(USAGE_TEXT, stdout);
+                       exit(0);
 
-               case 'F':
-                       check_cache_dir();
-                       v = atoi(optarg);
-                       if (stats_set_limits(v, -1) == 0) {
-                               if (v == 0) {
-                                       printf("Unset cache file limit\n");
+               case 'F': /* --max-files */
+                       {
+                               unsigned files;
+                               initialize();
+                               files = atoi(optarg);
+                               if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
+                                                          &errmsg)) {
+                                       if (files == 0) {
+                                               printf("Unset cache file limit\n");
+                                       } else {
+                                               printf("Set cache file limit to %u\n", files);
+                                       }
                                } else {
-                                       printf("Set cache file limit to %u\n", (unsigned)v);
+                                       fatal("could not set cache file limit: %s", errmsg);
                                }
-                       } else {
-                               printf("Could not set cache file limit.\n");
-                               exit(1);
                        }
                        break;
 
-               case 'M':
-                       check_cache_dir();
-                       v = value_units(optarg);
-                       if (stats_set_limits(-1, v) == 0) {
-                               if (v == 0) {
-                                       printf("Unset cache size limit\n");
+               case 'M': /* --max-size */
+                       {
+                               uint64_t size;
+                               initialize();
+                               if (!parse_size_with_suffix(optarg, &size)) {
+                                       fatal("invalid size: %s", optarg);
+                               }
+                               if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
+                                                          &errmsg)) {
+                                       if (size == 0) {
+                                               printf("Unset cache size limit\n");
+                                       } else {
+                                               char *s = format_human_readable_size(size);
+                                               printf("Set cache size limit to %s\n", s);
+                                               free(s);
+                                       }
                                } else {
-                                       char *s = format_size(v);
-                                       printf("Set cache size limit to %s\n", s);
-                                       free(s);
+                                       fatal("could not set cache size limit: %s", errmsg);
                                }
-                       } else {
-                               printf("Could not set cache size limit.\n");
-                               exit(1);
                        }
                        break;
 
+               case 'o': /* --set-config */
+                       {
+                               char *errmsg, *key, *value, *p;
+                               initialize();
+                               p = strchr(optarg, '=');
+                               if (!p) {
+                                       fatal("missing equal sign in \"%s\"", optarg);
+                               }
+                               key = x_strndup(optarg, p - optarg);
+                               value = p + 1;
+                               if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
+                                       fatal("%s", errmsg);
+                               }
+                               free(key);
+                       }
+                       break;
+
+               case 'p': /* --print-config */
+                       initialize();
+                       conf_print_items(conf, configuration_printer, stdout);
+                       break;
+
+               case 's': /* --show-stats */
+                       initialize();
+                       stats_summary(conf);
+                       break;
+
+               case 'V': /* --version */
+                       fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
+                       exit(0);
+
+               case 'z': /* --zero-stats */
+                       initialize();
+                       stats_zero();
+                       printf("Statistics cleared\n");
+                       break;
+
                default:
                        fputs(USAGE_TEXT, stderr);
                        exit(1);
@@ -2316,45 +2863,8 @@ ccache_main_options(int argc, char *argv[])
 int
 ccache_main(int argc, char *argv[])
 {
-       char *p;
-       char *program_name;
-       bool external_tempdir;
-
-       signal(SIGHUP, signal_handler);
-       signal(SIGINT, signal_handler);
-       signal(SIGTERM, signal_handler);
-
-       exitfn_init();
-       exitfn_add_nullary(stats_flush);
-       exitfn_add_nullary(clean_up_pending_tmp_files);
-
-       /* check for logging early so cc_log messages start working ASAP */
-       cache_logfile = getenv("CCACHE_LOGFILE");
-       cc_log("=== CCACHE STARTED =========================================");
-
-       /* the user might have set CCACHE_UMASK */
-       p = getenv("CCACHE_UMASK");
-       if (p) {
-               mode_t mask;
-               errno = 0;
-               mask = strtol(p, NULL, 8);
-               if (errno == 0) {
-                       umask(mask);
-               }
-       }
-
-       cache_dir = getenv("CCACHE_DIR");
-       if (cache_dir) {
-               cache_dir = x_strdup(cache_dir);
-       } else {
-               const char *home_directory = get_home_directory();
-               if (home_directory) {
-                       cache_dir = format("%s/.ccache", home_directory);
-               }
-       }
-
        /* check if we are being invoked as "ccache" */
-       program_name = basename(argv[0]);
+       char *program_name = basename(argv[0]);
        if (same_executable_name(program_name, MYNAME)) {
                if (argc < 2) {
                        fputs(USAGE_TEXT, stderr);
@@ -2368,51 +2878,6 @@ ccache_main(int argc, char *argv[])
        }
        free(program_name);
 
-       /* find_compiler sets up orig_args, needed by failed(), so do it early. */
-       find_compiler(argc, argv);
-
-       if (getenv("CCACHE_DISABLE")) {
-               cc_log("ccache is disabled");
-               failed();
-       }
-
-       check_cache_dir();
-
-       base_dir = getenv("CCACHE_BASEDIR");
-       if (base_dir && base_dir[0] != '/') {
-               cc_log("Ignoring non-absolute base directory %s", base_dir);
-               base_dir = NULL;
-       }
-
-       compile_preprocessed_source_code = !getenv("CCACHE_CPP2");
-
-       /* make sure the cache dir exists */
-       if (create_dir(cache_dir) != 0) {
-               fprintf(stderr,
-                       "ccache: failed to create %s (%s)\n",
-                       cache_dir, strerror(errno));
-               exit(1);
-       }
-
-       /* make sure the temp dir exists */
-       temp_dir = getenv("CCACHE_TEMPDIR");
-       if (temp_dir) {
-               external_tempdir = true;
-       } else {
-               temp_dir = format("%s/tmp", cache_dir);
-               external_tempdir = false;
-       }
-       if (create_dir(temp_dir) != 0) {
-               fprintf(stderr,
-                       "ccache: failed to create %s (%s)\n",
-                       temp_dir, strerror(errno));
-               exit(1);
-       }
-
-       if (!external_tempdir) {
-               clean_up_internal_tempdir();
-       }
-
-       ccache(argv);
+       ccache(argc, argv);
        return 1;
 }
index ce4d1cf5779c749a2235c7be7677c0e90bdbba3a..6fdd55f6c88e7f1e33275e92a40aa971d2a5109d 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -3,6 +3,7 @@
 
 #include "system.h"
 #include "mdfour.h"
+#include "conf.h"
 #include "counters.h"
 
 #ifdef __GNUC__
@@ -34,8 +35,8 @@ enum stats {
        STATS_LINK = 10,
        STATS_NUMFILES = 11,
        STATS_TOTALSIZE = 12,
-       STATS_MAXFILES = 13,
-       STATS_MAXSIZE = 14,
+       STATS_OBSOLETE_MAXFILES = 13,
+       STATS_OBSOLETE_MAXSIZE = 14,
        STATS_SOURCELANG = 15,
        STATS_DEVICE = 16,
        STATS_NOINPUT = 17,
@@ -55,9 +56,15 @@ enum stats {
 };
 
 #define SLOPPY_INCLUDE_FILE_MTIME 1
-#define SLOPPY_FILE_MACRO 2
-#define SLOPPY_TIME_MACROS 4
-#define SLOPPY_PCH_DEFINES 8
+#define SLOPPY_INCLUDE_FILE_CTIME 2
+#define SLOPPY_FILE_MACRO 4
+#define SLOPPY_TIME_MACROS 8
+#define SLOPPY_PCH_DEFINES 16
+/*
+ * Allow us to match files based on their stats (size, mtime, ctime), without
+ * looking at their contents.
+ */
+#define SLOPPY_FILE_STAT_MATCHES 32
 
 #define str_eq(s1, s2) (strcmp((s1), (s2)) == 0)
 #define str_startswith(s, p) (strncmp((s), (p), strlen((p))) == 0)
@@ -72,11 +79,13 @@ struct args {
 
 struct args *args_init(int, char **);
 struct args *args_init_from_string(const char *);
+struct args *args_init_from_gcc_atfile(const char *filename);
 struct args *args_copy(struct args *args);
 void args_free(struct args *args);
 void args_add(struct args *args, const char *s);
 void args_add_prefix(struct args *args, const char *s);
 void args_extend(struct args *args, struct args *to_append);
+void args_insert(struct args *dest, int index, struct args *src, bool replace);
 void args_pop(struct args *args, int n);
 void args_set(struct args *args, int index, const char *value);
 void args_strip(struct args *args, const char *prefix);
@@ -92,8 +101,9 @@ void hash_buffer(struct mdfour *md, const void *s, size_t len);
 char *hash_result(struct mdfour *md);
 void hash_result_as_bytes(struct mdfour *md, unsigned char *out);
 bool hash_equal(struct mdfour *md1, struct mdfour *md2);
-void hash_delimiter(struct mdfour *md, const chartype);
+void hash_delimiter(struct mdfour *md, const char *type);
 void hash_string(struct mdfour *md, const char *s);
+void hash_string_length(struct mdfour *md, const char *s, int length);
 void hash_int(struct mdfour *md, int x);
 bool hash_fd(struct mdfour *md, int fd);
 bool hash_file(struct mdfour *md, const char *fname);
@@ -102,40 +112,49 @@ bool hash_file(struct mdfour *md, const char *fname);
 /* util.c */
 
 void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
+void cc_bulklog(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
 void cc_log_argv(const char *prefix, char **argv);
 void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
 
 void copy_fd(int fd_in, int fd_out);
-int copy_file(const char *src, const char *dest, int compress_dest);
-int move_file(const char *src, const char *dest, int compress_dest);
+int copy_file(const char *src, const char *dest, int compress_level);
+int move_file(const char *src, const char *dest, int compress_level);
 int move_uncompressed_file(const char *src, const char *dest,
-                           int compress_dest);
+                           int compress_level);
 bool file_is_compressed(const char *filename);
-
 int create_dir(const char *dir);
+int create_parent_dirs(const char *path);
 const char *get_hostname(void);
 const char *tmp_string(void);
-char *format_hash_as_string(const unsigned char *hash, unsigned size);
-int create_hash_dir(char **dir, const char *hash, const char *cache_dir);
+char *format_hash_as_string(const unsigned char *hash, int size);
 int create_cachedirtag(const char *dir);
 char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
+void reformat(char **ptr, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
 char *x_strdup(const char *s);
 char *x_strndup(const char *s, size_t n);
-void *x_realloc(void *ptr, size_t size);
 void *x_malloc(size_t size);
 void *x_calloc(size_t nmemb, size_t size);
+void *x_realloc(void *ptr, size_t size);
+void x_unsetenv(const char *name);
 void traverse(const char *dir, void (*fn)(const char *, struct stat *));
-char *basename(const char *s);
-char *dirname(char *s);
+char *basename(const char *path);
+char *dirname(const char *path);
 const char *get_extension(const char *path);
 char *remove_extension(const char *path);
 size_t file_size(struct stat *st);
-int safe_open(const char *fname);
+char *format_human_readable_size(uint64_t size);
+char *format_parsable_size_with_suffix(uint64_t size);
+bool parse_size_with_suffix(const char *str, uint64_t *size);
 char *x_realpath(const char *path);
 char *gnu_getcwd(void);
-int create_empty_file(const char *fname);
+#ifndef HAVE_STRTOK_R
+char *strtok_r(char *str, const char *delim, char **saveptr);
+#endif
+int create_tmp_fd(char **fname);
+FILE *create_tmp_file(char **fname, const char *mode);
+void create_empty_tmp_file(char **fname);
 const char *get_home_directory(void);
-char *get_cwd();
+char *get_cwd(void);
 bool same_executable_name(const char *s1, const char *s2);
 size_t common_dir_prefix_length(const char *s1, const char *s2);
 char *get_relative_path(const char *from, const char *to);
@@ -143,11 +162,14 @@ bool is_absolute_path(const char *path);
 bool is_full_path(const char *path);
 void update_mtime(const char *path);
 int x_rename(const char *oldpath, const char *newpath);
-int x_unlink(const char *path);
 int tmp_unlink(const char *path);
+int x_unlink(const char *path);
+#ifndef _WIN32
 char *x_readlink(const char *path);
-char *read_text_file(const char *path);
+#endif
 bool read_file(const char *path, size_t size_hint, char **data, size_t *size);
+char *read_text_file(const char *path, size_t size_hint);
+char *subst_env_in_string(const char *str, char **errmsg);
 
 /* ------------------------------------------------------------------------- */
 /* stats.c */
@@ -156,12 +178,10 @@ void stats_update(enum stats stat);
 void stats_flush(void);
 unsigned stats_get_pending(enum stats stat);
 void stats_zero(void);
-void stats_summary(void);
-void stats_update_size(enum stats stat, size_t size, unsigned files);
-void stats_get_limits(const char *dir, unsigned *maxfiles, unsigned *maxsize);
-int stats_set_limits(long maxfiles, long maxsize);
-size_t value_units(const char *s);
-char *format_size(size_t v);
+void stats_summary(struct conf *conf);
+void stats_update_size(uint64_t size, unsigned files);
+void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
+                               uint64_t *maxsize);
 void stats_set_sizes(const char *dir, size_t num_files, size_t total_size);
 void stats_read(const char *path, struct counters *counters);
 void stats_write(const char *path, struct counters *counters);
@@ -182,16 +202,14 @@ void exitfn_call(void);
 /* ------------------------------------------------------------------------- */
 /* cleanup.c */
 
-void cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize);
-void cleanup_all(const char *dir);
-void wipe_all(const char *dir);
+void cleanup_dir(struct conf *conf, const char *dir);
+void cleanup_all(struct conf *conf);
+void wipe_all(struct conf *conf);
 
 /* ------------------------------------------------------------------------- */
 /* execute.c */
 
-int execute(char **argv,
-            const char *path_stdout,
-            const char *path_stderr);
+int execute(char **argv, int fd_out, int fd_err);
 char *find_executable(const char *name, const char *exclude_name);
 void print_command(FILE *fp, char **argv);
 
@@ -204,7 +222,8 @@ void lockfile_release(const char *path);
 /* ------------------------------------------------------------------------- */
 /* ccache.c */
 
-bool cc_process_args(struct args *orig_args, struct args **preprocessor_args,
+extern time_t time_of_compilation;
+bool cc_process_args(struct args *args, struct args **preprocessor_args,
                     struct args **compiler_args);
 void cc_reset(void);
 bool is_precompiled_header(const char *path);
@@ -229,6 +248,8 @@ typedef int (*COMPAR_FN_T)(const void *, const void *);
 #endif
 
 #ifdef _WIN32
+char *win32argvtos(char *prefix, char **argv);
+char *win32getshell(char *path);
 int win32execute(char *path, char **argv, int doreturn,
                  const char *path_stdout, const char *path_stderr);
 #    ifndef _WIN32_WINNT
@@ -239,6 +260,7 @@ int win32execute(char *path, char **argv, int doreturn,
 #    define link(src,dst) (CreateHardLink(dst,src,NULL) ? 0 : -1)
 #    define lstat(a,b) stat(a,b)
 #    define execv(a,b) win32execute(a,b,0,NULL,NULL)
+#error TODO: Adapt win32execute to new execute API
 #    define execute(a,b,c) win32execute(*(a),a,1,b,c)
 #    define PATH_DELIM ";"
 #    define F_RDLCK 0
@@ -247,4 +269,6 @@ int win32execute(char *path, char **argv, int doreturn,
 #    define PATH_DELIM ":"
 #endif
 
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
 #endif /* ifndef CCACHE_H */
index 530bd45bde107df9b1d12fbc9df24a12ccde950b..a8b4cf0b9020d2795cc9c46c84f393111d2e0437 100644 (file)
--- a/cleanup.c
+++ b/cleanup.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2002-2006 Andrew Tridgell
- * Copyright (C) 2009-2010 Joel Rosdahl
+ * Copyright (C) 2009-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
 static struct files {
        char *fname;
        time_t mtime;
-       size_t size; /* In KiB. */
+       uint64_t size;
 } **files;
 static unsigned allocated; /* Size of the files array. */
 static unsigned num_files; /* Number of used entries in the files array. */
 
-static size_t cache_size; /* In KiB. */
+static uint64_t cache_size;
 static size_t files_in_cache;
-static size_t cache_size_threshold;
+static uint64_t cache_size_threshold;
 static size_t files_in_cache_threshold;
 
 /* File comparison function that orders files in mtime order, oldest first. */
@@ -58,7 +58,9 @@ traverse_fn(const char *fname, struct stat *st)
 {
        char *p;
 
-       if (!S_ISREG(st->st_mode)) return;
+       if (!S_ISREG(st->st_mode)) {
+               return;
+       }
 
        p = basename(fname);
        if (str_eq(p, "stats")) {
@@ -70,7 +72,7 @@ traverse_fn(const char *fname, struct stat *st)
                goto out;
        }
 
-       if (strstr(p, ".tmp.") != NULL) {
+       if (strstr(p, ".tmp.")) {
                /* delete any tmp files older than 1 hour */
                if (st->st_mtime + 3600 < time(NULL)) {
                        x_unlink(fname);
@@ -86,7 +88,7 @@ traverse_fn(const char *fname, struct stat *st)
        files[num_files] = (struct files *)x_malloc(sizeof(struct files));
        files[num_files]->fname = x_strdup(fname);
        files[num_files]->mtime = st->st_mtime;
-       files[num_files]->size = file_size(st) / 1024;
+       files[num_files]->size = file_size(st);
        cache_size += files[num_files]->size;
        files_in_cache++;
        num_files++;
@@ -114,7 +116,7 @@ delete_sibling_file(const char *base, const char *extension)
 
        path = format("%s%s", base, extension);
        if (lstat(path, &st) == 0) {
-               delete_file(path, file_size(&st) / 1024);
+               delete_file(path, file_size(&st));
        } else if (errno != ENOENT) {
                cc_log("Failed to stat %s (%s)", path, strerror(errno));
        }
@@ -147,6 +149,7 @@ sort_and_clean(void)
                ext = get_extension(files[i]->fname);
                if (str_eq(ext, ".o")
                    || str_eq(ext, ".d")
+                   || str_eq(ext, ".dia")
                    || str_eq(ext, ".stderr")
                    || str_eq(ext, "")) {
                        char *base = remove_extension(files[i]->fname);
@@ -160,6 +163,7 @@ sort_and_clean(void)
                                 */
                                delete_sibling_file(base, ".o");
                                delete_sibling_file(base, ".d");
+                               delete_sibling_file(base, ".dia");
                                delete_sibling_file(base, ".stderr");
                                delete_sibling_file(base, ""); /* Object file from ccache 2.4. */
                        }
@@ -175,14 +179,14 @@ sort_and_clean(void)
 
 /* cleanup in one cache subdir */
 void
-cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize)
+cleanup_dir(struct conf *conf, const char *dir)
 {
        unsigned i;
 
        cc_log("Cleaning up cache directory %s", dir);
 
-       cache_size_threshold = maxsize * LIMIT_MULTIPLE;
-       files_in_cache_threshold = maxfiles * LIMIT_MULTIPLE;
+       cache_size_threshold = conf->max_size * LIMIT_MULTIPLE / 16;
+       files_in_cache_threshold = conf->max_files * LIMIT_MULTIPLE / 16;
 
        num_files = 0;
        cache_size = 0;
@@ -214,16 +218,14 @@ cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize)
 }
 
 /* cleanup in all cache subdirs */
-void cleanup_all(const char *dir)
+void cleanup_all(struct conf *conf)
 {
-       unsigned maxfiles, maxsize;
        char *dname;
        int i;
 
        for (i = 0; i <= 0xF; i++) {
-               dname = format("%s/%1x", dir, i);
-               stats_get_limits(dname, &maxfiles, &maxsize);
-               cleanup_dir(dname, maxfiles, maxsize);
+               dname = format("%s/%1x", conf->cache_dir, i);
+               cleanup_dir(conf, dname);
                free(dname);
        }
 }
@@ -233,7 +235,9 @@ static void wipe_fn(const char *fname, struct stat *st)
 {
        char *p;
 
-       if (!S_ISREG(st->st_mode)) return;
+       if (!S_ISREG(st->st_mode)) {
+               return;
+       }
 
        p = basename(fname);
        if (str_eq(p, "stats")) {
@@ -246,17 +250,17 @@ static void wipe_fn(const char *fname, struct stat *st)
 }
 
 /* wipe all cached files in all subdirs */
-void wipe_all(const char *dir)
+void wipe_all(struct conf *conf)
 {
        char *dname;
        int i;
 
        for (i = 0; i <= 0xF; i++) {
-               dname = format("%s/%1x", dir, i);
-               traverse(dir, wipe_fn);
+               dname = format("%s/%1x", conf->cache_dir, i);
+               traverse(dname, wipe_fn);
                free(dname);
        }
 
        /* and fix the counters */
-       cleanup_all(dir);
+       cleanup_all(conf);
 }
index 2417726b79afdcf379f443e449a7c82283719e63..1996fdfc36d25c912b6e62723bd014f6d6b784a8 100644 (file)
--- a/compopt.c
+++ b/compopt.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010, 2013 Joel Rosdahl
+ * Copyright (C) 2010, 2012-2013 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -32,8 +32,9 @@ struct compopt {
 };
 
 static const struct compopt compopts[] = {
-       {"--coverage",      TOO_HARD},
+       {"--coverage",      TOO_HARD}, /* implies -ftest-coverage */
        {"--param",         TAKES_ARG},
+       {"--serialize-diagnostics", TAKES_ARG | TAKES_PATH},
        {"-A",              TAKES_ARG},
        {"-D",              AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG},
        {"-E",              TOO_HARD},
@@ -51,21 +52,21 @@ static const struct compopt compopts[] = {
        {"-Xassembler",     TAKES_ARG},
        {"-Xclang",         TAKES_ARG},
        {"-Xlinker",        TAKES_ARG},
-       {"-Xpreprocessor",  TOO_HARD_DIRECT | TAKES_ARG},
+       {"-Xpreprocessor",  AFFECTS_CPP | TOO_HARD_DIRECT | TAKES_ARG},
+       {"-arch",           TAKES_ARG},
        {"-aux-info",       TAKES_ARG},
        {"-b",              TAKES_ARG},
-       {"-fbranch-probabilities", TOO_HARD},
-       {"-fprofile-arcs",  TOO_HARD},
-       {"-fprofile-generate", TOO_HARD},
-       {"-fprofile-use",   TOO_HARD},
+       {"-fno-working-directory", AFFECTS_CPP},
        {"-frepo",          TOO_HARD},
-       {"-ftest-coverage", TOO_HARD},
-       {"-gsplit-dwarf",   TOO_HARD},
+       {"-ftest-coverage", TOO_HARD}, /* generates a .gcno file at the same time */
+       {"-fworking-directory", AFFECTS_CPP},
+       {"-gsplit-dwarf",   TOO_HARD}, /* generates a .dwo file at the same time */
        {"-idirafter",      AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-iframework",     AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH},
        {"-imacros",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-imultilib",      AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-include",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
+       {"-include-pch",    AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-install_name",   TAKES_ARG}, /* Darwin linker option */
        {"-iprefix",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-iquote",         AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
@@ -75,10 +76,13 @@ static const struct compopt compopts[] = {
        {"-iwithprefixbefore", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
        {"-nostdinc",       AFFECTS_CPP},
        {"-nostdinc++",     AFFECTS_CPP},
+       {"-remap",          AFFECTS_CPP},
        {"-save-temps",     TOO_HARD},
+       {"-trigraphs",      AFFECTS_CPP},
        {"-u",              TAKES_ARG},
 };
 
+
 static int
 compare_compopts(const void *key1, const void *key2)
 {
@@ -87,6 +91,14 @@ compare_compopts(const void *key1, const void *key2)
        return strcmp(opt1->name, opt2->name);
 }
 
+static int
+compare_prefix_compopts(const void *key1, const void *key2)
+{
+       const struct compopt *opt1 = (const struct compopt *)key1;
+       const struct compopt *opt2 = (const struct compopt *)key2;
+       return strncmp(opt1->name, opt2->name, strlen(opt2->name));
+}
+
 static const struct compopt *
 find(const char *option)
 {
@@ -97,6 +109,16 @@ find(const char *option)
                sizeof(compopts[0]), compare_compopts);
 }
 
+static const struct compopt *
+find_prefix(const char *option)
+{
+       struct compopt key;
+       key.name = option;
+       return bsearch(
+               &key, compopts, sizeof(compopts) / sizeof(compopts[0]),
+               sizeof(compopts[0]), compare_prefix_compopts);
+}
+
 /* Runs fn on the first two characters of option. */
 bool
 compopt_short(bool (*fn)(const char *), const char *option)
@@ -158,3 +180,23 @@ compopt_takes_arg(const char *option)
        const struct compopt *co = find(option);
        return co && (co->type & TAKES_ARG);
 }
+
+/* Determines if argument takes a concatentated argument by comparing prefixes.
+ */
+bool
+compopt_takes_concat_arg(const char *option)
+{
+       const struct compopt *co = find_prefix(option);
+       return co && (co->type & TAKES_CONCAT_ARG);
+}
+
+/* Determines if the prefix of the option matches any option and affects the
+ * preprocessor.
+ */
+bool
+compopt_prefix_affects_cpp(const char *option)
+{
+       /* prefix options have to take concatentated args */
+       const struct compopt *co = find_prefix(option);
+       return co && (co->type & TAKES_CONCAT_ARG) && (co->type & AFFECTS_CPP);
+}
index 8985ac3d7853781e36d1ab8ebfde56ce3c24286e..109094b66a135352230a461c958789564b4540bb 100644 (file)
--- a/compopt.h
+++ b/compopt.h
@@ -9,5 +9,7 @@ bool compopt_too_hard(const char *option);
 bool compopt_too_hard_for_direct_mode(const char *option);
 bool compopt_takes_path(const char *option);
 bool compopt_takes_arg(const char *option);
+bool compopt_takes_concat_arg(const char *option);
+bool compopt_prefix_affects_cpp(const char *option);
 
 #endif /* CCACHE_COMPOPT_H */
diff --git a/conf.c b/conf.c
new file mode 100644 (file)
index 0000000..92c70c0
--- /dev/null
+++ b/conf.c
@@ -0,0 +1,637 @@
+/*
+ * Copyright (C) 2011-2014 Joel Rosdahl
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "conf.h"
+#include "ccache.h"
+
+typedef bool (*conf_item_parser)(const char *str, void *result, char **errmsg);
+typedef bool (*conf_item_verifier)(void *value, char **errmsg);
+
+struct conf_item {
+       const char *name;
+       size_t number;
+       conf_item_parser parser;
+       size_t offset;
+       conf_item_verifier verifier;
+};
+
+struct env_to_conf_item {
+       const char *env_name;
+       const char *conf_name;
+};
+
+static bool
+parse_bool(const char *str, void *result, char **errmsg)
+{
+       bool *value = (bool *)result;
+
+       if (str_eq(str, "true")) {
+               *value = true;
+               return true;
+       } else if (str_eq(str, "false")) {
+               *value = false;
+               return true;
+       } else {
+               *errmsg = format("not a boolean value: \"%s\"", str);
+               return false;
+       }
+}
+
+static bool
+parse_env_string(const char *str, void *result, char **errmsg)
+{
+       char **value = (char **)result;
+       free(*value);
+       *value = subst_env_in_string(str, errmsg);
+       return *value;
+}
+
+static bool
+parse_size(const char *str, void *result, char **errmsg)
+{
+       uint64_t *value = (uint64_t *)result;
+       uint64_t size;
+       *errmsg = NULL;
+       if (parse_size_with_suffix(str, &size)) {
+               *value = size;
+               return true;
+       } else {
+               *errmsg = format("invalid size: \"%s\"", str);
+               return false;
+       }
+}
+
+static bool
+parse_sloppiness(const char *str, void *result, char **errmsg)
+{
+       unsigned *value = (unsigned *)result;
+       char *word, *p, *q, *saveptr = NULL;
+
+       if (!str) {
+               return *value;
+       }
+       p = x_strdup(str);
+       q = p;
+       while ((word = strtok_r(q, ", ", &saveptr))) {
+               if (str_eq(word, "file_macro")) {
+                       *value |= SLOPPY_FILE_MACRO;
+               } else if (str_eq(word, "file_stat_matches")) {
+                       *value |= SLOPPY_FILE_STAT_MATCHES;
+               } else if (str_eq(word, "include_file_ctime")) {
+                       *value |= SLOPPY_INCLUDE_FILE_CTIME;
+               } else if (str_eq(word, "include_file_mtime")) {
+                       *value |= SLOPPY_INCLUDE_FILE_MTIME;
+               } else if (str_eq(word, "pch_defines")) {
+                       *value |= SLOPPY_PCH_DEFINES;
+               } else if (str_eq(word, "time_macros")) {
+                       *value |= SLOPPY_TIME_MACROS;
+               } else {
+                       *errmsg = format("unknown sloppiness: \"%s\"", word);
+                       free(p);
+                       return false;
+               }
+               q = NULL;
+       }
+       free(p);
+       return true;
+}
+
+static bool
+parse_string(const char *str, void *result, char **errmsg)
+{
+       char **value = (char **)result;
+       (void)errmsg;
+       free(*value);
+       *value = x_strdup(str);
+       return true;
+}
+
+static bool
+parse_umask(const char *str, void *result, char **errmsg)
+{
+       unsigned *value = (unsigned *)result;
+       char *endptr;
+       if (str_eq(str, "")) {
+               *value = UINT_MAX;
+               return true;
+       }
+       errno = 0;
+       *value = strtoul(str, &endptr, 8);
+       if (errno == 0 && *str != '\0' && *endptr == '\0') {
+               return true;
+       } else {
+               *errmsg = format("not an octal integer: \"%s\"", str);
+               return false;
+       }
+}
+
+static bool
+parse_unsigned(const char *str, void *result, char **errmsg)
+{
+       unsigned *value = (unsigned *)result;
+       long x;
+       char *endptr;
+       errno = 0;
+       x = strtol(str, &endptr, 10);
+       if (errno == 0 && x >= 0 && *str != '\0' && *endptr == '\0') {
+               *value = x;
+               return true;
+       } else {
+               *errmsg = format("invalid unsigned integer: \"%s\"", str);
+               return false;
+       }
+}
+
+static const char*
+bool_to_string(bool value)
+{
+       return value ? "true" : "false";
+}
+
+static bool
+verify_absolute_path(void *value, char **errmsg)
+{
+       char **path = (char **)value;
+       assert(*path);
+       if (str_eq(*path, "")) {
+               /* The empty string means "disable" in this case. */
+               return true;
+       } else if (is_absolute_path(*path)) {
+               return true;
+       } else {
+               *errmsg = format("not an absolute path: \"%s\"", *path);
+               return false;
+       }
+}
+
+static bool
+verify_dir_levels(void *value, char **errmsg)
+{
+       unsigned *levels = (unsigned *)value;
+       assert(levels);
+       if (*levels >= 1 && *levels <= 8) {
+               return true;
+       } else {
+               *errmsg = format("cache directory levels must be between 1 and 8");
+               return false;
+       }
+}
+
+#define ITEM(name, type) \
+       parse_##type, offsetof(struct conf, name), NULL
+#define ITEM_V(name, type, verification) \
+       parse_##type, offsetof(struct conf, name), verify_##verification
+
+#include "confitems_lookup.c"
+#include "envtoconfitems_lookup.c"
+
+static const struct conf_item *
+find_conf(const char *name)
+{
+       return confitems_get(name, strlen(name));
+}
+
+static const struct env_to_conf_item *
+find_env_to_conf(const char *name)
+{
+       return envtoconfitems_get(name, strlen(name));
+}
+
+static bool
+handle_conf_setting(struct conf *conf, const char *key, const char *value,
+                    char **errmsg, bool from_env_variable, bool negate_boolean,
+                    const char *origin)
+{
+       const struct conf_item *item;
+
+       item = find_conf(key);
+       if (!item) {
+               *errmsg = format("unknown configuration option \"%s\"", key);
+               return false;
+       }
+
+       if (from_env_variable && item->parser == parse_bool) {
+               /*
+                * Special rule for boolean settings from the environment: any value means
+                * true.
+                */
+               bool *value = (bool *)((char *)conf + item->offset);
+               *value = !negate_boolean;
+               goto out;
+       }
+
+       if (!item->parser(value, (char *)conf + item->offset, errmsg)) {
+               return false;
+       }
+       if (item->verifier && !item->verifier((char *)conf + item->offset, errmsg)) {
+               return false;
+       }
+
+out:
+       conf->item_origins[item->number] = origin;
+       return true;
+}
+
+static bool
+parse_line(const char *line, char **key, char **value, char **errmsg)
+{
+       const char *p, *q;
+
+#define SKIP_WS(x) while (isspace(*x)) { ++x; }
+
+       *key = NULL;
+       *value = NULL;
+
+       p = line;
+       SKIP_WS(p);
+       if (*p == '\0' || *p == '#') {
+               return true;
+       }
+       q = p;
+       while (isalpha(*q) || *q == '_') {
+               ++q;
+       }
+       *key = x_strndup(p, q - p);
+       p = q;
+       SKIP_WS(p);
+       if (*p != '=') {
+               *errmsg = x_strdup("missing equal sign");
+               free(*key);
+               *key = NULL;
+               return false;
+       }
+       ++p;
+
+       /* Skip leading whitespace. */
+       SKIP_WS(p);
+       q = p;
+       while (*q) {
+               ++q;
+       }
+       /* Skip trailing whitespace. */
+       while (isspace(q[-1])) {
+               --q;
+       }
+       *value = x_strndup(p, q - p);
+
+       return true;
+
+#undef SKIP_WS
+}
+
+/* Create a conf struct with default values. */
+struct conf *
+conf_create(void)
+{
+       size_t i;
+       struct conf *conf = x_malloc(sizeof(*conf));
+       conf->base_dir = x_strdup("");
+       conf->cache_dir = format("%s/.ccache", get_home_directory());
+       conf->cache_dir_levels = 2;
+       conf->compiler = x_strdup("");
+       conf->compiler_check = x_strdup("mtime");
+       conf->compression = false;
+       conf->compression_level = 6;
+       conf->cpp_extension = x_strdup("");
+       conf->direct_mode = true;
+       conf->disable = false;
+       conf->extra_files_to_hash = x_strdup("");
+       conf->hard_link = false;
+       conf->hash_dir = false;
+       conf->log_file = x_strdup("");
+       conf->max_files = 0;
+       conf->max_size = (uint64_t)5 * 1000 * 1000 * 1000;
+       conf->path = x_strdup("");
+       conf->prefix_command = x_strdup("");
+       conf->read_only = false;
+       conf->read_only_direct = false;
+       conf->recache = false;
+       conf->run_second_cpp = false;
+       conf->sloppiness = 0;
+       conf->stats = true;
+       conf->temporary_dir = x_strdup("");
+       conf->umask = UINT_MAX; /* default: don't set umask */
+       conf->unify = false;
+       conf->item_origins = x_malloc(CONFITEMS_TOTAL_KEYWORDS * sizeof(char *));
+       for (i = 0; i < CONFITEMS_TOTAL_KEYWORDS; ++i) {
+               conf->item_origins[i] = "default";
+       }
+       return conf;
+}
+
+void
+conf_free(struct conf *conf)
+{
+       if (!conf) {
+               return;
+       }
+       free(conf->base_dir);
+       free(conf->cache_dir);
+       free(conf->compiler);
+       free(conf->compiler_check);
+       free(conf->cpp_extension);
+       free(conf->extra_files_to_hash);
+       free(conf->log_file);
+       free(conf->path);
+       free(conf->prefix_command);
+       free(conf->temporary_dir);
+       free(conf->item_origins);
+       free(conf);
+}
+
+/* Note: The path pointer is stored in conf, so path must outlive conf. */
+bool
+conf_read(struct conf *conf, const char *path, char **errmsg)
+{
+       FILE *f;
+       char buf[10000];
+       bool result = true;
+       unsigned line_number;
+
+       assert(errmsg);
+       *errmsg = NULL;
+
+       f = fopen(path, "r");
+       if (!f) {
+               *errmsg = format("%s: %s", path, strerror(errno));
+               return false;
+       }
+
+       line_number = 0;
+       while (fgets(buf, sizeof(buf), f)) {
+               char *errmsg2, *key, *value;
+               bool ok;
+               ++line_number;
+               ok = parse_line(buf, &key, &value, &errmsg2);
+               if (ok && key) { /* key == NULL if comment or blank line */
+                       ok = handle_conf_setting(conf, key, value, &errmsg2, false, false, path);
+               }
+               free(key);
+               free(value);
+               if (!ok) {
+                       *errmsg = format("%s:%u: %s", path, line_number, errmsg2);
+                       free(errmsg2);
+                       result = false;
+                       goto out;
+               }
+       }
+       if (ferror(f)) {
+               *errmsg = x_strdup(strerror(errno));
+               result = false;
+       }
+
+out:
+       fclose(f);
+       return result;
+}
+
+bool
+conf_update_from_environment(struct conf *conf, char **errmsg)
+{
+       char **p;
+       char *q;
+       char *key;
+       char *errmsg2;
+       const struct env_to_conf_item *env_to_conf_item;
+       bool negate;
+       size_t key_start;
+
+       for (p = environ; *p; ++p) {
+               if (!str_startswith(*p, "CCACHE_")) {
+                       continue;
+               }
+               q = strchr(*p, '=');
+               if (!q) {
+                       continue;
+               }
+
+               if (str_startswith(*p + 7, "NO")) {
+                       negate = true;
+                       key_start = 9;
+               } else {
+                       negate = false;
+                       key_start = 7;
+               }
+               key = x_strndup(*p + key_start, q - *p - key_start);
+
+               ++q; /* Now points to the value. */
+
+               env_to_conf_item = find_env_to_conf(key);
+               if (!env_to_conf_item) {
+                       free(key);
+                       continue;
+               }
+
+               if (!handle_conf_setting(
+                           conf, env_to_conf_item->conf_name, q, &errmsg2, true, negate,
+                           "environment")) {
+                       *errmsg = format("%s: %s", key, errmsg2);
+                       free(errmsg2);
+                       free(key);
+                       return false;
+               }
+
+               free(key);
+       }
+
+       return true;
+}
+
+bool
+conf_set_value_in_file(const char *path, const char *key, const char *value,
+                       char **errmsg)
+{
+       FILE *infile, *outfile;
+       char *outpath;
+       char buf[10000];
+       bool found;
+       const struct conf_item *item;
+
+       item = find_conf(key);
+       if (!item) {
+               *errmsg = format("unknown configuration option \"%s\"", key);
+               return false;
+       }
+
+       infile = fopen(path, "r");
+       if (!infile) {
+               *errmsg = format("%s: %s", path, strerror(errno));
+               return false;
+       }
+
+       outpath = format("%s.tmp", path);
+       outfile = create_tmp_file(&outpath, "w");
+       if (!outfile) {
+               *errmsg = format("%s: %s", outpath, strerror(errno));
+               free(outpath);
+               fclose(infile);
+               return false;
+       }
+
+       found = false;
+       while (fgets(buf, sizeof(buf), infile)) {
+               char *errmsg2, *key2, *value2;
+               bool ok;
+               ok = parse_line(buf, &key2, &value2, &errmsg2);
+               if (ok && key2 && str_eq(key2, key)) {
+                       found = true;
+                       fprintf(outfile, "%s = %s\n", key, value);
+               } else {
+                       fputs(buf, outfile);
+               }
+               free(key2);
+               free(value2);
+       }
+
+       if (!found) {
+               fprintf(outfile, "%s = %s\n", key, value);
+       }
+
+       fclose(infile);
+       fclose(outfile);
+       if (x_rename(outpath, path) != 0) {
+               *errmsg = format("rename %s to %s: %s", outpath, path, strerror(errno));
+               return false;
+       }
+       free(outpath);
+
+       return true;
+}
+
+bool
+conf_print_items(struct conf *conf,
+                 void (*printer)(const char *descr, const char *origin,
+                                 void *context),
+                 void *context)
+{
+       char *s = x_strdup("");
+       char *s2;
+
+       reformat(&s, "base_dir = %s", conf->base_dir);
+       printer(s, conf->item_origins[find_conf("base_dir")->number], context);
+
+       reformat(&s, "cache_dir = %s", conf->cache_dir);
+       printer(s, conf->item_origins[find_conf("cache_dir")->number], context);
+
+       reformat(&s, "cache_dir_levels = %u", conf->cache_dir_levels);
+       printer(s, conf->item_origins[find_conf("cache_dir_levels")->number],
+               context);
+
+       reformat(&s, "compiler = %s", conf->compiler);
+       printer(s, conf->item_origins[find_conf("compiler")->number], context);
+
+       reformat(&s, "compiler_check = %s", conf->compiler_check);
+       printer(s, conf->item_origins[find_conf("compiler_check")->number], context);
+
+       reformat(&s, "compression = %s", bool_to_string(conf->compression));
+       printer(s, conf->item_origins[find_conf("compression")->number], context);
+
+       reformat(&s, "compression_level = %u", conf->compression_level);
+       printer(s, conf->item_origins[find_conf("compression_level")->number],
+               context);
+
+       reformat(&s, "cpp_extension = %s", conf->cpp_extension);
+       printer(s, conf->item_origins[find_conf("cpp_extension")->number], context);
+
+       reformat(&s, "direct_mode = %s", bool_to_string(conf->direct_mode));
+       printer(s, conf->item_origins[find_conf("direct_mode")->number], context);
+
+       reformat(&s, "disable = %s", bool_to_string(conf->disable));
+       printer(s, conf->item_origins[find_conf("disable")->number], context);
+
+       reformat(&s, "extra_files_to_hash = %s", conf->extra_files_to_hash);
+       printer(s, conf->item_origins[find_conf("extra_files_to_hash")->number],
+               context);
+
+       reformat(&s, "hard_link = %s", bool_to_string(conf->hard_link));
+       printer(s, conf->item_origins[find_conf("hard_link")->number], context);
+
+       reformat(&s, "hash_dir = %s", bool_to_string(conf->hash_dir));
+       printer(s, conf->item_origins[find_conf("hash_dir")->number], context);
+
+       reformat(&s, "log_file = %s", conf->log_file);
+       printer(s, conf->item_origins[find_conf("log_file")->number], context);
+
+       reformat(&s, "max_files = %u", conf->max_files);
+       printer(s, conf->item_origins[find_conf("max_files")->number], context);
+
+       s2 = format_parsable_size_with_suffix(conf->max_size);
+       reformat(&s, "max_size = %s", s2);
+       printer(s, conf->item_origins[find_conf("max_size")->number], context);
+       free(s2);
+
+       reformat(&s, "path = %s", conf->path);
+       printer(s, conf->item_origins[find_conf("path")->number], context);
+
+       reformat(&s, "prefix_command = %s", conf->prefix_command);
+       printer(s, conf->item_origins[find_conf("prefix_command")->number], context);
+
+       reformat(&s, "read_only = %s", bool_to_string(conf->read_only));
+       printer(s, conf->item_origins[find_conf("read_only")->number], context);
+
+       reformat(&s, "read_only_direct = %s", bool_to_string(conf->read_only_direct));
+       printer(s, conf->item_origins[find_conf("read_only_direct")->number],
+               context);
+
+       reformat(&s, "recache = %s", bool_to_string(conf->recache));
+       printer(s, conf->item_origins[find_conf("recache")->number], context);
+
+       reformat(&s, "run_second_cpp = %s", bool_to_string(conf->run_second_cpp));
+       printer(s, conf->item_origins[find_conf("run_second_cpp")->number], context);
+
+       reformat(&s, "sloppiness = ");
+       if (conf->sloppiness & SLOPPY_FILE_MACRO) {
+               reformat(&s, "%sfile_macro, ", s);
+       }
+       if (conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME) {
+               reformat(&s, "%sinclude_file_mtime, ", s);
+       }
+       if (conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME) {
+               reformat(&s, "%sinclude_file_ctime, ", s);
+       }
+       if (conf->sloppiness & SLOPPY_TIME_MACROS) {
+               reformat(&s, "%stime_macros, ", s);
+       }
+       if (conf->sloppiness & SLOPPY_FILE_STAT_MATCHES) {
+               reformat(&s, "%sfile_stat_matches, ", s);
+       }
+       if (conf->sloppiness) {
+               /* Strip last ", ". */
+               s[strlen(s) - 2] = '\0';
+       }
+       printer(s, conf->item_origins[find_conf("sloppiness")->number], context);
+
+       reformat(&s, "stats = %s", bool_to_string(conf->stats));
+       printer(s, conf->item_origins[find_conf("stats")->number], context);
+
+       reformat(&s, "temporary_dir = %s", conf->temporary_dir);
+       printer(s, conf->item_origins[find_conf("temporary_dir")->number], context);
+
+       if (conf->umask == UINT_MAX) {
+               reformat(&s, "umask = ");
+       } else {
+               reformat(&s, "umask = %03o", conf->umask);
+       }
+       printer(s, conf->item_origins[find_conf("umask")->number], context);
+
+       reformat(&s, "unify = %s", bool_to_string(conf->unify));
+       printer(s, conf->item_origins[find_conf("unify")->number], context);
+
+       free(s);
+       return true;
+}
diff --git a/conf.h b/conf.h
new file mode 100644 (file)
index 0000000..0e30028
--- /dev/null
+++ b/conf.h
@@ -0,0 +1,49 @@
+#ifndef CONF_H
+#define CONF_H
+
+#include "system.h"
+
+struct conf {
+       char *base_dir;
+       char *cache_dir;
+       unsigned cache_dir_levels;
+       char *compiler;
+       char *compiler_check;
+       bool compression;
+       unsigned compression_level;
+       char *cpp_extension;
+       bool direct_mode;
+       bool disable;
+       char *extra_files_to_hash;
+       bool hard_link;
+       bool hash_dir;
+       char *log_file;
+       unsigned max_files;
+       uint64_t max_size;
+       char *path;
+       char *prefix_command;
+       bool read_only;
+       bool read_only_direct;
+       bool recache;
+       bool run_second_cpp;
+       unsigned sloppiness;
+       bool stats;
+       char *temporary_dir;
+       unsigned umask;
+       bool unify;
+
+       const char **item_origins;
+};
+
+struct conf *conf_create(void);
+void conf_free(struct conf *conf);
+bool conf_read(struct conf *conf, const char *path, char **errmsg);
+bool conf_update_from_environment(struct conf *conf, char **errmsg);
+bool conf_set_value_in_file(const char *path, const char *key,
+                            const char *value, char **errmsg);
+bool conf_print_items(struct conf *conf,
+                      void (*printer)(const char *descr, const char *origin,
+                                      void *context),
+                      void *context);
+
+#endif
diff --git a/config.guess b/config.guess
new file mode 100755 (executable)
index 0000000..ad5f74a
--- /dev/null
@@ -0,0 +1,1534 @@
+#! /bin/sh
+# Attempt to guess a canonical system name.
+#   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+#   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
+#   2011, 2012 Free Software Foundation, Inc.
+
+timestamp='2012-07-31'
+
+# This file is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <http://www.gnu.org/licenses/>.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+
+# Originally written by Per Bothner.  Please send patches (context
+# diff format) to <config-patches@gnu.org> and include a ChangeLog
+# entry.
+#
+# This script attempts to guess a canonical system name similar to
+# config.sub.  If it succeeds, it prints the system name on stdout, and
+# exits with 0.  Otherwise, it exits with 1.
+#
+# You can get the latest version of this script from:
+# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD
+
+me=`echo "$0" | sed -e 's,.*/,,'`
+
+usage="\
+Usage: $0 [OPTION]
+
+Output the configuration name of the system \`$me' is run on.
+
+Operation modes:
+  -h, --help         print this help, then exit
+  -t, --time-stamp   print date of last modification, then exit
+  -v, --version      print version number, then exit
+
+Report bugs and patches to <config-patches@gnu.org>."
+
+version="\
+GNU config.guess ($timestamp)
+
+Originally written by Per Bothner.
+Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
+Free Software Foundation, Inc.
+
+This is free software; see the source for copying conditions.  There is NO
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
+
+help="
+Try \`$me --help' for more information."
+
+# Parse command line
+while test $# -gt 0 ; do
+  case $1 in
+    --time-stamp | --time* | -t )
+       echo "$timestamp" ; exit ;;
+    --version | -v )
+       echo "$version" ; exit ;;
+    --help | --h* | -h )
+       echo "$usage"; exit ;;
+    -- )     # Stop option processing
+       shift; break ;;
+    - )        # Use stdin as input.
+       break ;;
+    -* )
+       echo "$me: invalid option $1$help" >&2
+       exit 1 ;;
+    * )
+       break ;;
+  esac
+done
+
+if test $# != 0; then
+  echo "$me: too many arguments$help" >&2
+  exit 1
+fi
+
+trap 'exit 1' 1 2 15
+
+# CC_FOR_BUILD -- compiler used by this script. Note that the use of a
+# compiler to aid in system detection is discouraged as it requires
+# temporary files to be created and, as you can see below, it is a
+# headache to deal with in a portable fashion.
+
+# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still
+# use `HOST_CC' if defined, but it is deprecated.
+
+# Portable tmp directory creation inspired by the Autoconf team.
+
+set_cc_for_build='
+trap "exitcode=\$?; (rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null) && exit \$exitcode" 0 ;
+trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" 1 2 13 15 ;
+: ${TMPDIR=/tmp} ;
+ { tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } ||
+ { test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } ||
+ { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo "Warning: creating insecure temp directory" >&2 ; } ||
+ { echo "$me: cannot create a temporary directory in $TMPDIR" >&2 ; exit 1 ; } ;
+dummy=$tmp/dummy ;
+tmpfiles="$dummy.c $dummy.o $dummy.rel $dummy" ;
+case $CC_FOR_BUILD,$HOST_CC,$CC in
+ ,,)    echo "int x;" > $dummy.c ;
+       for c in cc gcc c89 c99 ; do
+         if ($c -c -o $dummy.o $dummy.c) >/dev/null 2>&1 ; then
+            CC_FOR_BUILD="$c"; break ;
+         fi ;
+       done ;
+       if test x"$CC_FOR_BUILD" = x ; then
+         CC_FOR_BUILD=no_compiler_found ;
+       fi
+       ;;
+ ,,*)   CC_FOR_BUILD=$CC ;;
+ ,*,*)  CC_FOR_BUILD=$HOST_CC ;;
+esac ; set_cc_for_build= ;'
+
+# This is needed to find uname on a Pyramid OSx when run in the BSD universe.
+# (ghazi@noc.rutgers.edu 1994-08-24)
+if (test -f /.attbin/uname) >/dev/null 2>&1 ; then
+       PATH=$PATH:/.attbin ; export PATH
+fi
+
+UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
+UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
+UNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown
+UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown
+
+# Note: order is significant - the case branches are not exclusive.
+
+case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
+    *:NetBSD:*:*)
+       # NetBSD (nbsd) targets should (where applicable) match one or
+       # more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*,
+       # *-*-netbsdecoff* and *-*-netbsd*.  For targets that recently
+       # switched to ELF, *-*-netbsd* would select the old
+       # object file format.  This provides both forward
+       # compatibility and a consistent mechanism for selecting the
+       # object file format.
+       #
+       # Note: NetBSD doesn't particularly care about the vendor
+       # portion of the name.  We always set it to "unknown".
+       sysctl="sysctl -n hw.machine_arch"
+       UNAME_MACHINE_ARCH=`(/sbin/$sysctl 2>/dev/null || \
+           /usr/sbin/$sysctl 2>/dev/null || echo unknown)`
+       case "${UNAME_MACHINE_ARCH}" in
+           armeb) machine=armeb-unknown ;;
+           arm*) machine=arm-unknown ;;
+           sh3el) machine=shl-unknown ;;
+           sh3eb) machine=sh-unknown ;;
+           sh5el) machine=sh5le-unknown ;;
+           *) machine=${UNAME_MACHINE_ARCH}-unknown ;;
+       esac
+       # The Operating System including object format, if it has switched
+       # to ELF recently, or will in the future.
+       case "${UNAME_MACHINE_ARCH}" in
+           arm*|i386|m68k|ns32k|sh3*|sparc|vax)
+               eval $set_cc_for_build
+               if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \
+                       | grep -q __ELF__
+               then
+                   # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout).
+                   # Return netbsd for either.  FIX?
+                   os=netbsd
+               else
+                   os=netbsdelf
+               fi
+               ;;
+           *)
+               os=netbsd
+               ;;
+       esac
+       # The OS release
+       # Debian GNU/NetBSD machines have a different userland, and
+       # thus, need a distinct triplet. However, they do not need
+       # kernel version information, so it can be replaced with a
+       # suitable tag, in the style of linux-gnu.
+       case "${UNAME_VERSION}" in
+           Debian*)
+               release='-gnu'
+               ;;
+           *)
+               release=`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'`
+               ;;
+       esac
+       # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM:
+       # contains redundant information, the shorter form:
+       # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used.
+       echo "${machine}-${os}${release}"
+       exit ;;
+    *:Bitrig:*:*)
+       UNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'`
+       echo ${UNAME_MACHINE_ARCH}-unknown-bitrig${UNAME_RELEASE}
+       exit ;;
+    *:OpenBSD:*:*)
+       UNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'`
+       echo ${UNAME_MACHINE_ARCH}-unknown-openbsd${UNAME_RELEASE}
+       exit ;;
+    *:ekkoBSD:*:*)
+       echo ${UNAME_MACHINE}-unknown-ekkobsd${UNAME_RELEASE}
+       exit ;;
+    *:SolidBSD:*:*)
+       echo ${UNAME_MACHINE}-unknown-solidbsd${UNAME_RELEASE}
+       exit ;;
+    macppc:MirBSD:*:*)
+       echo powerpc-unknown-mirbsd${UNAME_RELEASE}
+       exit ;;
+    *:MirBSD:*:*)
+       echo ${UNAME_MACHINE}-unknown-mirbsd${UNAME_RELEASE}
+       exit ;;
+    alpha:OSF1:*:*)
+       case $UNAME_RELEASE in
+       *4.0)
+               UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'`
+               ;;
+       *5.*)
+               UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'`
+               ;;
+       esac
+       # According to Compaq, /usr/sbin/psrinfo has been available on
+       # OSF/1 and Tru64 systems produced since 1995.  I hope that
+       # covers most systems running today.  This code pipes the CPU
+       # types through head -n 1, so we only detect the type of CPU 0.
+       ALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^  The alpha \(.*\) processor.*$/\1/p' | head -n 1`
+       case "$ALPHA_CPU_TYPE" in
+           "EV4 (21064)")
+               UNAME_MACHINE="alpha" ;;
+           "EV4.5 (21064)")
+               UNAME_MACHINE="alpha" ;;
+           "LCA4 (21066/21068)")
+               UNAME_MACHINE="alpha" ;;
+           "EV5 (21164)")
+               UNAME_MACHINE="alphaev5" ;;
+           "EV5.6 (21164A)")
+               UNAME_MACHINE="alphaev56" ;;
+           "EV5.6 (21164PC)")
+               UNAME_MACHINE="alphapca56" ;;
+           "EV5.7 (21164PC)")
+               UNAME_MACHINE="alphapca57" ;;
+           "EV6 (21264)")
+               UNAME_MACHINE="alphaev6" ;;
+           "EV6.7 (21264A)")
+               UNAME_MACHINE="alphaev67" ;;
+           "EV6.8CB (21264C)")
+               UNAME_MACHINE="alphaev68" ;;
+           "EV6.8AL (21264B)")
+               UNAME_MACHINE="alphaev68" ;;
+           "EV6.8CX (21264D)")
+               UNAME_MACHINE="alphaev68" ;;
+           "EV6.9A (21264/EV69A)")
+               UNAME_MACHINE="alphaev69" ;;
+           "EV7 (21364)")
+               UNAME_MACHINE="alphaev7" ;;
+           "EV7.9 (21364A)")
+               UNAME_MACHINE="alphaev79" ;;
+       esac
+       # A Pn.n version is a patched version.
+       # A Vn.n version is a released version.
+       # A Tn.n version is a released field test version.
+       # A Xn.n version is an unreleased experimental baselevel.
+       # 1.2 uses "1.2" for uname -r.
+       echo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[PVTX]//' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
+       # Reset EXIT trap before exiting to avoid spurious non-zero exit code.
+       exitcode=$?
+       trap '' 0
+       exit $exitcode ;;
+    Alpha\ *:Windows_NT*:*)
+       # How do we know it's Interix rather than the generic POSIX subsystem?
+       # Should we change UNAME_MACHINE based on the output of uname instead
+       # of the specific Alpha model?
+       echo alpha-pc-interix
+       exit ;;
+    21064:Windows_NT:50:3)
+       echo alpha-dec-winnt3.5
+       exit ;;
+    Amiga*:UNIX_System_V:4.0:*)
+       echo m68k-unknown-sysv4
+       exit ;;
+    *:[Aa]miga[Oo][Ss]:*:*)
+       echo ${UNAME_MACHINE}-unknown-amigaos
+       exit ;;
+    *:[Mm]orph[Oo][Ss]:*:*)
+       echo ${UNAME_MACHINE}-unknown-morphos
+       exit ;;
+    *:OS/390:*:*)
+       echo i370-ibm-openedition
+       exit ;;
+    *:z/VM:*:*)
+       echo s390-ibm-zvmoe
+       exit ;;
+    *:OS400:*:*)
+       echo powerpc-ibm-os400
+       exit ;;
+    arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*)
+       echo arm-acorn-riscix${UNAME_RELEASE}
+       exit ;;
+    arm:riscos:*:*|arm:RISCOS:*:*)
+       echo arm-unknown-riscos
+       exit ;;
+    SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*)
+       echo hppa1.1-hitachi-hiuxmpp
+       exit ;;
+    Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*)
+       # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE.
+       if test "`(/bin/universe) 2>/dev/null`" = att ; then
+               echo pyramid-pyramid-sysv3
+       else
+               echo pyramid-pyramid-bsd
+       fi
+       exit ;;
+    NILE*:*:*:dcosx)
+       echo pyramid-pyramid-svr4
+       exit ;;
+    DRS?6000:unix:4.0:6*)
+       echo sparc-icl-nx6
+       exit ;;
+    DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*)
+       case `/usr/bin/uname -p` in
+           sparc) echo sparc-icl-nx7; exit ;;
+       esac ;;
+    s390x:SunOS:*:*)
+       echo ${UNAME_MACHINE}-ibm-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    sun4H:SunOS:5.*:*)
+       echo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*)
+       echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*)
+       echo i386-pc-auroraux${UNAME_RELEASE}
+       exit ;;
+    i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*)
+       eval $set_cc_for_build
+       SUN_ARCH="i386"
+       # If there is a compiler, see if it is configured for 64-bit objects.
+       # Note that the Sun cc does not turn __LP64__ into 1 like gcc does.
+       # This test works for both compilers.
+       if [ "$CC_FOR_BUILD" != 'no_compiler_found' ]; then
+           if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \
+               (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | \
+               grep IS_64BIT_ARCH >/dev/null
+           then
+               SUN_ARCH="x86_64"
+           fi
+       fi
+       echo ${SUN_ARCH}-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    sun4*:SunOS:6*:*)
+       # According to config.sub, this is the proper way to canonicalize
+       # SunOS6.  Hard to guess exactly what SunOS6 will be like, but
+       # it's likely to be more like Solaris than SunOS4.
+       echo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    sun4*:SunOS:*:*)
+       case "`/usr/bin/arch -k`" in
+           Series*|S4*)
+               UNAME_RELEASE=`uname -v`
+               ;;
+       esac
+       # Japanese Language versions have a version number like `4.1.3-JL'.
+       echo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'`
+       exit ;;
+    sun3*:SunOS:*:*)
+       echo m68k-sun-sunos${UNAME_RELEASE}
+       exit ;;
+    sun*:*:4.2BSD:*)
+       UNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null`
+       test "x${UNAME_RELEASE}" = "x" && UNAME_RELEASE=3
+       case "`/bin/arch`" in
+           sun3)
+               echo m68k-sun-sunos${UNAME_RELEASE}
+               ;;
+           sun4)
+               echo sparc-sun-sunos${UNAME_RELEASE}
+               ;;
+       esac
+       exit ;;
+    aushp:SunOS:*:*)
+       echo sparc-auspex-sunos${UNAME_RELEASE}
+       exit ;;
+    # The situation for MiNT is a little confusing.  The machine name
+    # can be virtually everything (everything which is not
+    # "atarist" or "atariste" at least should have a processor
+    # > m68000).  The system name ranges from "MiNT" over "FreeMiNT"
+    # to the lowercase version "mint" (or "freemint").  Finally
+    # the system name "TOS" denotes a system which is actually not
+    # MiNT.  But MiNT is downward compatible to TOS, so this should
+    # be no problem.
+    atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*)
+       echo m68k-atari-mint${UNAME_RELEASE}
+       exit ;;
+    atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*)
+       echo m68k-atari-mint${UNAME_RELEASE}
+       exit ;;
+    *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*)
+       echo m68k-atari-mint${UNAME_RELEASE}
+       exit ;;
+    milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*)
+       echo m68k-milan-mint${UNAME_RELEASE}
+       exit ;;
+    hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*)
+       echo m68k-hades-mint${UNAME_RELEASE}
+       exit ;;
+    *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*)
+       echo m68k-unknown-mint${UNAME_RELEASE}
+       exit ;;
+    m68k:machten:*:*)
+       echo m68k-apple-machten${UNAME_RELEASE}
+       exit ;;
+    powerpc:machten:*:*)
+       echo powerpc-apple-machten${UNAME_RELEASE}
+       exit ;;
+    RISC*:Mach:*:*)
+       echo mips-dec-mach_bsd4.3
+       exit ;;
+    RISC*:ULTRIX:*:*)
+       echo mips-dec-ultrix${UNAME_RELEASE}
+       exit ;;
+    VAX*:ULTRIX*:*:*)
+       echo vax-dec-ultrix${UNAME_RELEASE}
+       exit ;;
+    2020:CLIX:*:* | 2430:CLIX:*:*)
+       echo clipper-intergraph-clix${UNAME_RELEASE}
+       exit ;;
+    mips:*:*:UMIPS | mips:*:*:RISCos)
+       eval $set_cc_for_build
+       sed 's/^        //' << EOF >$dummy.c
+#ifdef __cplusplus
+#include <stdio.h>  /* for printf() prototype */
+       int main (int argc, char *argv[]) {
+#else
+       int main (argc, argv) int argc; char *argv[]; {
+#endif
+       #if defined (host_mips) && defined (MIPSEB)
+       #if defined (SYSTYPE_SYSV)
+         printf ("mips-mips-riscos%ssysv\n", argv[1]); exit (0);
+       #endif
+       #if defined (SYSTYPE_SVR4)
+         printf ("mips-mips-riscos%ssvr4\n", argv[1]); exit (0);
+       #endif
+       #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD)
+         printf ("mips-mips-riscos%sbsd\n", argv[1]); exit (0);
+       #endif
+       #endif
+         exit (-1);
+       }
+EOF
+       $CC_FOR_BUILD -o $dummy $dummy.c &&
+         dummyarg=`echo "${UNAME_RELEASE}" | sed -n 's/\([0-9]*\).*/\1/p'` &&
+         SYSTEM_NAME=`$dummy $dummyarg` &&
+           { echo "$SYSTEM_NAME"; exit; }
+       echo mips-mips-riscos${UNAME_RELEASE}
+       exit ;;
+    Motorola:PowerMAX_OS:*:*)
+       echo powerpc-motorola-powermax
+       exit ;;
+    Motorola:*:4.3:PL8-*)
+       echo powerpc-harris-powermax
+       exit ;;
+    Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*)
+       echo powerpc-harris-powermax
+       exit ;;
+    Night_Hawk:Power_UNIX:*:*)
+       echo powerpc-harris-powerunix
+       exit ;;
+    m88k:CX/UX:7*:*)
+       echo m88k-harris-cxux7
+       exit ;;
+    m88k:*:4*:R4*)
+       echo m88k-motorola-sysv4
+       exit ;;
+    m88k:*:3*:R3*)
+       echo m88k-motorola-sysv3
+       exit ;;
+    AViiON:dgux:*:*)
+       # DG/UX returns AViiON for all architectures
+       UNAME_PROCESSOR=`/usr/bin/uname -p`
+       if [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ]
+       then
+           if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \
+              [ ${TARGET_BINARY_INTERFACE}x = x ]
+           then
+               echo m88k-dg-dgux${UNAME_RELEASE}
+           else
+               echo m88k-dg-dguxbcs${UNAME_RELEASE}
+           fi
+       else
+           echo i586-dg-dgux${UNAME_RELEASE}
+       fi
+       exit ;;
+    M88*:DolphinOS:*:*)        # DolphinOS (SVR3)
+       echo m88k-dolphin-sysv3
+       exit ;;
+    M88*:*:R3*:*)
+       # Delta 88k system running SVR3
+       echo m88k-motorola-sysv3
+       exit ;;
+    XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3)
+       echo m88k-tektronix-sysv3
+       exit ;;
+    Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD)
+       echo m68k-tektronix-bsd
+       exit ;;
+    *:IRIX*:*:*)
+       echo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'`
+       exit ;;
+    ????????:AIX?:[12].1:2)   # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX.
+       echo romp-ibm-aix     # uname -m gives an 8 hex-code CPU id
+       exit ;;               # Note that: echo "'`uname -s`'" gives 'AIX '
+    i*86:AIX:*:*)
+       echo i386-ibm-aix
+       exit ;;
+    ia64:AIX:*:*)
+       if [ -x /usr/bin/oslevel ] ; then
+               IBM_REV=`/usr/bin/oslevel`
+       else
+               IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}
+       fi
+       echo ${UNAME_MACHINE}-ibm-aix${IBM_REV}
+       exit ;;
+    *:AIX:2:3)
+       if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then
+               eval $set_cc_for_build
+               sed 's/^                //' << EOF >$dummy.c
+               #include <sys/systemcfg.h>
+
+               main()
+                       {
+                       if (!__power_pc())
+                               exit(1);
+                       puts("powerpc-ibm-aix3.2.5");
+                       exit(0);
+                       }
+EOF
+               if $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy`
+               then
+                       echo "$SYSTEM_NAME"
+               else
+                       echo rs6000-ibm-aix3.2.5
+               fi
+       elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then
+               echo rs6000-ibm-aix3.2.4
+       else
+               echo rs6000-ibm-aix3.2
+       fi
+       exit ;;
+    *:AIX:*:[4567])
+       IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'`
+       if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then
+               IBM_ARCH=rs6000
+       else
+               IBM_ARCH=powerpc
+       fi
+       if [ -x /usr/bin/oslevel ] ; then
+               IBM_REV=`/usr/bin/oslevel`
+       else
+               IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}
+       fi
+       echo ${IBM_ARCH}-ibm-aix${IBM_REV}
+       exit ;;
+    *:AIX:*:*)
+       echo rs6000-ibm-aix
+       exit ;;
+    ibmrt:4.4BSD:*|romp-ibm:BSD:*)
+       echo romp-ibm-bsd4.4
+       exit ;;
+    ibmrt:*BSD:*|romp-ibm:BSD:*)            # covers RT/PC BSD and
+       echo romp-ibm-bsd${UNAME_RELEASE}   # 4.3 with uname added to
+       exit ;;                             # report: romp-ibm BSD 4.3
+    *:BOSX:*:*)
+       echo rs6000-bull-bosx
+       exit ;;
+    DPX/2?00:B.O.S.:*:*)
+       echo m68k-bull-sysv3
+       exit ;;
+    9000/[34]??:4.3bsd:1.*:*)
+       echo m68k-hp-bsd
+       exit ;;
+    hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*)
+       echo m68k-hp-bsd4.4
+       exit ;;
+    9000/[34678]??:HP-UX:*:*)
+       HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`
+       case "${UNAME_MACHINE}" in
+           9000/31? )            HP_ARCH=m68000 ;;
+           9000/[34]?? )         HP_ARCH=m68k ;;
+           9000/[678][0-9][0-9])
+               if [ -x /usr/bin/getconf ]; then
+                   sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null`
+                   sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null`
+                   case "${sc_cpu_version}" in
+                     523) HP_ARCH="hppa1.0" ;; # CPU_PA_RISC1_0
+                     528) HP_ARCH="hppa1.1" ;; # CPU_PA_RISC1_1
+                     532)                      # CPU_PA_RISC2_0
+                       case "${sc_kernel_bits}" in
+                         32) HP_ARCH="hppa2.0n" ;;
+                         64) HP_ARCH="hppa2.0w" ;;
+                         '') HP_ARCH="hppa2.0" ;;   # HP-UX 10.20
+                       esac ;;
+                   esac
+               fi
+               if [ "${HP_ARCH}" = "" ]; then
+                   eval $set_cc_for_build
+                   sed 's/^            //' << EOF >$dummy.c
+
+               #define _HPUX_SOURCE
+               #include <stdlib.h>
+               #include <unistd.h>
+
+               int main ()
+               {
+               #if defined(_SC_KERNEL_BITS)
+                   long bits = sysconf(_SC_KERNEL_BITS);
+               #endif
+                   long cpu  = sysconf (_SC_CPU_VERSION);
+
+                   switch (cpu)
+                       {
+                       case CPU_PA_RISC1_0: puts ("hppa1.0"); break;
+                       case CPU_PA_RISC1_1: puts ("hppa1.1"); break;
+                       case CPU_PA_RISC2_0:
+               #if defined(_SC_KERNEL_BITS)
+                           switch (bits)
+                               {
+                               case 64: puts ("hppa2.0w"); break;
+                               case 32: puts ("hppa2.0n"); break;
+                               default: puts ("hppa2.0"); break;
+                               } break;
+               #else  /* !defined(_SC_KERNEL_BITS) */
+                           puts ("hppa2.0"); break;
+               #endif
+                       default: puts ("hppa1.0"); break;
+                       }
+                   exit (0);
+               }
+EOF
+                   (CCOPTS= $CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null) && HP_ARCH=`$dummy`
+                   test -z "$HP_ARCH" && HP_ARCH=hppa
+               fi ;;
+       esac
+       if [ ${HP_ARCH} = "hppa2.0w" ]
+       then
+           eval $set_cc_for_build
+
+           # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating
+           # 32-bit code.  hppa64-hp-hpux* has the same kernel and a compiler
+           # generating 64-bit code.  GNU and HP use different nomenclature:
+           #
+           # $ CC_FOR_BUILD=cc ./config.guess
+           # => hppa2.0w-hp-hpux11.23
+           # $ CC_FOR_BUILD="cc +DA2.0w" ./config.guess
+           # => hppa64-hp-hpux11.23
+
+           if echo __LP64__ | (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) |
+               grep -q __LP64__
+           then
+               HP_ARCH="hppa2.0w"
+           else
+               HP_ARCH="hppa64"
+           fi
+       fi
+       echo ${HP_ARCH}-hp-hpux${HPUX_REV}
+       exit ;;
+    ia64:HP-UX:*:*)
+       HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`
+       echo ia64-hp-hpux${HPUX_REV}
+       exit ;;
+    3050*:HI-UX:*:*)
+       eval $set_cc_for_build
+       sed 's/^        //' << EOF >$dummy.c
+       #include <unistd.h>
+       int
+       main ()
+       {
+         long cpu = sysconf (_SC_CPU_VERSION);
+         /* The order matters, because CPU_IS_HP_MC68K erroneously returns
+            true for CPU_PA_RISC1_0.  CPU_IS_PA_RISC returns correct
+            results, however.  */
+         if (CPU_IS_PA_RISC (cpu))
+           {
+             switch (cpu)
+               {
+                 case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break;
+                 case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break;
+                 case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break;
+                 default: puts ("hppa-hitachi-hiuxwe2"); break;
+               }
+           }
+         else if (CPU_IS_HP_MC68K (cpu))
+           puts ("m68k-hitachi-hiuxwe2");
+         else puts ("unknown-hitachi-hiuxwe2");
+         exit (0);
+       }
+EOF
+       $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` &&
+               { echo "$SYSTEM_NAME"; exit; }
+       echo unknown-hitachi-hiuxwe2
+       exit ;;
+    9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* )
+       echo hppa1.1-hp-bsd
+       exit ;;
+    9000/8??:4.3bsd:*:*)
+       echo hppa1.0-hp-bsd
+       exit ;;
+    *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*)
+       echo hppa1.0-hp-mpeix
+       exit ;;
+    hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* )
+       echo hppa1.1-hp-osf
+       exit ;;
+    hp8??:OSF1:*:*)
+       echo hppa1.0-hp-osf
+       exit ;;
+    i*86:OSF1:*:*)
+       if [ -x /usr/sbin/sysversion ] ; then
+           echo ${UNAME_MACHINE}-unknown-osf1mk
+       else
+           echo ${UNAME_MACHINE}-unknown-osf1
+       fi
+       exit ;;
+    parisc*:Lites*:*:*)
+       echo hppa1.1-hp-lites
+       exit ;;
+    C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*)
+       echo c1-convex-bsd
+       exit ;;
+    C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*)
+       if getsysinfo -f scalar_acc
+       then echo c32-convex-bsd
+       else echo c2-convex-bsd
+       fi
+       exit ;;
+    C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*)
+       echo c34-convex-bsd
+       exit ;;
+    C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*)
+       echo c38-convex-bsd
+       exit ;;
+    C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*)
+       echo c4-convex-bsd
+       exit ;;
+    CRAY*Y-MP:*:*:*)
+       echo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/'
+       exit ;;
+    CRAY*[A-Z]90:*:*:*)
+       echo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \
+       | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \
+             -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \
+             -e 's/\.[^.]*$/.X/'
+       exit ;;
+    CRAY*TS:*:*:*)
+       echo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/'
+       exit ;;
+    CRAY*T3E:*:*:*)
+       echo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/'
+       exit ;;
+    CRAY*SV1:*:*:*)
+       echo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/'
+       exit ;;
+    *:UNICOS/mp:*:*)
+       echo craynv-cray-unicosmp${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/'
+       exit ;;
+    F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*)
+       FUJITSU_PROC=`uname -m | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
+       FUJITSU_SYS=`uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\///'`
+       FUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'`
+       echo "${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}"
+       exit ;;
+    5000:UNIX_System_V:4.*:*)
+       FUJITSU_SYS=`uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\///'`
+       FUJITSU_REL=`echo ${UNAME_RELEASE} | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/ /_/'`
+       echo "sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}"
+       exit ;;
+    i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*)
+       echo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE}
+       exit ;;
+    sparc*:BSD/OS:*:*)
+       echo sparc-unknown-bsdi${UNAME_RELEASE}
+       exit ;;
+    *:BSD/OS:*:*)
+       echo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE}
+       exit ;;
+    *:FreeBSD:*:*)
+       UNAME_PROCESSOR=`/usr/bin/uname -p`
+       case ${UNAME_PROCESSOR} in
+           amd64)
+               echo x86_64-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;
+           *)
+               echo ${UNAME_PROCESSOR}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;
+       esac
+       exit ;;
+    i*:CYGWIN*:*)
+       echo ${UNAME_MACHINE}-pc-cygwin
+       exit ;;
+    *:MINGW64*:*)
+       echo ${UNAME_MACHINE}-pc-mingw64
+       exit ;;
+    *:MINGW*:*)
+       echo ${UNAME_MACHINE}-pc-mingw32
+       exit ;;
+    i*:MSYS*:*)
+       echo ${UNAME_MACHINE}-pc-msys
+       exit ;;
+    i*:windows32*:*)
+       # uname -m includes "-pc" on this system.
+       echo ${UNAME_MACHINE}-mingw32
+       exit ;;
+    i*:PW*:*)
+       echo ${UNAME_MACHINE}-pc-pw32
+       exit ;;
+    *:Interix*:*)
+       case ${UNAME_MACHINE} in
+           x86)
+               echo i586-pc-interix${UNAME_RELEASE}
+               exit ;;
+           authenticamd | genuineintel | EM64T)
+               echo x86_64-unknown-interix${UNAME_RELEASE}
+               exit ;;
+           IA64)
+               echo ia64-unknown-interix${UNAME_RELEASE}
+               exit ;;
+       esac ;;
+    [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*)
+       echo i${UNAME_MACHINE}-pc-mks
+       exit ;;
+    8664:Windows_NT:*)
+       echo x86_64-pc-mks
+       exit ;;
+    i*:Windows_NT*:* | Pentium*:Windows_NT*:*)
+       # How do we know it's Interix rather than the generic POSIX subsystem?
+       # It also conflicts with pre-2.0 versions of AT&T UWIN. Should we
+       # UNAME_MACHINE based on the output of uname instead of i386?
+       echo i586-pc-interix
+       exit ;;
+    i*:UWIN*:*)
+       echo ${UNAME_MACHINE}-pc-uwin
+       exit ;;
+    amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*)
+       echo x86_64-unknown-cygwin
+       exit ;;
+    p*:CYGWIN*:*)
+       echo powerpcle-unknown-cygwin
+       exit ;;
+    prep*:SunOS:5.*:*)
+       echo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+       exit ;;
+    *:GNU:*:*)
+       # the GNU system
+       echo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-gnu`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'`
+       exit ;;
+    *:GNU/*:*:*)
+       # other systems with GNU libc and userland
+       echo ${UNAME_MACHINE}-unknown-`echo ${UNAME_SYSTEM} | sed 's,^[^/]*/,,' | tr '[A-Z]' '[a-z]'``echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`-gnu
+       exit ;;
+    i*86:Minix:*:*)
+       echo ${UNAME_MACHINE}-pc-minix
+       exit ;;
+    aarch64:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    aarch64_be:Linux:*:*)
+       UNAME_MACHINE=aarch64_be
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    alpha:Linux:*:*)
+       case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in
+         EV5)   UNAME_MACHINE=alphaev5 ;;
+         EV56)  UNAME_MACHINE=alphaev56 ;;
+         PCA56) UNAME_MACHINE=alphapca56 ;;
+         PCA57) UNAME_MACHINE=alphapca56 ;;
+         EV6)   UNAME_MACHINE=alphaev6 ;;
+         EV67)  UNAME_MACHINE=alphaev67 ;;
+         EV68*) UNAME_MACHINE=alphaev68 ;;
+       esac
+       objdump --private-headers /bin/sh | grep -q ld.so.1
+       if test "$?" = 0 ; then LIBC="libc1" ; else LIBC="" ; fi
+       echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC}
+       exit ;;
+    arm*:Linux:*:*)
+       eval $set_cc_for_build
+       if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \
+           | grep -q __ARM_EABI__
+       then
+           echo ${UNAME_MACHINE}-unknown-linux-gnu
+       else
+           if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \
+               | grep -q __ARM_PCS_VFP
+           then
+               echo ${UNAME_MACHINE}-unknown-linux-gnueabi
+           else
+               echo ${UNAME_MACHINE}-unknown-linux-gnueabihf
+           fi
+       fi
+       exit ;;
+    avr32*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    cris:Linux:*:*)
+       echo ${UNAME_MACHINE}-axis-linux-gnu
+       exit ;;
+    crisv32:Linux:*:*)
+       echo ${UNAME_MACHINE}-axis-linux-gnu
+       exit ;;
+    frv:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    hexagon:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    i*86:Linux:*:*)
+       LIBC=gnu
+       eval $set_cc_for_build
+       sed 's/^        //' << EOF >$dummy.c
+       #ifdef __dietlibc__
+       LIBC=dietlibc
+       #endif
+EOF
+       eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC'`
+       echo "${UNAME_MACHINE}-pc-linux-${LIBC}"
+       exit ;;
+    ia64:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    m32r*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    m68*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    mips:Linux:*:* | mips64:Linux:*:*)
+       eval $set_cc_for_build
+       sed 's/^        //' << EOF >$dummy.c
+       #undef CPU
+       #undef ${UNAME_MACHINE}
+       #undef ${UNAME_MACHINE}el
+       #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)
+       CPU=${UNAME_MACHINE}el
+       #else
+       #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)
+       CPU=${UNAME_MACHINE}
+       #else
+       CPU=
+       #endif
+       #endif
+EOF
+       eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^CPU'`
+       test x"${CPU}" != x && { echo "${CPU}-unknown-linux-gnu"; exit; }
+       ;;
+    or32:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    padre:Linux:*:*)
+       echo sparc-unknown-linux-gnu
+       exit ;;
+    parisc64:Linux:*:* | hppa64:Linux:*:*)
+       echo hppa64-unknown-linux-gnu
+       exit ;;
+    parisc:Linux:*:* | hppa:Linux:*:*)
+       # Look for CPU level
+       case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in
+         PA7*) echo hppa1.1-unknown-linux-gnu ;;
+         PA8*) echo hppa2.0-unknown-linux-gnu ;;
+         *)    echo hppa-unknown-linux-gnu ;;
+       esac
+       exit ;;
+    ppc64:Linux:*:*)
+       echo powerpc64-unknown-linux-gnu
+       exit ;;
+    ppc:Linux:*:*)
+       echo powerpc-unknown-linux-gnu
+       exit ;;
+    s390:Linux:*:* | s390x:Linux:*:*)
+       echo ${UNAME_MACHINE}-ibm-linux
+       exit ;;
+    sh64*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    sh*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    sparc:Linux:*:* | sparc64:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    tile*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    vax:Linux:*:*)
+       echo ${UNAME_MACHINE}-dec-linux-gnu
+       exit ;;
+    x86_64:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    xtensa*:Linux:*:*)
+       echo ${UNAME_MACHINE}-unknown-linux-gnu
+       exit ;;
+    i*86:DYNIX/ptx:4*:*)
+       # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there.
+       # earlier versions are messed up and put the nodename in both
+       # sysname and nodename.
+       echo i386-sequent-sysv4
+       exit ;;
+    i*86:UNIX_SV:4.2MP:2.*)
+       # Unixware is an offshoot of SVR4, but it has its own version
+       # number series starting with 2...
+       # I am not positive that other SVR4 systems won't match this,
+       # I just have to hope.  -- rms.
+       # Use sysv4.2uw... so that sysv4* matches it.
+       echo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION}
+       exit ;;
+    i*86:OS/2:*:*)
+       # If we were able to find `uname', then EMX Unix compatibility
+       # is probably installed.
+       echo ${UNAME_MACHINE}-pc-os2-emx
+       exit ;;
+    i*86:XTS-300:*:STOP)
+       echo ${UNAME_MACHINE}-unknown-stop
+       exit ;;
+    i*86:atheos:*:*)
+       echo ${UNAME_MACHINE}-unknown-atheos
+       exit ;;
+    i*86:syllable:*:*)
+       echo ${UNAME_MACHINE}-pc-syllable
+       exit ;;
+    i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*)
+       echo i386-unknown-lynxos${UNAME_RELEASE}
+       exit ;;
+    i*86:*DOS:*:*)
+       echo ${UNAME_MACHINE}-pc-msdosdjgpp
+       exit ;;
+    i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*)
+       UNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\/MP$//'`
+       if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then
+               echo ${UNAME_MACHINE}-univel-sysv${UNAME_REL}
+       else
+               echo ${UNAME_MACHINE}-pc-sysv${UNAME_REL}
+       fi
+       exit ;;
+    i*86:*:5:[678]*)
+       # UnixWare 7.x, OpenUNIX and OpenServer 6.
+       case `/bin/uname -X | grep "^Machine"` in
+           *486*)           UNAME_MACHINE=i486 ;;
+           *Pentium)        UNAME_MACHINE=i586 ;;
+           *Pent*|*Celeron) UNAME_MACHINE=i686 ;;
+       esac
+       echo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION}
+       exit ;;
+    i*86:*:3.2:*)
+       if test -f /usr/options/cb.name; then
+               UNAME_REL=`sed -n 's/.*Version //p' </usr/options/cb.name`
+               echo ${UNAME_MACHINE}-pc-isc$UNAME_REL
+       elif /bin/uname -X 2>/dev/null >/dev/null ; then
+               UNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')`
+               (/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486
+               (/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \
+                       && UNAME_MACHINE=i586
+               (/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \
+                       && UNAME_MACHINE=i686
+               (/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \
+                       && UNAME_MACHINE=i686
+               echo ${UNAME_MACHINE}-pc-sco$UNAME_REL
+       else
+               echo ${UNAME_MACHINE}-pc-sysv32
+       fi
+       exit ;;
+    pc:*:*:*)
+       # Left here for compatibility:
+       # uname -m prints for DJGPP always 'pc', but it prints nothing about
+       # the processor, so we play safe by assuming i586.
+       # Note: whatever this is, it MUST be the same as what config.sub
+       # prints for the "djgpp" host, or else GDB configury will decide that
+       # this is a cross-build.
+       echo i586-pc-msdosdjgpp
+       exit ;;
+    Intel:Mach:3*:*)
+       echo i386-pc-mach3
+       exit ;;
+    paragon:*:*:*)
+       echo i860-intel-osf1
+       exit ;;
+    i860:*:4.*:*) # i860-SVR4
+       if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then
+         echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4
+       else # Add other i860-SVR4 vendors below as they are discovered.
+         echo i860-unknown-sysv${UNAME_RELEASE}  # Unknown i860-SVR4
+       fi
+       exit ;;
+    mini*:CTIX:SYS*5:*)
+       # "miniframe"
+       echo m68010-convergent-sysv
+       exit ;;
+    mc68k:UNIX:SYSTEM5:3.51m)
+       echo m68k-convergent-sysv
+       exit ;;
+    M680?0:D-NIX:5.3:*)
+       echo m68k-diab-dnix
+       exit ;;
+    M68*:*:R3V[5678]*:*)
+       test -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;;
+    3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0)
+       OS_REL=''
+       test -r /etc/.relid \
+       && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid`
+       /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+         && { echo i486-ncr-sysv4.3${OS_REL}; exit; }
+       /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \
+         && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;
+    3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*)
+       /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+         && { echo i486-ncr-sysv4; exit; } ;;
+    NCR*:*:4.2:* | MPRAS*:*:4.2:*)
+       OS_REL='.3'
+       test -r /etc/.relid \
+           && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid`
+       /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+           && { echo i486-ncr-sysv4.3${OS_REL}; exit; }
+       /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \
+           && { echo i586-ncr-sysv4.3${OS_REL}; exit; }
+       /bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \
+           && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;
+    m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*)
+       echo m68k-unknown-lynxos${UNAME_RELEASE}
+       exit ;;
+    mc68030:UNIX_System_V:4.*:*)
+       echo m68k-atari-sysv4
+       exit ;;
+    TSUNAMI:LynxOS:2.*:*)
+       echo sparc-unknown-lynxos${UNAME_RELEASE}
+       exit ;;
+    rs6000:LynxOS:2.*:*)
+       echo rs6000-unknown-lynxos${UNAME_RELEASE}
+       exit ;;
+    PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*)
+       echo powerpc-unknown-lynxos${UNAME_RELEASE}
+       exit ;;
+    SM[BE]S:UNIX_SV:*:*)
+       echo mips-dde-sysv${UNAME_RELEASE}
+       exit ;;
+    RM*:ReliantUNIX-*:*:*)
+       echo mips-sni-sysv4
+       exit ;;
+    RM*:SINIX-*:*:*)
+       echo mips-sni-sysv4
+       exit ;;
+    *:SINIX-*:*:*)
+       if uname -p 2>/dev/null >/dev/null ; then
+               UNAME_MACHINE=`(uname -p) 2>/dev/null`
+               echo ${UNAME_MACHINE}-sni-sysv4
+       else
+               echo ns32k-sni-sysv
+       fi
+       exit ;;
+    PENTIUM:*:4.0*:*)  # Unisys `ClearPath HMP IX 4000' SVR4/MP effort
+                       # says <Richard.M.Bartel@ccMail.Census.GOV>
+       echo i586-unisys-sysv4
+       exit ;;
+    *:UNIX_System_V:4*:FTX*)
+       # From Gerald Hewes <hewes@openmarket.com>.
+       # How about differentiating between stratus architectures? -djm
+       echo hppa1.1-stratus-sysv4
+       exit ;;
+    *:*:*:FTX*)
+       # From seanf@swdc.stratus.com.
+       echo i860-stratus-sysv4
+       exit ;;
+    i*86:VOS:*:*)
+       # From Paul.Green@stratus.com.
+       echo ${UNAME_MACHINE}-stratus-vos
+       exit ;;
+    *:VOS:*:*)
+       # From Paul.Green@stratus.com.
+       echo hppa1.1-stratus-vos
+       exit ;;
+    mc68*:A/UX:*:*)
+       echo m68k-apple-aux${UNAME_RELEASE}
+       exit ;;
+    news*:NEWS-OS:6*:*)
+       echo mips-sony-newsos6
+       exit ;;
+    R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*)
+       if [ -d /usr/nec ]; then
+               echo mips-nec-sysv${UNAME_RELEASE}
+       else
+               echo mips-unknown-sysv${UNAME_RELEASE}
+       fi
+       exit ;;
+    BeBox:BeOS:*:*)    # BeOS running on hardware made by Be, PPC only.
+       echo powerpc-be-beos
+       exit ;;
+    BeMac:BeOS:*:*)    # BeOS running on Mac or Mac clone, PPC only.
+       echo powerpc-apple-beos
+       exit ;;
+    BePC:BeOS:*:*)     # BeOS running on Intel PC compatible.
+       echo i586-pc-beos
+       exit ;;
+    BePC:Haiku:*:*)    # Haiku running on Intel PC compatible.
+       echo i586-pc-haiku
+       exit ;;
+    SX-4:SUPER-UX:*:*)
+       echo sx4-nec-superux${UNAME_RELEASE}
+       exit ;;
+    SX-5:SUPER-UX:*:*)
+       echo sx5-nec-superux${UNAME_RELEASE}
+       exit ;;
+    SX-6:SUPER-UX:*:*)
+       echo sx6-nec-superux${UNAME_RELEASE}
+       exit ;;
+    SX-7:SUPER-UX:*:*)
+       echo sx7-nec-superux${UNAME_RELEASE}
+       exit ;;
+    SX-8:SUPER-UX:*:*)
+       echo sx8-nec-superux${UNAME_RELEASE}
+       exit ;;
+    SX-8R:SUPER-UX:*:*)
+       echo sx8r-nec-superux${UNAME_RELEASE}
+       exit ;;
+    Power*:Rhapsody:*:*)
+       echo powerpc-apple-rhapsody${UNAME_RELEASE}
+       exit ;;
+    *:Rhapsody:*:*)
+       echo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE}
+       exit ;;
+    *:Darwin:*:*)
+       UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown
+       case $UNAME_PROCESSOR in
+           i386)
+               eval $set_cc_for_build
+               if [ "$CC_FOR_BUILD" != 'no_compiler_found' ]; then
+                 if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \
+                     (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | \
+                     grep IS_64BIT_ARCH >/dev/null
+                 then
+                     UNAME_PROCESSOR="x86_64"
+                 fi
+               fi ;;
+           unknown) UNAME_PROCESSOR=powerpc ;;
+       esac
+       echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}
+       exit ;;
+    *:procnto*:*:* | *:QNX:[0123456789]*:*)
+       UNAME_PROCESSOR=`uname -p`
+       if test "$UNAME_PROCESSOR" = "x86"; then
+               UNAME_PROCESSOR=i386
+               UNAME_MACHINE=pc
+       fi
+       echo ${UNAME_PROCESSOR}-${UNAME_MACHINE}-nto-qnx${UNAME_RELEASE}
+       exit ;;
+    *:QNX:*:4*)
+       echo i386-pc-qnx
+       exit ;;
+    NEO-?:NONSTOP_KERNEL:*:*)
+       echo neo-tandem-nsk${UNAME_RELEASE}
+       exit ;;
+    NSE-*:NONSTOP_KERNEL:*:*)
+       echo nse-tandem-nsk${UNAME_RELEASE}
+       exit ;;
+    NSR-?:NONSTOP_KERNEL:*:*)
+       echo nsr-tandem-nsk${UNAME_RELEASE}
+       exit ;;
+    *:NonStop-UX:*:*)
+       echo mips-compaq-nonstopux
+       exit ;;
+    BS2000:POSIX*:*:*)
+       echo bs2000-siemens-sysv
+       exit ;;
+    DS/*:UNIX_System_V:*:*)
+       echo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE}
+       exit ;;
+    *:Plan9:*:*)
+       # "uname -m" is not consistent, so use $cputype instead. 386
+       # is converted to i386 for consistency with other x86
+       # operating systems.
+       if test "$cputype" = "386"; then
+           UNAME_MACHINE=i386
+       else
+           UNAME_MACHINE="$cputype"
+       fi
+       echo ${UNAME_MACHINE}-unknown-plan9
+       exit ;;
+    *:TOPS-10:*:*)
+       echo pdp10-unknown-tops10
+       exit ;;
+    *:TENEX:*:*)
+       echo pdp10-unknown-tenex
+       exit ;;
+    KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*)
+       echo pdp10-dec-tops20
+       exit ;;
+    XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*)
+       echo pdp10-xkl-tops20
+       exit ;;
+    *:TOPS-20:*:*)
+       echo pdp10-unknown-tops20
+       exit ;;
+    *:ITS:*:*)
+       echo pdp10-unknown-its
+       exit ;;
+    SEI:*:*:SEIUX)
+       echo mips-sei-seiux${UNAME_RELEASE}
+       exit ;;
+    *:DragonFly:*:*)
+       echo ${UNAME_MACHINE}-unknown-dragonfly`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`
+       exit ;;
+    *:*VMS:*:*)
+       UNAME_MACHINE=`(uname -p) 2>/dev/null`
+       case "${UNAME_MACHINE}" in
+           A*) echo alpha-dec-vms ; exit ;;
+           I*) echo ia64-dec-vms ; exit ;;
+           V*) echo vax-dec-vms ; exit ;;
+       esac ;;
+    *:XENIX:*:SysV)
+       echo i386-pc-xenix
+       exit ;;
+    i*86:skyos:*:*)
+       echo ${UNAME_MACHINE}-pc-skyos`echo ${UNAME_RELEASE}` | sed -e 's/ .*$//'
+       exit ;;
+    i*86:rdos:*:*)
+       echo ${UNAME_MACHINE}-pc-rdos
+       exit ;;
+    i*86:AROS:*:*)
+       echo ${UNAME_MACHINE}-pc-aros
+       exit ;;
+    x86_64:VMkernel:*:*)
+       echo ${UNAME_MACHINE}-unknown-esx
+       exit ;;
+esac
+
+eval $set_cc_for_build
+cat >$dummy.c <<EOF
+#ifdef _SEQUENT_
+# include <sys/types.h>
+# include <sys/utsname.h>
+#endif
+main ()
+{
+#if defined (sony)
+#if defined (MIPSEB)
+  /* BFD wants "bsd" instead of "newsos".  Perhaps BFD should be changed,
+     I don't know....  */
+  printf ("mips-sony-bsd\n"); exit (0);
+#else
+#include <sys/param.h>
+  printf ("m68k-sony-newsos%s\n",
+#ifdef NEWSOS4
+       "4"
+#else
+       ""
+#endif
+       ); exit (0);
+#endif
+#endif
+
+#if defined (__arm) && defined (__acorn) && defined (__unix)
+  printf ("arm-acorn-riscix\n"); exit (0);
+#endif
+
+#if defined (hp300) && !defined (hpux)
+  printf ("m68k-hp-bsd\n"); exit (0);
+#endif
+
+#if defined (NeXT)
+#if !defined (__ARCHITECTURE__)
+#define __ARCHITECTURE__ "m68k"
+#endif
+  int version;
+  version=`(hostinfo | sed -n 's/.*NeXT Mach \([0-9]*\).*/\1/p') 2>/dev/null`;
+  if (version < 4)
+    printf ("%s-next-nextstep%d\n", __ARCHITECTURE__, version);
+  else
+    printf ("%s-next-openstep%d\n", __ARCHITECTURE__, version);
+  exit (0);
+#endif
+
+#if defined (MULTIMAX) || defined (n16)
+#if defined (UMAXV)
+  printf ("ns32k-encore-sysv\n"); exit (0);
+#else
+#if defined (CMU)
+  printf ("ns32k-encore-mach\n"); exit (0);
+#else
+  printf ("ns32k-encore-bsd\n"); exit (0);
+#endif
+#endif
+#endif
+
+#if defined (__386BSD__)
+  printf ("i386-pc-bsd\n"); exit (0);
+#endif
+
+#if defined (sequent)
+#if defined (i386)
+  printf ("i386-sequent-dynix\n"); exit (0);
+#endif
+#if defined (ns32000)
+  printf ("ns32k-sequent-dynix\n"); exit (0);
+#endif
+#endif
+
+#if defined (_SEQUENT_)
+    struct utsname un;
+
+    uname(&un);
+
+    if (strncmp(un.version, "V2", 2) == 0) {
+       printf ("i386-sequent-ptx2\n"); exit (0);
+    }
+    if (strncmp(un.version, "V1", 2) == 0) { /* XXX is V1 correct? */
+       printf ("i386-sequent-ptx1\n"); exit (0);
+    }
+    printf ("i386-sequent-ptx\n"); exit (0);
+
+#endif
+
+#if defined (vax)
+# if !defined (ultrix)
+#  include <sys/param.h>
+#  if defined (BSD)
+#   if BSD == 43
+      printf ("vax-dec-bsd4.3\n"); exit (0);
+#   else
+#    if BSD == 199006
+      printf ("vax-dec-bsd4.3reno\n"); exit (0);
+#    else
+      printf ("vax-dec-bsd\n"); exit (0);
+#    endif
+#   endif
+#  else
+    printf ("vax-dec-bsd\n"); exit (0);
+#  endif
+# else
+    printf ("vax-dec-ultrix\n"); exit (0);
+# endif
+#endif
+
+#if defined (alliant) && defined (i860)
+  printf ("i860-alliant-bsd\n"); exit (0);
+#endif
+
+  exit (1);
+}
+EOF
+
+$CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null && SYSTEM_NAME=`$dummy` &&
+       { echo "$SYSTEM_NAME"; exit; }
+
+# Apollos put the system type in the environment.
+
+test -d /usr/apollo && { echo ${ISP}-apollo-${SYSTYPE}; exit; }
+
+# Convex versions that predate uname can use getsysinfo(1)
+
+if [ -x /usr/convex/getsysinfo ]
+then
+    case `getsysinfo -f cpu_type` in
+    c1*)
+       echo c1-convex-bsd
+       exit ;;
+    c2*)
+       if getsysinfo -f scalar_acc
+       then echo c32-convex-bsd
+       else echo c2-convex-bsd
+       fi
+       exit ;;
+    c34*)
+       echo c34-convex-bsd
+       exit ;;
+    c38*)
+       echo c38-convex-bsd
+       exit ;;
+    c4*)
+       echo c4-convex-bsd
+       exit ;;
+    esac
+fi
+
+cat >&2 <<EOF
+$0: unable to guess system type
+
+This script, last modified $timestamp, has failed to recognize
+the operating system you are using. It is advised that you
+download the most up to date version of the config scripts from
+
+  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD
+and
+  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD
+
+If the version you run ($0) is already up to date, please
+send the following data and any information you think might be
+pertinent to <config-patches@gnu.org> in order to provide the needed
+information to handle your system.
+
+config.guess timestamp = $timestamp
+
+uname -m = `(uname -m) 2>/dev/null || echo unknown`
+uname -r = `(uname -r) 2>/dev/null || echo unknown`
+uname -s = `(uname -s) 2>/dev/null || echo unknown`
+uname -v = `(uname -v) 2>/dev/null || echo unknown`
+
+/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null`
+/bin/uname -X     = `(/bin/uname -X) 2>/dev/null`
+
+hostinfo               = `(hostinfo) 2>/dev/null`
+/bin/universe          = `(/bin/universe) 2>/dev/null`
+/usr/bin/arch -k       = `(/usr/bin/arch -k) 2>/dev/null`
+/bin/arch              = `(/bin/arch) 2>/dev/null`
+/usr/bin/oslevel       = `(/usr/bin/oslevel) 2>/dev/null`
+/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null`
+
+UNAME_MACHINE = ${UNAME_MACHINE}
+UNAME_RELEASE = ${UNAME_RELEASE}
+UNAME_SYSTEM  = ${UNAME_SYSTEM}
+UNAME_VERSION = ${UNAME_VERSION}
+EOF
+
+exit 1
+
+# Local variables:
+# eval: (add-hook 'write-file-hooks 'time-stamp)
+# time-stamp-start: "timestamp='"
+# time-stamp-format: "%:y-%02m-%02d"
+# time-stamp-end: "'"
+# End:
index 83c1e2acdb5e57b52e5acf15fd187cc76027f5f8..ae090081fcb5ee01e4ee0ffcfc6a83635819e47f 100644 (file)
@@ -1,5 +1,8 @@
 /* config.h.in.  Generated from configure.ac by autoheader.  */
 
+/* Define if building universal (internal helper macro) */
+#undef AC_APPLE_UNIVERSAL_BUILD
+
 /* Define to 1 if you have the `asprintf' function. */
 #undef HAVE_ASPRINTF
 
@@ -40,6 +43,9 @@
 /* Define to 1 if you have the <locale.h> header file. */
 #undef HAVE_LOCALE_H
 
+/* Define to 1 if the system has the type `long long'. */
+#undef HAVE_LONG_LONG
+
 /* Define to 1 if the system has the type `long long int'. */
 #undef HAVE_LONG_LONG_INT
 
@@ -88,6 +94,9 @@
 /* Define to 1 if you have the `strndup' function. */
 #undef HAVE_STRNDUP
 
+/* Define to 1 if you have the `strtok_r' function. */
+#undef HAVE_STRTOK_R
+
 /* Define to 1 if `decimal_point' is a member of `struct lconv'. */
 #undef HAVE_STRUCT_LCONV_DECIMAL_POINT
 
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
+/* Define to 1 if you have the `unsetenv' function. */
+#undef HAVE_UNSETENV
+
 /* Define to 1 if the system has the type `unsigned long long int'. */
 #undef HAVE_UNSIGNED_LONG_LONG_INT
 
 /* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
 #undef TIME_WITH_SYS_TIME
 
+/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
+   significant byte first (like Motorola and SPARC, unlike Intel). */
+#if defined AC_APPLE_UNIVERSAL_BUILD
+# if defined __BIG_ENDIAN__
+#  define WORDS_BIGENDIAN 1
+# endif
+#else
+# ifndef WORDS_BIGENDIAN
+#  undef WORDS_BIGENDIAN
+# endif
+#endif
+
 /* Define on OpenBSD to activate all library features */
 #undef _BSD_SOURCE
 
 /* Define to activate features from IEEE Stds 1003.1-2001 */
 #undef _POSIX_C_SOURCE
 
+/* Windows Vista or newer is required */
+#undef _WIN32_WINNT
+
 /* Define to the level of X/Open that your system supports */
 #undef _XOPEN_SOURCE
 
diff --git a/config.sub b/config.sub
new file mode 100755 (executable)
index 0000000..b15df57
--- /dev/null
@@ -0,0 +1,1786 @@
+#! /bin/sh
+# Configuration validation subroutine script.
+#   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+#   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
+#   2011, 2012 Free Software Foundation, Inc.
+
+timestamp='2012-07-31'
+
+# This file is (in principle) common to ALL GNU software.
+# The presence of a machine in this file suggests that SOME GNU software
+# can handle that machine.  It does not imply ALL GNU software can.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <http://www.gnu.org/licenses/>.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+
+# Please send patches to <config-patches@gnu.org>.  Submit a context
+# diff and a properly formatted GNU ChangeLog entry.
+#
+# Configuration subroutine to validate and canonicalize a configuration type.
+# Supply the specified configuration type as an argument.
+# If it is invalid, we print an error message on stderr and exit with code 1.
+# Otherwise, we print the canonical config type on stdout and succeed.
+
+# You can get the latest version of this script from:
+# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD
+
+# This file is supposed to be the same for all GNU packages
+# and recognize all the CPU types, system types and aliases
+# that are meaningful with *any* GNU software.
+# Each package is responsible for reporting which valid configurations
+# it does not support.  The user should be able to distinguish
+# a failure to support a valid configuration from a meaningless
+# configuration.
+
+# The goal of this file is to map all the various variations of a given
+# machine specification into a single specification in the form:
+#      CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM
+# or in some cases, the newer four-part form:
+#      CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM
+# It is wrong to echo any other type of specification.
+
+me=`echo "$0" | sed -e 's,.*/,,'`
+
+usage="\
+Usage: $0 [OPTION] CPU-MFR-OPSYS
+       $0 [OPTION] ALIAS
+
+Canonicalize a configuration name.
+
+Operation modes:
+  -h, --help         print this help, then exit
+  -t, --time-stamp   print date of last modification, then exit
+  -v, --version      print version number, then exit
+
+Report bugs and patches to <config-patches@gnu.org>."
+
+version="\
+GNU config.sub ($timestamp)
+
+Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
+Free Software Foundation, Inc.
+
+This is free software; see the source for copying conditions.  There is NO
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
+
+help="
+Try \`$me --help' for more information."
+
+# Parse command line
+while test $# -gt 0 ; do
+  case $1 in
+    --time-stamp | --time* | -t )
+       echo "$timestamp" ; exit ;;
+    --version | -v )
+       echo "$version" ; exit ;;
+    --help | --h* | -h )
+       echo "$usage"; exit ;;
+    -- )     # Stop option processing
+       shift; break ;;
+    - )        # Use stdin as input.
+       break ;;
+    -* )
+       echo "$me: invalid option $1$help"
+       exit 1 ;;
+
+    *local*)
+       # First pass through any local machine types.
+       echo $1
+       exit ;;
+
+    * )
+       break ;;
+  esac
+done
+
+case $# in
+ 0) echo "$me: missing argument$help" >&2
+    exit 1;;
+ 1) ;;
+ *) echo "$me: too many arguments$help" >&2
+    exit 1;;
+esac
+
+# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any).
+# Here we must recognize all the valid KERNEL-OS combinations.
+maybe_os=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'`
+case $maybe_os in
+  nto-qnx* | linux-gnu* | linux-android* | linux-dietlibc | linux-newlib* | \
+  linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \
+  knetbsd*-gnu* | netbsd*-gnu* | \
+  kopensolaris*-gnu* | \
+  storm-chaos* | os2-emx* | rtmk-nova*)
+    os=-$maybe_os
+    basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`
+    ;;
+  android-linux)
+    os=-linux-android
+    basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`-unknown
+    ;;
+  *)
+    basic_machine=`echo $1 | sed 's/-[^-]*$//'`
+    if [ $basic_machine != $1 ]
+    then os=`echo $1 | sed 's/.*-/-/'`
+    else os=; fi
+    ;;
+esac
+
+### Let's recognize common machines as not being operating systems so
+### that things like config.sub decstation-3100 work.  We also
+### recognize some manufacturers as not being operating systems, so we
+### can provide default operating systems below.
+case $os in
+       -sun*os*)
+               # Prevent following clause from handling this invalid input.
+               ;;
+       -dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \
+       -att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \
+       -unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \
+       -convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\
+       -c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \
+       -harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \
+       -apple | -axis | -knuth | -cray | -microblaze)
+               os=
+               basic_machine=$1
+               ;;
+       -bluegene*)
+               os=-cnk
+               ;;
+       -sim | -cisco | -oki | -wec | -winbond)
+               os=
+               basic_machine=$1
+               ;;
+       -scout)
+               ;;
+       -wrs)
+               os=-vxworks
+               basic_machine=$1
+               ;;
+       -chorusos*)
+               os=-chorusos
+               basic_machine=$1
+               ;;
+       -chorusrdb)
+               os=-chorusrdb
+               basic_machine=$1
+               ;;
+       -hiux*)
+               os=-hiuxwe2
+               ;;
+       -sco6)
+               os=-sco5v6
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco5)
+               os=-sco3.2v5
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco4)
+               os=-sco3.2v4
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco3.2.[4-9]*)
+               os=`echo $os | sed -e 's/sco3.2./sco3.2v/'`
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco3.2v[4-9]*)
+               # Don't forget version if it is 3.2v4 or newer.
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco5v6*)
+               # Don't forget version if it is 3.2v4 or newer.
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -sco*)
+               os=-sco3.2v2
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -udk*)
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -isc)
+               os=-isc2.2
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -clix*)
+               basic_machine=clipper-intergraph
+               ;;
+       -isc*)
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`
+               ;;
+       -lynx*178)
+               os=-lynxos178
+               ;;
+       -lynx*5)
+               os=-lynxos5
+               ;;
+       -lynx*)
+               os=-lynxos
+               ;;
+       -ptx*)
+               basic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'`
+               ;;
+       -windowsnt*)
+               os=`echo $os | sed -e 's/windowsnt/winnt/'`
+               ;;
+       -psos*)
+               os=-psos
+               ;;
+       -mint | -mint[0-9]*)
+               basic_machine=m68k-atari
+               os=-mint
+               ;;
+esac
+
+# Decode aliases for certain CPU-COMPANY combinations.
+case $basic_machine in
+       # Recognize the basic CPU types without company name.
+       # Some are omitted here because they have special meanings below.
+       1750a | 580 \
+       | a29k \
+       | aarch64 | aarch64_be \
+       | alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \
+       | alpha64 | alpha64ev[4-8] | alpha64ev56 | alpha64ev6[78] | alpha64pca5[67] \
+       | am33_2.0 \
+       | arc | arm | arm[bl]e | arme[lb] | armv[2345] | armv[345][lb] | avr | avr32 \
+        | be32 | be64 \
+       | bfin \
+       | c4x | clipper \
+       | d10v | d30v | dlx | dsp16xx \
+       | epiphany \
+       | fido | fr30 | frv \
+       | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \
+       | hexagon \
+       | i370 | i860 | i960 | ia64 \
+       | ip2k | iq2000 \
+       | le32 | le64 \
+       | lm32 \
+       | m32c | m32r | m32rle | m68000 | m68k | m88k \
+       | maxq | mb | microblaze | mcore | mep | metag \
+       | mips | mipsbe | mipseb | mipsel | mipsle \
+       | mips16 \
+       | mips64 | mips64el \
+       | mips64octeon | mips64octeonel \
+       | mips64orion | mips64orionel \
+       | mips64r5900 | mips64r5900el \
+       | mips64vr | mips64vrel \
+       | mips64vr4100 | mips64vr4100el \
+       | mips64vr4300 | mips64vr4300el \
+       | mips64vr5000 | mips64vr5000el \
+       | mips64vr5900 | mips64vr5900el \
+       | mipsisa32 | mipsisa32el \
+       | mipsisa32r2 | mipsisa32r2el \
+       | mipsisa64 | mipsisa64el \
+       | mipsisa64r2 | mipsisa64r2el \
+       | mipsisa64sb1 | mipsisa64sb1el \
+       | mipsisa64sr71k | mipsisa64sr71kel \
+       | mipstx39 | mipstx39el \
+       | mn10200 | mn10300 \
+       | moxie \
+       | mt \
+       | msp430 \
+       | nds32 | nds32le | nds32be \
+       | nios | nios2 \
+       | ns16k | ns32k \
+       | open8 \
+       | or32 \
+       | pdp10 | pdp11 | pj | pjl \
+       | powerpc | powerpc64 | powerpc64le | powerpcle \
+       | pyramid \
+       | rl78 | rx \
+       | score \
+       | sh | sh[1234] | sh[24]a | sh[24]aeb | sh[23]e | sh[34]eb | sheb | shbe | shle | sh[1234]le | sh3ele \
+       | sh64 | sh64le \
+       | sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \
+       | sparcv8 | sparcv9 | sparcv9b | sparcv9v \
+       | spu \
+       | tahoe | tic4x | tic54x | tic55x | tic6x | tic80 | tron \
+       | ubicom32 \
+       | v850 | v850e | v850e1 | v850e2 | v850es | v850e2v3 \
+       | we32k \
+       | x86 | xc16x | xstormy16 | xtensa \
+       | z8k | z80)
+               basic_machine=$basic_machine-unknown
+               ;;
+       c54x)
+               basic_machine=tic54x-unknown
+               ;;
+       c55x)
+               basic_machine=tic55x-unknown
+               ;;
+       c6x)
+               basic_machine=tic6x-unknown
+               ;;
+       m6811 | m68hc11 | m6812 | m68hc12 | m68hcs12x | picochip)
+               basic_machine=$basic_machine-unknown
+               os=-none
+               ;;
+       m88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65 | z8k)
+               ;;
+       ms1)
+               basic_machine=mt-unknown
+               ;;
+
+       strongarm | thumb | xscale)
+               basic_machine=arm-unknown
+               ;;
+       xgate)
+               basic_machine=$basic_machine-unknown
+               os=-none
+               ;;
+       xscaleeb)
+               basic_machine=armeb-unknown
+               ;;
+
+       xscaleel)
+               basic_machine=armel-unknown
+               ;;
+
+       # We use `pc' rather than `unknown'
+       # because (1) that's what they normally are, and
+       # (2) the word "unknown" tends to confuse beginning users.
+       i*86 | x86_64)
+         basic_machine=$basic_machine-pc
+         ;;
+       # Object if more than one company name word.
+       *-*-*)
+               echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
+               exit 1
+               ;;
+       # Recognize the basic CPU types with company name.
+       580-* \
+       | a29k-* \
+       | aarch64-* | aarch64_be-* \
+       | alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \
+       | alpha64-* | alpha64ev[4-8]-* | alpha64ev56-* | alpha64ev6[78]-* \
+       | alphapca5[67]-* | alpha64pca5[67]-* | arc-* \
+       | arm-*  | armbe-* | armle-* | armeb-* | armv*-* \
+       | avr-* | avr32-* \
+       | be32-* | be64-* \
+       | bfin-* | bs2000-* \
+       | c[123]* | c30-* | [cjt]90-* | c4x-* \
+       | clipper-* | craynv-* | cydra-* \
+       | d10v-* | d30v-* | dlx-* \
+       | elxsi-* \
+       | f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \
+       | h8300-* | h8500-* \
+       | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \
+       | hexagon-* \
+       | i*86-* | i860-* | i960-* | ia64-* \
+       | ip2k-* | iq2000-* \
+       | le32-* | le64-* \
+       | lm32-* \
+       | m32c-* | m32r-* | m32rle-* \
+       | m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \
+       | m88110-* | m88k-* | maxq-* | mcore-* | metag-* | microblaze-* \
+       | mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \
+       | mips16-* \
+       | mips64-* | mips64el-* \
+       | mips64octeon-* | mips64octeonel-* \
+       | mips64orion-* | mips64orionel-* \
+       | mips64r5900-* | mips64r5900el-* \
+       | mips64vr-* | mips64vrel-* \
+       | mips64vr4100-* | mips64vr4100el-* \
+       | mips64vr4300-* | mips64vr4300el-* \
+       | mips64vr5000-* | mips64vr5000el-* \
+       | mips64vr5900-* | mips64vr5900el-* \
+       | mipsisa32-* | mipsisa32el-* \
+       | mipsisa32r2-* | mipsisa32r2el-* \
+       | mipsisa64-* | mipsisa64el-* \
+       | mipsisa64r2-* | mipsisa64r2el-* \
+       | mipsisa64sb1-* | mipsisa64sb1el-* \
+       | mipsisa64sr71k-* | mipsisa64sr71kel-* \
+       | mipstx39-* | mipstx39el-* \
+       | mmix-* \
+       | mt-* \
+       | msp430-* \
+       | nds32-* | nds32le-* | nds32be-* \
+       | nios-* | nios2-* \
+       | none-* | np1-* | ns16k-* | ns32k-* \
+       | open8-* \
+       | orion-* \
+       | pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \
+       | powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* \
+       | pyramid-* \
+       | rl78-* | romp-* | rs6000-* | rx-* \
+       | sh-* | sh[1234]-* | sh[24]a-* | sh[24]aeb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \
+       | shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \
+       | sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \
+       | sparclite-* \
+       | sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | sv1-* | sx?-* \
+       | tahoe-* \
+       | tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \
+       | tile*-* \
+       | tron-* \
+       | ubicom32-* \
+       | v850-* | v850e-* | v850e1-* | v850es-* | v850e2-* | v850e2v3-* \
+       | vax-* \
+       | we32k-* \
+       | x86-* | x86_64-* | xc16x-* | xps100-* \
+       | xstormy16-* | xtensa*-* \
+       | ymp-* \
+       | z8k-* | z80-*)
+               ;;
+       # Recognize the basic CPU types without company name, with glob match.
+       xtensa*)
+               basic_machine=$basic_machine-unknown
+               ;;
+       # Recognize the various machine names and aliases which stand
+       # for a CPU type and a company and sometimes even an OS.
+       386bsd)
+               basic_machine=i386-unknown
+               os=-bsd
+               ;;
+       3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc)
+               basic_machine=m68000-att
+               ;;
+       3b*)
+               basic_machine=we32k-att
+               ;;
+       a29khif)
+               basic_machine=a29k-amd
+               os=-udi
+               ;;
+       abacus)
+               basic_machine=abacus-unknown
+               ;;
+       adobe68k)
+               basic_machine=m68010-adobe
+               os=-scout
+               ;;
+       alliant | fx80)
+               basic_machine=fx80-alliant
+               ;;
+       altos | altos3068)
+               basic_machine=m68k-altos
+               ;;
+       am29k)
+               basic_machine=a29k-none
+               os=-bsd
+               ;;
+       amd64)
+               basic_machine=x86_64-pc
+               ;;
+       amd64-*)
+               basic_machine=x86_64-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       amdahl)
+               basic_machine=580-amdahl
+               os=-sysv
+               ;;
+       amiga | amiga-*)
+               basic_machine=m68k-unknown
+               ;;
+       amigaos | amigados)
+               basic_machine=m68k-unknown
+               os=-amigaos
+               ;;
+       amigaunix | amix)
+               basic_machine=m68k-unknown
+               os=-sysv4
+               ;;
+       apollo68)
+               basic_machine=m68k-apollo
+               os=-sysv
+               ;;
+       apollo68bsd)
+               basic_machine=m68k-apollo
+               os=-bsd
+               ;;
+       aros)
+               basic_machine=i386-pc
+               os=-aros
+               ;;
+       aux)
+               basic_machine=m68k-apple
+               os=-aux
+               ;;
+       balance)
+               basic_machine=ns32k-sequent
+               os=-dynix
+               ;;
+       blackfin)
+               basic_machine=bfin-unknown
+               os=-linux
+               ;;
+       blackfin-*)
+               basic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'`
+               os=-linux
+               ;;
+       bluegene*)
+               basic_machine=powerpc-ibm
+               os=-cnk
+               ;;
+       c54x-*)
+               basic_machine=tic54x-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       c55x-*)
+               basic_machine=tic55x-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       c6x-*)
+               basic_machine=tic6x-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       c90)
+               basic_machine=c90-cray
+               os=-unicos
+               ;;
+       cegcc)
+               basic_machine=arm-unknown
+               os=-cegcc
+               ;;
+       convex-c1)
+               basic_machine=c1-convex
+               os=-bsd
+               ;;
+       convex-c2)
+               basic_machine=c2-convex
+               os=-bsd
+               ;;
+       convex-c32)
+               basic_machine=c32-convex
+               os=-bsd
+               ;;
+       convex-c34)
+               basic_machine=c34-convex
+               os=-bsd
+               ;;
+       convex-c38)
+               basic_machine=c38-convex
+               os=-bsd
+               ;;
+       cray | j90)
+               basic_machine=j90-cray
+               os=-unicos
+               ;;
+       craynv)
+               basic_machine=craynv-cray
+               os=-unicosmp
+               ;;
+       cr16 | cr16-*)
+               basic_machine=cr16-unknown
+               os=-elf
+               ;;
+       crds | unos)
+               basic_machine=m68k-crds
+               ;;
+       crisv32 | crisv32-* | etraxfs*)
+               basic_machine=crisv32-axis
+               ;;
+       cris | cris-* | etrax*)
+               basic_machine=cris-axis
+               ;;
+       crx)
+               basic_machine=crx-unknown
+               os=-elf
+               ;;
+       da30 | da30-*)
+               basic_machine=m68k-da30
+               ;;
+       decstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn)
+               basic_machine=mips-dec
+               ;;
+       decsystem10* | dec10*)
+               basic_machine=pdp10-dec
+               os=-tops10
+               ;;
+       decsystem20* | dec20*)
+               basic_machine=pdp10-dec
+               os=-tops20
+               ;;
+       delta | 3300 | motorola-3300 | motorola-delta \
+             | 3300-motorola | delta-motorola)
+               basic_machine=m68k-motorola
+               ;;
+       delta88)
+               basic_machine=m88k-motorola
+               os=-sysv3
+               ;;
+       dicos)
+               basic_machine=i686-pc
+               os=-dicos
+               ;;
+       djgpp)
+               basic_machine=i586-pc
+               os=-msdosdjgpp
+               ;;
+       dpx20 | dpx20-*)
+               basic_machine=rs6000-bull
+               os=-bosx
+               ;;
+       dpx2* | dpx2*-bull)
+               basic_machine=m68k-bull
+               os=-sysv3
+               ;;
+       ebmon29k)
+               basic_machine=a29k-amd
+               os=-ebmon
+               ;;
+       elxsi)
+               basic_machine=elxsi-elxsi
+               os=-bsd
+               ;;
+       encore | umax | mmax)
+               basic_machine=ns32k-encore
+               ;;
+       es1800 | OSE68k | ose68k | ose | OSE)
+               basic_machine=m68k-ericsson
+               os=-ose
+               ;;
+       fx2800)
+               basic_machine=i860-alliant
+               ;;
+       genix)
+               basic_machine=ns32k-ns
+               ;;
+       gmicro)
+               basic_machine=tron-gmicro
+               os=-sysv
+               ;;
+       go32)
+               basic_machine=i386-pc
+               os=-go32
+               ;;
+       h3050r* | hiux*)
+               basic_machine=hppa1.1-hitachi
+               os=-hiuxwe2
+               ;;
+       h8300hms)
+               basic_machine=h8300-hitachi
+               os=-hms
+               ;;
+       h8300xray)
+               basic_machine=h8300-hitachi
+               os=-xray
+               ;;
+       h8500hms)
+               basic_machine=h8500-hitachi
+               os=-hms
+               ;;
+       harris)
+               basic_machine=m88k-harris
+               os=-sysv3
+               ;;
+       hp300-*)
+               basic_machine=m68k-hp
+               ;;
+       hp300bsd)
+               basic_machine=m68k-hp
+               os=-bsd
+               ;;
+       hp300hpux)
+               basic_machine=m68k-hp
+               os=-hpux
+               ;;
+       hp3k9[0-9][0-9] | hp9[0-9][0-9])
+               basic_machine=hppa1.0-hp
+               ;;
+       hp9k2[0-9][0-9] | hp9k31[0-9])
+               basic_machine=m68000-hp
+               ;;
+       hp9k3[2-9][0-9])
+               basic_machine=m68k-hp
+               ;;
+       hp9k6[0-9][0-9] | hp6[0-9][0-9])
+               basic_machine=hppa1.0-hp
+               ;;
+       hp9k7[0-79][0-9] | hp7[0-79][0-9])
+               basic_machine=hppa1.1-hp
+               ;;
+       hp9k78[0-9] | hp78[0-9])
+               # FIXME: really hppa2.0-hp
+               basic_machine=hppa1.1-hp
+               ;;
+       hp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893)
+               # FIXME: really hppa2.0-hp
+               basic_machine=hppa1.1-hp
+               ;;
+       hp9k8[0-9][13679] | hp8[0-9][13679])
+               basic_machine=hppa1.1-hp
+               ;;
+       hp9k8[0-9][0-9] | hp8[0-9][0-9])
+               basic_machine=hppa1.0-hp
+               ;;
+       hppa-next)
+               os=-nextstep3
+               ;;
+       hppaosf)
+               basic_machine=hppa1.1-hp
+               os=-osf
+               ;;
+       hppro)
+               basic_machine=hppa1.1-hp
+               os=-proelf
+               ;;
+       i370-ibm* | ibm*)
+               basic_machine=i370-ibm
+               ;;
+       i*86v32)
+               basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+               os=-sysv32
+               ;;
+       i*86v4*)
+               basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+               os=-sysv4
+               ;;
+       i*86v)
+               basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+               os=-sysv
+               ;;
+       i*86sol2)
+               basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`
+               os=-solaris2
+               ;;
+       i386mach)
+               basic_machine=i386-mach
+               os=-mach
+               ;;
+       i386-vsta | vsta)
+               basic_machine=i386-unknown
+               os=-vsta
+               ;;
+       iris | iris4d)
+               basic_machine=mips-sgi
+               case $os in
+                   -irix*)
+                       ;;
+                   *)
+                       os=-irix4
+                       ;;
+               esac
+               ;;
+       isi68 | isi)
+               basic_machine=m68k-isi
+               os=-sysv
+               ;;
+       m68knommu)
+               basic_machine=m68k-unknown
+               os=-linux
+               ;;
+       m68knommu-*)
+               basic_machine=m68k-`echo $basic_machine | sed 's/^[^-]*-//'`
+               os=-linux
+               ;;
+       m88k-omron*)
+               basic_machine=m88k-omron
+               ;;
+       magnum | m3230)
+               basic_machine=mips-mips
+               os=-sysv
+               ;;
+       merlin)
+               basic_machine=ns32k-utek
+               os=-sysv
+               ;;
+       microblaze)
+               basic_machine=microblaze-xilinx
+               ;;
+       mingw64)
+               basic_machine=x86_64-pc
+               os=-mingw64
+               ;;
+       mingw32)
+               basic_machine=i386-pc
+               os=-mingw32
+               ;;
+       mingw32ce)
+               basic_machine=arm-unknown
+               os=-mingw32ce
+               ;;
+       miniframe)
+               basic_machine=m68000-convergent
+               ;;
+       *mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*)
+               basic_machine=m68k-atari
+               os=-mint
+               ;;
+       mips3*-*)
+               basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`
+               ;;
+       mips3*)
+               basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown
+               ;;
+       monitor)
+               basic_machine=m68k-rom68k
+               os=-coff
+               ;;
+       morphos)
+               basic_machine=powerpc-unknown
+               os=-morphos
+               ;;
+       msdos)
+               basic_machine=i386-pc
+               os=-msdos
+               ;;
+       ms1-*)
+               basic_machine=`echo $basic_machine | sed -e 's/ms1-/mt-/'`
+               ;;
+       msys)
+               basic_machine=i386-pc
+               os=-msys
+               ;;
+       mvs)
+               basic_machine=i370-ibm
+               os=-mvs
+               ;;
+       nacl)
+               basic_machine=le32-unknown
+               os=-nacl
+               ;;
+       ncr3000)
+               basic_machine=i486-ncr
+               os=-sysv4
+               ;;
+       netbsd386)
+               basic_machine=i386-unknown
+               os=-netbsd
+               ;;
+       netwinder)
+               basic_machine=armv4l-rebel
+               os=-linux
+               ;;
+       news | news700 | news800 | news900)
+               basic_machine=m68k-sony
+               os=-newsos
+               ;;
+       news1000)
+               basic_machine=m68030-sony
+               os=-newsos
+               ;;
+       news-3600 | risc-news)
+               basic_machine=mips-sony
+               os=-newsos
+               ;;
+       necv70)
+               basic_machine=v70-nec
+               os=-sysv
+               ;;
+       next | m*-next )
+               basic_machine=m68k-next
+               case $os in
+                   -nextstep* )
+                       ;;
+                   -ns2*)
+                     os=-nextstep2
+                       ;;
+                   *)
+                     os=-nextstep3
+                       ;;
+               esac
+               ;;
+       nh3000)
+               basic_machine=m68k-harris
+               os=-cxux
+               ;;
+       nh[45]000)
+               basic_machine=m88k-harris
+               os=-cxux
+               ;;
+       nindy960)
+               basic_machine=i960-intel
+               os=-nindy
+               ;;
+       mon960)
+               basic_machine=i960-intel
+               os=-mon960
+               ;;
+       nonstopux)
+               basic_machine=mips-compaq
+               os=-nonstopux
+               ;;
+       np1)
+               basic_machine=np1-gould
+               ;;
+       neo-tandem)
+               basic_machine=neo-tandem
+               ;;
+       nse-tandem)
+               basic_machine=nse-tandem
+               ;;
+       nsr-tandem)
+               basic_machine=nsr-tandem
+               ;;
+       op50n-* | op60c-*)
+               basic_machine=hppa1.1-oki
+               os=-proelf
+               ;;
+       openrisc | openrisc-*)
+               basic_machine=or32-unknown
+               ;;
+       os400)
+               basic_machine=powerpc-ibm
+               os=-os400
+               ;;
+       OSE68000 | ose68000)
+               basic_machine=m68000-ericsson
+               os=-ose
+               ;;
+       os68k)
+               basic_machine=m68k-none
+               os=-os68k
+               ;;
+       pa-hitachi)
+               basic_machine=hppa1.1-hitachi
+               os=-hiuxwe2
+               ;;
+       paragon)
+               basic_machine=i860-intel
+               os=-osf
+               ;;
+       parisc)
+               basic_machine=hppa-unknown
+               os=-linux
+               ;;
+       parisc-*)
+               basic_machine=hppa-`echo $basic_machine | sed 's/^[^-]*-//'`
+               os=-linux
+               ;;
+       pbd)
+               basic_machine=sparc-tti
+               ;;
+       pbb)
+               basic_machine=m68k-tti
+               ;;
+       pc532 | pc532-*)
+               basic_machine=ns32k-pc532
+               ;;
+       pc98)
+               basic_machine=i386-pc
+               ;;
+       pc98-*)
+               basic_machine=i386-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       pentium | p5 | k5 | k6 | nexgen | viac3)
+               basic_machine=i586-pc
+               ;;
+       pentiumpro | p6 | 6x86 | athlon | athlon_*)
+               basic_machine=i686-pc
+               ;;
+       pentiumii | pentium2 | pentiumiii | pentium3)
+               basic_machine=i686-pc
+               ;;
+       pentium4)
+               basic_machine=i786-pc
+               ;;
+       pentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*)
+               basic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       pentiumpro-* | p6-* | 6x86-* | athlon-*)
+               basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       pentiumii-* | pentium2-* | pentiumiii-* | pentium3-*)
+               basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       pentium4-*)
+               basic_machine=i786-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       pn)
+               basic_machine=pn-gould
+               ;;
+       power)  basic_machine=power-ibm
+               ;;
+       ppc | ppcbe)    basic_machine=powerpc-unknown
+               ;;
+       ppc-* | ppcbe-*)
+               basic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       ppcle | powerpclittle | ppc-le | powerpc-little)
+               basic_machine=powerpcle-unknown
+               ;;
+       ppcle-* | powerpclittle-*)
+               basic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       ppc64)  basic_machine=powerpc64-unknown
+               ;;
+       ppc64-*) basic_machine=powerpc64-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       ppc64le | powerpc64little | ppc64-le | powerpc64-little)
+               basic_machine=powerpc64le-unknown
+               ;;
+       ppc64le-* | powerpc64little-*)
+               basic_machine=powerpc64le-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       ps2)
+               basic_machine=i386-ibm
+               ;;
+       pw32)
+               basic_machine=i586-unknown
+               os=-pw32
+               ;;
+       rdos)
+               basic_machine=i386-pc
+               os=-rdos
+               ;;
+       rom68k)
+               basic_machine=m68k-rom68k
+               os=-coff
+               ;;
+       rm[46]00)
+               basic_machine=mips-siemens
+               ;;
+       rtpc | rtpc-*)
+               basic_machine=romp-ibm
+               ;;
+       s390 | s390-*)
+               basic_machine=s390-ibm
+               ;;
+       s390x | s390x-*)
+               basic_machine=s390x-ibm
+               ;;
+       sa29200)
+               basic_machine=a29k-amd
+               os=-udi
+               ;;
+       sb1)
+               basic_machine=mipsisa64sb1-unknown
+               ;;
+       sb1el)
+               basic_machine=mipsisa64sb1el-unknown
+               ;;
+       sde)
+               basic_machine=mipsisa32-sde
+               os=-elf
+               ;;
+       sei)
+               basic_machine=mips-sei
+               os=-seiux
+               ;;
+       sequent)
+               basic_machine=i386-sequent
+               ;;
+       sh)
+               basic_machine=sh-hitachi
+               os=-hms
+               ;;
+       sh5el)
+               basic_machine=sh5le-unknown
+               ;;
+       sh64)
+               basic_machine=sh64-unknown
+               ;;
+       sparclite-wrs | simso-wrs)
+               basic_machine=sparclite-wrs
+               os=-vxworks
+               ;;
+       sps7)
+               basic_machine=m68k-bull
+               os=-sysv2
+               ;;
+       spur)
+               basic_machine=spur-unknown
+               ;;
+       st2000)
+               basic_machine=m68k-tandem
+               ;;
+       stratus)
+               basic_machine=i860-stratus
+               os=-sysv4
+               ;;
+       strongarm-* | thumb-*)
+               basic_machine=arm-`echo $basic_machine | sed 's/^[^-]*-//'`
+               ;;
+       sun2)
+               basic_machine=m68000-sun
+               ;;
+       sun2os3)
+               basic_machine=m68000-sun
+               os=-sunos3
+               ;;
+       sun2os4)
+               basic_machine=m68000-sun
+               os=-sunos4
+               ;;
+       sun3os3)
+               basic_machine=m68k-sun
+               os=-sunos3
+               ;;
+       sun3os4)
+               basic_machine=m68k-sun
+               os=-sunos4
+               ;;
+       sun4os3)
+               basic_machine=sparc-sun
+               os=-sunos3
+               ;;
+       sun4os4)
+               basic_machine=sparc-sun
+               os=-sunos4
+               ;;
+       sun4sol2)
+               basic_machine=sparc-sun
+               os=-solaris2
+               ;;
+       sun3 | sun3-*)
+               basic_machine=m68k-sun
+               ;;
+       sun4)
+               basic_machine=sparc-sun
+               ;;
+       sun386 | sun386i | roadrunner)
+               basic_machine=i386-sun
+               ;;
+       sv1)
+               basic_machine=sv1-cray
+               os=-unicos
+               ;;
+       symmetry)
+               basic_machine=i386-sequent
+               os=-dynix
+               ;;
+       t3e)
+               basic_machine=alphaev5-cray
+               os=-unicos
+               ;;
+       t90)
+               basic_machine=t90-cray
+               os=-unicos
+               ;;
+       tile*)
+               basic_machine=$basic_machine-unknown
+               os=-linux-gnu
+               ;;
+       tx39)
+               basic_machine=mipstx39-unknown
+               ;;
+       tx39el)
+               basic_machine=mipstx39el-unknown
+               ;;
+       toad1)
+               basic_machine=pdp10-xkl
+               os=-tops20
+               ;;
+       tower | tower-32)
+               basic_machine=m68k-ncr
+               ;;
+       tpf)
+               basic_machine=s390x-ibm
+               os=-tpf
+               ;;
+       udi29k)
+               basic_machine=a29k-amd
+               os=-udi
+               ;;
+       ultra3)
+               basic_machine=a29k-nyu
+               os=-sym1
+               ;;
+       v810 | necv810)
+               basic_machine=v810-nec
+               os=-none
+               ;;
+       vaxv)
+               basic_machine=vax-dec
+               os=-sysv
+               ;;
+       vms)
+               basic_machine=vax-dec
+               os=-vms
+               ;;
+       vpp*|vx|vx-*)
+               basic_machine=f301-fujitsu
+               ;;
+       vxworks960)
+               basic_machine=i960-wrs
+               os=-vxworks
+               ;;
+       vxworks68)
+               basic_machine=m68k-wrs
+               os=-vxworks
+               ;;
+       vxworks29k)
+               basic_machine=a29k-wrs
+               os=-vxworks
+               ;;
+       w65*)
+               basic_machine=w65-wdc
+               os=-none
+               ;;
+       w89k-*)
+               basic_machine=hppa1.1-winbond
+               os=-proelf
+               ;;
+       xbox)
+               basic_machine=i686-pc
+               os=-mingw32
+               ;;
+       xps | xps100)
+               basic_machine=xps100-honeywell
+               ;;
+       xscale-* | xscalee[bl]-*)
+               basic_machine=`echo $basic_machine | sed 's/^xscale/arm/'`
+               ;;
+       ymp)
+               basic_machine=ymp-cray
+               os=-unicos
+               ;;
+       z8k-*-coff)
+               basic_machine=z8k-unknown
+               os=-sim
+               ;;
+       z80-*-coff)
+               basic_machine=z80-unknown
+               os=-sim
+               ;;
+       none)
+               basic_machine=none-none
+               os=-none
+               ;;
+
+# Here we handle the default manufacturer of certain CPU types.  It is in
+# some cases the only manufacturer, in others, it is the most popular.
+       w89k)
+               basic_machine=hppa1.1-winbond
+               ;;
+       op50n)
+               basic_machine=hppa1.1-oki
+               ;;
+       op60c)
+               basic_machine=hppa1.1-oki
+               ;;
+       romp)
+               basic_machine=romp-ibm
+               ;;
+       mmix)
+               basic_machine=mmix-knuth
+               ;;
+       rs6000)
+               basic_machine=rs6000-ibm
+               ;;
+       vax)
+               basic_machine=vax-dec
+               ;;
+       pdp10)
+               # there are many clones, so DEC is not a safe bet
+               basic_machine=pdp10-unknown
+               ;;
+       pdp11)
+               basic_machine=pdp11-dec
+               ;;
+       we32k)
+               basic_machine=we32k-att
+               ;;
+       sh[1234] | sh[24]a | sh[24]aeb | sh[34]eb | sh[1234]le | sh[23]ele)
+               basic_machine=sh-unknown
+               ;;
+       sparc | sparcv8 | sparcv9 | sparcv9b | sparcv9v)
+               basic_machine=sparc-sun
+               ;;
+       cydra)
+               basic_machine=cydra-cydrome
+               ;;
+       orion)
+               basic_machine=orion-highlevel
+               ;;
+       orion105)
+               basic_machine=clipper-highlevel
+               ;;
+       mac | mpw | mac-mpw)
+               basic_machine=m68k-apple
+               ;;
+       pmac | pmac-mpw)
+               basic_machine=powerpc-apple
+               ;;
+       *-unknown)
+               # Make sure to match an already-canonicalized machine name.
+               ;;
+       *)
+               echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
+               exit 1
+               ;;
+esac
+
+# Here we canonicalize certain aliases for manufacturers.
+case $basic_machine in
+       *-digital*)
+               basic_machine=`echo $basic_machine | sed 's/digital.*/dec/'`
+               ;;
+       *-commodore*)
+               basic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'`
+               ;;
+       *)
+               ;;
+esac
+
+# Decode manufacturer-specific aliases for certain operating systems.
+
+if [ x"$os" != x"" ]
+then
+case $os in
+       # First match some system type aliases
+       # that might get confused with valid system types.
+       # -solaris* is a basic system type, with this one exception.
+       -auroraux)
+               os=-auroraux
+               ;;
+       -solaris1 | -solaris1.*)
+               os=`echo $os | sed -e 's|solaris1|sunos4|'`
+               ;;
+       -solaris)
+               os=-solaris2
+               ;;
+       -svr4*)
+               os=-sysv4
+               ;;
+       -unixware*)
+               os=-sysv4.2uw
+               ;;
+       -gnu/linux*)
+               os=`echo $os | sed -e 's|gnu/linux|linux-gnu|'`
+               ;;
+       # First accept the basic system types.
+       # The portable systems comes first.
+       # Each alternative MUST END IN A *, to match a version number.
+       # -sysv* is not here because it comes later, after sysvr4.
+       -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \
+             | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\
+             | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \
+             | -sym* | -kopensolaris* \
+             | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \
+             | -aos* | -aros* \
+             | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \
+             | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \
+             | -hiux* | -386bsd* | -knetbsd* | -mirbsd* | -netbsd* \
+             | -bitrig* | -openbsd* | -solidbsd* \
+             | -ekkobsd* | -kfreebsd* | -freebsd* | -riscix* | -lynxos* \
+             | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \
+             | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \
+             | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \
+             | -chorusos* | -chorusrdb* | -cegcc* \
+             | -cygwin* | -msys* | -pe* | -psos* | -moss* | -proelf* | -rtems* \
+             | -mingw32* | -mingw64* | -linux-gnu* | -linux-android* \
+             | -linux-newlib* | -linux-uclibc* \
+             | -uxpv* | -beos* | -mpeix* | -udk* \
+             | -interix* | -uwin* | -mks* | -rhapsody* | -darwin* | -opened* \
+             | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \
+             | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \
+             | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \
+             | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \
+             | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \
+             | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es*)
+       # Remember, each alternative MUST END IN *, to match a version number.
+               ;;
+       -qnx*)
+               case $basic_machine in
+                   x86-* | i*86-*)
+                       ;;
+                   *)
+                       os=-nto$os
+                       ;;
+               esac
+               ;;
+       -nto-qnx*)
+               ;;
+       -nto*)
+               os=`echo $os | sed -e 's|nto|nto-qnx|'`
+               ;;
+       -sim | -es1800* | -hms* | -xray | -os68k* | -none* | -v88r* \
+             | -windows* | -osx | -abug | -netware* | -os9* | -beos* | -haiku* \
+             | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*)
+               ;;
+       -mac*)
+               os=`echo $os | sed -e 's|mac|macos|'`
+               ;;
+       -linux-dietlibc)
+               os=-linux-dietlibc
+               ;;
+       -linux*)
+               os=`echo $os | sed -e 's|linux|linux-gnu|'`
+               ;;
+       -sunos5*)
+               os=`echo $os | sed -e 's|sunos5|solaris2|'`
+               ;;
+       -sunos6*)
+               os=`echo $os | sed -e 's|sunos6|solaris3|'`
+               ;;
+       -opened*)
+               os=-openedition
+               ;;
+       -os400*)
+               os=-os400
+               ;;
+       -wince*)
+               os=-wince
+               ;;
+       -osfrose*)
+               os=-osfrose
+               ;;
+       -osf*)
+               os=-osf
+               ;;
+       -utek*)
+               os=-bsd
+               ;;
+       -dynix*)
+               os=-bsd
+               ;;
+       -acis*)
+               os=-aos
+               ;;
+       -atheos*)
+               os=-atheos
+               ;;
+       -syllable*)
+               os=-syllable
+               ;;
+       -386bsd)
+               os=-bsd
+               ;;
+       -ctix* | -uts*)
+               os=-sysv
+               ;;
+       -nova*)
+               os=-rtmk-nova
+               ;;
+       -ns2 )
+               os=-nextstep2
+               ;;
+       -nsk*)
+               os=-nsk
+               ;;
+       # Preserve the version number of sinix5.
+       -sinix5.*)
+               os=`echo $os | sed -e 's|sinix|sysv|'`
+               ;;
+       -sinix*)
+               os=-sysv4
+               ;;
+       -tpf*)
+               os=-tpf
+               ;;
+       -triton*)
+               os=-sysv3
+               ;;
+       -oss*)
+               os=-sysv3
+               ;;
+       -svr4)
+               os=-sysv4
+               ;;
+       -svr3)
+               os=-sysv3
+               ;;
+       -sysvr4)
+               os=-sysv4
+               ;;
+       # This must come after -sysvr4.
+       -sysv*)
+               ;;
+       -ose*)
+               os=-ose
+               ;;
+       -es1800*)
+               os=-ose
+               ;;
+       -xenix)
+               os=-xenix
+               ;;
+       -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)
+               os=-mint
+               ;;
+       -aros*)
+               os=-aros
+               ;;
+       -kaos*)
+               os=-kaos
+               ;;
+       -zvmoe)
+               os=-zvmoe
+               ;;
+       -dicos*)
+               os=-dicos
+               ;;
+       -nacl*)
+               ;;
+       -none)
+               ;;
+       *)
+               # Get rid of the `-' at the beginning of $os.
+               os=`echo $os | sed 's/[^-]*-//'`
+               echo Invalid configuration \`$1\': system \`$os\' not recognized 1>&2
+               exit 1
+               ;;
+esac
+else
+
+# Here we handle the default operating systems that come with various machines.
+# The value should be what the vendor currently ships out the door with their
+# machine or put another way, the most popular os provided with the machine.
+
+# Note that if you're going to try to match "-MANUFACTURER" here (say,
+# "-sun"), then you have to tell the case statement up towards the top
+# that MANUFACTURER isn't an operating system.  Otherwise, code above
+# will signal an error saying that MANUFACTURER isn't an operating
+# system, and we'll never get to this point.
+
+case $basic_machine in
+       score-*)
+               os=-elf
+               ;;
+       spu-*)
+               os=-elf
+               ;;
+       *-acorn)
+               os=-riscix1.2
+               ;;
+       arm*-rebel)
+               os=-linux
+               ;;
+       arm*-semi)
+               os=-aout
+               ;;
+       c4x-* | tic4x-*)
+               os=-coff
+               ;;
+       hexagon-*)
+               os=-elf
+               ;;
+       tic54x-*)
+               os=-coff
+               ;;
+       tic55x-*)
+               os=-coff
+               ;;
+       tic6x-*)
+               os=-coff
+               ;;
+       # This must come before the *-dec entry.
+       pdp10-*)
+               os=-tops20
+               ;;
+       pdp11-*)
+               os=-none
+               ;;
+       *-dec | vax-*)
+               os=-ultrix4.2
+               ;;
+       m68*-apollo)
+               os=-domain
+               ;;
+       i386-sun)
+               os=-sunos4.0.2
+               ;;
+       m68000-sun)
+               os=-sunos3
+               ;;
+       m68*-cisco)
+               os=-aout
+               ;;
+       mep-*)
+               os=-elf
+               ;;
+       mips*-cisco)
+               os=-elf
+               ;;
+       mips*-*)
+               os=-elf
+               ;;
+       or32-*)
+               os=-coff
+               ;;
+       *-tti)  # must be before sparc entry or we get the wrong os.
+               os=-sysv3
+               ;;
+       sparc-* | *-sun)
+               os=-sunos4.1.1
+               ;;
+       *-be)
+               os=-beos
+               ;;
+       *-haiku)
+               os=-haiku
+               ;;
+       *-ibm)
+               os=-aix
+               ;;
+       *-knuth)
+               os=-mmixware
+               ;;
+       *-wec)
+               os=-proelf
+               ;;
+       *-winbond)
+               os=-proelf
+               ;;
+       *-oki)
+               os=-proelf
+               ;;
+       *-hp)
+               os=-hpux
+               ;;
+       *-hitachi)
+               os=-hiux
+               ;;
+       i860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent)
+               os=-sysv
+               ;;
+       *-cbm)
+               os=-amigaos
+               ;;
+       *-dg)
+               os=-dgux
+               ;;
+       *-dolphin)
+               os=-sysv3
+               ;;
+       m68k-ccur)
+               os=-rtu
+               ;;
+       m88k-omron*)
+               os=-luna
+               ;;
+       *-next )
+               os=-nextstep
+               ;;
+       *-sequent)
+               os=-ptx
+               ;;
+       *-crds)
+               os=-unos
+               ;;
+       *-ns)
+               os=-genix
+               ;;
+       i370-*)
+               os=-mvs
+               ;;
+       *-next)
+               os=-nextstep3
+               ;;
+       *-gould)
+               os=-sysv
+               ;;
+       *-highlevel)
+               os=-bsd
+               ;;
+       *-encore)
+               os=-bsd
+               ;;
+       *-sgi)
+               os=-irix
+               ;;
+       *-siemens)
+               os=-sysv4
+               ;;
+       *-masscomp)
+               os=-rtu
+               ;;
+       f30[01]-fujitsu | f700-fujitsu)
+               os=-uxpv
+               ;;
+       *-rom68k)
+               os=-coff
+               ;;
+       *-*bug)
+               os=-coff
+               ;;
+       *-apple)
+               os=-macos
+               ;;
+       *-atari*)
+               os=-mint
+               ;;
+       *)
+               os=-none
+               ;;
+esac
+fi
+
+# Here we handle the case where we know the os, and the CPU type, but not the
+# manufacturer.  We pick the logical manufacturer.
+vendor=unknown
+case $basic_machine in
+       *-unknown)
+               case $os in
+                       -riscix*)
+                               vendor=acorn
+                               ;;
+                       -sunos*)
+                               vendor=sun
+                               ;;
+                       -cnk*|-aix*)
+                               vendor=ibm
+                               ;;
+                       -beos*)
+                               vendor=be
+                               ;;
+                       -hpux*)
+                               vendor=hp
+                               ;;
+                       -mpeix*)
+                               vendor=hp
+                               ;;
+                       -hiux*)
+                               vendor=hitachi
+                               ;;
+                       -unos*)
+                               vendor=crds
+                               ;;
+                       -dgux*)
+                               vendor=dg
+                               ;;
+                       -luna*)
+                               vendor=omron
+                               ;;
+                       -genix*)
+                               vendor=ns
+                               ;;
+                       -mvs* | -opened*)
+                               vendor=ibm
+                               ;;
+                       -os400*)
+                               vendor=ibm
+                               ;;
+                       -ptx*)
+                               vendor=sequent
+                               ;;
+                       -tpf*)
+                               vendor=ibm
+                               ;;
+                       -vxsim* | -vxworks* | -windiss*)
+                               vendor=wrs
+                               ;;
+                       -aux*)
+                               vendor=apple
+                               ;;
+                       -hms*)
+                               vendor=hitachi
+                               ;;
+                       -mpw* | -macos*)
+                               vendor=apple
+                               ;;
+                       -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)
+                               vendor=atari
+                               ;;
+                       -vos*)
+                               vendor=stratus
+                               ;;
+               esac
+               basic_machine=`echo $basic_machine | sed "s/unknown/$vendor/"`
+               ;;
+esac
+
+echo $basic_machine$os
+exit
+
+# Local variables:
+# eval: (add-hook 'write-file-hooks 'time-stamp)
+# time-stamp-start: "timestamp='"
+# time-stamp-format: "%:y-%02m-%02d"
+# time-stamp-end: "'"
+# End:
index 479d7c588bf89b93a0ca77145fde876b700938b4..29cd51c355669861f2606b330c81b09e5a584d96 100755 (executable)
--- a/configure
+++ b/configure
@@ -638,7 +638,17 @@ CFLAGS
 CC
 test_suites
 include_dev_mk
-extra_deps
+extra_libs
+extra_ldflags
+extra_cppflags
+host_os
+host_vendor
+host_cpu
+host
+build_os
+build_vendor
+build_cpu
+build
 target_alias
 host_alias
 build_alias
@@ -680,6 +690,7 @@ SHELL'
 ac_subst_files=''
 ac_user_opts='
 enable_option_checking
+with_bundled_zlib
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1286,6 +1297,10 @@ Fine tuning of the installation directories:
 _ACEOF
 
   cat <<\_ACEOF
+
+System types:
+  --build=BUILD     configure for building on BUILD [guessed]
+  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
 _ACEOF
 fi
 
@@ -1293,6 +1308,12 @@ if test -n "$ac_init_help"; then
 
   cat <<\_ACEOF
 
+Optional Packages:
+  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
+  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
+  --with-bundled-zlib     use bundled zlib instead of the system's default
+                          zlib
+
 Some influential environment variables:
   CC          C compiler command
   CFLAGS      C compiler flags
@@ -2203,6 +2224,117 @@ $as_echo "$as_me: Configuring ccache" >&6;}
 ac_config_headers="$ac_config_headers config.h"
 
 
+ac_aux_dir=
+for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
+  if test -f "$ac_dir/install-sh"; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/install-sh -c"
+    break
+  elif test -f "$ac_dir/install.sh"; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/install.sh -c"
+    break
+  elif test -f "$ac_dir/shtool"; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/shtool install -c"
+    break
+  fi
+done
+if test -z "$ac_aux_dir"; then
+  as_fn_error $? "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5
+fi
+
+# These three variables are undocumented and unsupported,
+# and are intended to be withdrawn in a future Autoconf release.
+# They can cause serious problems if a builder's source tree is in a directory
+# whose full name contains unusual characters.
+ac_config_guess="$SHELL $ac_aux_dir/config.guess"  # Please don't use this var.
+ac_config_sub="$SHELL $ac_aux_dir/config.sub"  # Please don't use this var.
+ac_configure="$SHELL $ac_aux_dir/configure"  # Please don't use this var.
+
+
+# Make sure we can run config.sub.
+$SHELL "$ac_aux_dir/config.sub" sun4 >/dev/null 2>&1 ||
+  as_fn_error $? "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking build system type" >&5
+$as_echo_n "checking build system type... " >&6; }
+if ${ac_cv_build+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_build_alias=$build_alias
+test "x$ac_build_alias" = x &&
+  ac_build_alias=`$SHELL "$ac_aux_dir/config.guess"`
+test "x$ac_build_alias" = x &&
+  as_fn_error $? "cannot guess build type; you must specify one" "$LINENO" 5
+ac_cv_build=`$SHELL "$ac_aux_dir/config.sub" $ac_build_alias` ||
+  as_fn_error $? "$SHELL $ac_aux_dir/config.sub $ac_build_alias failed" "$LINENO" 5
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_build" >&5
+$as_echo "$ac_cv_build" >&6; }
+case $ac_cv_build in
+*-*-*) ;;
+*) as_fn_error $? "invalid value of canonical build" "$LINENO" 5;;
+esac
+build=$ac_cv_build
+ac_save_IFS=$IFS; IFS='-'
+set x $ac_cv_build
+shift
+build_cpu=$1
+build_vendor=$2
+shift; shift
+# Remember, the first character of IFS is used to create $*,
+# except with old shells:
+build_os=$*
+IFS=$ac_save_IFS
+case $build_os in *\ *) build_os=`echo "$build_os" | sed 's/ /-/g'`;; esac
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking host system type" >&5
+$as_echo_n "checking host system type... " >&6; }
+if ${ac_cv_host+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test "x$host_alias" = x; then
+  ac_cv_host=$ac_cv_build
+else
+  ac_cv_host=`$SHELL "$ac_aux_dir/config.sub" $host_alias` ||
+    as_fn_error $? "$SHELL $ac_aux_dir/config.sub $host_alias failed" "$LINENO" 5
+fi
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_host" >&5
+$as_echo "$ac_cv_host" >&6; }
+case $ac_cv_host in
+*-*-*) ;;
+*) as_fn_error $? "invalid value of canonical host" "$LINENO" 5;;
+esac
+host=$ac_cv_host
+ac_save_IFS=$IFS; IFS='-'
+set x $ac_cv_host
+shift
+host_cpu=$1
+host_vendor=$2
+shift; shift
+# Remember, the first character of IFS is used to create $*,
+# except with old shells:
+host_os=$*
+IFS=$ac_save_IFS
+case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac
+
+
+
+case $host in
+    *mingw32* | *cygwin* | *wince* | *mingwce*)
+
+$as_echo "#define _WIN32_WINNT 0x0600" >>confdefs.h
+
+        ;;
+esac
+
+
+
 
 
 
@@ -3133,35 +3265,6 @@ ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
-ac_aux_dir=
-for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
-  if test -f "$ac_dir/install-sh"; then
-    ac_aux_dir=$ac_dir
-    ac_install_sh="$ac_aux_dir/install-sh -c"
-    break
-  elif test -f "$ac_dir/install.sh"; then
-    ac_aux_dir=$ac_dir
-    ac_install_sh="$ac_aux_dir/install.sh -c"
-    break
-  elif test -f "$ac_dir/shtool"; then
-    ac_aux_dir=$ac_dir
-    ac_install_sh="$ac_aux_dir/shtool install -c"
-    break
-  fi
-done
-if test -z "$ac_aux_dir"; then
-  as_fn_error $? "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5
-fi
-
-# These three variables are undocumented and unsupported,
-# and are intended to be withdrawn in a future Autoconf release.
-# They can cause serious problems if a builder's source tree is in a directory
-# whose full name contains unusual characters.
-ac_config_guess="$SHELL $ac_aux_dir/config.guess"  # Please don't use this var.
-ac_config_sub="$SHELL $ac_aux_dir/config.sub"  # Please don't use this var.
-ac_configure="$SHELL $ac_aux_dir/configure"  # Please don't use this var.
-
-
 # Find a good install program.  We prefer a C program (faster),
 # so one script is as good as another.  But avoid the broken or
 # incompatible versions:
@@ -4196,6 +4299,17 @@ $as_echo "#define HAVE_SYS_WAIT_H 1" >>confdefs.h
 fi
 
 
+ac_fn_c_check_type "$LINENO" "long long" "ac_cv_type_long_long" "$ac_includes_default"
+if test "x$ac_cv_type_long_long" = xyes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_LONG_LONG 1
+_ACEOF
+
+
+fi
+
+
 for ac_header in ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
@@ -4299,6 +4413,28 @@ _ACEOF
 fi
 done
 
+for ac_func in strtok_r
+do :
+  ac_fn_c_check_func "$LINENO" "strtok_r" "ac_cv_func_strtok_r"
+if test "x$ac_cv_func_strtok_r" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_STRTOK_R 1
+_ACEOF
+
+fi
+done
+
+for ac_func in unsetenv
+do :
+  ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
+if test "x$ac_cv_func_unsetenv" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_UNSETENV 1
+_ACEOF
+
+fi
+done
+
 for ac_func in utimes
 do :
   ac_fn_c_check_func "$LINENO" "utimes" "ac_cv_func_utimes"
@@ -5448,7 +5584,15 @@ if test "$ac_res" != no; then :
 fi
 
 
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for zlib >= 1.2.3" >&5
+
+
+# Check whether --with-bundled-zlib was given.
+if test "${with_bundled_zlib+set}" = set; then :
+  withval=$with_bundled_zlib;
+fi
+
+if test x${with_bundled_zlib} = x; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for zlib >= 1.2.3" >&5
 $as_echo_n "checking for zlib >= 1.2.3... " >&6; }
 if ${ccache_cv_zlib_1_2_3+:} false; then :
   $as_echo_n "(cached) " >&6
@@ -5460,10 +5604,10 @@ int
 main ()
 {
 
-        #if (ZLIB_VERNUM >= 0x1230)
-        #else
-        #error "ZLIB_VERNUM < 0x1230"
-        #endif
+            #if (ZLIB_VERNUM >= 0x1230)
+            #else
+            #error "ZLIB_VERNUM < 0x1230"
+            #endif
 
   ;
   return 0;
@@ -5478,7 +5622,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 fi
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ccache_cv_zlib_1_2_3" >&5
 $as_echo "$ccache_cv_zlib_1_2_3" >&6; }
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gzdopen in -lz" >&5
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for gzdopen in -lz" >&5
 $as_echo_n "checking for gzdopen in -lz... " >&6; }
 if ${ac_cv_lib_z_gzdopen+:} false; then :
   $as_echo_n "(cached) " >&6
@@ -5518,15 +5662,252 @@ if test "x$ac_cv_lib_z_gzdopen" = xyes; then :
   true
 fi
 
-if test $ccache_cv_zlib_1_2_3 = yes && test $ac_cv_lib_z_gzdopen = yes; then
-    use_bundled_zlib=no
+    if test $ccache_cv_zlib_1_2_3 = yes && test $ac_cv_lib_z_gzdopen = yes; then
+        use_bundled_zlib=no
+    else
+        { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using bundled zlib" >&5
+$as_echo "$as_me: WARNING: using bundled zlib" >&2;}
+        use_bundled_zlib=yes
+    fi
 else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: using bundled zlib as requested" >&5
+$as_echo "$as_me: using bundled zlib as requested" >&6;}
     use_bundled_zlib=yes
-    extra_deps="zlib/libz.a"
-    CPPFLAGS="$CPPFLAGS -I\$(srcdir)/zlib"
-    LIBS="-Lzlib $LIBS"
+fi
+
+if test x${use_bundled_zlib} = xyes; then
+    extra_cppflags="-I\$(srcdir)/zlib"
+    extra_ldflags="-Lzlib"
+    extra_libs="zlib/libz.a"
     mkdir -p zlib
+else
+    extra_ldflags="-lz"
+fi
+
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether byte ordering is bigendian" >&5
+$as_echo_n "checking whether byte ordering is bigendian... " >&6; }
+if ${ac_cv_c_bigendian+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_cv_c_bigendian=unknown
+    # See if we're dealing with a universal compiler.
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#ifndef __APPLE_CC__
+              not a universal capable compiler
+            #endif
+            typedef int dummy;
+
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+       # Check for potential -arch flags.  It is not universal unless
+       # there are at least two -arch flags with different values.
+       ac_arch=
+       ac_prev=
+       for ac_word in $CC $CFLAGS $CPPFLAGS $LDFLAGS; do
+        if test -n "$ac_prev"; then
+          case $ac_word in
+            i?86 | x86_64 | ppc | ppc64)
+              if test -z "$ac_arch" || test "$ac_arch" = "$ac_word"; then
+                ac_arch=$ac_word
+              else
+                ac_cv_c_bigendian=universal
+                break
+              fi
+              ;;
+          esac
+          ac_prev=
+        elif test "x$ac_word" = "x-arch"; then
+          ac_prev=arch
+        fi
+       done
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    if test $ac_cv_c_bigendian = unknown; then
+      # See if sys/param.h defines the BYTE_ORDER macro.
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <sys/types.h>
+            #include <sys/param.h>
+
+int
+main ()
+{
+#if ! (defined BYTE_ORDER && defined BIG_ENDIAN \
+                    && defined LITTLE_ENDIAN && BYTE_ORDER && BIG_ENDIAN \
+                    && LITTLE_ENDIAN)
+             bogus endian macros
+            #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  # It does; now see whether it defined to BIG_ENDIAN or not.
+        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <sys/types.h>
+               #include <sys/param.h>
+
+int
+main ()
+{
+#if BYTE_ORDER != BIG_ENDIAN
+                not big endian
+               #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_c_bigendian=yes
+else
+  ac_cv_c_bigendian=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    fi
+    if test $ac_cv_c_bigendian = unknown; then
+      # See if <limits.h> defines _LITTLE_ENDIAN or _BIG_ENDIAN (e.g., Solaris).
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <limits.h>
+
+int
+main ()
+{
+#if ! (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
+             bogus endian macros
+            #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  # It does; now see whether it defined to _BIG_ENDIAN or not.
+        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <limits.h>
+
+int
+main ()
+{
+#ifndef _BIG_ENDIAN
+                not big endian
+               #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_c_bigendian=yes
+else
+  ac_cv_c_bigendian=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    fi
+    if test $ac_cv_c_bigendian = unknown; then
+      # Compile a test program.
+      if test "$cross_compiling" = yes; then :
+  # Try to guess by grepping values from an object file.
+        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+short int ascii_mm[] =
+                 { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };
+               short int ascii_ii[] =
+                 { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };
+               int use_ascii (int i) {
+                 return ascii_mm[i] + ascii_ii[i];
+               }
+               short int ebcdic_ii[] =
+                 { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };
+               short int ebcdic_mm[] =
+                 { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };
+               int use_ebcdic (int i) {
+                 return ebcdic_mm[i] + ebcdic_ii[i];
+               }
+               extern int foo;
+
+int
+main ()
+{
+return use_ascii (foo) == use_ebcdic (foo);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  if grep BIGenDianSyS conftest.$ac_objext >/dev/null; then
+             ac_cv_c_bigendian=yes
+           fi
+           if grep LiTTleEnDian conftest.$ac_objext >/dev/null ; then
+             if test "$ac_cv_c_bigendian" = unknown; then
+               ac_cv_c_bigendian=no
+             else
+               # finding both strings is unlikely to happen, but who knows?
+               ac_cv_c_bigendian=unknown
+             fi
+           fi
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+
+            /* Are we little or big endian?  From Harbison&Steele.  */
+            union
+            {
+              long int l;
+              char c[sizeof (long int)];
+            } u;
+            u.l = 1;
+            return u.c[sizeof (long int) - 1] == 1;
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  ac_cv_c_bigendian=no
+else
+  ac_cv_c_bigendian=yes
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+    fi
 fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_bigendian" >&5
+$as_echo "$ac_cv_c_bigendian" >&6; }
+ case $ac_cv_c_bigendian in #(
+   yes)
+     $as_echo "#define WORDS_BIGENDIAN 1" >>confdefs.h
+;; #(
+   no)
+      ;; #(
+   universal)
+
+$as_echo "#define AC_APPLE_UNIVERSAL_BUILD 1" >>confdefs.h
+
+     ;; #(
+   *)
+     as_fn_error $? "unknown endianness
+ presetting ac_cv_c_bigendian=no (or yes) will help" "$LINENO" 5 ;;
+ esac
+
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for inline" >&5
 $as_echo_n "checking for inline... " >&6; }
@@ -5606,24 +5987,26 @@ $as_echo "#define HAVE_EXTERN_INLINE 1" >>confdefs.h
 
 fi
 
-if test -f $srcdir/dev.mk.in && test "$RUN_FROM_BUILD_FARM" != yes; then
-    { $as_echo "$as_me:${as_lineno-$LINENO}: Enabling developer mode" >&5
-$as_echo "$as_me: Enabling developer mode" >&6;}
+if test ! -f $srcdir/dev_mode_disabled && test "$RUN_FROM_BUILD_FARM" != yes; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: Developer mode enabled" >&5
+$as_echo "$as_me: Developer mode enabled" >&6;}
     ac_config_files="$ac_config_files dev.mk"
 
     include_dev_mk='include dev.mk'
     version=`(git --git-dir=$srcdir/.git describe --dirty 2>/dev/null || echo vunknown) | sed -e 's/v//' -e 's/-/+/' -e 's/-/_/g'`
     echo "const char CCACHE_VERSION[] = \"$version\";" >version.c
-elif test ! -f $srcdir/version.c; then
+else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: Developer mode disabled" >&5
+$as_echo "$as_me: Developer mode disabled" >&6;}
+fi
+
+if test ! -f $srcdir/version.c; then
     { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: unable to determine ccache version" >&5
 $as_echo "$as_me: WARNING: unable to determine ccache version" >&2;}
     echo "const char CCACHE_VERSION[] = \"unknown\";" >version.c
 fi
 
-test_suites=""
-for x in $srcdir/test/test_*.c; do
-    test_suites="$test_suites $x"
-done
+test_suites=`ls $srcdir/test/test_*.c | egrep -v 'BASE|BACKUP|LOCAL|REMOTE' | xargs echo`
 
 ac_config_files="$ac_config_files Makefile"
 
@@ -5737,6 +6120,7 @@ LTLIBOBJS=$ac_ltlibobjs
 
 
 
+
 : "${CONFIG_STATUS=./config.status}"
 ac_write_fail=0
 ac_clean_files_save=$ac_clean_files
@@ -6917,10 +7301,6 @@ mv config.h.tmp config.h
 
 mkdir -p .deps test
 
-if test x$use_bundled_zlib = xyes; then
-   { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using bundled zlib" >&5
-$as_echo "$as_me: WARNING: using bundled zlib" >&2;}
-fi
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: now build ccache by running make" >&5
 $as_echo "$as_me: now build ccache by running make" >&6;}
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..2cf2939
--- /dev/null
@@ -0,0 +1,181 @@
+dnl Process this file with autoconf to produce a configure script.
+
+AC_INIT()
+AC_PREREQ(2.52)
+
+AC_MSG_NOTICE([Configuring ccache])
+
+AC_CONFIG_HEADER(config.h)
+
+AC_CANONICAL_HOST
+
+case $host in
+    *mingw32* | *cygwin* | *wince* | *mingwce*)
+        AC_DEFINE(_WIN32_WINNT,0x0600, Windows Vista or newer is required)
+        ;;
+esac
+
+AC_SUBST(extra_cppflags)
+AC_SUBST(extra_ldflags)
+AC_SUBST(extra_libs)
+AC_SUBST(include_dev_mk)
+AC_SUBST(test_suites)
+
+dnl Checks for programs.
+AC_PROG_CC
+AC_PROG_CPP
+AC_PROG_INSTALL
+AC_PROG_RANLIB
+AC_CHECK_TOOL(AR, ar)
+if test -z "$AR"; then
+    AC_MSG_ERROR(cannot find ar)
+fi
+
+m4_include(m4/feature_macros.m4)
+
+# If GCC, turn on warnings.
+if test "x$GCC" = "xyes"; then
+    CFLAGS="$CFLAGS -Wall -W"
+else
+    CFLAGS="$CFLAGS -O"
+fi
+
+AC_HEADER_DIRENT
+AC_HEADER_TIME
+AC_HEADER_STDBOOL
+AC_HEADER_SYS_WAIT
+
+AC_CHECK_TYPES(long long)
+
+AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h)
+AC_CHECK_HEADERS(termios.h)
+
+AC_CHECK_FUNCS(gethostname)
+AC_CHECK_FUNCS(getopt_long)
+AC_CHECK_FUNCS(getpwuid)
+AC_CHECK_FUNCS(gettimeofday)
+AC_CHECK_FUNCS(mkstemp)
+AC_CHECK_FUNCS(realpath)
+AC_CHECK_FUNCS(strndup)
+AC_CHECK_FUNCS(strtok_r)
+AC_CHECK_FUNCS(unsetenv)
+AC_CHECK_FUNCS(utimes)
+
+AC_CACHE_CHECK([for compar_fn_t in stdlib.h],ccache_cv_COMPAR_FN_T, [
+    AC_TRY_COMPILE(
+        [#include <stdlib.h>],
+        [void test_fn(void) { qsort(NULL, 0, 0, (__compar_fn_t)NULL); }],
+        ccache_cv_COMPAR_FN_T=yes,
+        ccache_cv_COMPAR_FN_T=no)])
+if test x"$ccache_cv_COMPAR_FN_T" = x"yes"; then
+   AC_DEFINE(HAVE_COMPAR_FN_T, 1,
+             Define to 1 if you have the `__compar_fn_t' typedef.)
+fi
+
+dnl Replacements of snprintf and friends.
+m4_include(m4/snprintf.m4)
+HW_FUNC_VSNPRINTF
+HW_FUNC_SNPRINTF
+HW_FUNC_VASPRINTF
+HW_FUNC_ASPRINTF
+
+dnl Check if -lm is needed.
+AC_SEARCH_LIBS(cos, m)
+
+
+dnl Check for zlib
+AC_ARG_WITH(bundled-zlib,
+  [AS_HELP_STRING([--with-bundled-zlib],
+    [use bundled zlib instead of the system's default zlib])])
+if test x${with_bundled_zlib} = x; then
+    AC_CACHE_CHECK(
+        [for zlib >= 1.2.3],
+        [ccache_cv_zlib_1_2_3],
+        AC_TRY_COMPILE(
+            [#include <zlib.h>],
+            [
+            #if (ZLIB_VERNUM >= 0x1230)
+            #else
+            #error "ZLIB_VERNUM < 0x1230"
+            #endif
+            ],
+            [ccache_cv_zlib_1_2_3=yes],
+            [ccache_cv_zlib_1_2_3=no]))
+    AC_CHECK_LIB(z, gzdopen, true)
+    if test $ccache_cv_zlib_1_2_3 = yes && test $ac_cv_lib_z_gzdopen = yes; then
+        use_bundled_zlib=no
+    else
+        AC_MSG_WARN(using bundled zlib)
+        use_bundled_zlib=yes
+    fi
+else
+    AC_MSG_NOTICE(using bundled zlib as requested)
+    use_bundled_zlib=yes
+fi
+
+if test x${use_bundled_zlib} = xyes; then
+    extra_cppflags="-I\$(srcdir)/zlib"
+    extra_ldflags="-Lzlib"
+    extra_libs="zlib/libz.a"
+    mkdir -p zlib
+else
+    extra_ldflags="-lz"
+fi
+
+AC_C_BIGENDIAN
+
+AC_C_INLINE
+
+dnl Check for "extern inline".
+AC_CACHE_CHECK(
+    for extern inline,
+    ac_cv_c_extern_inline,
+    [
+    ac_cv_c_extern_inline=no
+    AC_TRY_COMPILE(
+        [
+        extern $ac_cv_c_inline double foo(double x);
+        extern $ac_cv_c_inline double foo(double x) { return x+1.0; };
+        double foo (double x) { return x + 1.0; };
+        ],
+        [foo(1.0)],
+        [ac_cv_c_extern_inline="yes"])])
+if test "$ac_cv_c_extern_inline" != no ; then
+    AC_DEFINE(HAVE_EXTERN_INLINE, 1,
+              Define to 1 if your compiler supports extern inline)
+fi
+
+dnl Enable developer mode if dev.mk.in exists.
+if test ! -f $srcdir/dev_mode_disabled && test "$RUN_FROM_BUILD_FARM" != yes; then
+    AC_MSG_NOTICE(Developer mode enabled)
+    AC_CONFIG_FILES([dev.mk])
+    include_dev_mk='include dev.mk'
+    version=`(git --git-dir=$srcdir/.git describe --dirty 2>/dev/null || echo vunknown) | sed -e 's/v//' -e 's/-/+/' -e 's/-/_/g'`
+    echo "const char CCACHE_VERSION@<:@@:>@ = \"$version\";" >version.c
+else
+    AC_MSG_NOTICE(Developer mode disabled)
+fi
+
+if test ! -f $srcdir/version.c; then
+    AC_MSG_WARN(unable to determine ccache version)
+    echo "const char CCACHE_VERSION@<:@@:>@ = \"unknown\";" >version.c
+fi
+
+dnl Find test suite files.
+test_suites=`ls $srcdir/test/test_*.c | egrep -v 'BASE|BACKUP|LOCAL|REMOTE' | xargs echo`
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+
+cat <<EOF >config.h.tmp
+#ifndef CCACHE_CONFIG_H
+#define CCACHE_CONFIG_H
+EOF
+cat config.h >>config.h.tmp
+echo '#endif' >>config.h.tmp
+mv config.h.tmp config.h
+
+mkdir -p .deps test
+
+
+AC_MSG_NOTICE(now build ccache by running make)
diff --git a/confitems.gperf b/confitems.gperf
new file mode 100644 (file)
index 0000000..6fb21ef
--- /dev/null
@@ -0,0 +1,36 @@
+%language=ANSI-C
+%enum
+%struct-type
+%readonly-tables
+%define hash-function-name confitems_hash
+%define lookup-function-name confitems_get
+%define initializer-suffix ,0,NULL,0,NULL
+struct conf_item;
+%%
+base_dir,             0, ITEM_V(base_dir, env_string, absolute_path)
+cache_dir,            1, ITEM(cache_dir, env_string)
+cache_dir_levels,     2, ITEM_V(cache_dir_levels, unsigned, dir_levels)
+compiler,             3, ITEM(compiler, string)
+compiler_check,       4, ITEM(compiler_check, string)
+compression,          5, ITEM(compression, bool)
+compression_level,    6, ITEM(compression_level, unsigned)
+cpp_extension,        7, ITEM(cpp_extension, string)
+direct_mode,          8, ITEM(direct_mode, bool)
+disable,              9, ITEM(disable, bool)
+extra_files_to_hash, 10, ITEM(extra_files_to_hash, env_string)
+hard_link,           11, ITEM(hard_link, bool)
+hash_dir,            12, ITEM(hash_dir, bool)
+log_file,            13, ITEM(log_file, env_string)
+max_files,           14, ITEM(max_files, unsigned)
+max_size,            15, ITEM(max_size, size)
+path,                16, ITEM(path, env_string)
+prefix_command,      17, ITEM(prefix_command, env_string)
+read_only,           18, ITEM(read_only, bool)
+read_only_direct,    19, ITEM(read_only_direct, bool)
+recache,             20, ITEM(recache, bool)
+run_second_cpp,      21, ITEM(run_second_cpp, bool)
+sloppiness,          22, ITEM(sloppiness, sloppiness)
+stats,               23, ITEM(stats, bool)
+temporary_dir,       24, ITEM(temporary_dir, env_string)
+umask,               25, ITEM(umask, umask)
+unify,               26, ITEM(unify, bool)
diff --git a/confitems_lookup.c b/confitems_lookup.c
new file mode 100644 (file)
index 0000000..2642dd6
--- /dev/null
@@ -0,0 +1,185 @@
+/* ANSI-C code produced by gperf version 3.0.4 */
+/* Command-line: gperf confitems.gperf  */
+/* Computed positions: -k'1-2' */
+
+#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
+      && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
+      && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
+      && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
+      && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
+      && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
+      && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
+      && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
+      && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
+      && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
+      && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
+      && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
+      && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
+      && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
+      && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
+      && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
+      && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
+      && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
+      && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
+      && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
+      && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
+      && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
+      && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
+/* The character set is not based on ISO-646.  */
+#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gnu-gperf@gnu.org>."
+#endif
+
+#line 8 "confitems.gperf"
+struct conf_item;
+/* maximum key range = 45, duplicates = 0 */
+
+#ifdef __GNUC__
+__inline
+#else
+#ifdef __cplusplus
+inline
+#endif
+#endif
+static unsigned int
+confitems_hash (register const char *str, register unsigned int len)
+{
+  static const unsigned char asso_values[] =
+    {
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 20,  5,  0,
+      10,  0, 50, 50, 15,  5, 50, 50, 20, 10,
+       0,  0, 10, 50,  0,  0,  0,  5, 50, 50,
+      30, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+      50, 50, 50, 50, 50, 50
+    };
+  return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];
+}
+
+#ifdef __GNUC__
+__inline
+#if defined __GNUC_STDC_INLINE__ || defined __GNUC_GNU_INLINE__
+__attribute__ ((__gnu_inline__))
+#endif
+#endif
+const struct conf_item *
+confitems_get (register const char *str, register unsigned int len)
+{
+  enum
+    {
+      TOTAL_KEYWORDS = 27,
+      MIN_WORD_LENGTH = 4,
+      MAX_WORD_LENGTH = 19,
+      MIN_HASH_VALUE = 5,
+      MAX_HASH_VALUE = 49
+    };
+
+  static const struct conf_item wordlist[] =
+    {
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+      {"",0,NULL,0,NULL},
+#line 33 "confitems.gperf"
+      {"stats",               23, ITEM(stats, bool)},
+      {"",0,NULL,0,NULL},
+#line 30 "confitems.gperf"
+      {"recache",             20, ITEM(recache, bool)},
+#line 13 "confitems.gperf"
+      {"compiler",             3, ITEM(compiler, string)},
+#line 28 "confitems.gperf"
+      {"read_only",           18, ITEM(read_only, bool)},
+#line 36 "confitems.gperf"
+      {"unify",               26, ITEM(unify, bool)},
+#line 15 "confitems.gperf"
+      {"compression",          5, ITEM(compression, bool)},
+      {"",0,NULL,0,NULL},
+#line 34 "confitems.gperf"
+      {"temporary_dir",       24, ITEM(temporary_dir, env_string)},
+#line 14 "confitems.gperf"
+      {"compiler_check",       4, ITEM(compiler_check, string)},
+      {"",0,NULL,0,NULL},
+#line 29 "confitems.gperf"
+      {"read_only_direct",    19, ITEM(read_only_direct, bool)},
+#line 16 "confitems.gperf"
+      {"compression_level",    6, ITEM(compression_level, unsigned)},
+      {"",0,NULL,0,NULL},
+#line 31 "confitems.gperf"
+      {"run_second_cpp",      21, ITEM(run_second_cpp, bool)},
+#line 35 "confitems.gperf"
+      {"umask",               25, ITEM(umask, umask)},
+      {"",0,NULL,0,NULL},
+#line 19 "confitems.gperf"
+      {"disable",              9, ITEM(disable, bool)},
+#line 17 "confitems.gperf"
+      {"cpp_extension",        7, ITEM(cpp_extension, string)},
+#line 27 "confitems.gperf"
+      {"prefix_command",      17, ITEM(prefix_command, env_string)},
+      {"",0,NULL,0,NULL},
+#line 18 "confitems.gperf"
+      {"direct_mode",          8, ITEM(direct_mode, bool)},
+      {"",0,NULL,0,NULL},
+#line 23 "confitems.gperf"
+      {"log_file",            13, ITEM(log_file, env_string)},
+#line 11 "confitems.gperf"
+      {"cache_dir",            1, ITEM(cache_dir, env_string)},
+#line 32 "confitems.gperf"
+      {"sloppiness",          22, ITEM(sloppiness, sloppiness)},
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+#line 10 "confitems.gperf"
+      {"base_dir",             0, ITEM_V(base_dir, env_string, absolute_path)},
+#line 26 "confitems.gperf"
+      {"path",                16, ITEM(path, env_string)},
+      {"",0,NULL,0,NULL},
+#line 12 "confitems.gperf"
+      {"cache_dir_levels",     2, ITEM_V(cache_dir_levels, unsigned, dir_levels)},
+      {"",0,NULL,0,NULL},
+#line 25 "confitems.gperf"
+      {"max_size",            15, ITEM(max_size, size)},
+#line 24 "confitems.gperf"
+      {"max_files",           14, ITEM(max_files, unsigned)},
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+      {"",0,NULL,0,NULL},
+#line 22 "confitems.gperf"
+      {"hash_dir",            12, ITEM(hash_dir, bool)},
+#line 21 "confitems.gperf"
+      {"hard_link",           11, ITEM(hard_link, bool)},
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+      {"",0,NULL,0,NULL}, {"",0,NULL,0,NULL},
+#line 20 "confitems.gperf"
+      {"extra_files_to_hash", 10, ITEM(extra_files_to_hash, env_string)}
+    };
+
+  if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
+    {
+      register int key = confitems_hash (str, len);
+
+      if (key <= MAX_HASH_VALUE && key >= 0)
+        {
+          register const char *s = wordlist[key].name;
+
+          if (*str == *s && !strcmp (str + 1, s + 1))
+            return &wordlist[key];
+        }
+    }
+  return 0;
+}
+static const size_t CONFITEMS_TOTAL_KEYWORDS = 27;
diff --git a/dev.mk.in b/dev.mk.in
new file mode 100644 (file)
index 0000000..914e7e5
--- /dev/null
+++ b/dev.mk.in
@@ -0,0 +1,147 @@
+# GNU make syntax reigns in this file.
+
+all_cflags += -Werror
+all_cppflags += -MD -MP -MF .deps/$(subst .._,,$(subst /,_,$<)).d
+
+ASCIIDOC = asciidoc
+GPERF = gperf
+XSLTPROC = xsltproc
+MANPAGE_XSL = $(shell if [ -e /usr/local/etc/asciidoc/docbook-xsl/manpage.xsl ]; \
+                then echo /usr/local/etc/asciidoc/docbook-xsl/manpage.xsl; \
+                else echo /etc/asciidoc/docbook-xsl/manpage.xsl; fi)
+
+
+version := \
+    $(shell (git --git-dir=$(srcdir)/.git describe --dirty || git --git-dir=$(srcdir)/.git describe || echo vunknown) \
+            2>/dev/null | sed -e 's/v//' -e 's/-/+/' -e 's/-/_/g')
+
+dist_dir = ccache-$(version)
+dist_archives = \
+    ccache-$(version).tar.bz2 \
+    ccache-$(version).tar.gz
+ifneq ($(shell uname), Darwin)
+    dist_archives += ccache-$(version).tar.xz
+endif
+
+generated_docs = \
+    ccache.1 AUTHORS.html INSTALL.html LICENSE.html MANUAL.html NEWS.html \
+    README.html
+built_dist_files = $(generated_docs)
+
+headers = \
+    ccache.h \
+    compopt.h \
+    conf.h \
+    counters.h \
+    getopt_long.h \
+    hashtable.h \
+    hashtable_itr.h \
+    hashtable_private.h \
+    hashutil.h \
+    language.h \
+    macroskip.h \
+    manifest.h \
+    mdfour.h \
+    murmurhashneutral2.h \
+    system.h \
+    test/framework.h \
+    test/suites.h \
+    test/util.h
+
+files_to_clean += *.tar.bz2 *.tar.gz *.tar.xz *.xml .deps/*
+files_to_distclean += $(built_dist_files) version.c test/suites.h
+files_to_distclean += .deps version.c dev.mk
+
+source_dist_files = \
+    $(base_sources) \
+    $(headers) \
+    $(test_sources) \
+    AUTHORS.txt \
+    GPL-3.0.txt \
+    HACKING.txt \
+    INSTALL.txt \
+    LICENSE.txt \
+    MANUAL.txt \
+    Makefile.in \
+    NEWS.txt \
+    README.txt \
+    autogen.sh \
+    config.guess \
+    config.h.in \
+    config.sub \
+    configure \
+    configure.ac \
+    confitems.gperf \
+    confitems_lookup.c \
+    dev.mk.in \
+    envtoconfitems.gperf \
+    envtoconfitems_lookup.c \
+    install-sh \
+    main.c \
+    test.sh \
+    zlib/*.c \
+    zlib/*.h
+
+dist_files = \
+    $(addprefix $(srcdir)/, $(source_dist_files)) \
+    $(built_dist_files)
+
+ifneq ($(shell sed 's/.*"\(.*\)".*/\1/' version.c 2>/dev/null),$(version))
+  $(shell echo 'const char CCACHE_VERSION[] = "$(version)";' >version.c)
+endif
+version.o: version.c
+
+%_lookup.c: %.gperf
+       $(GPERF) $< >$@
+       echo "static const size_t `echo $* | tr a-z A-Z`_TOTAL_KEYWORDS = `sed -nr 's/.*TOTAL_KEYWORDS = ([0-9]+).*/\1/p' $@`;" >>$@
+
+.PHONY: dist
+dist: $(dist_archives)
+
+$(dist_archives): $(dist_files)
+       tmpdir=$$(mktemp -d /tmp/tmp-ccache-dist.XXXXXX) && \
+       dir=$$tmpdir/$(dist_dir) && \
+       mkdir $$dir && \
+       (cd $(srcdir) && \
+        rsync -r --relative $(source_dist_files) $$dir) && \
+       cp $(built_dist_files) $$dir && \
+       echo "Remove this file to enable developer mode." >$$dir/dev_mode_disabled && \
+       (cd $$tmpdir && \
+        tarcompression= && \
+        case $@ in \
+            *.bz2) tarcompression=-j ;; \
+            *.gz) tarcompression=-z ;; \
+            *.xz) tarcompression=-J ;; \
+        esac && \
+        tar -c $$tarcompression -f $(CURDIR)/$@ $(dist_dir)) && \
+       rm -rf $$tmpdir
+
+.PHONY: distcheck
+distcheck: $(firstword $(dist_archives))
+       tmpdir=$$(mktemp -d /tmp/tmp-ccache-distcheck.XXXXXX) && \
+       (cd $$tmpdir && \
+        tar xjf $(CURDIR)/$< && \
+        mkdir -p $(dist_dir)/build && \
+        cd $(dist_dir)/build && \
+        ../configure --prefix=$$tmpdir/root && \
+        $(MAKE) install && \
+        $(MAKE) installcheck) && \
+       rm -rf $$tmpdir
+
+.PHONY: docs
+docs: $(generated_docs)
+
+%.html: %.txt
+       $(ASCIIDOC) -a revnumber=$(version) -a toc -b xhtml11 $<
+
+%.xml: %.txt
+       $(ASCIIDOC) -a revnumber=$(version) -d manpage -b docbook $<
+
+ccache.1: MANUAL.xml
+       $(XSLTPROC) --nonet $(MANPAGE_XSL) $<
+
+.PHONY: check-syntax
+check-syntax:
+       $(CC) $(all_cppflags) -I. $(all_cflags) -S -o /dev/null $(CHK_SOURCES)
+
+-include .deps/*.d
diff --git a/dev_mode_disabled b/dev_mode_disabled
new file mode 100644 (file)
index 0000000..ba72f4f
--- /dev/null
@@ -0,0 +1 @@
+Remove this file to enable developer mode.
diff --git a/envtoconfitems.gperf b/envtoconfitems.gperf
new file mode 100644 (file)
index 0000000..f623c45
--- /dev/null
@@ -0,0 +1,37 @@
+%language=ANSI-C
+%enum
+%struct-type
+%readonly-tables
+%define hash-function-name envtoconfitems_hash
+%define lookup-function-name envtoconfitems_get
+%define slot-name env_name
+%define initializer-suffix ,""
+struct env_to_conf_item;
+%%
+BASEDIR, "base_dir"
+CC, "compiler"
+COMPILERCHECK, "compiler_check"
+COMPRESS, "compression"
+COMPRESSLEVEL, "compression_level"
+CPP2, "run_second_cpp"
+DIR, "cache_dir"
+DIRECT, "direct_mode"
+DISABLE, "disable"
+EXTENSION, "cpp_extension"
+EXTRAFILES, "extra_files_to_hash"
+HARDLINK, "hard_link"
+HASHDIR, "hash_dir"
+LOGFILE, "log_file"
+MAXFILES, "max_files"
+MAXSIZE, "max_size"
+NLEVELS, "cache_dir_levels"
+PATH, "path"
+PREFIX, "prefix_command"
+READONLY, "read_only"
+READONLY_DIRECT, "read_only_direct"
+RECACHE, "recache"
+SLOPPINESS, "sloppiness"
+STATS, "stats"
+TEMPDIR, "temporary_dir"
+UMASK, "umask"
+UNIFY, "unify"
diff --git a/envtoconfitems_lookup.c b/envtoconfitems_lookup.c
new file mode 100644 (file)
index 0000000..2bfd2f8
--- /dev/null
@@ -0,0 +1,192 @@
+/* ANSI-C code produced by gperf version 3.0.4 */
+/* Command-line: gperf envtoconfitems.gperf  */
+/* Computed positions: -k'1,5' */
+
+#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
+      && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
+      && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
+      && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
+      && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
+      && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
+      && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
+      && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
+      && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
+      && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
+      && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
+      && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
+      && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
+      && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
+      && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
+      && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
+      && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
+      && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
+      && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
+      && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
+      && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
+      && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
+      && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
+/* The character set is not based on ISO-646.  */
+#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gnu-gperf@gnu.org>."
+#endif
+
+#line 9 "envtoconfitems.gperf"
+struct env_to_conf_item;
+/* maximum key range = 41, duplicates = 0 */
+
+#ifdef __GNUC__
+__inline
+#else
+#ifdef __cplusplus
+inline
+#endif
+#endif
+static unsigned int
+envtoconfitems_hash (register const char *str, register unsigned int len)
+{
+  static const unsigned char asso_values[] =
+    {
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 20,  0,  0, 10,
+      20, 43, 15, 43, 10, 43,  5, 10, 15,  0,
+       5, 10,  5,  0,  0,  0, 43, 43, 43, 43,
+      10, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
+      43, 43, 43, 43, 43, 43, 43
+    };
+  register int hval = len;
+
+  switch (hval)
+    {
+      default:
+        hval += asso_values[(unsigned char)str[4]+1];
+      /*FALLTHROUGH*/
+      case 4:
+      case 3:
+      case 2:
+      case 1:
+        hval += asso_values[(unsigned char)str[0]];
+        break;
+    }
+  return hval;
+}
+
+#ifdef __GNUC__
+__inline
+#if defined __GNUC_STDC_INLINE__ || defined __GNUC_GNU_INLINE__
+__attribute__ ((__gnu_inline__))
+#endif
+#endif
+const struct env_to_conf_item *
+envtoconfitems_get (register const char *str, register unsigned int len)
+{
+  enum
+    {
+      TOTAL_KEYWORDS = 27,
+      MIN_WORD_LENGTH = 2,
+      MAX_WORD_LENGTH = 15,
+      MIN_HASH_VALUE = 2,
+      MAX_HASH_VALUE = 42
+    };
+
+  static const struct env_to_conf_item wordlist[] =
+    {
+      {"",""}, {"",""},
+#line 12 "envtoconfitems.gperf"
+      {"CC", "compiler"},
+#line 17 "envtoconfitems.gperf"
+      {"DIR", "cache_dir"},
+#line 16 "envtoconfitems.gperf"
+      {"CPP2", "run_second_cpp"},
+#line 34 "envtoconfitems.gperf"
+      {"STATS", "stats"},
+#line 18 "envtoconfitems.gperf"
+      {"DIRECT", "direct_mode"},
+#line 19 "envtoconfitems.gperf"
+      {"DISABLE", "disable"},
+#line 14 "envtoconfitems.gperf"
+      {"COMPRESS", "compression"},
+#line 28 "envtoconfitems.gperf"
+      {"PATH", "path"},
+#line 36 "envtoconfitems.gperf"
+      {"UMASK", "umask"},
+      {"",""},
+#line 32 "envtoconfitems.gperf"
+      {"RECACHE", "recache"},
+#line 15 "envtoconfitems.gperf"
+      {"COMPRESSLEVEL", "compression_level"},
+      {"",""},
+#line 37 "envtoconfitems.gperf"
+      {"UNIFY", "unify"},
+      {"",""},
+#line 35 "envtoconfitems.gperf"
+      {"TEMPDIR", "temporary_dir"},
+#line 30 "envtoconfitems.gperf"
+      {"READONLY", "read_only"},
+#line 20 "envtoconfitems.gperf"
+      {"EXTENSION", "cpp_extension"},
+#line 33 "envtoconfitems.gperf"
+      {"SLOPPINESS", "sloppiness"},
+#line 29 "envtoconfitems.gperf"
+      {"PREFIX", "prefix_command"},
+#line 24 "envtoconfitems.gperf"
+      {"LOGFILE", "log_file"},
+#line 13 "envtoconfitems.gperf"
+      {"COMPILERCHECK", "compiler_check"},
+      {"",""},
+#line 31 "envtoconfitems.gperf"
+      {"READONLY_DIRECT", "read_only_direct"},
+      {"",""},
+#line 26 "envtoconfitems.gperf"
+      {"MAXSIZE", "max_size"},
+#line 25 "envtoconfitems.gperf"
+      {"MAXFILES", "max_files"},
+      {"",""}, {"",""}, {"",""},
+#line 23 "envtoconfitems.gperf"
+      {"HASHDIR", "hash_dir"},
+#line 22 "envtoconfitems.gperf"
+      {"HARDLINK", "hard_link"},
+      {"",""}, {"",""}, {"",""},
+#line 11 "envtoconfitems.gperf"
+      {"BASEDIR", "base_dir"},
+      {"",""}, {"",""},
+#line 21 "envtoconfitems.gperf"
+      {"EXTRAFILES", "extra_files_to_hash"},
+      {"",""},
+#line 27 "envtoconfitems.gperf"
+      {"NLEVELS", "cache_dir_levels"}
+    };
+
+  if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
+    {
+      register int key = envtoconfitems_hash (str, len);
+
+      if (key <= MAX_HASH_VALUE && key >= 0)
+        {
+          register const char *s = wordlist[key].env_name;
+
+          if (*str == *s && !strcmp (str + 1, s + 1))
+            return &wordlist[key];
+        }
+    }
+  return 0;
+}
+static const size_t ENVTOCONFITEMS_TOTAL_KEYWORDS = 27;
index 7c5e8b5d8f99b9d7fdda52fc2f59558bb0d053de..78f1e4b7888e9a948c58ee92042e4e10c252ecf8 100644 (file)
--- a/execute.c
+++ b/execute.c
@@ -19,6 +19,8 @@
 
 #include "ccache.h"
 
+extern struct conf *conf;
+
 static char *
 find_executable_in_path(const char *name, const char *exclude_name, char *path);
 
@@ -27,8 +29,8 @@ find_executable_in_path(const char *name, const char *exclude_name, char *path);
  * Re-create a win32 command line string based on **argv.
  * http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
  */
-static char *
-argvtos(char *prefix, char **argv)
+char *
+win32argvtos(char *prefix, char **argv)
 {
        char *arg;
        char *ptr;
@@ -56,7 +58,7 @@ argvtos(char *prefix, char **argv)
        } while ((arg = argv[i++]));
 
        str = ptr = malloc(l + 1);
-       if (str == NULL)
+       if (!str)
                return NULL;
 
        i = 0;
@@ -88,22 +90,13 @@ argvtos(char *prefix, char **argv)
        return str;
 }
 
-int
-win32execute(char *path, char **argv, int doreturn,
-             const char *path_stdout, const char *path_stderr)
+char *
+win32getshell(char *path)
 {
-       PROCESS_INFORMATION pi;
-       STARTUPINFO si;
-       BOOL ret;
-       DWORD exitcode;
        char *path_env;
        char *sh = NULL;
-       char *args;
        const char *ext;
 
-       memset(&pi, 0x00, sizeof(pi));
-       memset(&si, 0x00, sizeof(si));
-
        ext = get_extension(path);
        if (ext && strcasecmp(ext, ".sh") == 0 && (path_env = getenv("PATH")))
                sh = find_executable_in_path("sh.exe", NULL, path_env);
@@ -120,6 +113,25 @@ win32execute(char *path, char **argv, int doreturn,
                        fclose(fp);
                }
        }
+
+       return sh;
+}
+
+int
+win32execute(char *path, char **argv, int doreturn,
+             const char *path_stdout, const char *path_stderr)
+{
+       PROCESS_INFORMATION pi;
+       STARTUPINFO si;
+       BOOL ret;
+       DWORD exitcode;
+       char *sh = NULL;
+       char *args;
+
+       memset(&pi, 0x00, sizeof(pi));
+       memset(&si, 0x00, sizeof(si));
+
+       sh = win32getshell(path);
        if (sh)
                path = sh;
 
@@ -138,7 +150,7 @@ win32execute(char *path, char **argv, int doreturn,
                    si.hStdError  == INVALID_HANDLE_VALUE)
                        return -1;
        }
-       args = argvtos(sh, argv);
+       args = win32argvtos(sh, argv);
        ret = CreateProcess(path, args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
        free(args);
        if (path_stdout) {
@@ -163,27 +175,16 @@ win32execute(char *path, char **argv, int doreturn,
   the full path to the compiler to run is in argv[0]
 */
 int
-execute(char **argv, const char *path_stdout, const char *path_stderr)
+execute(char **argv, int fd_out, int fd_err)
 {
        pid_t pid;
-       int status, fd_out, fd_err;
+       int status;
 
        cc_log_argv("Executing ", argv);
-
-       tmp_unlink(path_stdout);
-       fd_out = open(path_stdout, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
-       if (fd_out == -1) {
-               fatal("Error creating %s: %s", path_stdout, strerror(errno));
-       }
-
-       tmp_unlink(path_stderr);
-       fd_err = open(path_stderr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
-       if (fd_err == -1) {
-               fatal("Error creating %s: %s", path_stderr, strerror(errno));
-       }
-
        pid = fork();
-       if (pid == -1) fatal("Failed to fork: %s", strerror(errno));
+       if (pid == -1) {
+               fatal("Failed to fork: %s", strerror(errno));
+       }
 
        if (pid == 0) {
                /* Child. */
@@ -223,8 +224,8 @@ find_executable(const char *name, const char *exclude_name)
                return x_strdup(name);
        }
 
-       path = getenv("CCACHE_PATH");
-       if (!path) {
+       path = conf->path;
+       if (str_eq(path, "")) {
                path = getenv("PATH");
        }
        if (!path) {
@@ -249,11 +250,14 @@ find_executable_in_path(const char *name, const char *exclude_name, char *path)
             tok = strtok_r(NULL, PATH_DELIM, &saveptr)) {
 #ifdef _WIN32
                char namebuf[MAX_PATH];
-               int ret = SearchPath(tok, name, ".exe",
+               int ret = SearchPath(tok, name, NULL,
                                     sizeof(namebuf), namebuf, NULL);
-               if (!ret)
-                       ret = SearchPath(tok, name, NULL,
+               if (!ret) {
+                       char *exename = format("%s.exe", name);
+                       ret = SearchPath(tok, exename, NULL,
                                         sizeof(namebuf), namebuf, NULL);
+                       free(exename);
+               }
                (void) exclude_name;
                if (ret) {
                        free(path);
index 1393f3dd1bc7e5b2735020264d6bb02b9a2a9df3..bf98e5ec741a6a91517c0b4c522c1a507464043b 100644 (file)
--- a/exitfn.c
+++ b/exitfn.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -50,8 +50,8 @@ exitfn_init(void)
 }
 
 /*
- * Add a nullary function to be called context when ccache exits. Functions are
- * called in reverse order.
+ * Add a nullary function to be called when ccache exits. Functions are called
+ * in reverse order.
  */
 void
 exitfn_add_nullary(void (*function)(void))
diff --git a/hash.c b/hash.c
index b6a9e5ea2dad46f4cad36106ee6bc7f9403a7124..80beed23aebf6bcdd9f94b0a7dd5ee9103302f89 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -80,7 +80,13 @@ hash_delimiter(struct mdfour *md, const char *type)
 void
 hash_string(struct mdfour *md, const char *s)
 {
-       hash_buffer(md, s, strlen(s));
+       hash_string_length(md, s, strlen(s));
+}
+
+void
+hash_string_length(struct mdfour *md, const char *s, int length)
+{
+        hash_buffer(md, s, length);
 }
 
 void
index 1ead7680e0c439c463e0514aad68c70b3d871b3f..82b73675c88aa3761adf7b9c36ba03fd7278c5db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2015 Joel Rosdahl
+ * Copyright (C) 2009-2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -18,8 +18,8 @@
 
 #include "ccache.h"
 #include "hashutil.h"
-#include "macroskip.h"
 #include "murmurhashneutral2.h"
+#include "macroskip.h"
 
 unsigned
 hash_from_string(void *str)
@@ -46,16 +46,6 @@ file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2)
                && fh1->size == fh2->size;
 }
 
-#define HASH(ch) \
-       do {\
-               hashbuf[hashbuflen] = ch; \
-               hashbuflen++; \
-               if (hashbuflen == sizeof(hashbuf)) {\
-                       hash_buffer(hash, hashbuf, sizeof(hashbuf)); \
-                       hashbuflen = 0; \
-               } \
-       } while (0)
-
 /*
  * Search for the strings "__DATE__" and "__TIME__" in str.
  *
@@ -94,7 +84,7 @@ check_for_temporal_macros(const char *str, size_t len)
                                result |= HASH_SOURCE_CODE_FOUND_DATE;
                        }
                        else if (str[i - 5] == 'T' && str[i - 4] == 'I' &&
-                                str[i - 3] == 'M') {
+                                str[i - 3] == 'M') {
                                result |= HASH_SOURCE_CODE_FOUND_TIME;
                        }
                }
@@ -110,21 +100,20 @@ check_for_temporal_macros(const char *str, size_t len)
 }
 
 /*
- * Hash a string ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
- * results.
+ * Hash a string. Returns a bitmask of HASH_SOURCE_CODE_* results.
  */
 int
 hash_source_code_string(
-       struct mdfour *hash, const char *str, size_t len, const char *path)
+       struct conf *conf, struct mdfour *hash, const char *str, size_t len,
+       const char *path)
 {
        int result = HASH_SOURCE_CODE_OK;
-       extern unsigned sloppiness;
 
        /*
         * Check for __DATE__ and __TIME__ if the sloppiness configuration tells us
         * we should.
         */
-       if (!(sloppiness & SLOPPY_TIME_MACROS)) {
+       if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
                result |= check_for_temporal_macros(str, len);
        }
 
@@ -166,11 +155,10 @@ hash_source_code_string(
  * results.
  */
 int
-hash_source_code_file(struct mdfour *hash, const char *path)
+hash_source_code_file(struct conf *conf, struct mdfour *hash, const char *path)
 {
        char *data;
        size_t size;
-       int result;
 
        if (is_precompiled_header(path)) {
                if (hash_file(hash, path)) {
@@ -179,10 +167,12 @@ hash_source_code_file(struct mdfour *hash, const char *path)
                        return HASH_SOURCE_CODE_ERROR;
                }
        } else {
+               int result;
+
                if (!read_file(path, 0, &data, &size)) {
                        return HASH_SOURCE_CODE_ERROR;
                }
-               result = hash_source_code_string(hash, data, size, path);
+               result = hash_source_code_string(conf, hash, data, size, path);
                free(data);
                return result;
        }
@@ -192,8 +182,22 @@ bool
 hash_command_output(struct mdfour *hash, const char *command,
                     const char *compiler)
 {
+#ifdef _WIN32
+       SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
+       HANDLE pipe_out[2];
+       PROCESS_INFORMATION pi;
+       STARTUPINFO si;
+       DWORD exitcode;
+       char *sh = NULL;
+       char *win32args;
+       char *path;
+       BOOL ret;
+       bool ok;
+       int fd;
+#else
        pid_t pid;
        int pipefd[2];
+#endif
 
        struct args *args = args_init_from_string(command);
        int i;
@@ -204,6 +208,51 @@ hash_command_output(struct mdfour *hash, const char *command,
        }
        cc_log_argv("Executing compiler check command ", args->argv);
 
+#ifdef _WIN32
+       memset(&pi, 0x00, sizeof(pi));
+       memset(&si, 0x00, sizeof(si));
+
+       path = find_executable(args->argv[0], NULL);
+       if (!path)
+               path = args->argv[0];
+       sh = win32getshell(path);
+       if (sh)
+               path = sh;
+
+       si.cb = sizeof(STARTUPINFO);
+       CreatePipe(&pipe_out[0], &pipe_out[1], &sa, 0);
+       SetHandleInformation(pipe_out[0], HANDLE_FLAG_INHERIT, 0);
+       si.hStdOutput = pipe_out[1];
+       si.hStdError  = pipe_out[1];
+       si.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
+       si.dwFlags    = STARTF_USESTDHANDLES;
+       win32args = win32argvtos(sh, args->argv);
+       ret = CreateProcess(path, win32args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+       CloseHandle(pipe_out[1]);
+       args_free(args);
+       free(win32args);
+       if (ret == 0) {
+               stats_update(STATS_COMPCHECK);
+               return false;
+       }
+       fd = _open_osfhandle((intptr_t) pipe_out[0], O_BINARY);
+       ok = hash_fd(hash, fd);
+       if (!ok) {
+               cc_log("Error hashing compiler check command output: %s", strerror(errno));
+               stats_update(STATS_COMPCHECK);
+       }
+       WaitForSingleObject(pi.hProcess, INFINITE);
+       GetExitCodeProcess(pi.hProcess, &exitcode);
+       CloseHandle(pipe_out[0]);
+       CloseHandle(pi.hProcess);
+       CloseHandle(pi.hThread);
+       if (exitcode != 0) {
+               cc_log("Compiler check command returned %d", (int) exitcode);
+               stats_update(STATS_COMPCHECK);
+               return false;
+       }
+       return ok;
+#else
        if (pipe(pipefd) == -1) {
                fatal("pipe failed");
        }
@@ -243,6 +292,7 @@ hash_command_output(struct mdfour *hash, const char *command,
                }
                return ok;
        }
+#endif
 }
 
 bool
index ec4e059fdac9876a6a431041f84a8e2b4d7aed25..ae9abf19f190a974a08cd54211c3e2a1338317e0 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef HASHUTIL_H
 #define HASHUTIL_H
 
+#include "conf.h"
 #include "mdfour.h"
 #include <inttypes.h>
 
@@ -22,8 +23,10 @@ int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2);
 
 int check_for_temporal_macros(const char *str, size_t len);
 int hash_source_code_string(
-       struct mdfour *hash, const char *str, size_t len, const char *path);
-int hash_source_code_file(struct mdfour *hash, const char *path);
+       struct conf *conf, struct mdfour *hash, const char *str, size_t len,
+       const char *path);
+int hash_source_code_file(
+       struct conf *conf, struct mdfour *hash, const char *path);
 bool hash_command_output(struct mdfour *hash, const char *command,
                          const char *compiler);
 bool hash_multicommand_output(struct mdfour *hash, const char *command,
index acbeb89c39aa8454e3d22408fa1d60d6695ac0e8..110066ac957c823241566637e8bbbe657cca04f8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2013 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -44,8 +44,8 @@ static const struct {
        /* Preprocessed: */
        {".i",   "cpp-output"},
        {".ii",  "c++-cpp-output"},
-       {".mi",  "objc-cpp-output"},
-       {".mii", "objc++-cpp-output"},
+       {".mi",  "objective-c-cpp-output"},
+       {".mii", "objective-c++-cpp-output"},
        /* Header file (for precompilation): */
        {".h",   "c-header"},
        {".H",   "c++-header"},
@@ -70,18 +70,20 @@ static const struct {
        const char *language;
        const char *p_language;
 } languages[] = {
-       {"c",                    "cpp-output"},
-       {"cpp-output",           "cpp-output"},
-       {"c-header",             "cpp-output"},
-       {"c++",                  "c++-cpp-output"},
-       {"c++-cpp-output",       "c++-cpp-output"},
-       {"c++-header",           "c++-cpp-output"},
-       {"objective-c",          "objc-cpp-output"},
-       {"objective-c-header",   "objc-cpp-output"},
-       {"objc-cpp-output",      "objc-cpp-output"},
-       {"objective-c++",        "objc++-cpp-output"},
-       {"objc++-cpp-output",    "objc++-cpp-output"},
-       {"objective-c++-header", "objc++-cpp-output"},
+       {"c",                        "cpp-output"},
+       {"cpp-output",               "cpp-output"},
+       {"c-header",                 "cpp-output"},
+       {"c++",                      "c++-cpp-output"},
+       {"c++-cpp-output",           "c++-cpp-output"},
+       {"c++-header",               "c++-cpp-output"},
+       {"objective-c",              "objective-c-cpp-output"},
+       {"objective-c-header",       "objective-c-cpp-output"},
+       {"objc-cpp-output",          "objective-c-cpp-output"},
+       {"objective-c-cpp-output",   "objective-c-cpp-output"},
+       {"objective-c++",            "objective-c++-cpp-output"},
+       {"objc++-cpp-output",        "objective-c++-cpp-output"},
+       {"objective-c++-header",     "objective-c++-cpp-output"},
+       {"objective-c++-cpp-output", "objective-c++-cpp-output"},
        {NULL,  NULL}};
 
 /*
@@ -145,7 +147,7 @@ extension_for_language(const char *language)
 bool
 language_is_supported(const char *language)
 {
-       return p_language_for_language(language) != NULL;
+       return p_language_for_language(language);
 }
 
 bool
index 84144a3ea48f3dd21aecf8b9ec933c7991dc5438..af4fa43af406098bd30644ff40db3846164fa1ca 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2011 Joel Rosdahl
+ * Copyright (C) 2010-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -43,7 +43,7 @@ lockfile_acquire(const char *path, unsigned staleness_limit)
 #endif
        unsigned to_sleep = 1000, slept = 0; /* Microseconds. */
 
-       while (1) {
+       while (true) {
                free(my_content);
                my_content = format("%s:%d:%d", hostname, (int)getpid(), (int)time(NULL));
 
@@ -51,6 +51,13 @@ lockfile_acquire(const char *path, unsigned staleness_limit)
                fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0666);
                if (fd == -1) {
                        cc_log("lockfile_acquire: open WRONLY %s: %s", lockfile, strerror(errno));
+                       if (errno == ENOENT) {
+                               /* Directory doesn't exist? */
+                               if (create_parent_dirs(lockfile) == 0) {
+                                       /* OK. Retry. */
+                                       continue;
+                               }
+                       }
                        if (errno != EEXIST) {
                                /* Directory doesn't exist or isn't writable? */
                                goto out;
@@ -99,6 +106,13 @@ lockfile_acquire(const char *path, unsigned staleness_limit)
                        goto out;
                }
                cc_log("lockfile_acquire: symlink %s: %s", lockfile, strerror(errno));
+               if (errno == ENOENT) {
+                       /* Directory doesn't exist? */
+                       if (create_parent_dirs(lockfile) == 0) {
+                               /* OK. Retry. */
+                               continue;
+                       }
+               }
                if (errno == EPERM) {
                        /*
                         * The file system does not support symbolic links. We have no choice but
index 7977418b0e3eee9c2d7f2504f7b485bdec014f01..6eb53fd92996ef9f9cff65bf07d9f044be97e47d 100644 (file)
  * <index[0]>      index of include file path          (4 bytes unsigned int)
  * <hash[0]>       hash of include file                (<hash_size> bytes)
  * <size[0]>       size of include file                (4 bytes unsigned int)
+ * <mtime[0]>      mtime of include file               (8 bytes signed int)
+ * <ctime[0]>      ctime of include file               (8 bytes signed int)
  * ...
+ * <index[n-1]>
  * <hash[n-1]>
  * <size[n-1]>
- * <index[n-1]>
+ * <mtime[n-1]>
+ * <ctime[n-1]>
  * ----------------------------------------------------------------------------
  * <n>             number of object name entries       (4 bytes unsigned int)
  * <m[0]>          number of include file hash indexes (4 bytes unsigned int)
  */
 
 static const uint32_t MAGIC = 0x63436d46U;
-static const uint8_t  VERSION = 0;
 static const uint32_t MAX_MANIFEST_ENTRIES = 100;
 static const uint32_t MAX_MANIFEST_FILE_INFO_ENTRIES = 10000;
 
 #define ccache_static_assert(e) \
-       do { enum { ccache_static_assert__ = 1/(e) }; } while (0)
+       do { enum { ccache_static_assert__ = 1/(e) }; } while (false)
 
 struct file_info {
        /* Index to n_files. */
@@ -77,6 +80,10 @@ struct file_info {
        uint8_t hash[16];
        /* Size of referenced file. */
        uint32_t size;
+       /* mtime of referenced file. */
+       int64_t mtime;
+       /* ctime of referenced file. */
+       int64_t ctime;
 };
 
 struct object {
@@ -89,6 +96,12 @@ struct object {
 };
 
 struct manifest {
+       /* Version of decoded file. */
+       uint8_t version;
+
+       /* Reserved for future use. */
+       uint16_t reserved;
+
        /* Size of hash fields (in bytes). */
        uint8_t hash_size;
 
@@ -105,10 +118,16 @@ struct manifest {
        struct object *objects;
 };
 
+struct file_stats {
+       uint32_t size;
+       int64_t mtime;
+       int64_t ctime;
+};
+
 static unsigned int
 hash_from_file_info(void *key)
 {
-       ccache_static_assert(sizeof(struct file_info) == 24); /* No padding. */
+       ccache_static_assert(sizeof(struct file_info) == 40); /* No padding. */
        return murmurhashneutral2(key, sizeof(struct file_info), 0);
 }
 
@@ -119,7 +138,9 @@ file_infos_equal(void *key1, void *key2)
        struct file_info *fi2 = (struct file_info *)key2;
        return fi1->index == fi2->index
               && memcmp(fi1->hash, fi2->hash, 16) == 0
-              && fi1->size == fi2->size;
+              && fi1->size == fi2->size
+              && fi1->mtime == fi2->mtime
+              && fi1->ctime == fi2->ctime;
 }
 
 static void
@@ -146,7 +167,7 @@ free_manifest(struct manifest *mf)
                        goto error; \
                } \
                (var) = ch_ & 0xFF; \
-       } while (0)
+       } while (false)
 
 #define READ_INT(size, var) \
        do { \
@@ -161,7 +182,7 @@ free_manifest(struct manifest *mf)
                        (var) <<= 8; \
                        (var) |= ch_ & 0xFF; \
                } \
-       } while (0)
+       } while (false)
 
 #define READ_STR(var) \
        do { \
@@ -182,7 +203,7 @@ free_manifest(struct manifest *mf)
                        goto error; \
                } \
                (var) = x_strdup(buf_); \
-       } while (0)
+       } while (false)
 
 #define READ_BYTES(n, var) \
        do { \
@@ -195,7 +216,7 @@ free_manifest(struct manifest *mf)
                        } \
                        (var)[i_] = ch_; \
                } \
-       } while (0)
+       } while (false)
 
 static struct manifest *
 create_empty_manifest(void)
@@ -220,8 +241,6 @@ read_manifest(gzFile f)
        struct manifest *mf;
        uint32_t i, j;
        uint32_t magic;
-       uint8_t version;
-       uint16_t dummy;
 
        mf = create_empty_manifest();
 
@@ -231,9 +250,9 @@ read_manifest(gzFile f)
                free_manifest(mf);
                return NULL;
        }
-       READ_BYTE(version);
-       if (version != VERSION) {
-               cc_log("Manifest file has unknown version %u", version);
+       READ_BYTE(mf->version);
+       if (mf->version != MANIFEST_VERSION) {
+               cc_log("Manifest file has unknown version %u", mf->version);
                free_manifest(mf);
                return NULL;
        }
@@ -246,7 +265,7 @@ read_manifest(gzFile f)
                return NULL;
        }
 
-       READ_INT(2, dummy);
+       READ_INT(2, mf->reserved);
 
        READ_INT(4, mf->n_files);
        mf->files = x_calloc(mf->n_files, sizeof(*mf->files));
@@ -260,6 +279,8 @@ read_manifest(gzFile f)
                READ_INT(4, mf->file_infos[i].index);
                READ_BYTES(mf->hash_size, mf->file_infos[i].hash);
                READ_INT(4, mf->file_infos[i].size);
+               READ_INT(8, mf->file_infos[i].mtime);
+               READ_INT(8, mf->file_infos[i].ctime);
        }
 
        READ_INT(4, mf->n_objects);
@@ -294,14 +315,14 @@ error:
                                goto error; \
                        } \
                } \
-       } while (0)
+       } while (false)
 
 #define WRITE_STR(var) \
        do { \
                if (gzputs(f, var) == EOF || gzputc(f, '\0') == EOF) { \
                        goto error; \
                } \
-       } while (0)
+       } while (false)
 
 #define WRITE_BYTES(n, var) \
        do { \
@@ -311,7 +332,7 @@ error:
                                goto error; \
                        } \
                } \
-       } while (0)
+       } while (false)
 
 static int
 write_manifest(gzFile f, const struct manifest *mf)
@@ -319,7 +340,7 @@ write_manifest(gzFile f, const struct manifest *mf)
        uint32_t i, j;
 
        WRITE_INT(4, MAGIC);
-       WRITE_INT(1, VERSION);
+       WRITE_INT(1, MANIFEST_VERSION);
        WRITE_INT(1, 16);
        WRITE_INT(2, 0);
 
@@ -333,6 +354,8 @@ write_manifest(gzFile f, const struct manifest *mf)
                WRITE_INT(4, mf->file_infos[i].index);
                WRITE_BYTES(mf->hash_size, mf->file_infos[i].hash);
                WRITE_INT(4, mf->file_infos[i].size);
+               WRITE_INT(8, mf->file_infos[i].mtime);
+               WRITE_INT(8, mf->file_infos[i].ctime);
        }
 
        WRITE_INT(4, mf->n_objects);
@@ -353,24 +376,59 @@ error:
 }
 
 static int
-verify_object(struct manifest *mf, struct object *obj,
-              struct hashtable *hashed_files)
+verify_object(struct conf *conf, struct manifest *mf, struct object *obj,
+              struct hashtable *stated_files, struct hashtable *hashed_files)
 {
        uint32_t i;
        struct file_info *fi;
        struct file_hash *actual;
+       struct file_stats *st;
        struct mdfour hash;
        int result;
+       char *path;
 
        for (i = 0; i < obj->n_file_info_indexes; i++) {
                fi = &mf->file_infos[obj->file_info_indexes[i]];
-               actual = hashtable_search(hashed_files, mf->files[fi->index]);
+               path = mf->files[fi->index];
+               st = hashtable_search(hashed_files, path);
+               if (!st) {
+                       struct stat file_stat;
+                       if (stat(path, &file_stat) == -1) {
+                               cc_log("Failed to stat include file %s: %s", path, strerror(errno));
+                               return 0;
+                       }
+                       st = x_malloc(sizeof(*st));
+                       st->size = file_stat.st_size;
+                       st->mtime = file_stat.st_mtime;
+                       st->ctime = file_stat.st_ctime;
+                       hashtable_insert(stated_files, x_strdup(path), st);
+               }
+
+               if (conf->sloppiness & SLOPPY_FILE_STAT_MATCHES) {
+                       /*
+                        * st->ctime is sometimes 0, so we can't check that both st->ctime and
+                        * st->mtime are greater than time_of_compilation. But it's sufficient to
+                        * check that either is.
+                        */
+                       if (fi->size == st->size
+                           && fi->mtime == st->mtime
+                           && fi->ctime == st->ctime
+                           && MAX(st->mtime, st->ctime) >= time_of_compilation) {
+                               cc_log("size/mtime/ctime hit for %s", path);
+                               continue;
+                       }
+                       else {
+                               cc_log("size/mtime/ctime miss for %s", path);
+                       }
+               }
+
+               actual = hashtable_search(hashed_files, path);
                if (!actual) {
                        actual = x_malloc(sizeof(*actual));
                        hash_start(&hash);
-                       result = hash_source_code_file(&hash, mf->files[fi->index]);
+                       result = hash_source_code_file(conf, &hash, path);
                        if (result & HASH_SOURCE_CODE_ERROR) {
-                               cc_log("Failed hashing %s", mf->files[fi->index]);
+                               cc_log("Failed hashing %s", path);
                                free(actual);
                                return 0;
                        }
@@ -380,7 +438,7 @@ verify_object(struct manifest *mf, struct object *obj,
                        }
                        hash_result_as_bytes(&hash, actual->hash);
                        actual->size = hash.totalN;
-                       hashtable_insert(hashed_files, x_strdup(mf->files[fi->index]), actual);
+                       hashtable_insert(hashed_files, x_strdup(path), actual);
                }
                if (memcmp(fi->hash, actual->hash, mf->hash_size) != 0
                    || fi->size != actual->size) {
@@ -456,11 +514,31 @@ get_file_hash_index(struct manifest *mf,
        struct file_info fi;
        uint32_t *fi_index;
        uint32_t n;
+       struct stat file_stat;
 
        fi.index = get_include_file_index(mf, path, mf_files);
        memcpy(fi.hash, file_hash->hash, sizeof(fi.hash));
        fi.size = file_hash->size;
 
+       /*
+        * file_stat.st_{m,c}time has a resolution of 1 second, so we can cache the
+        * file's mtime and ctime only if they're at least one second older than
+        * time_of_compilation.
+        *
+        * st->ctime may be 0, so we have to check time_of_compilation against
+        * MAX(mtime, ctime).
+        */
+
+       if (stat(path, &file_stat) != -1
+           && time_of_compilation > MAX(file_stat.st_mtime, file_stat.st_ctime)) {
+               fi.mtime = file_stat.st_mtime;
+               fi.ctime = file_stat.st_ctime;
+       }
+       else {
+               fi.mtime = -1;
+               fi.ctime = -1;
+       }
+
        fi_index = hashtable_search(mf_file_infos, &fi);
        if (fi_index) {
                return *fi_index;
@@ -532,12 +610,13 @@ add_object_entry(struct manifest *mf,
  * on failure.
  */
 struct file_hash *
-manifest_get(const char *manifest_path)
+manifest_get(struct conf *conf, const char *manifest_path)
 {
        int fd;
        gzFile f = NULL;
        struct manifest *mf = NULL;
        struct hashtable *hashed_files = NULL; /* path --> struct file_hash */
+       struct hashtable *stated_files = NULL; /* path --> struct file_stats */
        uint32_t i;
        struct file_hash *fh = NULL;
 
@@ -560,10 +639,12 @@ manifest_get(const char *manifest_path)
        }
 
        hashed_files = create_hashtable(1000, hash_from_string, strings_equal);
+       stated_files = create_hashtable(1000, hash_from_string, strings_equal);
 
        /* Check newest object first since it's a bit more likely to match. */
        for (i = mf->n_objects; i > 0; i--) {
-               if (verify_object(mf, &mf->objects[i - 1], hashed_files)) {
+               if (verify_object(conf, mf, &mf->objects[i - 1],
+                                 stated_files, hashed_files)) {
                        fh = x_malloc(sizeof(*fh));
                        *fh = mf->objects[i - 1].hash;
                        goto out;
@@ -574,6 +655,9 @@ out:
        if (hashed_files) {
                hashtable_destroy(hashed_files, 1);
        }
+       if (stated_files) {
+               hashtable_destroy(stated_files, 1);
+       }
        if (f) {
                gzclose(f);
        }
@@ -652,12 +736,8 @@ manifest_put(const char *manifest_path, struct file_hash *object_hash,
                mf = create_empty_manifest();
        }
 
-       tmp_file = format("%s.tmp.%s", manifest_path, tmp_string());
-       fd2 = safe_open(tmp_file);
-       if (fd2 == -1) {
-               cc_log("Failed to open %s", tmp_file);
-               goto out;
-       }
+       tmp_file = format("%s.tmp", manifest_path);
+       fd2 = create_tmp_fd(&tmp_file);
        f2 = gzdopen(fd2, "wb");
        if (!f2) {
                cc_log("Failed to gzdopen %s", tmp_file);
@@ -691,3 +771,80 @@ out:
        }
        return ret;
 }
+
+bool
+manifest_dump(const char *manifest_path, FILE *stream)
+{
+       struct manifest *mf = NULL;
+       int fd;
+       gzFile f = NULL;
+       bool ret = false;
+       unsigned i, j;
+
+       fd = open(manifest_path, O_RDONLY | O_BINARY);
+       if (fd == -1) {
+               fprintf(stderr, "No such manifest file: %s\n", manifest_path);
+               goto out;
+       }
+       f = gzdopen(fd, "rb");
+       if (!f) {
+               fprintf(stderr, "Failed to dzopen manifest file\n");
+               close(fd);
+               goto out;
+       }
+       mf = read_manifest(f);
+       if (!mf) {
+               fprintf(stderr, "Error reading manifest file\n");
+               goto out;
+       }
+
+       fprintf(stream, "Magic: %c%c%c%c\n",
+               (MAGIC >> 24) & 0xFF,
+               (MAGIC >> 16) & 0xFF,
+               (MAGIC >> 8) & 0xFF,
+               MAGIC & 0xFF);
+       fprintf(stream, "Version: %u\n", mf->version);
+       fprintf(stream, "Hash size: %u\n", (unsigned)mf->hash_size);
+       fprintf(stream, "Reserved field: %u\n", (unsigned)mf->reserved);
+       fprintf(stream, "File paths (%u):\n", (unsigned)mf->n_files);
+       for (i = 0; i < mf->n_files; ++i) {
+               fprintf(stream, "  %u: %s\n", i, mf->files[i]);
+       }
+       fprintf(stream, "File infos (%u):\n", (unsigned)mf->n_file_infos);
+       for (i = 0; i < mf->n_file_infos; ++i) {
+               char *hash;
+               fprintf(stream, "  %u:\n", i);
+               fprintf(stream, "    Path index: %u\n", mf->file_infos[i].index);
+               hash = format_hash_as_string(mf->file_infos[i].hash, -1);
+               fprintf(stream, "    Hash: %s\n", hash);
+               free(hash);
+               fprintf(stream, "    Size: %u\n", mf->file_infos[i].size);
+               fprintf(stream, "    Mtime: %lld\n", (long long)mf->file_infos[i].mtime);
+               fprintf(stream, "    Ctime: %lld\n", (long long)mf->file_infos[i].ctime);
+       }
+       fprintf(stream, "Results (%u):\n", (unsigned)mf->n_objects);
+       for (i = 0; i < mf->n_objects; ++i) {
+               char *hash;
+               fprintf(stream, "  %u:\n", i);
+               fprintf(stream, "    File hash indexes:");
+               for (j = 0; j < mf->objects[i].n_file_info_indexes; ++j) {
+                       fprintf(stream, " %u", mf->objects[i].file_info_indexes[j]);
+               }
+               fprintf(stream, "\n");
+               hash = format_hash_as_string(mf->objects[i].hash.hash, -1);
+               fprintf(stream, "    Hash: %s\n", hash);
+               free(hash);
+               fprintf(stream, "    Size: %u\n", (unsigned)mf->objects[i].hash.size);
+       }
+
+       ret = true;
+
+out:
+       if (mf) {
+               free_manifest(mf);
+       }
+       if (f) {
+               gzclose(f);
+       }
+       return ret;
+}
index 80333e7a2a8cbafedf72bbe6df834e05e2eba25c..e116c3491014b2ca73edc6079e3081248ed6428a 100644 (file)
@@ -1,11 +1,15 @@
 #ifndef MANIFEST_H
 #define MANIFEST_H
 
+#include "conf.h"
 #include "hashutil.h"
 #include "hashtable.h"
 
-struct file_hash *manifest_get(const char *manifest_path);
+#define MANIFEST_VERSION 1
+
+struct file_hash *manifest_get(struct conf *conf, const char *manifest_path);
 bool manifest_put(const char *manifest_path, struct file_hash *object_hash,
                   struct hashtable *included_files);
+bool manifest_dump(const char *manifest_path, FILE *stream);
 
 #endif
index 8d5b9a72f906871d49465e499e57dd5bbb25eccf..06bc17fa6e0f6a376f6b49be82b7eb8ff48216cb 100644 (file)
--- a/mdfour.c
+++ b/mdfour.c
@@ -83,20 +83,28 @@ mdfour64(uint32_t *M)
 static void
 copy64(uint32_t *M, const unsigned char *in)
 {
+#ifdef WORDS_BIGENDIAN
        int i;
 
        for (i = 0; i < 16; i++)
                M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
                        (in[i*4+1]<<8) | (in[i*4+0]<<0);
+#else
+       memcpy(M, in, 16*4);
+#endif
 }
 
 static void
 copy4(unsigned char *out, uint32_t x)
 {
+#ifdef WORDS_BIGENDIAN
        out[0] = x&0xFF;
        out[1] = (x>>8)&0xFF;
        out[2] = (x>>16)&0xFF;
        out[3] = (x>>24)&0xFF;
+#else
+       memcpy(out, &x, 4);
+#endif
 }
 
 void
@@ -122,7 +130,9 @@ void mdfour_tail(const unsigned char *in, size_t n)
 
        b = m->totalN * 8;
 
-       if (n) memcpy(buf, in, n);
+       if (n) {
+               memcpy(buf, in, n);
+       }
        buf[n] = 0x80;
 
        if (n <= 55) {
@@ -153,7 +163,7 @@ mdfour_update(struct mdfour *md, const unsigned char *in, size_t n)
 
        m = md;
 
-       if (in == NULL) {
+       if (!in) {
                if (!md->finalized) {
                        mdfour_tail(md->tail, md->tail_len);
                        md->finalized = 1;
@@ -163,7 +173,9 @@ mdfour_update(struct mdfour *md, const unsigned char *in, size_t n)
 
        if (md->tail_len) {
                size_t len = 64 - md->tail_len;
-               if (len > n) len = n;
+               if (len > n) {
+                       len = n;
+               }
                memcpy(md->tail+md->tail_len, in, len);
                md->tail_len += len;
                n -= len;
diff --git a/stats.c b/stats.c
index 003a55450445b63d36c8a8c7cdfef4dbdd2d859f..71fbb7af6af14b554bef7dd65836e8df9e56a542 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -18,7 +18,7 @@
  */
 
 /*
- * Routines to handle the stats files The stats file is stored one per cache
+ * Routines to handle the stats files. The stats file is stored one per cache
  * subdirectory to make this more scalable.
  */
 
 #include <unistd.h>
 
 extern char *stats_file;
-extern char *cache_dir;
+extern struct conf *conf;
 extern unsigned lock_staleness_limit;
+extern char *primary_config_path;
+extern char *secondary_config_path;
 
 static struct counters *counter_updates;
 
-/* default maximum cache size */
-#ifndef DEFAULT_MAXSIZE
-#define DEFAULT_MAXSIZE (1024*1024)
-#endif
-
 #define FLAG_NOZERO 1 /* don't zero with the -z option */
 #define FLAG_ALWAYS 2 /* always show, even if zero */
+#define FLAG_NEVER 4 /* never show */
 
-static void display_size(size_t v);
+static void display_size_times_1024(uint64_t size);
 
 /* statistics fields in display order */
 static struct {
        enum stats stat;
        char *message;
-       void (*fn)(size_t );
+       void (*fn)(uint64_t);
        unsigned flags;
 } stats_info[] = {
        { STATS_CACHEHIT_DIR, "cache hit (direct)             ", NULL, FLAG_ALWAYS },
@@ -81,20 +79,26 @@ static struct {
        { STATS_NOINPUT,      "no input file                  ", NULL, 0 },
        { STATS_BADEXTRAFILE, "error hashing extra file       ", NULL, 0 },
        { STATS_NUMFILES,     "files in cache                 ", NULL, FLAG_NOZERO|FLAG_ALWAYS },
-       { STATS_TOTALSIZE,    "cache size                     ", display_size , FLAG_NOZERO|FLAG_ALWAYS },
-       { STATS_MAXFILES,     "max files                      ", NULL, FLAG_NOZERO },
-       { STATS_MAXSIZE,      "max cache size                 ", display_size, FLAG_NOZERO },
+       { STATS_TOTALSIZE,    "cache size                     ", display_size_times_1024 , FLAG_NOZERO|FLAG_ALWAYS },
+       { STATS_OBSOLETE_MAXFILES, "OBSOLETE",                   NULL, FLAG_NOZERO|FLAG_NEVER},
+       { STATS_OBSOLETE_MAXSIZE, "OBSOLETE",                    NULL, FLAG_NOZERO|FLAG_NEVER},
        { STATS_NONE, NULL, NULL, 0 }
 };
 
 static void
-display_size(size_t v)
+display_size(uint64_t size)
 {
-       char *s = format_size(v);
-       printf("%15s", s);
+       char *s = format_human_readable_size(size);
+       printf("%11s", s);
        free(s);
 }
 
+static void
+display_size_times_1024(uint64_t size)
+{
+       display_size(size * 1024);
+}
+
 /* parse a stats file from a buffer - adding to the counters */
 static void
 parse_stats(struct counters *counters, const char *buf)
@@ -105,7 +109,7 @@ parse_stats(struct counters *counters, const char *buf)
        long val;
 
        p = buf;
-       while (1) {
+       while (true) {
                val = strtol(p, &p2, 10);
                if (p2 == p) {
                        break;
@@ -127,12 +131,8 @@ stats_write(const char *path, struct counters *counters)
        char *tmp_file;
        FILE *f;
 
-       tmp_file = format("%s.tmp.%s", path, tmp_string());
-       f = fopen(tmp_file, "wb");
-       if (!f) {
-               cc_log("Failed to open %s", tmp_file);
-               goto end;
-       }
+       tmp_file = format("%s.tmp", path);
+       f = create_tmp_file(&tmp_file, "wb");
        for (i = 0; i < counters->size; i++) {
                if (fprintf(f, "%u\n", counters->data[i]) < 0) {
                        fatal("Failed to write to %s", tmp_file);
@@ -140,18 +140,9 @@ stats_write(const char *path, struct counters *counters)
        }
        fclose(f);
        x_rename(tmp_file, path);
-
-end:
        free(tmp_file);
 }
 
-/* fill in some default stats values */
-static void
-stats_default(struct counters *counters)
-{
-       counters->data[STATS_MAXSIZE] += DEFAULT_MAXSIZE / 16;
-}
-
 static void
 init_counter_updates(void)
 {
@@ -161,29 +152,24 @@ init_counter_updates(void)
 }
 
 /*
- * Update a statistics counter (unless it's STATS_NONE) and also record that a
- * number of bytes and files have been added to the cache. Size is in KiB.
+ * Record that a number of bytes and files have been added to the cache. Size
+ * is in KiB.
  */
 void
-stats_update_size(enum stats stat, size_t size, unsigned files)
+stats_update_size(uint64_t size, unsigned files)
 {
        init_counter_updates();
-       if (stat != STATS_NONE) {
-               counter_updates->data[stat]++;
-       }
        counter_updates->data[STATS_NUMFILES] += files;
-       counter_updates->data[STATS_TOTALSIZE] += size;
+       counter_updates->data[STATS_TOTALSIZE] += size / 1024;
 }
 
 /* Read in the stats from one directory and add to the counters. */
 void
 stats_read(const char *sfile, struct counters *counters)
 {
-       char *data = read_text_file(sfile);
+       char *data = read_text_file(sfile, 1024);
        if (data) {
                parse_stats(counters, data);
-       } else {
-               stats_default(counters);
        }
        free(data);
 }
@@ -198,9 +184,10 @@ stats_flush(void)
        bool need_cleanup = false;
        bool should_flush = false;
        int i;
-       extern char *cache_logfile;
 
-       if (getenv("CCACHE_NOSTATS")) {
+       assert(conf);
+
+       if (!conf->stats) {
                return;
        }
 
@@ -225,10 +212,8 @@ stats_flush(void)
                 * A NULL stats_file means that we didn't get past calculate_object_hash(),
                 * so we just choose one of stats files in the 16 subdirectories.
                 */
-               if (!cache_dir) return;
-               stats_dir = format("%s/%x", cache_dir, hash_from_int(getpid()) % 16);
+               stats_dir = format("%s/%x", conf->cache_dir, hash_from_int(getpid()) % 16);
                stats_file = format("%s/stats", stats_dir);
-               create_dir(stats_dir);
                free(stats_dir);
        }
 
@@ -243,7 +228,7 @@ stats_flush(void)
        stats_write(stats_file, counters);
        lockfile_release(stats_file);
 
-       if (cache_logfile) {
+       if (!str_eq(conf->log_file, "")) {
                for (i = 0; i < STATS_END; ++i) {
                        if (counter_updates->data[stats_info[i].stat] != 0
                            && !(stats_info[i].flags & FLAG_NOZERO)) {
@@ -252,29 +237,31 @@ stats_flush(void)
                }
        }
 
-       if (counters->data[STATS_MAXFILES] != 0 &&
-           counters->data[STATS_NUMFILES] > counters->data[STATS_MAXFILES]) {
+       if (conf->max_files != 0
+           && counters->data[STATS_NUMFILES] > conf->max_files / 16) {
                need_cleanup = true;
        }
-       if (counters->data[STATS_MAXSIZE] != 0 &&
-           counters->data[STATS_TOTALSIZE] > counters->data[STATS_MAXSIZE]) {
+       if (conf->max_size != 0
+           && counters->data[STATS_TOTALSIZE] > conf->max_size / 1024 / 16) {
                need_cleanup = true;
        }
 
        if (need_cleanup) {
                char *p = dirname(stats_file);
-               cleanup_dir(p,
-                           counters->data[STATS_MAXFILES],
-                           counters->data[STATS_MAXSIZE]);
+               cleanup_dir(conf, p);
                free(p);
        }
+
+       counters_free(counters);
 }
 
 /* update a normal stat */
 void
 stats_update(enum stats stat)
 {
-       stats_update_size(stat, 0, 0);
+       assert(stat > STATS_NONE && stat < STATS_END);
+       init_counter_updates();
+       counter_updates->data[stat]++;
 }
 
 /* Get the pending update of a counter value. */
@@ -287,36 +274,40 @@ stats_get_pending(enum stats stat)
 
 /* sum and display the total stats for all cache dirs */
 void
-stats_summary(void)
+stats_summary(struct conf *conf)
 {
        int dir, i;
        struct counters *counters = counters_init(STATS_END);
 
+       assert(conf);
+
        /* add up the stats in each directory */
        for (dir = -1; dir <= 0xF; dir++) {
                char *fname;
 
                if (dir == -1) {
-                       fname = format("%s/stats", cache_dir);
+                       fname = format("%s/stats", conf->cache_dir);
                } else {
-                       fname = format("%s/%1x/stats", cache_dir, dir);
+                       fname = format("%s/%1x/stats", conf->cache_dir, dir);
                }
 
                stats_read(fname, counters);
                free(fname);
-
-               /* oh what a nasty hack ... */
-               if (dir == -1) {
-                       counters->data[STATS_MAXSIZE] = 0;
-               }
        }
 
-       printf("cache directory                     %s\n", cache_dir);
+       printf("cache directory                     %s\n", conf->cache_dir);
+       printf("primary config                      %s\n",
+              primary_config_path ? primary_config_path : "");
+       printf("secondary config      (readonly)    %s\n",
+              secondary_config_path ? secondary_config_path : "");
 
        /* and display them */
        for (i = 0; stats_info[i].message; i++) {
                enum stats stat = stats_info[i].stat;
 
+               if (stats_info[i].flags & FLAG_NEVER) {
+                       continue;
+               }
                if (counters->data[stat] == 0 && !(stats_info[i].flags & FLAG_ALWAYS)) {
                        continue;
                }
@@ -330,6 +321,15 @@ stats_summary(void)
                }
        }
 
+       if (conf->max_files != 0) {
+               printf("max files                       %8u\n", conf->max_files);
+       }
+       if (conf->max_size != 0) {
+               printf("max cache size                  ");
+               display_size(conf->max_size);
+               printf("\n");
+       }
+
        counters_free(counters);
 }
 
@@ -341,14 +341,16 @@ stats_zero(void)
        unsigned i;
        char *fname;
 
-       fname = format("%s/stats", cache_dir);
+       assert(conf);
+
+       fname = format("%s/stats", conf->cache_dir);
        x_unlink(fname);
        free(fname);
 
        for (dir = 0; dir <= 0xF; dir++) {
                struct counters *counters = counters_init(STATS_END);
                struct stat st;
-               fname = format("%s/%1x/stats", cache_dir, dir);
+               fname = format("%s/%1x/stats", conf->cache_dir, dir);
                if (stat(fname, &st) != 0) {
                        /* No point in trying to reset the stats file if it doesn't exist. */
                        free(fname);
@@ -371,65 +373,17 @@ stats_zero(void)
 
 /* Get the per directory limits */
 void
-stats_get_limits(const char *dir, unsigned *maxfiles, unsigned *maxsize)
+stats_get_obsolete_limits(const char *dir, unsigned *maxfiles, uint64_t *maxsize)
 {
        struct counters *counters = counters_init(STATS_END);
        char *sname = format("%s/stats", dir);
        stats_read(sname, counters);
-       *maxfiles = counters->data[STATS_MAXFILES];
-       *maxsize = counters->data[STATS_MAXSIZE];
+       *maxfiles = counters->data[STATS_OBSOLETE_MAXFILES];
+       *maxsize = (uint64_t)counters->data[STATS_OBSOLETE_MAXSIZE] * 1024;
        free(sname);
        counters_free(counters);
 }
 
-/* set the per directory limits */
-int
-stats_set_limits(long maxfiles, long maxsize)
-{
-       int dir;
-
-       if (maxfiles != -1) {
-               maxfiles /= 16;
-       }
-       if (maxsize != -1) {
-               maxsize /= 16;
-       }
-
-       if (create_dir(cache_dir) != 0) {
-               return 1;
-       }
-
-       /* set the limits in each directory */
-       for (dir = 0; dir <= 0xF; dir++) {
-               char *fname, *cdir;
-
-               cdir = format("%s/%1x", cache_dir, dir);
-               if (create_dir(cdir) != 0) {
-                       free(cdir);
-                       return 1;
-               }
-               fname = format("%s/stats", cdir);
-               free(cdir);
-
-               if (lockfile_acquire(fname, lock_staleness_limit)) {
-                       struct counters *counters = counters_init(STATS_END);
-                       stats_read(fname, counters);
-                       if (maxfiles != -1) {
-                               counters->data[STATS_MAXFILES] = maxfiles;
-                       }
-                       if (maxsize != -1) {
-                               counters->data[STATS_MAXSIZE] = maxsize;
-                       }
-                       stats_write(fname, counters);
-                       lockfile_release(fname);
-                       counters_free(counters);
-               }
-               free(fname);
-       }
-
-       return 0;
-}
-
 /* set the per directory sizes */
 void
 stats_set_sizes(const char *dir, size_t num_files, size_t total_size)
@@ -437,13 +391,12 @@ stats_set_sizes(const char *dir, size_t num_files, size_t total_size)
        struct counters *counters = counters_init(STATS_END);
        char *statsfile;
 
-       create_dir(dir);
        statsfile = format("%s/stats", dir);
 
        if (lockfile_acquire(statsfile, lock_staleness_limit)) {
                stats_read(statsfile, counters);
                counters->data[STATS_NUMFILES] = num_files;
-               counters->data[STATS_TOTALSIZE] = total_size;
+               counters->data[STATS_TOTALSIZE] = total_size / 1024;
                stats_write(statsfile, counters);
                lockfile_release(statsfile);
        }
index 0d796e8b870b9b44a70a88be69c23d07bce51bfd..0692dcd46af90a4b524c8df29054bbfb55bb94ba 100644 (file)
--- a/system.h
+++ b/system.h
@@ -37,6 +37,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stddef.h>
@@ -47,6 +48,8 @@
 #include <unistd.h>
 #include <utime.h>
 
+extern char **environ;
+
 #if !HAVE_VSNPRINTF
   int rpl_vsnprintf(char *, size_t, const char *, va_list);
   #define vsnprintf rpl_vsnprintf
diff --git a/test.sh b/test.sh
index 490e975762f7851eeac3b4048ca611034abfa589..79e3e0d1ee57031388234570f59cfb980a3395a5 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -3,7 +3,7 @@
 # A simple test suite for ccache.
 #
 # Copyright (C) 2002-2007 Andrew Tridgell
-# Copyright (C) 2009-2016 Joel Rosdahl
+# Copyright (C) 2009-2014 Joel Rosdahl
 #
 # This program is free software; you can redistribute it and/or modify it under
 # the terms of the GNU General Public License as published by the Free Software
@@ -37,12 +37,19 @@ unset CCACHE_NOSTATS
 unset CCACHE_PATH
 unset CCACHE_PREFIX
 unset CCACHE_READONLY
+unset CCACHE_READONLY_DIRECT
 unset CCACHE_RECACHE
 unset CCACHE_SLOPPINESS
 unset CCACHE_TEMPDIR
 unset CCACHE_UMASK
 unset CCACHE_UNIFY
 
+# Many tests backdate files, which updates their ctimes.  In those tests, we
+# must ignore ctimes.  Might as well do so everywhere.
+default_sloppiness=include_file_ctime
+CCACHE_SLOPPINESS="$default_sloppiness"
+export CCACHE_SLOPPINESS
+
 test_failed() {
     echo "SUITE: \"$testsuite\", TEST: \"$testname\" - $1"
     $CCACHE -s
@@ -64,10 +71,9 @@ randcode() {
     ) >> "$outfile"
 }
 
-
 getstat() {
     stat="$1"
-    value=`$CCACHE -s | grep "$stat" | cut -c34-40`
+    value=`$CCACHE -s | grep "$stat" | cut -c34-`
     echo $value
 }
 
@@ -80,6 +86,13 @@ checkstat() {
     fi
 }
 
+compare_file() {
+    cmp -s "$1" "$2"
+    if [ $? -ne 0 ]; then
+        test_failed "Files differ: $1 != $2"
+    fi
+}
+
 checkfile() {
     if [ ! -f $1 ]; then
         test_failed "$1 not found"
@@ -140,17 +153,21 @@ base_tests() {
         j=`expr $j + 1`
     done
 
+    CCACHE_DISABLE=1 $COMPILER -c -o reference_test1.o test1.c
+
     testname="BASIC"
     $CCACHE_COMPILE -c test1.c
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkstat 'files in cache' 1
+    compare_file reference_test1.o test1.o
 
     testname="BASIC2"
     $CCACHE_COMPILE -c test1.c
     checkstat 'cache hit (preprocessed)' 1
     checkstat 'cache miss' 1
     checkstat 'files in cache' 1
+    compare_file reference_test1.o test1.o
 
     testname="debug"
     $CCACHE_COMPILE -c test1.c -g
@@ -167,6 +184,7 @@ base_tests() {
     $CCACHE_COMPILE -c test1.c -o foo.o
     checkstat 'cache hit (preprocessed)' 3
     checkstat 'cache miss' 2
+    compare_file reference_test1.o foo.o
 
     testname="link"
     $CCACHE_COMPILE test1.c -o test 2> /dev/null
@@ -208,7 +226,7 @@ base_tests() {
     testname="non-regular"
     mkdir testd
     $CCACHE_COMPILE -o testd -c test1.c > /dev/null 2>&1
-    rmdir testd
+    rmdir testd > /dev/null 2>&1
     checkstat 'output to a non-regular file' 1
 
     testname="no-input"
@@ -217,10 +235,13 @@ base_tests() {
 
     testname="CCACHE_DISABLE"
     mv $CCACHE_DIR $CCACHE_DIR.saved
+    saved_config_path=$CCACHE_CONFIGPATH
+    unset CCACHE_CONFIGPATH
     CCACHE_DISABLE=1 $CCACHE_COMPILE -c test1.c 2> /dev/null
     if [ -d $CCACHE_DIR ]; then
-        test_failed "$CCACHE_DIR created despite CCACHE_DISABLE being set"
+        test_failed "$CCACHE_DIR created dispite CCACHE_DISABLE being set"
     fi
+    CCACHE_CONFIGPATH=$saved_config_path
     mv $CCACHE_DIR.saved $CCACHE_DIR
     checkstat 'cache hit (preprocessed)' 3
     $CCACHE_COMPILE -c test1.c
@@ -230,10 +251,13 @@ base_tests() {
     CCACHE_CPP2=1 $CCACHE_COMPILE -c test1.c -O -O
     checkstat 'cache hit (preprocessed)' 4
     checkstat 'cache miss' 3
+    CCACHE_DISABLE=1 $COMPILER -c test1.c -o reference_test1.o -O -O
+    compare_file reference_test1.o test1.o
 
     CCACHE_CPP2=1 $CCACHE_COMPILE -c test1.c -O -O
     checkstat 'cache hit (preprocessed)' 5
     checkstat 'cache miss' 3
+    compare_file reference_test1.o test1.o
 
     testname="CCACHE_NOSTATS"
     CCACHE_NOSTATS=1 $CCACHE_COMPILE -c test1.c -O -O
@@ -244,21 +268,24 @@ base_tests() {
     CCACHE_RECACHE=1 $CCACHE_COMPILE -c test1.c -O -O
     checkstat 'cache hit (preprocessed)' 5
     checkstat 'cache miss' 4
+    compare_file reference_test1.o test1.o
 
-    # strictly speaking should be 4 - RECACHE causes a double counting!
+    # strictly speaking should be 3 - RECACHE causes a double counting!
     checkstat 'files in cache' 4
     $CCACHE -c > /dev/null
-    checkstat 'files in cache' 4
+    checkstat 'files in cache' 3
 
     testname="CCACHE_HASHDIR"
     CCACHE_HASHDIR=1 $CCACHE_COMPILE -c test1.c -O -O
     checkstat 'cache hit (preprocessed)' 5
     checkstat 'cache miss' 5
+    compare_file reference_test1.o test1.o
 
     CCACHE_HASHDIR=1 $CCACHE_COMPILE -c test1.c -O -O
     checkstat 'cache hit (preprocessed)' 6
     checkstat 'cache miss' 5
-    checkstat 'files in cache' 5
+    checkstat 'files in cache' 4
+    compare_file reference_test1.o test1.o
 
     testname="comments"
     echo '/* a silly comment */' > test1-comment.c
@@ -279,6 +306,8 @@ base_tests() {
     mv test1-saved.c test1.c
     checkstat 'cache hit (preprocessed)' 7
     checkstat 'cache miss' 7
+    CCACHE_DISABLE=1 $COMPILER -c test1.c -o reference_test1.o
+    compare_file reference_test1.o test1.o
 
     testname="cache-size"
     for f in *.c; do
@@ -286,7 +315,7 @@ base_tests() {
     done
     checkstat 'cache hit (preprocessed)' 8
     checkstat 'cache miss' 37
-    checkstat 'files in cache' 37
+    checkstat 'files in cache' 36
 
     $CCACHE -C >/dev/null
 
@@ -344,18 +373,27 @@ base_tests() {
             CCACHE_CPP2=1 $CCACHE_COMPILE -c -finput-charset=latin1 latin1.c
             checkstat 'cache hit (preprocessed)' 14
             checkstat 'cache miss' 40
-            CCACHE_CPP2=1 $CCACHE_COMPILE -c -finput-charset=latin1 latin1.c
-            checkstat 'cache hit (preprocessed)' 15
-            checkstat 'cache miss' 40
             $CCACHE_COMPILE -c -finput-charset=latin1 latin1.c
             checkstat 'cache hit (preprocessed)' 15
-            checkstat 'cache miss' 41
-            $CCACHE_COMPILE -c -finput-charset=latin1 latin1.c
-            checkstat 'cache hit (preprocessed)' 16
-            checkstat 'cache miss' 41
+            checkstat 'cache miss' 40
         fi
     fi
 
+    testname="override path"
+    $CCACHE -Cz >/dev/null
+    override_path=`pwd`/override_path
+    rm -rf $override_path
+    mkdir $override_path
+    cat >$override_path/cc <<EOF
+#!/bin/sh
+touch override_path_compiler_executed
+EOF
+    chmod +x $override_path/cc
+    CCACHE_PATH=$override_path $CCACHE cc -c test1.c
+    if [ ! -f override_path_compiler_executed ]; then
+        test_failed "CCACHE_PATH had no effect"
+    fi
+
     testname="compilercheck=mtime"
     $CCACHE -Cz >/dev/null
     cat >compiler.sh <<EOF
@@ -442,7 +480,26 @@ EOF
     fi
     checkstat 'compiler check failed' 1
 
+    testname="recache should remove previous .stderr"
+    $CCACHE -Cz >/dev/null
+    $CCACHE_COMPILE -c test1.c
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    num=`find $CCACHE_DIR -name '*.stderr' | wc -l`
+    if [ $num -ne 0 ]; then
+        test_failed "$num stderr files found, expected 0 (#1)"
+    fi
+    obj_file=`find $CCACHE_DIR -name '*.o'`
+    stderr_file=`echo $obj_file | sed 's/..$/.stderr/'`
+    echo "Warning: foo" >$stderr_file
+    CCACHE_RECACHE=1 $CCACHE_COMPILE -c test1.c
+    num=`find $CCACHE_DIR -name '*.stderr' | wc -l`
+    if [ $num -ne 0 ]; then
+        test_failed "$num stderr files found, expected 0 (#2)"
+    fi
+
     testname="no object file"
+    $CCACHE -Cz >/dev/null
     cat <<'EOF' >test_no_obj.c
 int test_no_obj;
 EOF
@@ -498,6 +555,90 @@ EOF
     $CCACHE -C > /dev/null
     checkstat 'files in cache' 0
 
+    # the profile options do not seem to work correctly with clang or gcc-llvm
+    # on darwin machines
+    darwin_llvm=0
+    if [ $HOST_OS_APPLE -eq 1 ] && [ $COMPILER_USES_LLVM -eq 1 ]; then
+        darwin_llvm=1
+    fi
+
+    $COMPILER -c -fprofile-generate test1.c 2>/dev/null
+    if [ $? -eq 0 ] && [ $darwin_llvm -eq 0 ]; then
+        testname="profile-generate"
+        $CCACHE -Cz > /dev/null
+        $CCACHE_COMPILE -c -fprofile-generate test1.c
+        checkstat 'cache hit (preprocessed)' 0
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 1
+        $CCACHE_COMPILE -c -fprofile-generate test1.c
+        checkstat 'cache hit (preprocessed)' 1
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 1
+
+        testname="profile-arcs"
+        $CCACHE_COMPILE -c -fprofile-arcs test1.c
+        checkstat 'cache hit (preprocessed)' 1
+        checkstat 'cache miss' 2
+        checkstat 'files in cache' 2
+        $CCACHE_COMPILE -c -fprofile-arcs test1.c
+        checkstat 'cache hit (preprocessed)' 2
+        checkstat 'cache miss' 2
+        checkstat 'files in cache' 2
+
+        testname="profile-use"
+        $CCACHE_COMPILE -c -fprofile-use test1.c 2>/dev/null
+        checkstat 'cache hit (preprocessed)' 2
+        checkstat 'cache miss' 3
+        $CCACHE_COMPILE -c -fprofile-use test1.c 2>/dev/null
+        checkstat 'cache hit (preprocessed)' 3
+        checkstat 'cache miss' 3
+    fi
+
+    ##################################################################
+    # Check that -Wp,-P disables ccache. (-P removes preprocessor information
+    # in such a way that the object file from compiling the preprocessed file
+    # will not be equal to the object file produced when compiling without
+    # ccache.)
+    testname="-Wp,-P"
+    $CCACHE -Cz >/dev/null
+    $CCACHE_COMPILE -c -Wp,-P test1.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    checkstat 'unsupported compiler option' 1
+    $CCACHE_COMPILE -c -Wp,-P test1.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    checkstat 'unsupported compiler option' 2
+    $CCACHE_COMPILE -c -Wp,-DFOO,-P,-DGOO test1.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    checkstat 'unsupported compiler option' 3
+    $CCACHE_COMPILE -c -Wp,-DFOO,-P,-DGOO test1.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    checkstat 'unsupported compiler option' 4
+
+    ##################################################################
+
+    if [ $COMPILER_TYPE_CLANG -eq 1 ]; then
+        $CCACHE -Cz > /dev/null
+        testname="serialize-diagnostics"
+        $CCACHE_COMPILE -c --serialize-diagnostics test.dia test1.c 2> /dev/null
+        checkstat 'cache hit (preprocessed)' 0
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 2
+        $CCACHE_COMPILE -c --serialize-diagnostics test.dia test1.c 2> /dev/null
+        checkstat 'cache hit (preprocessed)' 1
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 2
+    fi
+
+    ##################################################################
+
     rm -f test1.c
 }
 
@@ -511,6 +652,7 @@ link_suite() {
         ln -s "$CCACHE" $COMPILER
         CCACHE_COMPILE="./$COMPILER"
         base_tests
+        rm -f $COMPILER
     else
         echo "Compiler ($COMPILER) not taken from PATH -- not running link test"
     fi
@@ -520,11 +662,8 @@ hardlink_suite() {
     CCACHE_COMPILE="$CCACHE $COMPILER"
     CCACHE_HARDLINK=1
     export CCACHE_HARDLINK
-    CCACHE_NOCOMPRESS=1
-    export CCACHE_NOCOMPRESS
     base_tests
     unset CCACHE_HARDLINK
-    unset CCACHE_NOCOMPRESS
 }
 
 cpp2_suite() {
@@ -695,6 +834,8 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -Wp,-MD,other.d test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     rm -f other.d
 
@@ -703,9 +844,19 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     rm -f other.d
 
+    $CCACHE $COMPILER -c -Wp,-MD,different_name.d test.c
+    checkstat 'cache hit (direct)' 2
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    checkfile different_name.d "$expected_d_content"
+    compare_file reference_test.o test.o
+
+    rm -f different_name.d
+
     ##################################################################
     # Check that -Wp,-MMD,file.d works.
     testname="-Wp,-MMD"
@@ -716,6 +867,8 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_mmd_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -Wp,-MMD,other.d test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     rm -f other.d
 
@@ -724,36 +877,18 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_mmd_d_content"
+    compare_file reference_test.o test.o
 
     rm -f other.d
 
-    ##################################################################
-    # Check that -Wp,-MD,file.d,-P disables direct mode.
-    testname="-Wp,-MD,file.d,-P"
-    $CCACHE -z >/dev/null
-    $CCACHE $COMPILER -c -Wp,-MD,$DEVNULL,-P test.c
-    checkstat 'cache hit (direct)' 0
-    checkstat 'cache hit (preprocessed)' 0
-    checkstat 'cache miss' 1
-
-    $CCACHE $COMPILER -c -Wp,-MD,$DEVNULL,-P test.c
-    checkstat 'cache hit (direct)' 0
-    checkstat 'cache hit (preprocessed)' 1
-    checkstat 'cache miss' 1
-
-    ##################################################################
-    # Check that -Wp,-MMD,file.d,-P disables direct mode.
-    testname="-Wp,-MDD,file.d,-P"
-    $CCACHE -z >/dev/null
-    $CCACHE $COMPILER -c -Wp,-MMD,$DEVNULL,-P test.c
-    checkstat 'cache hit (direct)' 0
+    $CCACHE $COMPILER -c -Wp,-MMD,different_name.d test.c
+    checkstat 'cache hit (direct)' 2
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
+    checkfile different_name.d "$expected_mmd_d_content"
+    compare_file reference_test.o test.o
 
-    $CCACHE $COMPILER -c -Wp,-MMD,$DEVNULL,-P test.c
-    checkstat 'cache hit (direct)' 0
-    checkstat 'cache hit (preprocessed)' 1
-    checkstat 'cache miss' 1
+    rm -f different_name.d
 
     ##################################################################
     # Test some header modifications to get multiple objects in the manifest.
@@ -778,6 +913,8 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -MD test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     rm -f test.d
 
@@ -786,6 +923,7 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     ##################################################################
     # Check the scenario of running a ccache with direct mode on a cache
@@ -798,6 +936,8 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -MD test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     rm -f test.d
 
@@ -806,6 +946,7 @@ EOF
     checkstat 'cache hit (preprocessed)' 1
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     rm -f test.d
 
@@ -814,6 +955,7 @@ EOF
     checkstat 'cache hit (preprocessed)' 2
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     rm -f test.d
 
@@ -822,6 +964,7 @@ EOF
     checkstat 'cache hit (preprocessed)' 2
     checkstat 'cache miss' 1
     checkfile test.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     ##################################################################
     # Check that -MF works.
@@ -833,6 +976,8 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -MD -MF other.d test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     rm -f other.d
 
@@ -841,6 +986,25 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    compare_file reference_test.o test.o
+
+    $CCACHE $COMPILER -c -MD -MF different_name.d test.c
+    checkstat 'cache hit (direct)' 2
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    checkfile different_name.d "$expected_d_content"
+    compare_file reference_test.o test.o
+
+    rm -f different_name.d
+
+    $CCACHE $COMPILER -c -MD -MFthird_name.d test.c
+    checkstat 'cache hit (direct)' 3
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    checkfile third_name.d "$expected_d_content"
+    compare_file reference_test.o test.o
+
+    rm -f third_name.d
 
     ##################################################################
     # Check that a missing .d file in the cache is handled correctly.
@@ -853,12 +1017,15 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    CCACHE_DISABLE=1 $COMPILER -c -MD test.c -o reference_test.o
+    compare_file reference_test.o test.o
 
     $CCACHE $COMPILER -c -MD test.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     find $CCACHE_DIR -name '*.d' -exec rm -f '{}' \;
 
@@ -867,6 +1034,7 @@ EOF
     checkstat 'cache hit (preprocessed)' 1
     checkstat 'cache miss' 1
     checkfile other.d "$expected_d_content"
+    compare_file reference_test.o test.o
 
     ##################################################################
     # Check that stderr from both the preprocessor and the compiler is emitted
@@ -904,7 +1072,7 @@ EOF
     checkfile stderr-mf.txt "`cat stderr-orig.txt`"
 
     ##################################################################
-    # Check that it's possible to compile and cache an empty source code file.
+    # Check that it is possible to compile and cache an empty source code file.
     testname="empty source file"
     $CCACHE -Cz >/dev/null
     cp /dev/null empty.c
@@ -988,15 +1156,15 @@ EOF
 #define file __FILE__
 int test;
 EOF
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c file.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c file.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c file.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c file.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c `pwd`/file.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c `pwd`/file.c
     checkstat 'cache hit (direct)' 2
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1011,16 +1179,16 @@ EOF
     cat <<EOF >file_h.c
 #include "file.h"
 EOF
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c file_h.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c file_h.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c file_h.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c file_h.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     mv file_h.c file2_h.c
-    CCACHE_SLOPPINESS=file_macro $CCACHE $COMPILER -c `pwd`/file2_h.c
+    CCACHE_SLOPPINESS="$default_sloppiness file_macro" $CCACHE $COMPILER -c `pwd`/file2_h.c
     checkstat 'cache hit (direct)' 2
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1069,11 +1237,11 @@ EOF
 #define time __TIME__
 int test;
 EOF
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c time.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER -c time.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c time.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER -c time.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1088,11 +1256,11 @@ EOF
     cat <<EOF >time_h.c
 #include "time.h"
 EOF
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c time_h.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER -c time_h.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c time_h.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER -c time_h.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1128,11 +1296,11 @@ EOF
 int test;
 EOF
     touch -t 203801010000 new.h
-    CCACHE_SLOPPINESS=include_file_mtime $CCACHE $COMPILER -c new.c
+    CCACHE_SLOPPINESS="$default_sloppiness include_file_mtime" $CCACHE $COMPILER -c new.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=include_file_mtime $CCACHE $COMPILER -c new.c
+    CCACHE_SLOPPINESS="$default_sloppiness include_file_mtime" $CCACHE $COMPILER -c new.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1171,22 +1339,42 @@ EOF
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 2
 
-    testname="comment in strings"
+    #################################################################
+    # Check that strange "#line" directives are handled.
+    testname="#line directives with troublesome files"
     $CCACHE -Cz >/dev/null
-    echo 'char *comment = " /* \\\\u" "foo" " */";' >comment.c
-    $CCACHE $COMPILER -c comment.c
-    checkstat 'cache hit (direct)' 0
-    checkstat 'cache hit (preprocessed)' 0
-    checkstat 'cache miss' 1
-    $CCACHE $COMPILER -c comment.c
-    checkstat 'cache hit (direct)' 1
-    checkstat 'cache hit (preprocessed)' 0
-    checkstat 'cache miss' 1
-    echo 'char *comment = " /* \\\\u" "goo" " */";' >comment.c
-    $CCACHE $COMPILER -c comment.c
-    checkstat 'cache hit (direct)' 1
-    checkstat 'cache hit (preprocessed)' 0
-    checkstat 'cache miss' 2
+    cat <<EOF >strange.c
+int foo;
+EOF
+    for x in stdout tty sda hda; do
+        if [ -b /dev/$x ] || [ -c /dev/$x ]; then
+            echo "#line 1 \"/dev/$x\"" >> strange.c
+        fi
+    done
+    CCACHE_SLOPPINESS="$default_sloppiness include_file_mtime" $CCACHE $COMPILER -c strange.c
+    manifest=`find $CCACHE_DIR -name '*.manifest'`
+    if [ -n "$manifest" ]; then
+        data="`$CCACHE --dump-manifest $manifest | egrep '/dev/(stdout|tty|sda|hda'`"
+        if [ -n "$data" ]; then
+            test_failed "$manifest contained troublesome file(s): $data"
+        fi
+    fi
+
+    ##################################################################
+    # Test --dump-manifest output.
+    testname="--dump-manifest"
+    $CCACHE -Cz >/dev/null
+    $CCACHE $COMPILER test.c -c -o test.o
+    manifest=`find $CCACHE_DIR -name '*.manifest'`
+    $CCACHE --dump-manifest $manifest >manifest.dump
+
+    if grep 'Hash: e6b009695d072974f2c4d1dd7e7ed4fc' manifest.dump >/dev/null 2>&1 && \
+       grep 'Hash: e94ceb9f1b196c387d098a5f1f4fe862' manifest.dump >/dev/null 2>&1 && \
+       grep 'Hash: c2f5392dbc7e8ff6138d01608445240a' manifest.dump >/dev/null 2>&1; then
+        : OK
+    else
+        test_failed "unexpected output of --dump-manifest"
+    fi
 }
 
 basedir_suite() {
@@ -1240,7 +1428,8 @@ EOF
     $CCACHE -z >/dev/null
     $CCACHE -C >/dev/null
 
-    cd dir1
+    ln -s dir1 symlink_to_dir1
+    cd symlink_to_dir1
     CCACHE_BASEDIR="`pwd`" $CCACHE $COMPILER -I`pwd`/include -c src/test.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
@@ -1340,6 +1529,29 @@ EOF
         checkstat 'cache miss' 1
         cd ..
     done
+
+    ##################################################################
+    # Check that clang's --serialize-diagnostics arguments with absolute paths
+    # are rewritten to relative.
+    if [ $COMPILER_TYPE_CLANG -eq 1 ]; then
+        testname="serialize-diagnostics"
+        $CCACHE -Cz >/dev/null
+        cd dir1
+        CCACHE_BASEDIR=`pwd` $CCACHE $COMPILER -w -MD -MF `pwd`/test.d -I`pwd`/include --serialize-diagnostics `pwd`/test.dia -c src/test.c -o `pwd`/test.o
+        checkstat 'cache hit (direct)' 0
+        checkstat 'cache hit (preprocessed)' 0
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 4
+        cd ..
+
+        cd dir2
+        CCACHE_BASEDIR=`pwd` $CCACHE $COMPILER -w -MD -MF `pwd`/test.d -I`pwd`/include --serialize-diagnostics `pwd`/test.dia -c src/test.c -o `pwd`/test.o
+        checkstat 'cache hit (direct)' 1
+        checkstat 'cache hit (preprocessed)' 0
+        checkstat 'cache miss' 1
+        checkstat 'files in cache' 4
+        cd ..
+    fi
 }
 
 compression_suite() {
@@ -1435,6 +1647,35 @@ readonly_suite() {
     ##################################################################
 }
 
+readonly_direct_suite() {
+    unset CCACHE_NODIRECT
+
+    ##################################################################
+    # Create some code to compile.
+    echo "int test;" >test.c
+
+    # Cache a compilation.
+    testname="fill cache"
+    $CCACHE $COMPILER -c test.c -o test.o
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+
+    # Check that "readonly direct" mode gets a direct hit.
+    testname="direct hit"
+    CCACHE_READONLY_DIRECT=1 $CCACHE $COMPILER -c test.c -o test.o
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+
+    # Check that "readonly direct" mode doesn't get a preprocessed hit.
+    testname="preprocessed miss"
+    CCACHE_READONLY_DIRECT=1 $CCACHE $COMPILER -DFOO -c test.c -o test.o
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+}
+
 extrafiles_suite() {
     ##################################################################
     # Create some code to compile.
@@ -1623,7 +1864,7 @@ cleanup_suite() {
     $CCACHE -C >/dev/null
     prepare_cleanup_test $CCACHE_DIR/a
     touch $CCACHE_DIR/a/abcd.unknown
-    $CCACHE -c >/dev/null # update counters
+    $CCACHE -F 0 -M 0 -c >/dev/null # update counters
     checkstat 'files in cache' 31
     # (9/10) * 30 * 16 = 432
     $CCACHE -F 432 -M 0 >/dev/null
@@ -1688,13 +1929,29 @@ int main()
 }
 EOF
 
-    if $COMPILER -fpch-preprocess pch.h 2>/dev/null && [ -f pch.h.gch ] && $COMPILER pch.c -o pch; then
-        :
+    if $COMPILER $SYSROOT -fpch-preprocess pch.h 2>/dev/null && [ -f pch.h.gch ] && $COMPILER $SYSROOT pch.c -o pch; then
+        rm pch.h.gch
     else
         echo "Compiler (`$COMPILER --version | head -1`) doesn't support precompiled headers -- not running pch test"
         return
     fi
 
+    # clang and gcc handle precompiled headers similarly, but gcc is much more
+    # forgiving with precompiled headers. Both gcc and clang keep an absolute
+    # path reference to the original file except that clang uses that reference
+    # to validate the pch and gcc ignores the reference. Also, clang has an
+    # additional feature: pre-tokenized headers. For these reasons clang should
+    # be tested separately than gcc. clang can only use pch or pth headers on
+    # the command line and not as an #include statement inside a source file.
+
+    if [ $COMPILER_TYPE_CLANG -eq 1 ]; then
+        clang_pch_suite
+    else
+        gcc_pch_suite
+    fi
+}
+
+gcc_pch_suite() {
     ##################################################################
     # Tests for creating a .gch without opt-in.
 
@@ -1702,7 +1959,7 @@ EOF
 
     testname="create .gch, -c, no -o, without opt-in"
     $CCACHE -zC >/dev/null
-    $CCACHE $COMPILER -c pch.h
+    $CCACHE $COMPILER $SYSROOT -c pch.h
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 0
@@ -1723,12 +1980,12 @@ EOF
 
     testname="create .gch, -c, no -o, with opt-in"
     $CCACHE -zC >/dev/null
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c pch.h
+    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER $SYSROOT -c pch.h
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     rm -f pch.h.gch
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c pch.h
+    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER $SYSROOT -c pch.h
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1738,17 +1995,18 @@ EOF
 
     testname="create .gch, no -c, -o, with opt-in"
     $CCACHE -Cz >/dev/null
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER pch.h -o pch.gch
+    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER $SYSROOT pch.h -o pch.gch
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER pch.h -o pch.gch
+    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER $SYSROOT pch.h -o pch.gch
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
     if [ ! -f pch.gch ]; then
         test_failed "pch.gch missing"
     fi
+    rm pch.gch
 
     ##################################################################
     # Tests for using a .gch.
@@ -1758,7 +2016,7 @@ EOF
 
     testname="no -fpch-preprocess, #include"
     $CCACHE -Cz >/dev/null
-    $CCACHE $COMPILER -c pch.c 2>/dev/null
+    $CCACHE $COMPILER $SYSROOT -c pch.c 2>/dev/null
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 0
@@ -1768,7 +2026,7 @@ EOF
 
     testname="no -fpch-preprocess, -include, no sloppiness"
     $CCACHE -Cz >/dev/null
-    $CCACHE $COMPILER -c -include pch.h pch2.c 2>/dev/null
+    $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 0
@@ -1777,18 +2035,18 @@ EOF
 
     testname="no -fpch-preprocess, -include"
     $CCACHE -Cz >/dev/null
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -include pch.h pch2.c 2>/dev/null
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -include pch.h pch2.c 2>/dev/null
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
 
     testname="-fpch-preprocess, #include, no sloppiness"
     $CCACHE -Cz >/dev/null
-    $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     # Must enable sloppy time macros:
@@ -1796,11 +2054,11 @@ EOF
 
     testname="-fpch-preprocess, #include, sloppiness"
     $CCACHE -Cz >/dev/null
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
@@ -1808,18 +2066,18 @@ EOF
     testname="-fpch-preprocess, #include, file changed"
     echo "updated" >>pch.h.gch # GCC seems to cope with this...
     backdate pch.h.gch
-    CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 1
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 2
 
     testname="preprocessor mode"
     $CCACHE -Cz >/dev/null
-    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 0
     checkstat 'cache miss' 1
-    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 1
     checkstat 'cache miss' 1
@@ -1827,72 +2085,241 @@ EOF
     testname="preprocessor mode, file changed"
     echo "updated" >>pch.h.gch # GCC seems to cope with this...
     backdate pch.h.gch
-    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 1
     checkstat 'cache miss' 2
-    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=pch_defines,time_macros $CCACHE $COMPILER -c -fpch-preprocess pch.c
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS="$default_sloppiness pch_defines time_macros" $CCACHE $COMPILER $SYSROOT -c -fpch-preprocess pch.c
     checkstat 'cache hit (direct)' 0
     checkstat 'cache hit (preprocessed)' 2
     checkstat 'cache miss' 2
 }
 
-symlinks_suite() {
+clang_pch_suite() {
     ##################################################################
-    testname="symlink to source directory"
-
-    mkdir dir
-    cd dir
-    mkdir -p d1/d2
-    echo '#define A "OK"' >d1/h.h
-    cat <<EOF >d1/d2/c.c
-#include <stdio.h>
-#include "../h.h"
-int main() { printf("%s\n", A); }
-EOF
-    echo '#define A "BUG"' >h.h
-    ln -s d1/d2 d3
-
-    CCACHE_BASEDIR=/ $CCACHE $COMPILER -c $PWD/d3/c.c
-    $COMPILER -c $PWD/d3/c.c
-    $COMPILER c.o -o c
-    result=$(./c)
-    if [ "$result" != OK ]; then
-        test_failed "Incorrect header file used"
+    # Tests for creating a .gch.
+
+    backdate pch.h
+
+    testname="create .gch, -c, no -o"
+    $CCACHE -zC >/dev/null
+    $CCACHE $COMPILER $SYSROOT -c pch.h
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    rm -f pch.h.gch
+    $CCACHE $COMPILER $SYSROOT -c pch.h
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    if [ ! -f pch.h.gch ]; then
+        test_failed "pch.h.gch missing"
     fi
 
-    cd ..
-    rm -rf dir
+    testname="create .gch, no -c, -o"
+    $CCACHE -Cz >/dev/null
+    $CCACHE $COMPILER $SYSROOT pch.h -o pch.gch
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    $CCACHE $COMPILER $SYSROOT pch.h -o pch.gch
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    if [ ! -f pch.gch ]; then
+        test_failed "pch.gch missing"
+    fi
+    rm pch.gch
 
     ##################################################################
-    testname="symlink to source file"
-
-    mkdir dir
-    cd dir
-    mkdir d
-    echo '#define A "BUG"' >d/h.h
-    cat <<EOF >d/c.c
-#include <stdio.h>
-#include "h.h"
-int main() { printf("%s\n", A); }
-EOF
-    echo '#define A "OK"' >h.h
-    ln -s d/c.c c.c
-
-    CCACHE_BASEDIR=/ $CCACHE $COMPILER -c $PWD/c.c
-    $COMPILER c.o -o c
-    result=$(./c)
-    if [ "$result" != OK ]; then
-        test_failed "Incorrect header file used"
+    # Tests for using a .gch.
+
+    backdate pch.h.gch
+
+    testname="gch, no -fpch-preprocess, -include, no sloppy time macros"
+    $CCACHE -Cz >/dev/null
+    $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    # Must enable sloppy time macros:
+    checkstat "can't use precompiled header" 1
+
+    testname="gch, no -fpch-preprocess, -include, sloppy time macros"
+    $CCACHE -Cz >/dev/null
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+
+    testname="gch, -fpch-preprocess, -include, file changed"
+    echo "updated" >>pch.h.gch # clang seems to cope with this...
+    backdate pch.h.gch
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 2
+
+    testname="gch, preprocessor mode"
+    $CCACHE -Cz >/dev/null
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 1
+    checkstat 'cache miss' 1
+
+    testname="gch, preprocessor mode, file changed"
+    echo "updated" >>pch.h.gch # clang seems to cope with this...
+    backdate pch.h.gch
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 1
+    checkstat 'cache miss' 2
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 2
+    checkstat 'cache miss' 2
+
+    rm pch.h.gch
+
+    ##################################################################
+    # Tests for creating a .pth.
+
+    backdate pch.h
+
+    testname="create .pth, -c, -o"
+    $CCACHE -zC >/dev/null
+    $CCACHE $COMPILER $SYSROOT -c pch.h -o pch.h.pth
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    rm -f pch.h.pth
+    $CCACHE $COMPILER $SYSROOT -c pch.h -o pch.h.pth
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    if [ ! -f pch.h.pth ]; then
+        test_failed "pch.h.pth missing"
     fi
 
-    cd ..
-    rm -rf dir
+    ##################################################################
+    # Tests for using a .pth.
+
+    backdate pch.h.pth
+
+    testname="pth, no -fpch-preprocess, -include, no sloppy time macros"
+    $CCACHE -Cz >/dev/null
+    $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 0
+    # Must enable sloppy time macros:
+    checkstat "can't use precompiled header" 1
+
+    testname="pth, no -fpch-preprocess, -include, sloppy time macros"
+    $CCACHE -Cz >/dev/null
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h pch2.c 2>/dev/null
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+
+    testname="pth, -fpch-preprocess, -include, file changed"
+    echo "updated" >>pch.h.pth # clang seems to cope with this...
+    backdate pch.h.pth
+    CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 1
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 2
+
+    testname="pth, preprocessor mode"
+    $CCACHE -Cz >/dev/null
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 1
+    checkstat 'cache miss' 1
+
+    testname="pth, preprocessor mode, file changed"
+    echo "updated" >>pch.h.pth # clang seems to cope with this...
+    backdate pch.h.pth
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 1
+    checkstat 'cache miss' 2
+    CCACHE_NODIRECT=1 CCACHE_SLOPPINESS=time_macros $CCACHE $COMPILER $SYSROOT -c -include pch.h -fpch-preprocess pch.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 2
+    checkstat 'cache miss' 2
+
+    rm pch.h.pth
+}
+
+upgrade_suite() {
+    testname="keep maxfiles and maxsize settings"
+    rm -rf $CCACHE_DIR $CCACHE_CONFIGPATH
+    mkdir -p $CCACHE_DIR/0
+    echo "0 0 0 0 0 0 0 0 0 0 0 0 0 2000 131072" >$CCACHE_DIR/0/stats
+    checkstat 'max files' 32000
+    checkstat 'max cache size' '2.1 GB'
+}
+
+prefix_suite() {
+    testname="prefix"
+    $CCACHE -Cz >/dev/null
+    rm -f prefix.result
+    cat <<'EOF' >prefix-a
+#!/bin/sh
+echo a >>prefix.result
+exec "$@"
+EOF
+    cat <<'EOF' >prefix-b
+#!/bin/sh
+echo b >>prefix.result
+exec "$@"
+EOF
+    chmod +x prefix-a prefix-b
+    cat <<'EOF' >file.c
+int foo;
+EOF
+    PATH=.:$PATH CCACHE_PREFIX="prefix-a prefix-b" $CCACHE $COMPILER -c file.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 0
+    checkstat 'cache miss' 1
+    checkfile prefix.result "a
+b"
+    PATH=.:$PATH CCACHE_PREFIX="prefix-a prefix-b" $CCACHE $COMPILER -c file.c
+    checkstat 'cache hit (direct)' 0
+    checkstat 'cache hit (preprocessed)' 1
+    checkstat 'cache miss' 1
+    checkfile prefix.result "a
+b"
 }
 
 ######################################################################
 # main program
 
+if pwd | grep '[^A-Za-z0-9/.,=_%+-]' >/dev/null 2>&1; then
+    cat <<EOF
+Error: The test suite doesn't work in directories with whitespace or other
+funny characters in the name. Sorry.
+EOF
+    exit 1
+fi
+
 suites="$*"
 if [ -n "$CC" ]; then
     COMPILER="$CC"
@@ -1903,9 +2330,20 @@ if [ -z "$CCACHE" ]; then
     CCACHE=`pwd`/ccache
 fi
 
+# save the type of compiler because some test may not work on all compilers
+COMPILER_TYPE_CLANG=0
+COMPILER_TYPE_GCC=0
+
+COMPILER_USES_LLVM=0
+HOST_OS_APPLE=0
+
 compiler_version="`$COMPILER --version 2>&1 | head -1`"
 case $compiler_version in
-    *gcc*|2.95*)
+    *gcc*|*g++*|2.95*)
+        COMPILER_TYPE_GCC=1
+        ;;
+    *clang*)
+        COMPILER_TYPE_CLANG=1
         ;;
     *)
         echo "WARNING: Compiler $COMPILER not supported (version: $compiler_version) -- not running tests" >&2
@@ -1913,6 +2351,19 @@ case $compiler_version in
         ;;
 esac
 
+case $compiler_version in
+    *llvm*|*LLVM*)
+        COMPILER_USES_LLVM=1
+        ;;
+esac
+
+host_os="`uname -s`"
+case $host_os in
+    *Darwin*)
+        HOST_OS_APPLE=1
+        ;;
+esac
+
 TESTDIR=testdir.$$
 rm -rf $TESTDIR
 mkdir $TESTDIR
@@ -1922,6 +2373,35 @@ CCACHE_DIR=`pwd`/.ccache
 export CCACHE_DIR
 CCACHE_LOGFILE=`pwd`/ccache.log
 export CCACHE_LOGFILE
+CCACHE_CONFIGPATH=`pwd`/ccache.conf
+export CCACHE_CONFIGPATH
+touch $CCACHE_CONFIGPATH
+
+
+if [ $HOST_OS_APPLE -eq 1 ]; then
+    # Grab the developer directory from the environment or try xcode-select
+    if [ "$XCODE_DEVELOPER_DIR" = "" ]; then
+      XCODE_DEVELOPER_DIR=`xcode-select --print-path`
+      if [ "$XCODE_DEVELOPER_DIR" = "" ]; then
+        echo "Error: XCODE_DEVELOPER_DIR environment variable not set and xcode-select path not set"
+        exit 1
+      fi
+    fi
+
+    # Choose the latest SDK if an SDK root is not set
+    MAC_PLATFORM_DIR=$XCODE_DEVELOPER_DIR/Platforms/MacOSX.platform
+    if [ "$SDKROOT" = "" ]; then
+        SDKROOT="`eval ls -f -1 -d \"$MAC_PLATFORM_DIR/Developer/SDKs/\"*.sdk | tail -1`"
+        if [ "$SDKROOT" = "" ]; then
+            echo "Error: Cannot find a valid SDK root directory"
+            exit 1
+        fi
+    fi
+
+    SYSROOT="-isysroot `echo \"$SDKROOT\" | sed 's/ /\\ /g'`"
+else
+    SYSROOT=
+fi
 
 # ---------------------------------------
 
@@ -1929,34 +2409,38 @@ all_suites="
 base
 link          !win32
 hardlink
+cpp2
 nlevels4
 nlevels1
 basedir       !win32
 direct
 compression
 readonly
+readonly_direct
 extrafiles
 cleanup
 pch
-symlinks
+upgrade
+prefix
 "
 
-host_os="`uname -s`"
 case $host_os in
     *MINGW*|*mingw*)
         export CCACHE_DETECT_SHEBANG
         CCACHE_DETECT_SHEBANG=1
-        DEVNULL=NUL
         PATH_DELIM=";"
         all_suites="`echo "$all_suites" | grep -v '!win32'`"
         ;;
     *)
-        DEVNULL=/dev/null
         PATH_DELIM=":"
         all_suites="`echo "$all_suites" | cut -d' ' -f1`"
         ;;
 esac
 
+echo compiler: `which $COMPILER`
+echo version: `$COMPILER --version`
+echo test dir: $TESTDIR
+
 if [ -z "$suites" ]; then
     suites="$all_suites"
 fi
index 53a8d60d212fac6a555235349901ae43186ae3b3..bbb0480099f696ebf8489677ebd98b37a07339e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010-2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -68,7 +68,7 @@ cct_run(suite_fn *suites, int verbose_output)
 
        for (suite = suites; *suite; suite++) {
                unsigned test_index = 0;
-               while (1) {
+               while (true) {
                        test_index = (*suite)(test_index + 1);
                        if (test_index == 0) {
                                /* We have reached the end of the suite. */
@@ -115,8 +115,6 @@ cct_suite_end()
 void
 cct_test_begin(const char *name)
 {
-       extern char *cache_logfile;
-
        ++total_tests;
        if (verbose) {
                printf("--- TEST: %s ---\n", name);
@@ -126,8 +124,8 @@ cct_test_begin(const char *name)
        cct_chdir(name);
        current_test = name;
 
+       putenv("CCACHE_CONFIG_PATH=/dev/null");
        cc_reset();
-       cache_logfile = getenv("CCACHE_LOGFILE");
 }
 
 void
@@ -158,67 +156,59 @@ cct_check_failed(const char *file, int line, const char *what,
        fprintf(stderr, "%s:%d: Failed assertion:\n", file, line);
        fprintf(stderr, "  Suite:      %s\n", current_suite);
        fprintf(stderr, "  Test:       %s\n", current_test);
-       if (expected && actual) {
+       if (expected) {
                fprintf(stderr, "  Expression: %s\n", what);
-               fprintf(stderr, "  Expected:   %s\n", expected);
-               fprintf(stderr, "  Actual:     %s\n", actual);
+               if (actual) {
+                       fprintf(stderr, "  Expected:   %s\n", expected);
+                       fprintf(stderr, "  Actual:     %s\n", actual);
+               } else {
+                       fprintf(stderr, "  Message:    %s\n", expected);
+               }
        } else {
                fprintf(stderr, "  Assertion:  %s\n", what);
        }
        fprintf(stderr, "\n");
 }
 
-int
+bool
 cct_check_int_eq(const char *file, int line, const char *expression,
-                 int expected, int actual)
-{
-       if (expected == actual) {
-               cct_check_passed(file, line, expression);
-               return 1;
-       } else {
-               char *exp_str = format("%i", expected);
-               char *act_str = format("%i", actual);
-               cct_check_failed(file, line, expression, exp_str, act_str);
-               free(exp_str);
-               free(act_str);
-               return 0;
-       }
-}
-
-int
-cct_check_uns_eq(const char *file, int line, const char *expression,
-                 unsigned expected, unsigned actual)
+                 int64_t expected, int64_t actual)
 {
        if (expected == actual) {
                cct_check_passed(file, line, expression);
-               return 1;
+               return true;
        } else {
-               char *exp_str = format("%i", expected);
-               char *act_str = format("%i", actual);
+#ifdef HAVE_LONG_LONG
+               char *exp_str = format("%lld", (long long)expected);
+               char *act_str = format("%lld", (long long)actual);
+#else
+               char *exp_str = format("%ld", (long)expected);
+               char *act_str = format("%ld", (long)actual);
+#endif
                cct_check_failed(file, line, expression, exp_str, act_str);
                free(exp_str);
                free(act_str);
-               return 0;
+               return false;
        }
 }
 
-int
+bool
 cct_check_str_eq(const char *file, int line, const char *expression,
-                 const char *expected, const char *actual, int free1,
-                 int free2)
+                 const char *expected, const char *actual, bool free1,
+                 bool free2)
 {
-       int result;
+       bool result;
 
        if (expected && actual && str_eq(actual, expected)) {
                cct_check_passed(file, line, expression);
-               result = 1;
+               result = true;
        } else {
                char *exp_str = expected ? format("\"%s\"", expected) : x_strdup("(null)");
                char *act_str = actual ? format("\"%s\"", actual) : x_strdup("(null)");
                cct_check_failed(file, line, expression, exp_str, act_str);
                free(exp_str);
                free(act_str);
-               result = 0;
+               result = false;
        }
 
        if (free1) {
@@ -230,23 +220,23 @@ cct_check_str_eq(const char *file, int line, const char *expression,
        return result;
 }
 
-int
+bool
 cct_check_args_eq(const char *file, int line, const char *expression,
                   struct args *expected, struct args *actual,
-                  int free1, int free2)
+                  bool free1, bool free2)
 {
-       int result;
+       bool result;
 
        if (expected && actual && args_equal(actual, expected)) {
                cct_check_passed(file, line, expression);
-               result = 1;
+               result = true;
        } else {
                char *exp_str = expected ? args_to_string(expected) : x_strdup("(null)");
                char *act_str = actual ? args_to_string(actual) : x_strdup("(null)");
                cct_check_failed(file, line, expression, exp_str, act_str);
                free(exp_str);
                free(act_str);
-               result = 0;
+               result = false;
        }
 
        if (free1) {
index 3c616b5aff8d6b0d963f03bf0a7ed7e307224f2f..fc9ff8df06c6cae68432933429a68e28fe8ed093 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010-2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
 
 /*****************************************************************************/
 
-#define CHECK(assertion) \
+#define CHECKM(assertion, message) \
        do { \
                if ((assertion)) { \
                        cct_check_passed(__FILE__, __LINE__, #assertion); \
                } else { \
-                       cct_check_failed(__FILE__, __LINE__, #assertion, NULL, NULL); \
+                       cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
                        cct_test_end(); \
                        cct_suite_end(); \
                        return _test_counter; \
                } \
-       } while (0)
+       } while (false)
+
+#define CHECK(assertion) \
+       CHECKM(assertion, NULL)
 
-#define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2)        \
+#define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
        do { \
                if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
                        cct_test_end(); \
                        cct_suite_end(); \
                        return _test_counter; \
                } \
-       } while (0)
+       } while (false)
 
 /*****************************************************************************/
 
 #define CHECK_INT_EQ(expected, actual) \
        do { \
-               if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
-                       cct_test_end(); \
-                       cct_suite_end(); \
-                       return _test_counter; \
-               } \
-       } while (0)
-
-#define CHECK_UNS_EQ(expected, actual) \
-       do { \
-               if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
+               if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
+                                     (actual))) { \
                        cct_test_end(); \
                        cct_suite_end(); \
                        return _test_counter; \
                } \
-       } while (0)
+       } while (false)
 
 /*****************************************************************************/
 
 #define CHECK_STR_EQ(expected, actual) \
-       CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 0)
+       CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
 
 #define CHECK_STR_EQ_FREE1(expected, actual) \
-       CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 0)
+       CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
 
 #define CHECK_STR_EQ_FREE2(expected, actual) \
-       CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 1)
+       CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
 
 #define CHECK_STR_EQ_FREE12(expected, actual) \
-       CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 1)
+       CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
 
 /*****************************************************************************/
 
 #define CHECK_ARGS_EQ(expected, actual) \
-       CHECK_POINTER_EQ_BASE(args, expected, actual, 0, 0)
+       CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)
 
 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
-       CHECK_POINTER_EQ_BASE(args, expected, actual, 1, 0)
+       CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)
 
 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
-       CHECK_POINTER_EQ_BASE(args, expected, actual, 0, 1)
+       CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)
 
 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
-       CHECK_POINTER_EQ_BASE(args, expected, actual, 1, 1)
+       CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)
 
 /*****************************************************************************/
 
@@ -123,22 +118,20 @@ typedef unsigned (*suite_fn)(unsigned);
 int cct_run(suite_fn *suites, int verbose);
 
 void cct_suite_begin(const char *name);
-void cct_suite_end();
+void cct_suite_end(void);
 void cct_test_begin(const char *name);
-void cct_test_end();
+void cct_test_end(void);
 void cct_check_passed(const char *file, int line, const char *assertion);
 void cct_check_failed(const char *file, int line, const char *assertion,
                       const char *expected, const char *actual);
-int cct_check_int_eq(const char *file, int line, const char *expression,
-                     int expected, int actual);
-int cct_check_uns_eq(const char *file, int line, const char *expression,
-                     unsigned expected, unsigned actual);
-int cct_check_str_eq(const char *file, int line, const char *expression,
-                     const char *expected, const char *actual, int free1,
-                     int free2);
-int cct_check_args_eq(const char *file, int line, const char *expression,
-                      struct args *expected, struct args *actual,
-                      int free1, int free2);
+bool cct_check_int_eq(const char *file, int line, const char *expression,
+                      int64_t expected, int64_t actual);
+bool cct_check_str_eq(const char *file, int line, const char *expression,
+                      const char *expected, const char *actual, bool free1,
+                      bool free2);
+bool cct_check_args_eq(const char *file, int line, const char *expression,
+                       struct args *expected, struct args *actual,
+                       bool free1, bool free2);
 void cct_chdir(const char *path);
 void cct_wipe(const char *path);
 void cct_create_fresh_dir(const char *path);
index 739d45255eadc8235b22532013c7923860d2843f..82f96699cd914be88196790b1e0b0aa4a7d2abd4 100644 (file)
@@ -55,6 +55,10 @@ main(int argc, char **argv)
        char *testdir, *dir_before;
        int result;
 
+#ifdef _WIN32
+       putenv("CCACHE_DETECT_SHEBANG=1");
+#endif
+
        while ((c = getopt_long(argc, argv, "hv", options, NULL)) != -1) {
                switch (c) {
                case 'h':
index 0d07dda86d342a50a142f1d9f6b9e65f6cf9718d..335b29df918db736bf89b9e2b651beb6fac7d1a7 100644 (file)
@@ -1,6 +1,7 @@
 SUITE(args)
 SUITE(argument_processing)
 SUITE(compopt)
+SUITE(conf)
 SUITE(counters)
 SUITE(hash)
 SUITE(hashutil)
index 50608fc0b3b0690da45fdfbf98938a1be5bca7d8..27c6fb9d06d2370f8849e8bb95895fb6f7adca03 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -22,6 +22,7 @@
 
 #include "ccache.h"
 #include "test/framework.h"
+#include "test/util.h"
 
 TEST_SUITE(args)
 
@@ -59,6 +60,29 @@ TEST(args_init_from_string)
        args_free(args);
 }
 
+TEST(args_init_from_gcc_atfile)
+{
+       struct args *args;
+       const char *argtext =
+               "first\rsec\\\tond\tthi\\\\rd\nfourth  \tfif\\ th \"si'x\\\" th\""
+               " 'seve\nth'\\";
+
+       create_file("gcc_atfile", argtext);
+
+       args = args_init_from_gcc_atfile("gcc_atfile");
+       CHECK(args);
+       CHECK_INT_EQ(7, args->argc);
+       CHECK_STR_EQ("first", args->argv[0]);
+       CHECK_STR_EQ("sec\tond", args->argv[1]);
+       CHECK_STR_EQ("thi\\rd", args->argv[2]);
+       CHECK_STR_EQ("fourth", args->argv[3]);
+       CHECK_STR_EQ("fif th", args->argv[4]);
+       CHECK_STR_EQ("si'x\" th", args->argv[5]);
+       CHECK_STR_EQ("seve\nth", args->argv[6]);
+       CHECK(!args->argv[7]);
+       args_free(args);
+}
+
 TEST(args_copy)
 {
        struct args *args1 = args_init_from_string("foo");
@@ -144,4 +168,46 @@ TEST(args_to_string)
        args_free(args);
 }
 
+TEST(args_insert)
+{
+       struct args *args = args_init_from_string("first second third fourth fifth");
+
+       struct args *src1 = args_init_from_string("alpha beta gamma");
+       struct args *src2 = args_init_from_string("one");
+       struct args *src3 = args_init_from_string("");
+       struct args *src4 = args_init_from_string("alpha beta gamma");
+       struct args *src5 = args_init_from_string("one");
+       struct args *src6 = args_init_from_string("");
+
+       args_insert(args, 2, src1, true);
+       CHECK_STR_EQ_FREE2("first second alpha beta gamma fourth fifth",
+                          args_to_string(args));
+       CHECK_INT_EQ(7, args->argc);
+       args_insert(args, 2, src2, true);
+       CHECK_STR_EQ_FREE2("first second one beta gamma fourth fifth",
+                          args_to_string(args));
+       CHECK_INT_EQ(7, args->argc);
+       args_insert(args, 2, src3, true);
+       CHECK_STR_EQ_FREE2("first second beta gamma fourth fifth",
+                          args_to_string(args));
+       CHECK_INT_EQ(6, args->argc);
+
+       args_insert(args, 1, src4, false);
+       CHECK_STR_EQ_FREE2("first alpha beta gamma second beta gamma fourth fifth",
+                          args_to_string(args));
+       CHECK_INT_EQ(9, args->argc);
+       args_insert(args, 1, src5, false);
+       CHECK_STR_EQ_FREE2(
+               "first one alpha beta gamma second beta gamma fourth fifth",
+               args_to_string(args));
+       CHECK_INT_EQ(10, args->argc);
+       args_insert(args, 1, src6, false);
+       CHECK_STR_EQ_FREE2(
+               "first one alpha beta gamma second beta gamma fourth fifth",
+               args_to_string(args));
+       CHECK_INT_EQ(10, args->argc);
+
+       args_free(args);
+}
+
 TEST_SUITE_END
index 6ed0daa8ea01f3792e9ce477916159419c7ea9ea..e0a44794908022a7afee89d1c116fb074e031aa4 100644 (file)
  */
 
 #include "ccache.h"
+#include "conf.h"
 #include "test/framework.h"
 #include "test/util.h"
 
+extern struct conf *conf;
+
 TEST_SUITE(argument_processing)
 
 TEST(dash_E_should_result_in_called_for_preprocessing)
@@ -33,7 +36,7 @@ TEST(dash_E_should_result_in_called_for_preprocessing)
 
        create_file("foo.c", "");
        CHECK(!cc_process_args(orig, &preprocessed, &compiler));
-       CHECK_UNS_EQ(1, stats_get_pending(STATS_PREPROCESSING));
+       CHECK_INT_EQ(1, stats_get_pending(STATS_PREPROCESSING));
 
        args_free(orig);
 }
@@ -45,7 +48,7 @@ TEST(dash_M_should_be_unsupported)
 
        create_file("foo.c", "");
        CHECK(!cc_process_args(orig, &preprocessed, &compiler));
-       CHECK_UNS_EQ(1, stats_get_pending(STATS_UNSUPPORTED));
+       CHECK_INT_EQ(1, stats_get_pending(STATS_UNSUPPORTED));
 
        args_free(orig);
 }
@@ -53,9 +56,32 @@ TEST(dash_M_should_be_unsupported)
 TEST(dependency_flags_should_only_be_sent_to_the_preprocessor)
 {
 #define CMD \
-       "cc -c -MD -MMD -MP -MF foo.d -MT mt1 -MT mt2 -MQ mq1 -MQ mq2" \
+       "cc -MD -MMD -MP -MF foo.d -MT mt1 -MT mt2 -MQ mq1 -MQ mq2" \
        " -Wp,-MD,wpmd -Wp,-MMD,wpmmd"
-       struct args *orig = args_init_from_string(CMD " foo.c -o foo.o");
+       struct args *orig = args_init_from_string(CMD " -c foo.c -o foo.o");
+       struct args *exp_cpp = args_init_from_string(CMD);
+#undef CMD
+       struct args *exp_cc = args_init_from_string("cc -c");
+       struct args *act_cpp = NULL, *act_cc = NULL;
+       create_file("foo.c", "");
+
+       CHECK(cc_process_args(orig, &act_cpp, &act_cc));
+       CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
+       CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
+
+       args_free(orig);
+}
+
+TEST(preprocessor_only_flags_should_only_be_sent_to_the_preprocessor)
+{
+#define CMD \
+       "cc -I. -idirafter . -iframework. -imacros . -imultilib ." \
+       " -include test.h -include-pch test.pch -iprefix . -iquote ." \
+       " -isysroot . -isystem . -iwithprefix . -iwithprefixbefore ." \
+       " -DTEST_MACRO -DTEST_MACRO2=1 -F. -trigraphs -fworking-directory" \
+       " -fno-working-directory -MD -MMD -MP -MF foo.d -MT mt1 -MT mt2 "\
+       " -MQ mq1 -MQ mq2 -Wp,-MD,wpmd -Wp,-MMD,wpmmd"
+       struct args *orig = args_init_from_string(CMD " -c foo.c -o foo.o");
        struct args *exp_cpp = args_init_from_string(CMD);
 #undef CMD
        struct args *exp_cc = args_init_from_string("cc -c");
@@ -74,7 +100,7 @@ TEST(dependency_flags_that_take_an_argument_should_not_require_space_delimiter)
        struct args *orig = args_init_from_string(
                "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq foo.c -o foo.o");
        struct args *exp_cpp = args_init_from_string(
-               "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq");
+               "cc -MMD -MFfoo.d -MT mt -MTmt -MQmq");
        struct args *exp_cc = args_init_from_string("cc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
@@ -88,25 +114,24 @@ TEST(dependency_flags_that_take_an_argument_should_not_require_space_delimiter)
 
 TEST(sysroot_should_be_rewritten_if_basedir_is_used)
 {
-       extern char *base_dir;
        extern char *current_working_dir;
        char *arg_string;
        struct args *orig;
        struct args *act_cpp = NULL, *act_cc = NULL;
 
        create_file("foo.c", "");
+       free(conf->base_dir);
+       conf->base_dir = x_strdup("/");
        current_working_dir = get_cwd();
        arg_string = format("cc --sysroot=%s/foo -c foo.c", current_working_dir);
        orig = args_init_from_string(arg_string);
 
-       base_dir = "/";
-
        CHECK(cc_process_args(orig, &act_cpp, &act_cc));
        CHECK(str_startswith(act_cpp->argv[1], "--sysroot=./foo"));
 
        args_free(orig);
-       base_dir = NULL;
-       current_working_dir = NULL;
+       args_free(act_cpp);
+       args_free(act_cc);
 }
 
 TEST(MF_flag_with_immediate_argument_should_work_as_last_argument)
@@ -114,7 +139,7 @@ TEST(MF_flag_with_immediate_argument_should_work_as_last_argument)
        struct args *orig = args_init_from_string(
                "cc -c foo.c -o foo.o -MMD -MT bar -MFfoo.d");
        struct args *exp_cpp = args_init_from_string(
-               "cc -c -MMD -MT bar -MFfoo.d");
+               "cc -MMD -MT bar -MFfoo.d");
        struct args *exp_cc = args_init_from_string("cc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
@@ -131,7 +156,7 @@ TEST(MT_flag_with_immediate_argument_should_work_as_last_argument)
        struct args *orig = args_init_from_string(
                "cc -c foo.c -o foo.o -MMD -MFfoo.d -MT foo -MTbar");
        struct args *exp_cpp = args_init_from_string(
-               "cc -c -MMD -MFfoo.d -MT foo -MTbar");
+               "cc -MMD -MFfoo.d -MT foo -MTbar");
        struct args *exp_cc = args_init_from_string("cc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
@@ -148,7 +173,7 @@ TEST(MQ_flag_with_immediate_argument_should_work_as_last_argument)
        struct args *orig = args_init_from_string(
                "cc -c foo.c -o foo.o -MMD -MFfoo.d -MQ foo -MQbar");
        struct args *exp_cpp = args_init_from_string(
-               "cc -c -MMD -MFfoo.d -MQ foo -MQbar");
+               "cc -MMD -MFfoo.d -MQ foo -MQbar");
        struct args *exp_cc = args_init_from_string("cc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
@@ -165,9 +190,8 @@ TEST(MQ_flag_without_immediate_argument_should_not_add_MQobj)
        struct args *orig = args_init_from_string(
                "gcc -c -MD -MP -MFfoo.d -MQ foo.d foo.c");
        struct args *exp_cpp = args_init_from_string(
-               "gcc -c -MD -MP -MFfoo.d -MQ foo.d");
-       struct args *exp_cc = args_init_from_string(
-               "gcc -c");
+               "gcc -MD -MP -MFfoo.d -MQ foo.d");
+       struct args *exp_cc = args_init_from_string("gcc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
 
@@ -183,9 +207,8 @@ TEST(MT_flag_without_immediate_argument_should_not_add_MTobj)
        struct args *orig = args_init_from_string(
                "gcc -c -MD -MP -MFfoo.d -MT foo.d foo.c");
        struct args *exp_cpp = args_init_from_string(
-               "gcc -c -MD -MP -MFfoo.d -MT foo.d");
-       struct args *exp_cc = args_init_from_string(
-               "gcc -c");
+               "gcc -MD -MP -MFfoo.d -MT foo.d");
+       struct args *exp_cc = args_init_from_string("gcc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
 
@@ -201,9 +224,8 @@ TEST(MQ_flag_with_immediate_argument_should_not_add_MQobj)
        struct args *orig = args_init_from_string(
                "gcc -c -MD -MP -MFfoo.d -MQfoo.d foo.c");
        struct args *exp_cpp = args_init_from_string(
-               "gcc -c -MD -MP -MFfoo.d -MQfoo.d");
-       struct args *exp_cc = args_init_from_string(
-               "gcc -c");
+               "gcc -MD -MP -MFfoo.d -MQfoo.d");
+       struct args *exp_cc = args_init_from_string("gcc -c");
        struct args *act_cpp = NULL, *act_cc = NULL;
        create_file("foo.c", "");
 
@@ -219,11 +241,35 @@ TEST(MT_flag_with_immediate_argument_should_not_add_MQobj)
        struct args *orig = args_init_from_string(
                "gcc -c -MD -MP -MFfoo.d -MTfoo.d foo.c");
        struct args *exp_cpp = args_init_from_string(
-               "gcc -c -MD -MP -MFfoo.d -MTfoo.d");
-       struct args *exp_cc = args_init_from_string(
-               "gcc -c");
+               "gcc -MD -MP -MFfoo.d -MTfoo.d");
+       struct args *exp_cc = args_init_from_string("gcc -c");
+       struct args *act_cpp = NULL, *act_cc = NULL;
+       create_file("foo.c", "");
+
+       CHECK(cc_process_args(orig, &act_cpp, &act_cc));
+       CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
+       CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
+
+       args_free(orig);
+}
+
+TEST(fprofile_flag_with_existing_dir_should_be_rewritten_to_real_path)
+{
+       struct args *orig = args_init_from_string(
+               "gcc -c -fprofile-generate=some/dir foo.c");
+       struct args *exp_cpp = args_init_from_string("gcc");
+       struct args *exp_cc = args_init_from_string("gcc");
        struct args *act_cpp = NULL, *act_cc = NULL;
+       char *s;
+
        create_file("foo.c", "");
+       mkdir("some", 0777);
+       mkdir("some/dir", 0777);
+       s = format("-fprofile-generate=%s", x_realpath("some/dir"));
+       args_add(exp_cpp, s);
+       args_add(exp_cc, s);
+       args_add(exp_cc, "-c");
+       free(s);
 
        CHECK(cc_process_args(orig, &act_cpp, &act_cc));
        CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
@@ -232,5 +278,23 @@ TEST(MT_flag_with_immediate_argument_should_not_add_MQobj)
        args_free(orig);
 }
 
+TEST(fprofile_flag_with_nonexisting_dir_not_be_rewritten)
+{
+       struct args *orig = args_init_from_string(
+               "gcc -c -fprofile-generate=some/dir foo.c");
+       struct args *exp_cpp = args_init_from_string(
+               "gcc -fprofile-generate=some/dir");
+       struct args *exp_cc = args_init_from_string(
+               "gcc -fprofile-generate=some/dir -c");
+       struct args *act_cpp = NULL, *act_cc = NULL;
+
+       create_file("foo.c", "");
+
+       CHECK(cc_process_args(orig, &act_cpp, &act_cc));
+       CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
+       CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
+
+       args_free(orig);
+}
 
 TEST_SUITE_END
index 509825c8d2d71abbe24b1d6e80cc2220c17718ba..9af5e4e1677e9f315ddb80145736bfdc29aa8a6d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -94,4 +94,9 @@ TEST(dash_xxx_doesnt_take_arg)
        CHECK(!compopt_takes_arg("-xxx"));
 }
 
+TEST(dash_iframework_prefix_affects_cpp)
+{
+       CHECK(compopt_prefix_affects_cpp("-iframework"));
+}
+
 TEST_SUITE_END
diff --git a/test/test_conf.c b/test/test_conf.c
new file mode 100644 (file)
index 0000000..46be780
--- /dev/null
@@ -0,0 +1,425 @@
+/*
+ * Copyright (C) 2011-2014 Joel Rosdahl
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "conf.h"
+#include "test/framework.h"
+#include "test/util.h"
+
+#define N_CONFIG_ITEMS 27
+static struct {
+       char *descr;
+       const char *origin;
+} received_conf_items[N_CONFIG_ITEMS];
+static size_t n_received_conf_items = 0;
+
+static void
+conf_item_receiver(const char *descr, const char *origin, void *context)
+{
+       (void)context;
+       received_conf_items[n_received_conf_items].descr = x_strdup(descr);
+       received_conf_items[n_received_conf_items].origin = origin;
+       ++n_received_conf_items;
+}
+
+static void
+free_received_conf_items(void)
+{
+       while (n_received_conf_items > 0) {
+               --n_received_conf_items;
+               free(received_conf_items[n_received_conf_items].descr);
+       }
+}
+
+TEST_SUITE(conf)
+
+TEST(conf_create)
+{
+       struct conf *conf = conf_create();
+       CHECK_STR_EQ("", conf->base_dir);
+       CHECK_STR_EQ_FREE1(format("%s/.ccache", get_home_directory()),
+                          conf->cache_dir);
+       CHECK_INT_EQ(2, conf->cache_dir_levels);
+       CHECK_STR_EQ("", conf->compiler);
+       CHECK_STR_EQ("mtime", conf->compiler_check);
+       CHECK(!conf->compression);
+       CHECK_INT_EQ(6, conf->compression_level);
+       CHECK_STR_EQ("", conf->cpp_extension);
+       CHECK(conf->direct_mode);
+       CHECK(!conf->disable);
+       CHECK_STR_EQ("", conf->extra_files_to_hash);
+       CHECK(!conf->hard_link);
+       CHECK(!conf->hash_dir);
+       CHECK_STR_EQ("", conf->log_file);
+       CHECK_INT_EQ(0, conf->max_files);
+       CHECK_INT_EQ((uint64_t)5 * 1000 * 1000 * 1000, conf->max_size);
+       CHECK_STR_EQ("", conf->path);
+       CHECK_STR_EQ("", conf->prefix_command);
+       CHECK(!conf->read_only);
+       CHECK(!conf->read_only_direct);
+       CHECK(!conf->recache);
+       CHECK(!conf->run_second_cpp);
+       CHECK_INT_EQ(0, conf->sloppiness);
+       CHECK(conf->stats);
+       CHECK_STR_EQ("", conf->temporary_dir);
+       CHECK_INT_EQ(UINT_MAX, conf->umask);
+       CHECK(!conf->unify);
+       conf_free(conf);
+}
+
+TEST(conf_read_valid_config)
+{
+       struct conf *conf = conf_create();
+       char *errmsg, *user;
+       putenv("USER=rabbit");
+       user = getenv("USER");
+       CHECK_STR_EQ("rabbit", user);
+       create_file(
+               "ccache.conf",
+               "base_dir =  /$USER/foo/${USER} \n"
+               "cache_dir=\n"
+               "cache_dir = $USER$/${USER}/.ccache\n"
+               "\n"
+               "\n"
+               "  #A comment\n"
+               " cache_dir_levels = 4\n"
+               "\t compiler = foo\n"
+               "compiler_check = none\n"
+               "compression=true\n"
+               "compression_level= 2\n"
+               "cpp_extension = .foo\n"
+               "direct_mode = false\n"
+               "disable = true\n"
+               "extra_files_to_hash = a:b c:$USER\n"
+               "hard_link = true\n"
+               "hash_dir = true\n"
+               "log_file = $USER${USER} \n"
+               "max_files = 17\n"
+               "max_size = 123M\n"
+               "path = $USER.x\n"
+               "prefix_command = x$USER\n"
+               "read_only = true\n"
+               "read_only_direct = true\n"
+               "recache = true\n"
+               "run_second_cpp = true\n"
+               "sloppiness =     file_macro   ,time_macros,  include_file_mtime,include_file_ctime,file_stat_matches  pch_defines  \n"
+               "stats = false\n"
+               "temporary_dir = ${USER}_foo\n"
+               "umask = 777\n"
+               "unify = true"); /* Note: no newline */
+       CHECK(conf_read(conf, "ccache.conf", &errmsg));
+       CHECK(!errmsg);
+
+       CHECK_STR_EQ_FREE1(format("/%s/foo/%s", user, user), conf->base_dir);
+       CHECK_STR_EQ_FREE1(format("%s$/%s/.ccache", user, user), conf->cache_dir);
+       CHECK_INT_EQ(4, conf->cache_dir_levels);
+       CHECK_STR_EQ("foo", conf->compiler);
+       CHECK_STR_EQ("none", conf->compiler_check);
+       CHECK(conf->compression);
+       CHECK_INT_EQ(2, conf->compression_level);
+       CHECK_STR_EQ(".foo", conf->cpp_extension);
+       CHECK(!conf->direct_mode);
+       CHECK(conf->disable);
+       CHECK_STR_EQ_FREE1(format("a:b c:%s", user), conf->extra_files_to_hash);
+       CHECK(conf->hard_link);
+       CHECK(conf->hash_dir);
+       CHECK_STR_EQ_FREE1(format("%s%s", user, user), conf->log_file);
+       CHECK_INT_EQ(17, conf->max_files);
+       CHECK_INT_EQ(123 * 1000 * 1000, conf->max_size);
+       CHECK_STR_EQ_FREE1(format("%s.x", user), conf->path);
+       CHECK_STR_EQ_FREE1(format("x%s", user), conf->prefix_command);
+       CHECK(conf->read_only);
+       CHECK(conf->read_only_direct);
+       CHECK(conf->recache);
+       CHECK(conf->run_second_cpp);
+       CHECK_INT_EQ(SLOPPY_INCLUDE_FILE_MTIME|SLOPPY_INCLUDE_FILE_CTIME|
+                    SLOPPY_FILE_MACRO|SLOPPY_TIME_MACROS|
+                    SLOPPY_FILE_STAT_MATCHES|SLOPPY_PCH_DEFINES,
+                    conf->sloppiness);
+       CHECK(!conf->stats);
+       CHECK_STR_EQ_FREE1(format("%s_foo", user), conf->temporary_dir);
+       CHECK_INT_EQ(0777, conf->umask);
+       CHECK(conf->unify);
+
+       conf_free(conf);
+}
+
+TEST(conf_read_with_missing_equal_sign)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "no equal sign");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: missing equal sign",
+                          errmsg);
+       conf_free(conf);
+}
+
+TEST(conf_read_with_bad_config_key)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "# Comment\nfoo = bar");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:2: unknown configuration option \"foo\"",
+                          errmsg);
+       conf_free(conf);
+}
+
+TEST(conf_read_invalid_bool)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       create_file("ccache.conf", "disable=");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: not a boolean value: \"\"",
+                          errmsg);
+
+       create_file("ccache.conf", "disable=foo");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: not a boolean value: \"foo\"",
+                          errmsg);
+       conf_free(conf);
+}
+
+TEST(conf_read_invalid_env_string)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "base_dir = ${foo");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: syntax error: missing '}' after \"foo\"",
+                          errmsg);
+       /* Other cases tested in test_util.c. */
+       conf_free(conf);
+}
+
+TEST(conf_read_empty_umask)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "umask = ");
+       CHECK(conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_INT_EQ(conf->umask, UINT_MAX);
+       conf_free(conf);
+}
+
+TEST(conf_read_invalid_size)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "max_size = foo");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: invalid size: \"foo\"",
+                          errmsg);
+       /* Other cases tested in test_util.c. */
+       conf_free(conf);
+}
+
+TEST(conf_read_invalid_sloppiness)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       create_file("ccache.conf", "sloppiness = file_macro, foo");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: unknown sloppiness: \"foo\"",
+                          errmsg);
+       conf_free(conf);
+}
+
+TEST(conf_read_invalid_unsigned)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       create_file("ccache.conf", "max_files =");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: invalid unsigned integer: \"\"",
+                          errmsg);
+
+       create_file("ccache.conf", "max_files = -42");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: invalid unsigned integer: \"-42\"",
+                          errmsg);
+
+       create_file("ccache.conf", "max_files = foo");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: invalid unsigned integer: \"foo\"",
+                          errmsg);
+
+       conf_free(conf);
+}
+
+TEST(verify_absolute_base_dir)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       create_file("ccache.conf", "base_dir = relative/path");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: not an absolute path: \"relative/path\"",
+                          errmsg);
+
+       create_file("ccache.conf", "base_dir =");
+       CHECK(conf_read(conf, "ccache.conf", &errmsg));
+
+       conf_free(conf);
+}
+
+TEST(verify_dir_levels)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       create_file("ccache.conf", "cache_dir_levels = 0");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: cache directory levels must be between 1 and 8",
+                          errmsg);
+       create_file("ccache.conf", "cache_dir_levels = 9");
+       CHECK(!conf_read(conf, "ccache.conf", &errmsg));
+       CHECK_STR_EQ_FREE2("ccache.conf:1: cache directory levels must be between 1 and 8",
+                          errmsg);
+
+       conf_free(conf);
+}
+
+TEST(conf_update_from_environment)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       putenv("CCACHE_COMPRESS=1");
+       CHECK(conf_update_from_environment(conf, &errmsg));
+       CHECK(conf->compression);
+
+       x_unsetenv("CCACHE_COMPRESS");
+       putenv("CCACHE_NOCOMPRESS=1");
+       CHECK(conf_update_from_environment(conf, &errmsg));
+       CHECK(!conf->compression);
+
+       conf_free(conf);
+}
+
+TEST(conf_set_new_value)
+{
+       char *errmsg;
+       char *data;
+
+       create_file("ccache.conf", "path = vanilla\n");
+       CHECK(conf_set_value_in_file("ccache.conf", "stats", "chocolate", &errmsg));
+       data = read_text_file("ccache.conf", 0);
+       CHECK(data);
+       CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data);
+}
+
+TEST(conf_set_existing_value)
+{
+       char *errmsg;
+       char *data;
+
+       create_file("ccache.conf", "path = chocolate\nstats = chocolate\n");
+       CHECK(conf_set_value_in_file("ccache.conf", "path", "vanilla", &errmsg));
+       data = read_text_file("ccache.conf", 0);
+       CHECK(data);
+       CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data);
+}
+
+TEST(conf_print_items)
+{
+       size_t i;
+       struct conf conf = {
+               "bd",
+               "cd",
+               7,
+               "c",
+               "cc",
+               true,
+               8,
+               "ce",
+               false,
+               true,
+               "efth",
+               true,
+               true,
+               "lf",
+               4711,
+               98.7 * 1000 * 1000,
+               "p",
+               "pc",
+               true,
+               true,
+               true,
+               true,
+               SLOPPY_FILE_MACRO|SLOPPY_INCLUDE_FILE_MTIME|
+                 SLOPPY_INCLUDE_FILE_CTIME|SLOPPY_TIME_MACROS|
+                 SLOPPY_FILE_STAT_MATCHES,
+               false,
+               "td",
+               022,
+               true,
+               NULL
+       };
+       size_t n = 0;
+
+       conf.item_origins = x_malloc(N_CONFIG_ITEMS * sizeof(char *));
+       for (i = 0; i < N_CONFIG_ITEMS; ++i) {
+               conf.item_origins[i] = format("origin%zu", i);
+       }
+
+       conf_print_items(&conf, conf_item_receiver, NULL);
+       CHECK_INT_EQ(N_CONFIG_ITEMS, n_received_conf_items);
+       CHECK_STR_EQ("base_dir = bd", received_conf_items[n++].descr);
+       CHECK_STR_EQ("cache_dir = cd", received_conf_items[n++].descr);
+       CHECK_STR_EQ("cache_dir_levels = 7", received_conf_items[n++].descr);
+       CHECK_STR_EQ("compiler = c", received_conf_items[n++].descr);
+       CHECK_STR_EQ("compiler_check = cc", received_conf_items[n++].descr);
+       CHECK_STR_EQ("compression = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("compression_level = 8", received_conf_items[n++].descr);
+       CHECK_STR_EQ("cpp_extension = ce", received_conf_items[n++].descr);
+       CHECK_STR_EQ("direct_mode = false", received_conf_items[n++].descr);
+       CHECK_STR_EQ("disable = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("extra_files_to_hash = efth", received_conf_items[n++].descr);
+       CHECK_STR_EQ("hard_link = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("hash_dir = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("log_file = lf", received_conf_items[n++].descr);
+       CHECK_STR_EQ("max_files = 4711", received_conf_items[n++].descr);
+       CHECK_STR_EQ("max_size = 98.7M", received_conf_items[n++].descr);
+       CHECK_STR_EQ("path = p", received_conf_items[n++].descr);
+       CHECK_STR_EQ("prefix_command = pc", received_conf_items[n++].descr);
+       CHECK_STR_EQ("read_only = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("read_only_direct = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("recache = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("run_second_cpp = true", received_conf_items[n++].descr);
+       CHECK_STR_EQ("sloppiness = file_macro, include_file_mtime,"
+                    " include_file_ctime, time_macros, file_stat_matches",
+                    received_conf_items[n++].descr);
+       CHECK_STR_EQ("stats = false", received_conf_items[n++].descr);
+       CHECK_STR_EQ("temporary_dir = td", received_conf_items[n++].descr);
+       CHECK_STR_EQ("umask = 022", received_conf_items[n++].descr);
+       CHECK_STR_EQ("unify = true", received_conf_items[n++].descr);
+
+       for (i = 0; i < N_CONFIG_ITEMS; ++i) {
+               char *expected = format("origin%zu", i);
+               CHECK_STR_EQ(expected, received_conf_items[i].origin);
+       }
+
+       free_received_conf_items();
+       free(conf.item_origins);
+}
+
+TEST_SUITE_END
index a00d1ee8f39c9ae71a958c7a878bdb0da47efd95..df12a8b59817a79afea094b4da3fd64be8c987bb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010-2011 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -27,8 +27,8 @@ TEST(counters_init_0_should_allocate_0)
 {
        struct counters *counters = counters_init(0);
 
-       CHECK_UNS_EQ(0, counters->allocated);
-       CHECK_UNS_EQ(0, counters->size);
+       CHECK_INT_EQ(0, counters->allocated);
+       CHECK_INT_EQ(0, counters->size);
 
        counters_free(counters);
 }
@@ -38,10 +38,10 @@ TEST(counters_init_7_should_allocate_32)
        int i;
        struct counters *counters = counters_init(7);
 
-       CHECK_UNS_EQ(32, counters->allocated);
-       CHECK_UNS_EQ(7, counters->size);
+       CHECK_INT_EQ(32, counters->allocated);
+       CHECK_INT_EQ(7, counters->size);
        for (i = 0; i < 7; i++) {
-               CHECK_UNS_EQ(0, counters->data[i]);
+               CHECK_INT_EQ(0, counters->data[i]);
        }
 
        counters_free(counters);
@@ -51,10 +51,10 @@ TEST(counters_resize_50_should_allocate_96)
 {
        struct counters *counters = counters_init(0);
 
-       CHECK_UNS_EQ(0, counters->allocated);
+       CHECK_INT_EQ(0, counters->allocated);
        counters_resize(counters, 50);
-       CHECK_UNS_EQ(50, counters->size);
-       CHECK_UNS_EQ(96, counters->allocated);
+       CHECK_INT_EQ(50, counters->size);
+       CHECK_INT_EQ(96, counters->allocated);
 
        counters_free(counters);
 }
index 243af69af1651ec6f9a40f1eb39df71250220f10..98316f014936d84e58f9cc324bb45bb8cac99f91 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2015 Joel Rosdahl
+ * Copyright (C) 2010, 2012 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -99,16 +99,6 @@ TEST(hash_multicommand_output_error_handling)
        CHECK(!hash_multicommand_output(&h2, "false; true", "not used"));
 }
 
-TEST(hash_source_code_simple_case)
-{
-       struct mdfour h;
-       char input[] = "abc";
-       size_t input_len = strlen(input);
-       hash_start(&h);
-       hash_source_code_string(&h, input, input_len, "");
-       CHECK_STR_EQ_FREE2("a448017aaf21d8525fc10ae87aa6729d-3", hash_result(&h));
-}
-
 TEST(check_for_temporal_macros)
 {
        const char time_start[] =
index ec9889494b0a019b596dab900458e862d68741df..9af995f973b6bcc8ccdb1b7bea2d62b765b94422 100644 (file)
@@ -59,7 +59,7 @@ TEST(lock_breaking)
        CHECK(lockfile_acquire("test", 1000));
 
 #ifdef _WIN32
-       p = read_text_file("test.lock");
+       p = read_text_file("test.lock", 0);
 #else
        p = x_readlink("test.lock");
 #endif
index acf6a3423d4e4814b95b53dc77d2b6b216a1537f..79482d104012d6ca4c1e658c6ffbde8d835921a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010-2011 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -40,12 +40,12 @@ TEST(forward_compatibility)
        fclose(f);
 
        stats_read("stats", counters);
-       CHECK_UNS_EQ(100, counters->size);
-       CHECK_UNS_EQ(73, counters->data[73]);
+       CHECK_INT_EQ(100, counters->size);
+       CHECK_INT_EQ(73, counters->data[73]);
 
        stats_write("stats", counters);
-       CHECK_UNS_EQ(100, counters->size);
-       CHECK_UNS_EQ(99, counters->data[99]);
+       CHECK_INT_EQ(100, counters->size);
+       CHECK_INT_EQ(99, counters->data[99]);
 
        counters_free(counters);
 }
index 258c3279565281b9cc90390354aa46731e6a3aa1..a9c54793b1976746b94f1a5b8189bd6726602d19 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010, 2012-2014 Joel Rosdahl
+ * Copyright (C) 2010-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -38,6 +38,7 @@ TEST(dirname)
        CHECK_STR_EQ_FREE2(".", dirname("foo.c"));
        CHECK_STR_EQ_FREE2(".", dirname(""));
        CHECK_STR_EQ_FREE2("/", dirname("/"));
+       CHECK_STR_EQ_FREE2("/", dirname("/foo.c"));
        CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/foo.c"));
        CHECK_STR_EQ_FREE2("/dir", dirname("/dir/foo.c"));
        CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/"));
@@ -45,14 +46,14 @@ TEST(dirname)
 
 TEST(common_dir_prefix_length)
 {
-       CHECK_UNS_EQ(0, common_dir_prefix_length("", ""));
-       CHECK_UNS_EQ(0, common_dir_prefix_length("/", "/"));
-       CHECK_UNS_EQ(0, common_dir_prefix_length("/", "/b"));
-       CHECK_UNS_EQ(0, common_dir_prefix_length("/a", "/b"));
-       CHECK_UNS_EQ(2, common_dir_prefix_length("/a", "/a"));
-       CHECK_UNS_EQ(2, common_dir_prefix_length("/a", "/a/b"));
-       CHECK_UNS_EQ(2, common_dir_prefix_length("/a/b", "/a/c"));
-       CHECK_UNS_EQ(4, common_dir_prefix_length("/a/b", "/a/b"));
+       CHECK_INT_EQ(0, common_dir_prefix_length("", ""));
+       CHECK_INT_EQ(0, common_dir_prefix_length("/", "/"));
+       CHECK_INT_EQ(0, common_dir_prefix_length("/", "/b"));
+       CHECK_INT_EQ(0, common_dir_prefix_length("/a", "/b"));
+       CHECK_INT_EQ(2, common_dir_prefix_length("/a", "/a"));
+       CHECK_INT_EQ(2, common_dir_prefix_length("/a", "/a/b"));
+       CHECK_INT_EQ(2, common_dir_prefix_length("/a/b", "/a/c"));
+       CHECK_INT_EQ(4, common_dir_prefix_length("/a/b", "/a/b"));
        CHECK_INT_EQ(2, common_dir_prefix_length("/a/bc", "/a/b"));
        CHECK_INT_EQ(2, common_dir_prefix_length("/a/b", "/a/bc"));
 }
@@ -73,4 +74,117 @@ TEST(get_relative_path)
        CHECK_STR_EQ_FREE2("a/b", get_relative_path("/", "/a/b"));
 }
 
+TEST(format_hash_as_string)
+{
+       unsigned char hash[16] = {
+               "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"};
+
+       CHECK_STR_EQ_FREE2("00000000000000000000000000000000",
+                          format_hash_as_string(hash, -1));
+       CHECK_STR_EQ_FREE2("00000000000000000000000000000000-0",
+                          format_hash_as_string(hash, 0));
+       hash[0] = 17;
+       hash[15] = 42;
+       CHECK_STR_EQ_FREE2("1100000000000000000000000000002a-12345",
+                          format_hash_as_string(hash, 12345));
+}
+
+TEST(subst_env_in_string)
+{
+       char *errmsg;
+       const char *shell = getenv("SHELL");
+
+       errmsg = "";
+       CHECK_STR_EQ_FREE2(shell,
+                          subst_env_in_string("$SHELL", &errmsg));
+       CHECK(!errmsg);
+
+       errmsg = "";
+       CHECK_STR_EQ_FREE2("$",
+                          subst_env_in_string("$", &errmsg));
+       CHECK(!errmsg);
+
+       errmsg = "";
+       CHECK_STR_EQ_FREE12(format("%s %s:%s", shell, shell, shell),
+                           subst_env_in_string("$SHELL $SHELL:$SHELL", &errmsg));
+       CHECK(!errmsg);
+
+       errmsg = "";
+       CHECK_STR_EQ_FREE12(format("x%s", shell),
+                           subst_env_in_string("x$SHELL", &errmsg));
+       CHECK(!errmsg);
+
+       errmsg = "";
+       CHECK_STR_EQ_FREE12(format("%sx", shell),
+                           subst_env_in_string("${SHELL}x", &errmsg));
+       CHECK(!errmsg);
+
+       CHECK(!subst_env_in_string("$surelydoesntexist", &errmsg));
+       CHECK_STR_EQ_FREE2("environment variable \"surelydoesntexist\" not set",
+                          errmsg);
+
+       CHECK(!subst_env_in_string("${SHELL", &errmsg));
+       CHECK_STR_EQ_FREE2("syntax error: missing '}' after \"SHELL\"", errmsg);
+}
+
+TEST(format_human_readable_size)
+{
+       CHECK_STR_EQ_FREE2("0.0 kB", format_human_readable_size(0));
+       CHECK_STR_EQ_FREE2("0.0 kB", format_human_readable_size(49));
+       CHECK_STR_EQ_FREE2("0.1 kB", format_human_readable_size(50));
+       CHECK_STR_EQ_FREE2("42.0 kB", format_human_readable_size(42 * 1000));
+       CHECK_STR_EQ_FREE2("1.0 MB", format_human_readable_size(1000 * 1000));
+       CHECK_STR_EQ_FREE2("1.2 MB", format_human_readable_size(1234 * 1000));
+       CHECK_STR_EQ_FREE2("438.5 MB",
+                          format_human_readable_size(438.5 * 1000 * 1000));
+       CHECK_STR_EQ_FREE2("1.0 GB",
+                          format_human_readable_size(1000 * 1000 * 1000));
+       CHECK_STR_EQ_FREE2("17.1 GB",
+                          format_human_readable_size(17.11 * 1000 * 1000 * 1000));
+}
+
+TEST(format_parsable_size_with_suffix)
+{
+       CHECK_STR_EQ_FREE2("0", format_parsable_size_with_suffix(0));
+       CHECK_STR_EQ_FREE2("42.0k", format_parsable_size_with_suffix(42 * 1000));
+       CHECK_STR_EQ_FREE2("1.0M", format_parsable_size_with_suffix(1000 * 1000));
+       CHECK_STR_EQ_FREE2("1.2M", format_parsable_size_with_suffix(1234 * 1000));
+       CHECK_STR_EQ_FREE2("438.5M",
+                          format_parsable_size_with_suffix(438.5 * 1000 * 1000));
+       CHECK_STR_EQ_FREE2("1.0G",
+                          format_parsable_size_with_suffix(1000 * 1000 * 1000));
+       CHECK_STR_EQ_FREE2(
+               "17.1G",
+               format_parsable_size_with_suffix(17.11 * 1000 * 1000 * 1000));
+}
+
+TEST(parse_size_with_suffix)
+{
+       uint64_t size;
+       size_t i;
+       struct { const char *size; int64_t expected; } sizes[] = {
+               {"0", 0},
+               {"42", 42},
+
+               {"78k",       78 * 1000},
+               {"78K",       78 * 1000},
+               {"1.1 M",     1.1 * 1000 * 1000},
+               {"438.55M",   438.55 * 1000 * 1000},
+               {"1 G",       1 * 1000 * 1000 * 1000},
+               {"2T",        (int64_t)2 * 1000 * 1000 * 1000 * 1000},
+
+               {"78 Ki",     78 * 1024},
+               {"1.1Mi",     1.1 * 1024 * 1024},
+               {"438.55 Mi", 438.55 * 1024 * 1024},
+               {"1Gi",       1 * 1024 * 1024 * 1024},
+               {"2 Ti",      (int64_t)2 * 1024 * 1024 * 1024 * 1024},
+
+       };
+
+       for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); ++i) {
+               CHECKM(parse_size_with_suffix(sizes[i].size, &size), sizes[i].size);
+               CHECK_INT_EQ(sizes[i].expected, size);
+       }
+}
+
 TEST_SUITE_END
index 9196d6214b0c79fc42f90d7e1956aed2f79b1074..14a858fd1ca3090ebc6d9301030faf7567dc41ab 100644 (file)
 #include "system.h"
 #include "test/util.h"
 
+#ifdef _WIN32
+#    define lstat(a,b) stat(a,b)
+#endif
+
 bool
 path_exists(const char *path)
 {
@@ -29,8 +33,13 @@ path_exists(const char *path)
 bool
 is_symlink(const char *path)
 {
+#ifdef _WIN32
+       (void) path;
+       return 0;
+#else
        struct stat st;
        return lstat(path, &st) == 0 && S_ISLNK(st.st_mode);
+#endif
 }
 
 void
diff --git a/unify.c b/unify.c
index b10b3f5d98b55f139944593aaa5ba66ea4b95a5a..3128623db8b39e3e7c4b3453bbe27a5f7ae261a0 100644 (file)
--- a/unify.c
+++ b/unify.c
@@ -64,15 +64,25 @@ build_table(void)
        int i;
        static bool done;
 
-       if (done) return;
+       if (done) {
+               return;
+       }
        done = true;
 
        memset(tokens, 0, sizeof(tokens));
        for (c = 0; c < 128; c++) {
-               if (isalpha(c) || c == '_') tokens[c].type |= C_ALPHA;
-               if (isdigit(c)) tokens[c].type |= C_DIGIT;
-               if (isspace(c)) tokens[c].type |= C_SPACE;
-               if (isxdigit(c)) tokens[c].type |= C_HEX;
+               if (isalpha(c) || c == '_') {
+                       tokens[c].type |= C_ALPHA;
+               }
+               if (isdigit(c)) {
+                       tokens[c].type |= C_DIGIT;
+               }
+               if (isspace(c)) {
+                       tokens[c].type |= C_SPACE;
+               }
+               if (isxdigit(c)) {
+                       tokens[c].type |= C_HEX;
+               }
        }
        tokens['\''].type |= C_QUOTE;
        tokens['"'].type |= C_QUOTE;
diff --git a/util.c b/util.c
index ed470d2c3d376b1a46bf0eaafcf52f1f88039974..f453959e534a71ab6b34684347c286235eccab32 100644 (file)
--- a/util.c
+++ b/util.c
@@ -38,21 +38,24 @@ static FILE *logfile;
 static bool
 init_log(void)
 {
-       extern char *cache_logfile;
+       extern struct conf *conf;
 
        if (logfile) {
                return true;
        }
-       if (!cache_logfile) {
+       assert(conf);
+       if (str_eq(conf->log_file, "")) {
                return false;
        }
-       logfile = fopen(cache_logfile, "a");
+       logfile = fopen(conf->log_file, "a");
        if (logfile) {
+#ifndef _WIN32
                int fd = fileno(logfile);
                int flags = fcntl(fd, F_GETFD, 0);
                if (flags >= 0) {
                        fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
                }
+#endif
                return true;
        } else {
                return false;
@@ -60,45 +63,88 @@ init_log(void)
 }
 
 static void
-log_prefix(void)
+log_prefix(bool log_updated_time)
 {
 #ifdef HAVE_GETTIMEOFDAY
        char timestamp[100];
        struct timeval tv;
        struct tm *tm;
+       static char prefix[200];
 
-       gettimeofday(&tv, NULL);
+       if (log_updated_time) {
+               gettimeofday(&tv, NULL);
 #ifdef __MINGW64_VERSION_MAJOR
-       tm = _localtime32(&tv.tv_sec);
+               tm = localtime((time_t*)&tv.tv_sec);
 #else
-       tm = localtime(&tv.tv_sec);
+               tm = localtime(&tv.tv_sec);
 #endif
-       strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", tm);
-       fprintf(logfile, "[%s.%06d %-5d] ", timestamp, (int)tv.tv_usec,
-               (int)getpid());
+               strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", tm);
+               snprintf(prefix, sizeof(prefix),
+                        "[%s.%06d %-5d] ", timestamp, (int)tv.tv_usec, (int)getpid());
+       }
+       fputs(prefix, logfile);
 #else
        fprintf(logfile, "[%-5d] ", (int)getpid());
 #endif
 }
 
+static long
+path_max(const char *path)
+{
+#ifdef PATH_MAX
+       (void)path;
+       return PATH_MAX;
+#elif defined(MAXPATHLEN)
+       (void)path;
+       return MAXPATHLEN;
+#elif defined(_PC_PATH_MAX)
+       long maxlen = pathconf(path, _PC_PATH_MAX);
+       if (maxlen >= 4096) {
+               return maxlen;
+       } else {
+               return 4096;
+       }
+#endif
+}
+
+static void
+vlog(const char *format, va_list ap, bool log_updated_time)
+{
+       if (!init_log()) {
+               return;
+       }
+
+       log_prefix(log_updated_time);
+       vfprintf(logfile, format, ap);
+       fprintf(logfile, "\n");
+}
+
 /*
- * Write a message to the CCACHE_LOGFILE location (adding a newline).
+ * Write a message to the log file (adding a newline) and flush.
  */
 void
 cc_log(const char *format, ...)
 {
        va_list ap;
-
-       if (!init_log()) {
-               return;
+       va_start(ap, format);
+       vlog(format, ap, true);
+       va_end(ap);
+       if (logfile) {
+               fflush(logfile);
        }
+}
 
-       log_prefix();
+/*
+ * Write a message to the log file (adding a newline) without flushing and with
+ * a reused timestamp.
+ */
+void
+cc_bulklog(const char *format, ...)
+{
+       va_list ap;
        va_start(ap, format);
-       vfprintf(logfile, format, ap);
+       vlog(format, ap, false);
        va_end(ap);
-       fprintf(logfile, "\n");
-       fflush(logfile);
 }
 
 /*
@@ -111,7 +157,7 @@ cc_log_argv(const char *prefix, char **argv)
                return;
        }
 
-       log_prefix();
+       log_prefix(true);
        fputs(prefix, logfile);
        print_command(logfile, argv);
        fflush(logfile);
@@ -129,7 +175,7 @@ fatal(const char *format, ...)
        va_end(ap);
 
        cc_log("FATAL: %s", msg);
-       fprintf(stderr, "ccache: FATAL: %s\n", msg);
+       fprintf(stderr, "ccache: error: %s\n", msg);
 
        exit(1);
 }
@@ -178,11 +224,11 @@ mkstemp(char *template)
 #endif
 
 /*
- * Copy src to dest, decompressing src if needed. compress_dest decides whether
- * dest will be compressed.
+ * Copy src to dest, decompressing src if needed. compress_level > 0 decides
+ * whether dest will be compressed, and with which compression level.
  */
 int
-copy_file(const char *src, const char *dest, int compress_dest)
+copy_file(const char *src, const char *dest, int compress_level)
 {
        int fd_in, fd_out;
        gzFile gz_in = NULL, gz_out = NULL;
@@ -195,32 +241,27 @@ copy_file(const char *src, const char *dest, int compress_dest)
        struct stat st;
        int errnum;
 
-       tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
-       cc_log("Copying %s to %s via %s (%s)",
-              src, dest, tmp_name, compress_dest ? "compressed": "uncompressed");
+       /* open destination file */
+       tmp_name = x_strdup(dest);
+       fd_out = create_tmp_fd(&tmp_name);
+       cc_log("Copying %s to %s via %s (%scompressed)",
+              src, dest, tmp_name, compress_level > 0 ? "" : "un");
 
        /* open source file */
        fd_in = open(src, O_RDONLY | O_BINARY);
        if (fd_in == -1) {
                cc_log("open error: %s", strerror(errno));
-               return -1;
+               goto error;
        }
 
        gz_in = gzdopen(fd_in, "rb");
        if (!gz_in) {
                cc_log("gzdopen(src) error: %s", strerror(errno));
                close(fd_in);
-               return -1;
-       }
-
-       /* open destination file */
-       fd_out = mkstemp(tmp_name);
-       if (fd_out == -1) {
-               cc_log("mkstemp error: %s", strerror(errno));
                goto error;
        }
 
-       if (compress_dest) {
+       if (compress_level > 0) {
                /*
                 * A gzip file occupies at least 20 bytes, so it will always
                 * occupy an entire filesystem block, even for empty files.
@@ -231,20 +272,21 @@ copy_file(const char *src, const char *dest, int compress_dest)
                        goto error;
                }
                if (file_size(&st) == 0) {
-                       compress_dest = 0;
+                       compress_level = 0;
                }
        }
 
-       if (compress_dest) {
+       if (compress_level > 0) {
                gz_out = gzdopen(dup(fd_out), "wb");
                if (!gz_out) {
                        cc_log("gzdopen(dest) error: %s", strerror(errno));
                        goto error;
                }
+               gzsetparams(gz_out, compress_level, Z_DEFAULT_STRATEGY);
        }
 
        while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
-               if (compress_dest) {
+               if (compress_level > 0) {
                        written = gzwrite(gz_out, buf, n);
                } else {
                        ssize_t count;
@@ -258,7 +300,7 @@ copy_file(const char *src, const char *dest, int compress_dest)
                        } while (written < n);
                }
                if (written != n) {
-                       if (compress_dest) {
+                       if (compress_level > 0) {
                                cc_log("gzwrite error: %s (errno: %s)",
                                       gzerror(gz_in, &errnum),
                                       strerror(errno));
@@ -333,11 +375,11 @@ error:
 
 /* Run copy_file() and, if successful, delete the source file. */
 int
-move_file(const char *src, const char *dest, int compress_dest)
+move_file(const char *src, const char *dest, int compress_level)
 {
        int ret;
 
-       ret = copy_file(src, dest, compress_dest);
+       ret = copy_file(src, dest, compress_level);
        if (ret != -1) {
                x_unlink(src);
        }
@@ -349,10 +391,10 @@ move_file(const char *src, const char *dest, int compress_dest)
  * are on the same file system.
  */
 int
-move_uncompressed_file(const char *src, const char *dest, int compress_dest)
+move_uncompressed_file(const char *src, const char *dest, int compress_level)
 {
-       if (compress_dest) {
-               return move_file(src, dest, compress_dest);
+       if (compress_level > 0) {
+               return move_file(src, dest, compress_level);
        } else {
                return x_rename(src, dest);
        }
@@ -398,6 +440,45 @@ create_dir(const char *dir)
        return 0;
 }
 
+/* Create directories leading to path. Returns 0 on success, otherwise -1. */
+int
+create_parent_dirs(const char *path)
+{
+       struct stat st;
+       int res;
+       char *parent = dirname(path);
+
+       if (stat(parent, &st) == 0) {
+               if (S_ISDIR(st.st_mode)) {
+                       res = 0;
+               } else {
+                       res = -1;
+                       errno = ENOTDIR;
+               }
+       } else {
+               res = create_parent_dirs(parent);
+               if (res == 0) {
+                       res = mkdir(parent, 0777);
+                       /* Have to handle the condition of the directory already existing because
+                        * the file system could have changed in between calling stat and
+                        * actually creating the directory. This can happen when there are
+                        * multiple instances of ccache running and trying to create the same
+                        * directory chain, which usually is the case when the cache root does
+                        * not initially exist. As long as one of the processes creates the
+                        * directories then our condition is satisfied and we avoid a race
+                        * condition.
+                        */
+                       if (res != 0 && errno == EEXIST) {
+                               res = 0;
+                       }
+               } else {
+                       res = -1;
+               }
+       }
+       free(parent);
+       return res;
+}
+
 /*
  * Return a static string with the current hostname.
  */
@@ -418,8 +499,8 @@ get_hostname(void)
 }
 
 /*
- * Return a string to be used to distinguish temporary files. Also tries to
- * cope with NFS by adding the local hostname.
+ * Return a string to be passed to mkstemp to create a temporary file. Also
+ * tries to cope with NFS by adding the local hostname.
  */
 const char *
 tmp_string(void)
@@ -427,15 +508,18 @@ tmp_string(void)
        static char *ret;
 
        if (!ret) {
-               ret = format("%s.%u", get_hostname(), (unsigned)getpid());
+               ret = format("%s.%u.XXXXXX", get_hostname(), (unsigned)getpid());
        }
 
        return ret;
 }
 
-/* Return the hash result as a hex string. Caller frees. */
+/*
+ * Return the hash result as a hex string. Size -1 means don't include size
+ * suffix. Caller frees.
+ */
 char *
-format_hash_as_string(const unsigned char *hash, unsigned size)
+format_hash_as_string(const unsigned char *hash, int size)
 {
        char *ret;
        int i;
@@ -444,7 +528,9 @@ format_hash_as_string(const unsigned char *hash, unsigned size)
        for (i = 0; i < 16; i++) {
                sprintf(&ret[i*2], "%02x", (unsigned) hash[i]);
        }
-       sprintf(&ret[i*2], "-%u", size);
+       if (size >= 0) {
+               sprintf(&ret[i*2], "-%u", size);
+       }
 
        return ret;
 }
@@ -469,12 +555,16 @@ create_cachedirtag(const char *dir)
                goto error;
        }
        f = fopen(filename, "w");
-       if (!f) goto error;
+       if (!f) {
+               goto error;
+       }
        if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG)-1, 1, f) != 1) {
                fclose(f);
                goto error;
        }
-       if (fclose(f)) goto error;
+       if (fclose(f)) {
+               goto error;
+       }
 success:
        free(filename);
        return 0;
@@ -496,7 +586,9 @@ format(const char *format, ...)
        }
        va_end(ap);
 
-       if (!*ptr) fatal("Internal error in format");
+       if (!*ptr) {
+               fatal("Internal error in format");
+       }
        return ptr;
 }
 
@@ -591,7 +683,9 @@ void *
 x_realloc(void *ptr, size_t size)
 {
        void *p2;
-       if (!ptr) return x_malloc(size);
+       if (!ptr) {
+               return x_malloc(size);
+       }
        p2 = realloc(ptr, size);
        if (!p2) {
                fatal("x_realloc: Could not allocate %lu bytes", (unsigned long)size);
@@ -599,12 +693,22 @@ x_realloc(void *ptr, size_t size)
        return p2;
 }
 
+/* This is like unsetenv. */
+void x_unsetenv(const char *name)
+{
+#ifdef HAVE_UNSETENV
+       unsetenv(name);
+#else
+       putenv(x_strdup(name)); /* Leak to environment. */
+#endif
+}
 
 /*
- * This is like x_asprintf() but frees *ptr if *ptr != NULL.
+ * Construct a string according to the format and store it in *ptr. The
+ * original *ptr is then freed.
  */
 void
-x_asprintf2(char **ptr, const char *format, ...)
+reformat(char **ptr, const char *format, ...)
 {
        char *saved = *ptr;
        va_list ap;
@@ -612,11 +716,13 @@ x_asprintf2(char **ptr, const char *format, ...)
        *ptr = NULL;
        va_start(ap, format);
        if (vasprintf(ptr, format, ap) == -1) {
-               fatal("Out of memory in x_asprintf2");
+               fatal("Out of memory in reformat");
        }
        va_end(ap);
 
-       if (!ptr) fatal("Out of memory in x_asprintf2");
+       if (!ptr) {
+               fatal("Out of memory in reformat");
+       }
        if (saved) {
                free(saved);
        }
@@ -632,16 +738,24 @@ traverse(const char *dir, void (*fn)(const char *, struct stat *))
        struct dirent *de;
 
        d = opendir(dir);
-       if (!d) return;
+       if (!d) {
+               return;
+       }
 
        while ((de = readdir(d))) {
                char *fname;
                struct stat st;
 
-               if (str_eq(de->d_name, ".")) continue;
-               if (str_eq(de->d_name, "..")) continue;
+               if (str_eq(de->d_name, ".")) {
+                       continue;
+               }
+               if (str_eq(de->d_name, "..")) {
+                       continue;
+               }
 
-               if (strlen(de->d_name) == 0) continue;
+               if (strlen(de->d_name) == 0) {
+                       continue;
+               }
 
                fname = format("%s/%s", dir, de->d_name);
                if (lstat(fname, &st)) {
@@ -666,41 +780,49 @@ traverse(const char *dir, void (*fn)(const char *, struct stat *))
 
 /* return the base name of a file - caller frees */
 char *
-basename(const char *s)
+basename(const char *path)
 {
        char *p;
-       p = strrchr(s, '/');
-       if (p) s = p + 1;
+       p = strrchr(path, '/');
+       if (p) {
+               path = p + 1;
+       }
 #ifdef _WIN32
-       p = strrchr(s, '\\');
-       if (p) s = p + 1;
+       p = strrchr(path, '\\');
+       if (p) {
+               path = p + 1;
+       }
 #endif
 
-       return x_strdup(s);
+       return x_strdup(path);
 }
 
 /* return the dir name of a file - caller frees */
 char *
-dirname(char *s)
+dirname(const char *path)
 {
        char *p;
-       char *p2 = NULL;
-       s = x_strdup(s);
+#ifdef _WIN32
+       char *p2;
+#endif
+       char *s;
+       s = x_strdup(path);
        p = strrchr(s, '/');
 #ifdef _WIN32
        p2 = strrchr(s, '\\');
-#endif
-       if (p < p2)
+       if (!p || (p2 && p < p2)) {
                p = p2;
-       if (p == s) {
-               return s;
-       } else if (p) {
-               *p = 0;
-               return s;
-       } else {
+       }
+#endif
+       if (!p) {
                free(s);
-               return x_strdup(".");
+               s = x_strdup(".");
+       } else if (p == s) {
+               *(p + 1) = 0;
+       } else {
+               *p = 0;
        }
+       return s;
 }
 
 /*
@@ -751,82 +873,86 @@ file_size(struct stat *st)
 #endif
 }
 
-/* a safe open/create for read-write */
-int
-safe_open(const char *fname)
+/* Format a size as a human-readable string. Caller frees. */
+char *
+format_human_readable_size(uint64_t v)
 {
-       int fd = open(fname, O_RDWR|O_BINARY);
-       if (fd == -1 && errno == ENOENT) {
-               fd = open(fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
-               if (fd == -1 && errno == EEXIST) {
-                       fd = open(fname, O_RDWR|O_BINARY);
-               }
+       char *s;
+       if (v >= 1000*1000*1000) {
+               s = format("%.1f GB", v/((double)(1000*1000*1000)));
+       } else if (v >= 1000*1000) {
+               s = format("%.1f MB", v/((double)(1000*1000)));
+       } else {
+               s = format("%.1f kB", v/((double)(1000)));
        }
-       return fd;
+       return s;
 }
 
-/* Format a size (in KiB) as a human-readable string. Caller frees. */
+/* Format a size as a parsable string. Caller frees. */
 char *
-format_size(size_t v)
+format_parsable_size_with_suffix(uint64_t size)
 {
        char *s;
-       if (v >= 1024*1024) {
-               s = format("%.1f Gbytes", v/((double)(1024*1024)));
-       } else if (v >= 1024) {
-               s = format("%.1f Mbytes", v/((double)(1024)));
+       if (size >= 1000*1000*1000) {
+               s = format("%.1fG", size / ((double)(1000*1000*1000)));
+       } else if (size >= 1000*1000) {
+               s = format("%.1fM", size / ((double)(1000*1000)));
+       } else if (size >= 1000) {
+               s = format("%.1fk", size / ((double)(1000)));
        } else {
-               s = format("%.0f Kbytes", (double)v);
+               s = format("%u", (unsigned)size);
        }
        return s;
 }
 
-/* return a value in multiples of 1024 give a string that can end
-   in K, M or G
-*/
-size_t
-value_units(const char *s)
+/*
+ * Parse a "size value", i.e. a string that can end in k, M, G, T (10-based
+ * suffixes) or Ki, Mi, Gi, Ti (2-based suffixes). For backward compatibility,
+ * K is also recognized as a synonym of k.
+ */
+bool
+parse_size_with_suffix(const char *str, uint64_t *size)
 {
-       char m;
-       double v = atof(s);
-       m = s[strlen(s)-1];
-       switch (m) {
-       case 'G':
-       case 'g':
-       default:
-               v *= 1024*1024;
-               break;
-       case 'M':
-       case 'm':
-               v *= 1024;
-               break;
-       case 'K':
-       case 'k':
-               v *= 1;
-               break;
-       }
-       return (size_t)v;
-}
+       char *p;
+       double x;
 
-#ifndef _WIN32
-static long
-path_max(const char *path)
-{
-#ifdef PATH_MAX
-       (void)path;
-       return PATH_MAX;
-#elif defined(MAXPATHLEN)
-       (void)path;
-       return MAXPATHLEN;
-#elif defined(_PC_PATH_MAX)
-       long maxlen = pathconf(path, _PC_PATH_MAX);
-       if (maxlen >= 4096) {
-               return maxlen;
-       } else {
-               return 4096;
+       errno = 0;
+       x = strtod(str, &p);
+       if (errno != 0 || x < 0 || p == str || *str == '\0') {
+               return false;
        }
-#endif
+
+       while (isspace(*p)) {
+               ++p;
+       }
+
+       if (*p != '\0') {
+               unsigned multiplier;
+               if (*(p+1) == 'i') {
+                       multiplier = 1024;
+               } else {
+                       multiplier = 1000;
+               }
+               switch (*p) {
+               case 'T':
+                       x *= multiplier;
+               case 'G':
+                       x *= multiplier;
+               case 'M':
+                       x *= multiplier;
+               case 'K':
+               case 'k':
+                       x *= multiplier;
+                       break;
+               default:
+                       return false;
+               }
+       }
+       *size = x;
+       return true;
 }
 
+
 /*
   a sane realpath() function, trying to cope with stupid path limits and
   a broken API
@@ -836,11 +962,21 @@ x_realpath(const char *path)
 {
        long maxlen = path_max(path);
        char *ret, *p;
+#ifdef _WIN32
+       HANDLE path_handle;
+#endif
 
        ret = x_malloc(maxlen);
 
 #if HAVE_REALPATH
        p = realpath(path, ret);
+#elif defined(_WIN32)
+       path_handle = CreateFile(
+               path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
+               FILE_ATTRIBUTE_NORMAL, NULL);
+       GetFinalPathNameByHandle(path_handle, ret, maxlen, FILE_NAME_NORMALIZED);
+       CloseHandle(path_handle);
+       p = ret+4;// strip the \\?\ from the file name
 #else
        /* yes, there are such systems. This replacement relies on
           the fact that when we call x_realpath we only care about symlinks */
@@ -862,7 +998,6 @@ x_realpath(const char *path)
        free(ret);
        return NULL;
 }
-#endif /* !_WIN32 */
 
 /* a getcwd that will returns an allocated buffer */
 char *
@@ -870,7 +1005,7 @@ gnu_getcwd(void)
 {
        unsigned size = 128;
 
-       while (1) {
+       while (true) {
                char *buffer = (char *)x_malloc(size);
                if (getcwd(buffer, size) == buffer) {
                        return buffer;
@@ -884,18 +1019,75 @@ gnu_getcwd(void)
        }
 }
 
-/* create an empty file */
-int
-create_empty_file(const char *fname)
+#ifndef HAVE_STRTOK_R
+/* strtok_r replacement */
+char *
+strtok_r(char *str, const char *delim, char **saveptr)
 {
-       int fd;
+       int len;
+       char *ret;
+       if (!str)
+               str = *saveptr;
+       len = strlen(str);
+       ret = strtok(str, delim);
+       if (ret) {
+               char *save = ret;
+               while (*save++);
+               if ((len + 1) == (intptr_t) (save - str))
+                       save--;
+               *saveptr = save;
+       }
+       return ret;
+}
+#endif
 
-       fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
+/*
+ * Create an empty temporary file. *fname will be reallocated and set to the
+ * resulting filename. Returns an open file descriptor to the file.
+ */
+int
+create_tmp_fd(char **fname)
+{
+       char *template = format("%s.%s", *fname, tmp_string());
+       int fd = mkstemp(template);
+       if (fd == -1 && errno == ENOENT) {
+               if (create_parent_dirs(template) != 0) {
+                       fatal("Failed to create directory %s: %s",
+                             dirname(template), strerror(errno));
+               }
+               reformat(&template, "%s.%s", *fname, tmp_string());
+               fd = mkstemp(template);
+       }
        if (fd == -1) {
-               return -1;
+               fatal("Failed to create file %s: %s", template, strerror(errno));
        }
-       close(fd);
-       return 0;
+       free(*fname);
+       *fname = template;
+       return fd;
+}
+
+/*
+ * Create an empty temporary file. *fname will be reallocated and set to the
+ * resulting filename. Returns an open FILE*.
+ */
+FILE *
+create_tmp_file(char **fname, const char *mode)
+{
+       FILE *file = fdopen(create_tmp_fd(fname), mode);
+       if (!file) {
+               fatal("Failed to create file %s: %s", *fname, strerror(errno));
+       }
+       return file;
+}
+
+/*
+ * Create an empty temporary file. *fname will be reallocated and set to the
+ * resulting filename.
+ */
+void
+create_empty_tmp_file(char **fname)
+{
+       close(create_tmp_fd(fname));
 }
 
 /*
@@ -908,6 +1100,12 @@ get_home_directory(void)
        if (p) {
                return p;
        }
+#ifdef _WIN32
+       p = getenv("APPDATA");
+       if (p) {
+               return p;
+       }
+#endif
 #ifdef HAVE_GETPWUID
        {
                struct passwd *pwd = getpwuid(getuid());
@@ -1022,12 +1220,12 @@ get_relative_path(const char *from, const char *to)
        if (common_prefix_len > 0 || !str_eq(from, "/")) {
                for (p = from + common_prefix_len; *p; p++) {
                        if (*p == '/') {
-                               x_asprintf2(&result, "../%s", result);
+                               reformat(&result, "../%s", result);
                        }
                }
        }
        if (strlen(to) > common_prefix_len) {
-               x_asprintf2(&result, "%s%s", result, to + common_prefix_len + 1);
+               reformat(&result, "%s%s", result, to + common_prefix_len + 1);
        }
        i = strlen(result) - 1;
        while (i >= 0 && result[i] == '/') {
@@ -1061,12 +1259,12 @@ bool
 is_full_path(const char *path)
 {
        if (strchr(path, '/'))
-               return 1;
+               return true;
 #ifdef _WIN32
        if (strchr(path, '\\'))
-               return 1;
+               return true;
 #endif
-       return 0;
+       return false;
 }
 
 /*
@@ -1103,7 +1301,7 @@ x_rename(const char *oldpath, const char *newpath)
 int
 tmp_unlink(const char *path)
 {
-       cc_log("Unlink %s (as-tmp)", path);
+       cc_log("Unlink %s", path);
        return unlink(path);
 }
 
@@ -1118,7 +1316,7 @@ x_unlink(const char *path)
         * file. We don't care if the temp file is trashed, so it's always safe to
         * unlink it first.
         */
-       char *tmp_name = format("%s.tmp.rm.%s", path, tmp_string());
+       char *tmp_name = format("%s.rm.%s", path, tmp_string());
        int result = 0;
        cc_log("Unlink %s via %s", path, tmp_name);
        if (x_rename(path, tmp_name) == -1) {
@@ -1126,7 +1324,10 @@ x_unlink(const char *path)
                goto out;
        }
        if (unlink(tmp_name) == -1) {
-               result = -1;
+               /* If it was released in a race, that's OK. */
+               if (errno != ENOENT) {
+                       result = -1;
+               }
        }
 out:
        free(tmp_name);
@@ -1141,7 +1342,6 @@ x_readlink(const char *path)
        long maxlen = path_max(path);
        ssize_t len;
        char *buf;
-       if (maxlen < 4096) maxlen = 4096;
 
        buf = x_malloc(maxlen);
        len = readlink(path, buf, maxlen-1);
@@ -1172,7 +1372,7 @@ read_file(const char *path, size_t size_hint, char **data, size_t *size)
        }
        size_hint = (size_hint < 1024) ? 1024 : size_hint;
 
-       fd = open(path, O_RDONLY);
+       fd = open(path, O_RDONLY | O_BINARY);
        if (fd == -1) {
                return false;
        }
@@ -1205,15 +1405,15 @@ read_file(const char *path, size_t size_hint, char **data, size_t *size)
 
 /*
  * Return the content (with NUL termination) of a text file, or NULL on error.
- * Caller frees.
+ * Caller frees. Size hint 0 means no hint.
  */
 char *
-read_text_file(const char *path)
+read_text_file(const char *path, size_t size_hint)
 {
        size_t size;
        char *data;
 
-       if (read_file(path, 0, &data, &size)) {
+       if (read_file(path, size_hint, &data, &size)) {
                data = x_realloc(data, size + 1);
                data[size] = '\0';
                return data;
@@ -1221,3 +1421,85 @@ read_text_file(const char *path)
                return NULL;
        }
 }
+
+static bool
+expand_variable(const char **str, char **result, char **errmsg)
+{
+       bool curly;
+       const char *p, *q;
+       char *name;
+       const char *value;
+
+       assert(**str == '$');
+       p = *str + 1;
+       if (*p == '{') {
+               curly = true;
+               ++p;
+       } else {
+               curly = false;
+       }
+       q = p;
+       while (isalnum(*q) || *q == '_') {
+               ++q;
+       }
+       if (curly) {
+               if (*q != '}') {
+                       *errmsg = format("syntax error: missing '}' after \"%s\"", p);
+                       return NULL;
+               }
+       }
+
+       if (q == p) {
+               /* Special case: don't consider a single $ the start of a variable. */
+               reformat(result, "%s$", *result);
+               return true;
+       }
+
+       name = x_strndup(p, q - p);
+       value = getenv(name);
+       if (!value) {
+               *errmsg = format("environment variable \"%s\" not set", name);
+               free(name);
+               return false;
+       }
+       reformat(result, "%s%s", *result, value);
+       if (!curly) {
+               --q;
+       }
+       *str = q;
+       free(name);
+       return true;
+}
+
+/*
+ * Substitute all instances of $VAR or ${VAR}, where VAR is an environment
+ * variable, in a string. Caller frees. If one of the environment variables
+ * doesn't exist, NULL will be returned and *errmsg will be an appropriate
+ * error message (caller frees).
+ */
+char *
+subst_env_in_string(const char *str, char **errmsg)
+{
+       const char *p; /* Interval start. */
+       const char *q; /* Interval end. */
+       char *result;
+
+       assert(errmsg);
+       *errmsg = NULL;
+
+       result = x_strdup("");
+       p = str;
+       q = str;
+       for (q = str; *q; ++q) {
+               if (*q == '$') {
+                       reformat(&result, "%s%.*s", result, (int)(q - p), p);
+                       if (!expand_variable(&q, &result, errmsg)) {
+                               free(result);
+                               return NULL;
+                       }
+                       p = q + 1;
+               }
+       }
+       reformat(&result, "%s%.*s", result, (int)(q - p), p);
+       return result;
+}
index 3792342ff3997afa0715b32858235c4f1d92b461..61f42e4f43f407dd02e14c6ec4173a8be2740e9c 100644 (file)
--- a/version.c
+++ b/version.c
@@ -1 +1 @@
-const char CCACHE_VERSION[] = "3.1.12";
+const char CCACHE_VERSION[] = "3.2";
index 007ba26277c8470d897faa87b7b9fb4b5d15e606..a868f073d8a0e35dcb3ec812b41b1d3f0acdd84d 100644 (file)
@@ -1,14 +1,17 @@
 /* adler32.c -- compute the Adler-32 checksum of a data stream
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2011 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
 /* @(#) $Id$ */
 
-#define ZLIB_INTERNAL
-#include "zlib.h"
+#include "zutil.h"
 
-#define BASE 65521UL    /* largest prime smaller than 65536 */
+#define local static
+
+local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
+
+#define BASE 65521      /* largest prime smaller than 65536 */
 #define NMAX 5552
 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
 
 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
 
-/* use NO_DIVIDE if your processor does not do division in hardware */
+/* use NO_DIVIDE if your processor does not do division in hardware --
+   try it both ways to see which is faster */
 #ifdef NO_DIVIDE
-#  define MOD(a) \
+/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
+   (thank you to John Reiser for pointing this out) */
+#  define CHOP(a) \
     do { \
-        if (a >= (BASE << 16)) a -= (BASE << 16); \
-        if (a >= (BASE << 15)) a -= (BASE << 15); \
-        if (a >= (BASE << 14)) a -= (BASE << 14); \
-        if (a >= (BASE << 13)) a -= (BASE << 13); \
-        if (a >= (BASE << 12)) a -= (BASE << 12); \
-        if (a >= (BASE << 11)) a -= (BASE << 11); \
-        if (a >= (BASE << 10)) a -= (BASE << 10); \
-        if (a >= (BASE << 9)) a -= (BASE << 9); \
-        if (a >= (BASE << 8)) a -= (BASE << 8); \
-        if (a >= (BASE << 7)) a -= (BASE << 7); \
-        if (a >= (BASE << 6)) a -= (BASE << 6); \
-        if (a >= (BASE << 5)) a -= (BASE << 5); \
-        if (a >= (BASE << 4)) a -= (BASE << 4); \
-        if (a >= (BASE << 3)) a -= (BASE << 3); \
-        if (a >= (BASE << 2)) a -= (BASE << 2); \
-        if (a >= (BASE << 1)) a -= (BASE << 1); \
+        unsigned long tmp = a >> 16; \
+        a &= 0xffffUL; \
+        a += (tmp << 4) - tmp; \
+    } while (0)
+#  define MOD28(a) \
+    do { \
+        CHOP(a); \
         if (a >= BASE) a -= BASE; \
     } while (0)
-#  define MOD4(a) \
+#  define MOD(a) \
     do { \
-        if (a >= (BASE << 4)) a -= (BASE << 4); \
-        if (a >= (BASE << 3)) a -= (BASE << 3); \
-        if (a >= (BASE << 2)) a -= (BASE << 2); \
-        if (a >= (BASE << 1)) a -= (BASE << 1); \
+        CHOP(a); \
+        MOD28(a); \
+    } while (0)
+#  define MOD63(a) \
+    do { /* this assumes a is not negative */ \
+        z_off64_t tmp = a >> 32; \
+        a &= 0xffffffffL; \
+        a += (tmp << 8) - (tmp << 5) + tmp; \
+        tmp = a >> 16; \
+        a &= 0xffffL; \
+        a += (tmp << 4) - tmp; \
+        tmp = a >> 16; \
+        a &= 0xffffL; \
+        a += (tmp << 4) - tmp; \
         if (a >= BASE) a -= BASE; \
     } while (0)
 #else
 #  define MOD(a) a %= BASE
-#  define MOD4(a) a %= BASE
+#  define MOD28(a) a %= BASE
+#  define MOD63(a) a %= BASE
 #endif
 
 /* ========================================================================= */
@@ -89,7 +97,7 @@ uLong ZEXPORT adler32(adler, buf, len)
         }
         if (adler >= BASE)
             adler -= BASE;
-        MOD4(sum2);             /* only added so many BASE's */
+        MOD28(sum2);            /* only added so many BASE's */
         return adler | (sum2 << 16);
     }
 
@@ -125,25 +133,47 @@ uLong ZEXPORT adler32(adler, buf, len)
 }
 
 /* ========================================================================= */
-uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+local uLong adler32_combine_(adler1, adler2, len2)
     uLong adler1;
     uLong adler2;
-    z_off_t len2;
+    z_off64_t len2;
 {
     unsigned long sum1;
     unsigned long sum2;
     unsigned rem;
 
+    /* for negative len, return invalid adler32 as a clue for debugging */
+    if (len2 < 0)
+        return 0xffffffffUL;
+
     /* the derivation of this formula is left as an exercise for the reader */
-    rem = (unsigned)(len2 % BASE);
+    MOD63(len2);                /* assumes len2 >= 0 */
+    rem = (unsigned)len2;
     sum1 = adler1 & 0xffff;
     sum2 = rem * sum1;
     MOD(sum2);
     sum1 += (adler2 & 0xffff) + BASE - 1;
     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
-    if (sum1 > BASE) sum1 -= BASE;
-    if (sum1 > BASE) sum1 -= BASE;
-    if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
-    if (sum2 > BASE) sum2 -= BASE;
+    if (sum1 >= BASE) sum1 -= BASE;
+    if (sum1 >= BASE) sum1 -= BASE;
+    if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
+    if (sum2 >= BASE) sum2 -= BASE;
     return sum1 | (sum2 << 16);
 }
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+    uLong adler1;
+    uLong adler2;
+    z_off_t len2;
+{
+    return adler32_combine_(adler1, adler2, len2);
+}
+
+uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
+    uLong adler1;
+    uLong adler2;
+    z_off64_t len2;
+{
+    return adler32_combine_(adler1, adler2, len2);
+}
diff --git a/zlib/compress.c b/zlib/compress.c
deleted file mode 100644 (file)
index df04f01..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/* compress.c -- compress a memory buffer
- * Copyright (C) 1995-2003 Jean-loup Gailly.
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
-
-/* @(#) $Id$ */
-
-#define ZLIB_INTERNAL
-#include "zlib.h"
-
-/* ===========================================================================
-     Compresses the source buffer into the destination buffer. The level
-   parameter has the same meaning as in deflateInit.  sourceLen is the byte
-   length of the source buffer. Upon entry, destLen is the total size of the
-   destination buffer, which must be at least 0.1% larger than sourceLen plus
-   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
-
-     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
-   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
-   Z_STREAM_ERROR if the level parameter is invalid.
-*/
-int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
-    Bytef *dest;
-    uLongf *destLen;
-    const Bytef *source;
-    uLong sourceLen;
-    int level;
-{
-    z_stream stream;
-    int err;
-
-    stream.next_in = (Bytef*)source;
-    stream.avail_in = (uInt)sourceLen;
-#ifdef MAXSEG_64K
-    /* Check for source > 64K on 16-bit machine: */
-    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
-#endif
-    stream.next_out = dest;
-    stream.avail_out = (uInt)*destLen;
-    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
-
-    stream.zalloc = (alloc_func)0;
-    stream.zfree = (free_func)0;
-    stream.opaque = (voidpf)0;
-
-    err = deflateInit(&stream, level);
-    if (err != Z_OK) return err;
-
-    err = deflate(&stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-        deflateEnd(&stream);
-        return err == Z_OK ? Z_BUF_ERROR : err;
-    }
-    *destLen = stream.total_out;
-
-    err = deflateEnd(&stream);
-    return err;
-}
-
-/* ===========================================================================
- */
-int ZEXPORT compress (dest, destLen, source, sourceLen)
-    Bytef *dest;
-    uLongf *destLen;
-    const Bytef *source;
-    uLong sourceLen;
-{
-    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
-}
-
-/* ===========================================================================
-     If the default memLevel or windowBits for deflateInit() is changed, then
-   this function needs to be updated.
- */
-uLong ZEXPORT compressBound (sourceLen)
-    uLong sourceLen;
-{
-    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
-}
index f658a9ef55ee89e930df72907790d11f5504e3ce..979a7190a3ca44c66797f6f994804f825bf82d4a 100644 (file)
@@ -1,5 +1,5 @@
 /* crc32.c -- compute the CRC-32 of a data stream
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  *
  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
@@ -17,6 +17,8 @@
   of the crc tables.  Therefore, if you #define DYNAMIC_CRC_TABLE, you should
   first call get_crc_table() to initialize the tables before allowing more than
   one thread to use crc32().
+
+  DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
  */
 
 #ifdef MAKECRCH
 
 #define local static
 
-/* Find a four-byte integer type for crc32_little() and crc32_big(). */
-#ifndef NOBYFOUR
-#  ifdef STDC           /* need ANSI C limits.h to determine sizes */
-#    include <limits.h>
-#    define BYFOUR
-#    if (UINT_MAX == 0xffffffffUL)
-       typedef unsigned int u4;
-#    else
-#      if (ULONG_MAX == 0xffffffffUL)
-         typedef unsigned long u4;
-#      else
-#        if (USHRT_MAX == 0xffffffffUL)
-           typedef unsigned short u4;
-#        else
-#          undef BYFOUR     /* can't find a four-byte integer type! */
-#        endif
-#      endif
-#    endif
-#  endif /* STDC */
-#endif /* !NOBYFOUR */
-
 /* Definitions for doing the crc four data bytes at a time. */
+#if !defined(NOBYFOUR) && defined(Z_U4)
+#  define BYFOUR
+#endif
 #ifdef BYFOUR
-#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
-                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
    local unsigned long crc32_little OF((unsigned long,
                         const unsigned char FAR *, unsigned));
    local unsigned long crc32_big OF((unsigned long,
 local unsigned long gf2_matrix_times OF((unsigned long *mat,
                                          unsigned long vec));
 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
+local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
+
 
 #ifdef DYNAMIC_CRC_TABLE
 
 local volatile int crc_table_empty = 1;
-local unsigned long FAR crc_table[TBLS][256];
+local z_crc_t FAR crc_table[TBLS][256];
 local void make_crc_table OF((void));
 #ifdef MAKECRCH
-   local void write_table OF((FILE *, const unsigned long FAR *));
+   local void write_table OF((FILE *, const z_crc_t FAR *));
 #endif /* MAKECRCH */
 /*
   Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
@@ -105,9 +89,9 @@ local void make_crc_table OF((void));
 */
 local void make_crc_table()
 {
-    unsigned long c;
+    z_crc_t c;
     int n, k;
-    unsigned long poly;                 /* polynomial exclusive-or pattern */
+    z_crc_t poly;                       /* polynomial exclusive-or pattern */
     /* terms of polynomial defining this crc (except x^32): */
     static volatile int first = 1;      /* flag to limit concurrent making */
     static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
@@ -119,13 +103,13 @@ local void make_crc_table()
         first = 0;
 
         /* make exclusive-or pattern from polynomial (0xedb88320UL) */
-        poly = 0UL;
-        for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
-            poly |= 1UL << (31 - p[n]);
+        poly = 0;
+        for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
+            poly |= (z_crc_t)1 << (31 - p[n]);
 
         /* generate a crc for every 8-bit value */
         for (n = 0; n < 256; n++) {
-            c = (unsigned long)n;
+            c = (z_crc_t)n;
             for (k = 0; k < 8; k++)
                 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
             crc_table[0][n] = c;
@@ -136,11 +120,11 @@ local void make_crc_table()
            and then the byte reversal of those as well as the first table */
         for (n = 0; n < 256; n++) {
             c = crc_table[0][n];
-            crc_table[4][n] = REV(c);
+            crc_table[4][n] = ZSWAP32(c);
             for (k = 1; k < 4; k++) {
                 c = crc_table[0][c & 0xff] ^ (c >> 8);
                 crc_table[k][n] = c;
-                crc_table[k + 4][n] = REV(c);
+                crc_table[k + 4][n] = ZSWAP32(c);
             }
         }
 #endif /* BYFOUR */
@@ -162,7 +146,7 @@ local void make_crc_table()
         if (out == NULL) return;
         fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
         fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
-        fprintf(out, "local const unsigned long FAR ");
+        fprintf(out, "local const z_crc_t FAR ");
         fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
         write_table(out, crc_table[0]);
 #  ifdef BYFOUR
@@ -182,12 +166,13 @@ local void make_crc_table()
 #ifdef MAKECRCH
 local void write_table(out, table)
     FILE *out;
-    const unsigned long FAR *table;
+    const z_crc_t FAR *table;
 {
     int n;
 
     for (n = 0; n < 256; n++)
-        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
+        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ",
+                (unsigned long)(table[n]),
                 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
 }
 #endif /* MAKECRCH */
@@ -202,13 +187,13 @@ local void write_table(out, table)
 /* =========================================================================
  * This function can be used by asm versions of crc32()
  */
-const unsigned long FAR * ZEXPORT get_crc_table()
+const z_crc_t FAR * ZEXPORT get_crc_table()
 {
 #ifdef DYNAMIC_CRC_TABLE
     if (crc_table_empty)
         make_crc_table();
 #endif /* DYNAMIC_CRC_TABLE */
-    return (const unsigned long FAR *)crc_table;
+    return (const z_crc_t FAR *)crc_table;
 }
 
 /* ========================================================================= */
@@ -219,7 +204,7 @@ const unsigned long FAR * ZEXPORT get_crc_table()
 unsigned long ZEXPORT crc32(crc, buf, len)
     unsigned long crc;
     const unsigned char FAR *buf;
-    unsigned len;
+    uInt len;
 {
     if (buf == Z_NULL) return 0UL;
 
@@ -230,7 +215,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
 
 #ifdef BYFOUR
     if (sizeof(void *) == sizeof(ptrdiff_t)) {
-        u4 endian;
+        z_crc_t endian;
 
         endian = 1;
         if (*((unsigned char *)(&endian)))
@@ -264,17 +249,17 @@ local unsigned long crc32_little(crc, buf, len)
     const unsigned char FAR *buf;
     unsigned len;
 {
-    register u4 c;
-    register const u4 FAR *buf4;
+    register z_crc_t c;
+    register const z_crc_t FAR *buf4;
 
-    c = (u4)crc;
+    c = (z_crc_t)crc;
     c = ~c;
     while (len && ((ptrdiff_t)buf & 3)) {
         c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
         len--;
     }
 
-    buf4 = (const u4 FAR *)(const void FAR *)buf;
+    buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
     while (len >= 32) {
         DOLIT32;
         len -= 32;
@@ -304,17 +289,17 @@ local unsigned long crc32_big(crc, buf, len)
     const unsigned char FAR *buf;
     unsigned len;
 {
-    register u4 c;
-    register const u4 FAR *buf4;
+    register z_crc_t c;
+    register const z_crc_t FAR *buf4;
 
-    c = REV((u4)crc);
+    c = ZSWAP32((z_crc_t)crc);
     c = ~c;
     while (len && ((ptrdiff_t)buf & 3)) {
         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
         len--;
     }
 
-    buf4 = (const u4 FAR *)(const void FAR *)buf;
+    buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
     buf4--;
     while (len >= 32) {
         DOBIG32;
@@ -331,7 +316,7 @@ local unsigned long crc32_big(crc, buf, len)
         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
     } while (--len);
     c = ~c;
-    return (unsigned long)(REV(c));
+    return (unsigned long)(ZSWAP32(c));
 }
 
 #endif /* BYFOUR */
@@ -367,22 +352,22 @@ local void gf2_matrix_square(square, mat)
 }
 
 /* ========================================================================= */
-uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+local uLong crc32_combine_(crc1, crc2, len2)
     uLong crc1;
     uLong crc2;
-    z_off_t len2;
+    z_off64_t len2;
 {
     int n;
     unsigned long row;
     unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
     unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
 
-    /* degenerate case */
-    if (len2 == 0)
+    /* degenerate case (also disallow negative lengths) */
+    if (len2 <= 0)
         return crc1;
 
     /* put operator for one zero bit in odd */
-    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
+    odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
     row = 1;
     for (n = 1; n < GF2_DIM; n++) {
         odd[n] = row;
@@ -421,3 +406,20 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
     crc1 ^= crc2;
     return crc1;
 }
+
+/* ========================================================================= */
+uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+    uLong crc1;
+    uLong crc2;
+    z_off_t len2;
+{
+    return crc32_combine_(crc1, crc2, len2);
+}
+
+uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
+    uLong crc1;
+    uLong crc2;
+    z_off64_t len2;
+{
+    return crc32_combine_(crc1, crc2, len2);
+}
index 8053b6117c023f64554ea783b23fa06985acff7b..9e0c7781025148380d130d6f7b6e590117ad3a8c 100644 (file)
@@ -2,7 +2,7 @@
  * Generated automatically by crc32.c
  */
 
-local const unsigned long FAR crc_table[TBLS][256] =
+local const z_crc_t FAR crc_table[TBLS][256] =
 {
   {
     0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
index 29ce1f64a57a18f3c939f879d301ece10ac6da1b..696957705b756b1457a18c7a23a91affafa17d91 100644 (file)
@@ -1,5 +1,5 @@
 /* deflate.c -- compress data using the deflation algorithm
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -37,7 +37,7 @@
  *  REFERENCES
  *
  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
- *      Available in http://www.ietf.org/rfc/rfc1951.txt
+ *      Available in http://tools.ietf.org/html/rfc1951
  *
  *      A description of the Rabin and Karp algorithm is given in the book
  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
@@ -52,7 +52,7 @@
 #include "deflate.h"
 
 const char deflate_copyright[] =
-   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
+   " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
 /*
   If you use the zlib library in a product, an acknowledgment is welcome
   in the documentation of your product. If for some reason you cannot
@@ -79,19 +79,18 @@ local block_state deflate_fast   OF((deflate_state *s, int flush));
 #ifndef FASTEST
 local block_state deflate_slow   OF((deflate_state *s, int flush));
 #endif
+local block_state deflate_rle    OF((deflate_state *s, int flush));
+local block_state deflate_huff   OF((deflate_state *s, int flush));
 local void lm_init        OF((deflate_state *s));
 local void putShortMSB    OF((deflate_state *s, uInt b));
 local void flush_pending  OF((z_streamp strm));
 local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
-#ifndef FASTEST
 #ifdef ASMV
       void match_init OF((void)); /* asm code initialization */
       uInt longest_match  OF((deflate_state *s, IPos cur_match));
 #else
 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
 #endif
-#endif
-local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
 
 #ifdef DEBUG
 local  void check_match OF((deflate_state *s, IPos start, IPos match,
@@ -110,11 +109,6 @@ local  void check_match OF((deflate_state *s, IPos start, IPos match,
 #endif
 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
 
-#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
-/* Minimum amount of lookahead, except at the end of the input file.
- * See deflate.c for comments about the MIN_MATCH+1.
- */
-
 /* Values for max_lazy_match, good_match and max_chain_length, depending on
  * the desired pack level (0..9). The values given below have been tuned to
  * exclude worst case performance for pathological files. Better values may be
@@ -161,6 +155,9 @@ local const config configuration_table[10] = {
 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
 #endif
 
+/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
+#define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
+
 /* ===========================================================================
  * Update a hash value with the given input byte
  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
@@ -241,10 +238,19 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
 
     strm->msg = Z_NULL;
     if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+        return Z_STREAM_ERROR;
+#else
         strm->zalloc = zcalloc;
         strm->opaque = (voidpf)0;
+#endif
     }
-    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
+    if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+        return Z_STREAM_ERROR;
+#else
+        strm->zfree = zcfree;
+#endif
 
 #ifdef FASTEST
     if (level != 0) level = 1;
@@ -288,6 +294,8 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
 
+    s->high_water = 0;      /* nothing written to s->window yet */
+
     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
 
     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
@@ -297,7 +305,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
         s->pending_buf == Z_NULL) {
         s->status = FINISH_STATE;
-        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
+        strm->msg = ERR_MSG(Z_MEM_ERROR);
         deflateEnd (strm);
         return Z_MEM_ERROR;
     }
@@ -318,43 +326,70 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
     uInt  dictLength;
 {
     deflate_state *s;
-    uInt length = dictLength;
-    uInt n;
-    IPos hash_head = 0;
+    uInt str, n;
+    int wrap;
+    unsigned avail;
+    z_const unsigned char *next;
 
-    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
-        strm->state->wrap == 2 ||
-        (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
+    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
         return Z_STREAM_ERROR;
-
     s = strm->state;
-    if (s->wrap)
-        strm->adler = adler32(strm->adler, dictionary, dictLength);
+    wrap = s->wrap;
+    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
+        return Z_STREAM_ERROR;
 
-    if (length < MIN_MATCH) return Z_OK;
-    if (length > MAX_DIST(s)) {
-        length = MAX_DIST(s);
-        dictionary += dictLength - length; /* use the tail of the dictionary */
+    /* when using zlib wrappers, compute Adler-32 for provided dictionary */
+    if (wrap == 1)
+        strm->adler = adler32(strm->adler, dictionary, dictLength);
+    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
+
+    /* if dictionary would fill window, just replace the history */
+    if (dictLength >= s->w_size) {
+        if (wrap == 0) {            /* already empty otherwise */
+            CLEAR_HASH(s);
+            s->strstart = 0;
+            s->block_start = 0L;
+            s->insert = 0;
+        }
+        dictionary += dictLength - s->w_size;  /* use the tail */
+        dictLength = s->w_size;
     }
-    zmemcpy(s->window, dictionary, length);
-    s->strstart = length;
-    s->block_start = (long)length;
 
-    /* Insert all strings in the hash table (except for the last two bytes).
-     * s->lookahead stays null, so s->ins_h will be recomputed at the next
-     * call of fill_window.
-     */
-    s->ins_h = s->window[0];
-    UPDATE_HASH(s, s->ins_h, s->window[1]);
-    for (n = 0; n <= length - MIN_MATCH; n++) {
-        INSERT_STRING(s, n, hash_head);
+    /* insert dictionary into window and hash */
+    avail = strm->avail_in;
+    next = strm->next_in;
+    strm->avail_in = dictLength;
+    strm->next_in = (z_const Bytef *)dictionary;
+    fill_window(s);
+    while (s->lookahead >= MIN_MATCH) {
+        str = s->strstart;
+        n = s->lookahead - (MIN_MATCH-1);
+        do {
+            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+            s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+            s->head[s->ins_h] = (Pos)str;
+            str++;
+        } while (--n);
+        s->strstart = str;
+        s->lookahead = MIN_MATCH-1;
+        fill_window(s);
     }
-    if (hash_head) hash_head = 0;  /* to make compiler happy */
+    s->strstart += s->lookahead;
+    s->block_start = (long)s->strstart;
+    s->insert = s->lookahead;
+    s->lookahead = 0;
+    s->match_length = s->prev_length = MIN_MATCH-1;
+    s->match_available = 0;
+    strm->next_in = next;
+    strm->avail_in = avail;
+    s->wrap = wrap;
     return Z_OK;
 }
 
 /* ========================================================================= */
-int ZEXPORT deflateReset (strm)
+int ZEXPORT deflateResetKeep (strm)
     z_streamp strm;
 {
     deflate_state *s;
@@ -384,11 +419,22 @@ int ZEXPORT deflateReset (strm)
     s->last_flush = Z_NO_FLUSH;
 
     _tr_init(s);
-    lm_init(s);
 
     return Z_OK;
 }
 
+/* ========================================================================= */
+int ZEXPORT deflateReset (strm)
+    z_streamp strm;
+{
+    int ret;
+
+    ret = deflateResetKeep(strm);
+    if (ret == Z_OK)
+        lm_init(strm->state);
+    return ret;
+}
+
 /* ========================================================================= */
 int ZEXPORT deflateSetHeader (strm, head)
     z_streamp strm;
@@ -400,15 +446,43 @@ int ZEXPORT deflateSetHeader (strm, head)
     return Z_OK;
 }
 
+/* ========================================================================= */
+int ZEXPORT deflatePending (strm, pending, bits)
+    unsigned *pending;
+    int *bits;
+    z_streamp strm;
+{
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    if (pending != Z_NULL)
+        *pending = strm->state->pending;
+    if (bits != Z_NULL)
+        *bits = strm->state->bi_valid;
+    return Z_OK;
+}
+
 /* ========================================================================= */
 int ZEXPORT deflatePrime (strm, bits, value)
     z_streamp strm;
     int bits;
     int value;
 {
+    deflate_state *s;
+    int put;
+
     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
-    strm->state->bi_valid = bits;
-    strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
+    s = strm->state;
+    if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
+        return Z_BUF_ERROR;
+    do {
+        put = Buf_size - s->bi_valid;
+        if (put > bits)
+            put = bits;
+        s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
+        s->bi_valid += put;
+        _tr_flush_bits(s);
+        value >>= put;
+        bits -= put;
+    } while (bits);
     return Z_OK;
 }
 
@@ -435,9 +509,12 @@ int ZEXPORT deflateParams(strm, level, strategy)
     }
     func = configuration_table[s->level].func;
 
-    if (func != configuration_table[level].func && strm->total_in != 0) {
+    if ((strategy != s->strategy || func != configuration_table[level].func) &&
+        strm->total_in != 0) {
         /* Flush the last buffer: */
-        err = deflate(strm, Z_PARTIAL_FLUSH);
+        err = deflate(strm, Z_BLOCK);
+        if (err == Z_BUF_ERROR && s->pending == 0)
+            err = Z_OK;
     }
     if (s->level != level) {
         s->level = level;
@@ -481,33 +558,66 @@ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
  * resulting from using fixed blocks instead of stored blocks, which deflate
  * can emit on compressed data for some combinations of the parameters.
  *
- * This function could be more sophisticated to provide closer upper bounds
- * for every combination of windowBits and memLevel, as well as wrap.
- * But even the conservative upper bound of about 14% expansion does not
- * seem onerous for output buffer allocation.
+ * This function could be more sophisticated to provide closer upper bounds for
+ * every combination of windowBits and memLevel.  But even the conservative
+ * upper bound of about 14% expansion does not seem onerous for output buffer
+ * allocation.
  */
 uLong ZEXPORT deflateBound(strm, sourceLen)
     z_streamp strm;
     uLong sourceLen;
 {
     deflate_state *s;
-    uLong destLen;
+    uLong complen, wraplen;
+    Bytef *str;
 
-    /* conservative upper bound */
-    destLen = sourceLen +
-              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
+    /* conservative upper bound for compressed data */
+    complen = sourceLen +
+              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
 
-    /* if can't get parameters, return conservative bound */
+    /* if can't get parameters, return conservative bound plus zlib wrapper */
     if (strm == Z_NULL || strm->state == Z_NULL)
-        return destLen;
+        return complen + 6;
 
-    /* if not default parameters, return conservative bound */
+    /* compute wrapper length */
     s = strm->state;
+    switch (s->wrap) {
+    case 0:                                 /* raw deflate */
+        wraplen = 0;
+        break;
+    case 1:                                 /* zlib wrapper */
+        wraplen = 6 + (s->strstart ? 4 : 0);
+        break;
+    case 2:                                 /* gzip wrapper */
+        wraplen = 18;
+        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
+            if (s->gzhead->extra != Z_NULL)
+                wraplen += 2 + s->gzhead->extra_len;
+            str = s->gzhead->name;
+            if (str != Z_NULL)
+                do {
+                    wraplen++;
+                } while (*str++);
+            str = s->gzhead->comment;
+            if (str != Z_NULL)
+                do {
+                    wraplen++;
+                } while (*str++);
+            if (s->gzhead->hcrc)
+                wraplen += 2;
+        }
+        break;
+    default:                                /* for compiler happiness */
+        wraplen = 6;
+    }
+
+    /* if not default parameters, return conservative bound */
     if (s->w_bits != 15 || s->hash_bits != 8 + 7)
-        return destLen;
+        return complen + wraplen;
 
     /* default settings: return tight bound for that case */
-    return compressBound(sourceLen);
+    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+           (sourceLen >> 25) + 13 - 6 + wraplen;
 }
 
 /* =========================================================================
@@ -532,19 +642,22 @@ local void putShortMSB (s, b)
 local void flush_pending(strm)
     z_streamp strm;
 {
-    unsigned len = strm->state->pending;
+    unsigned len;
+    deflate_state *s = strm->state;
 
+    _tr_flush_bits(s);
+    len = s->pending;
     if (len > strm->avail_out) len = strm->avail_out;
     if (len == 0) return;
 
-    zmemcpy(strm->next_out, strm->state->pending_out, len);
+    zmemcpy(strm->next_out, s->pending_out, len);
     strm->next_out  += len;
-    strm->state->pending_out  += len;
+    s->pending_out  += len;
     strm->total_out += len;
     strm->avail_out  -= len;
-    strm->state->pending -= len;
-    if (strm->state->pending == 0) {
-        strm->state->pending_out = strm->state->pending_buf;
+    s->pending -= len;
+    if (s->pending == 0) {
+        s->pending_out = s->pending_buf;
     }
 }
 
@@ -557,7 +670,7 @@ int ZEXPORT deflate (strm, flush)
     deflate_state *s;
 
     if (strm == Z_NULL || strm->state == Z_NULL ||
-        flush > Z_FINISH || flush < 0) {
+        flush > Z_BLOCK || flush < 0) {
         return Z_STREAM_ERROR;
     }
     s = strm->state;
@@ -581,7 +694,7 @@ int ZEXPORT deflate (strm, flush)
             put_byte(s, 31);
             put_byte(s, 139);
             put_byte(s, 8);
-            if (s->gzhead == NULL) {
+            if (s->gzhead == Z_NULL) {
                 put_byte(s, 0);
                 put_byte(s, 0);
                 put_byte(s, 0);
@@ -608,7 +721,7 @@ int ZEXPORT deflate (strm, flush)
                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
                              4 : 0));
                 put_byte(s, s->gzhead->os & 0xff);
-                if (s->gzhead->extra != NULL) {
+                if (s->gzhead->extra != Z_NULL) {
                     put_byte(s, s->gzhead->extra_len & 0xff);
                     put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
                 }
@@ -650,7 +763,7 @@ int ZEXPORT deflate (strm, flush)
     }
 #ifdef GZIP
     if (s->status == EXTRA_STATE) {
-        if (s->gzhead->extra != NULL) {
+        if (s->gzhead->extra != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
 
             while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
@@ -678,7 +791,7 @@ int ZEXPORT deflate (strm, flush)
             s->status = NAME_STATE;
     }
     if (s->status == NAME_STATE) {
-        if (s->gzhead->name != NULL) {
+        if (s->gzhead->name != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
             int val;
 
@@ -709,7 +822,7 @@ int ZEXPORT deflate (strm, flush)
             s->status = COMMENT_STATE;
     }
     if (s->status == COMMENT_STATE) {
-        if (s->gzhead->comment != NULL) {
+        if (s->gzhead->comment != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
             int val;
 
@@ -771,7 +884,7 @@ int ZEXPORT deflate (strm, flush)
      * flushes. For repeated and useless calls with Z_FINISH, we keep
      * returning Z_STREAM_END instead of Z_BUF_ERROR.
      */
-    } else if (strm->avail_in == 0 && flush <= old_flush &&
+    } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
                flush != Z_FINISH) {
         ERR_RETURN(strm, Z_BUF_ERROR);
     }
@@ -787,7 +900,9 @@ int ZEXPORT deflate (strm, flush)
         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
         block_state bstate;
 
-        bstate = (*(configuration_table[s->level].func))(s, flush);
+        bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
+                    (s->strategy == Z_RLE ? deflate_rle(s, flush) :
+                        (*(configuration_table[s->level].func))(s, flush));
 
         if (bstate == finish_started || bstate == finish_done) {
             s->status = FINISH_STATE;
@@ -808,13 +923,18 @@ int ZEXPORT deflate (strm, flush)
         if (bstate == block_done) {
             if (flush == Z_PARTIAL_FLUSH) {
                 _tr_align(s);
-            } else { /* FULL_FLUSH or SYNC_FLUSH */
+            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
                 _tr_stored_block(s, (char*)0, 0L, 0);
                 /* For a full flush, this empty block will be recognized
                  * as a special marker by inflate_sync().
                  */
                 if (flush == Z_FULL_FLUSH) {
                     CLEAR_HASH(s);             /* forget history */
+                    if (s->lookahead == 0) {
+                        s->strstart = 0;
+                        s->block_start = 0L;
+                        s->insert = 0;
+                    }
                 }
             }
             flush_pending(strm);
@@ -909,12 +1029,12 @@ int ZEXPORT deflateCopy (dest, source)
 
     ss = source->state;
 
-    zmemcpy(dest, source, sizeof(z_stream));
+    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
 
     ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
     if (ds == Z_NULL) return Z_MEM_ERROR;
     dest->state = (struct internal_state FAR *) ds;
-    zmemcpy(ds, ss, sizeof(deflate_state));
+    zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
     ds->strm = dest;
 
     ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
@@ -930,8 +1050,8 @@ int ZEXPORT deflateCopy (dest, source)
     }
     /* following zmemcpy do not work for 16-bit MSDOS */
     zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
-    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
-    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
+    zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
+    zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
     zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
 
     ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
@@ -965,15 +1085,15 @@ local int read_buf(strm, buf, size)
 
     strm->avail_in  -= len;
 
+    zmemcpy(buf, strm->next_in, len);
     if (strm->state->wrap == 1) {
-        strm->adler = adler32(strm->adler, strm->next_in, len);
+        strm->adler = adler32(strm->adler, buf, len);
     }
 #ifdef GZIP
     else if (strm->state->wrap == 2) {
-        strm->adler = crc32(strm->adler, strm->next_in, len);
+        strm->adler = crc32(strm->adler, buf, len);
     }
 #endif
-    zmemcpy(buf, strm->next_in, len);
     strm->next_in  += len;
     strm->total_in += len;
 
@@ -1000,6 +1120,7 @@ local void lm_init (s)
     s->strstart = 0;
     s->block_start = 0L;
     s->lookahead = 0;
+    s->insert = 0;
     s->match_length = s->prev_length = MIN_MATCH-1;
     s->match_available = 0;
     s->ins_h = 0;
@@ -1167,12 +1288,13 @@ local uInt longest_match(s, cur_match)
     return s->lookahead;
 }
 #endif /* ASMV */
-#endif /* FASTEST */
+
+#else /* FASTEST */
 
 /* ---------------------------------------------------------------------------
- * Optimized version for level == 1 or strategy == Z_RLE only
+ * Optimized version for FASTEST only
  */
-local uInt longest_match_fast(s, cur_match)
+local uInt longest_match(s, cur_match)
     deflate_state *s;
     IPos cur_match;                             /* current match */
 {
@@ -1225,6 +1347,8 @@ local uInt longest_match_fast(s, cur_match)
     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
 }
 
+#endif /* FASTEST */
+
 #ifdef DEBUG
 /* ===========================================================================
  * Check that the match at match_start is indeed a match.
@@ -1271,6 +1395,8 @@ local void fill_window(s)
     unsigned more;    /* Amount of free space at the end of the window. */
     uInt wsize = s->w_size;
 
+    Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
+
     do {
         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
 
@@ -1303,7 +1429,6 @@ local void fill_window(s)
                later. (Using level 0 permanently is not an optimal usage of
                zlib, so we don't care about this pathological case.)
              */
-            /* %%% avoid this when Z_RLE */
             n = s->hash_size;
             p = &s->head[n];
             do {
@@ -1324,7 +1449,7 @@ local void fill_window(s)
 #endif
             more += wsize;
         }
-        if (s->strm->avail_in == 0) return;
+        if (s->strm->avail_in == 0) break;
 
         /* If there was no sliding:
          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
@@ -1343,39 +1468,88 @@ local void fill_window(s)
         s->lookahead += n;
 
         /* Initialize the hash value now that we have some input: */
-        if (s->lookahead >= MIN_MATCH) {
-            s->ins_h = s->window[s->strstart];
-            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+        if (s->lookahead + s->insert >= MIN_MATCH) {
+            uInt str = s->strstart - s->insert;
+            s->ins_h = s->window[str];
+            UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
 #if MIN_MATCH != 3
             Call UPDATE_HASH() MIN_MATCH-3 more times
 #endif
+            while (s->insert) {
+                UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+                s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+                s->head[s->ins_h] = (Pos)str;
+                str++;
+                s->insert--;
+                if (s->lookahead + s->insert < MIN_MATCH)
+                    break;
+            }
         }
         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
          * but this is not important since only literal bytes will be emitted.
          */
 
     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+
+    /* If the WIN_INIT bytes after the end of the current data have never been
+     * written, then zero those bytes in order to avoid memory check reports of
+     * the use of uninitialized (or uninitialised as Julian writes) bytes by
+     * the longest match routines.  Update the high water mark for the next
+     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
+     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
+     */
+    if (s->high_water < s->window_size) {
+        ulg curr = s->strstart + (ulg)(s->lookahead);
+        ulg init;
+
+        if (s->high_water < curr) {
+            /* Previous high water mark below current data -- zero WIN_INIT
+             * bytes or up to end of window, whichever is less.
+             */
+            init = s->window_size - curr;
+            if (init > WIN_INIT)
+                init = WIN_INIT;
+            zmemzero(s->window + curr, (unsigned)init);
+            s->high_water = curr + init;
+        }
+        else if (s->high_water < (ulg)curr + WIN_INIT) {
+            /* High water mark at or above current data, but below current data
+             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
+             * to end of window, whichever is less.
+             */
+            init = (ulg)curr + WIN_INIT - s->high_water;
+            if (init > s->window_size - s->high_water)
+                init = s->window_size - s->high_water;
+            zmemzero(s->window + s->high_water, (unsigned)init);
+            s->high_water += init;
+        }
+    }
+
+    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
+           "not enough room for search");
 }
 
 /* ===========================================================================
  * Flush the current block, with given end-of-file flag.
  * IN assertion: strstart is set to the end of the current match.
  */
-#define FLUSH_BLOCK_ONLY(s, eof) { \
+#define FLUSH_BLOCK_ONLY(s, last) { \
    _tr_flush_block(s, (s->block_start >= 0L ? \
                    (charf *)&s->window[(unsigned)s->block_start] : \
                    (charf *)Z_NULL), \
                 (ulg)((long)s->strstart - s->block_start), \
-                (eof)); \
+                (last)); \
    s->block_start = s->strstart; \
    flush_pending(s->strm); \
    Tracev((stderr,"[FLUSH]")); \
 }
 
 /* Same but force premature exit if necessary. */
-#define FLUSH_BLOCK(s, eof) { \
-   FLUSH_BLOCK_ONLY(s, eof); \
-   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
+#define FLUSH_BLOCK(s, last) { \
+   FLUSH_BLOCK_ONLY(s, last); \
+   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
 }
 
 /* ===========================================================================
@@ -1434,8 +1608,14 @@ local block_state deflate_stored(s, flush)
             FLUSH_BLOCK(s, 0);
         }
     }
-    FLUSH_BLOCK(s, flush == Z_FINISH);
-    return flush == Z_FINISH ? finish_done : block_done;
+    s->insert = 0;
+    if (flush == Z_FINISH) {
+        FLUSH_BLOCK(s, 1);
+        return finish_done;
+    }
+    if ((long)s->strstart > s->block_start)
+        FLUSH_BLOCK(s, 0);
+    return block_done;
 }
 
 /* ===========================================================================
@@ -1449,7 +1629,7 @@ local block_state deflate_fast(s, flush)
     deflate_state *s;
     int flush;
 {
-    IPos hash_head = NIL; /* head of the hash chain */
+    IPos hash_head;       /* head of the hash chain */
     int bflush;           /* set if current block must be flushed */
 
     for (;;) {
@@ -1469,6 +1649,7 @@ local block_state deflate_fast(s, flush)
         /* Insert the string window[strstart .. strstart+2] in the
          * dictionary, and set hash_head to the head of the hash chain:
          */
+        hash_head = NIL;
         if (s->lookahead >= MIN_MATCH) {
             INSERT_STRING(s, s->strstart, hash_head);
         }
@@ -1481,19 +1662,8 @@ local block_state deflate_fast(s, flush)
              * of window index 0 (in particular we have to avoid a match
              * of the string with itself at the start of the input file).
              */
-#ifdef FASTEST
-            if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
-                (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-#else
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
-                s->match_length = longest_match (s, hash_head);
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-#endif
-            /* longest_match() or longest_match_fast() sets match_start */
+            s->match_length = longest_match (s, hash_head);
+            /* longest_match() sets match_start */
         }
         if (s->match_length >= MIN_MATCH) {
             check_match(s, s->strstart, s->match_start, s->match_length);
@@ -1541,8 +1711,14 @@ local block_state deflate_fast(s, flush)
         }
         if (bflush) FLUSH_BLOCK(s, 0);
     }
-    FLUSH_BLOCK(s, flush == Z_FINISH);
-    return flush == Z_FINISH ? finish_done : block_done;
+    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+    if (flush == Z_FINISH) {
+        FLUSH_BLOCK(s, 1);
+        return finish_done;
+    }
+    if (s->last_lit)
+        FLUSH_BLOCK(s, 0);
+    return block_done;
 }
 
 #ifndef FASTEST
@@ -1555,7 +1731,7 @@ local block_state deflate_slow(s, flush)
     deflate_state *s;
     int flush;
 {
-    IPos hash_head = NIL;    /* head of hash chain */
+    IPos hash_head;          /* head of hash chain */
     int bflush;              /* set if current block must be flushed */
 
     /* Process the input block. */
@@ -1576,6 +1752,7 @@ local block_state deflate_slow(s, flush)
         /* Insert the string window[strstart .. strstart+2] in the
          * dictionary, and set hash_head to the head of the hash chain:
          */
+        hash_head = NIL;
         if (s->lookahead >= MIN_MATCH) {
             INSERT_STRING(s, s->strstart, hash_head);
         }
@@ -1591,12 +1768,8 @@ local block_state deflate_slow(s, flush)
              * of window index 0 (in particular we have to avoid a match
              * of the string with itself at the start of the input file).
              */
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
-                s->match_length = longest_match (s, hash_head);
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-            /* longest_match() or longest_match_fast() sets match_start */
+            s->match_length = longest_match (s, hash_head);
+            /* longest_match() sets match_start */
 
             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
 #if TOO_FAR <= 32767
@@ -1669,12 +1842,17 @@ local block_state deflate_slow(s, flush)
         _tr_tally_lit(s, s->window[s->strstart-1], bflush);
         s->match_available = 0;
     }
-    FLUSH_BLOCK(s, flush == Z_FINISH);
-    return flush == Z_FINISH ? finish_done : block_done;
+    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+    if (flush == Z_FINISH) {
+        FLUSH_BLOCK(s, 1);
+        return finish_done;
+    }
+    if (s->last_lit)
+        FLUSH_BLOCK(s, 0);
+    return block_done;
 }
 #endif /* FASTEST */
 
-#if 0
 /* ===========================================================================
  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
@@ -1684,43 +1862,52 @@ local block_state deflate_rle(s, flush)
     deflate_state *s;
     int flush;
 {
-    int bflush;         /* set if current block must be flushed */
-    uInt run;           /* length of run */
-    uInt max;           /* maximum length of run */
-    uInt prev;          /* byte at distance one to match */
-    Bytef *scan;        /* scan for end of run */
+    int bflush;             /* set if current block must be flushed */
+    uInt prev;              /* byte at distance one to match */
+    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
 
     for (;;) {
         /* Make sure that we always have enough lookahead, except
          * at the end of the input file. We need MAX_MATCH bytes
-         * for the longest encodable run.
+         * for the longest run, plus one for the unrolled loop.
          */
-        if (s->lookahead < MAX_MATCH) {
+        if (s->lookahead <= MAX_MATCH) {
             fill_window(s);
-            if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
+            if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
                 return need_more;
             }
             if (s->lookahead == 0) break; /* flush the current block */
         }
 
         /* See how many times the previous byte repeats */
-        run = 0;
-        if (s->strstart > 0) {      /* if there is a previous byte, that is */
-            max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
+        s->match_length = 0;
+        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
             scan = s->window + s->strstart - 1;
-            prev = *scan++;
-            do {
-                if (*scan++ != prev)
-                    break;
-            } while (++run < max);
+            prev = *scan;
+            if (prev == *++scan && prev == *++scan && prev == *++scan) {
+                strend = s->window + s->strstart + MAX_MATCH;
+                do {
+                } while (prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         scan < strend);
+                s->match_length = MAX_MATCH - (int)(strend - scan);
+                if (s->match_length > s->lookahead)
+                    s->match_length = s->lookahead;
+            }
+            Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
         }
 
         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
-        if (run >= MIN_MATCH) {
-            check_match(s, s->strstart, s->strstart - 1, run);
-            _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
-            s->lookahead -= run;
-            s->strstart += run;
+        if (s->match_length >= MIN_MATCH) {
+            check_match(s, s->strstart, s->strstart - 1, s->match_length);
+
+            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
+
+            s->lookahead -= s->match_length;
+            s->strstart += s->match_length;
+            s->match_length = 0;
         } else {
             /* No match, output a literal byte */
             Tracevv((stderr,"%c", s->window[s->strstart]));
@@ -1730,7 +1917,51 @@ local block_state deflate_rle(s, flush)
         }
         if (bflush) FLUSH_BLOCK(s, 0);
     }
-    FLUSH_BLOCK(s, flush == Z_FINISH);
-    return flush == Z_FINISH ? finish_done : block_done;
+    s->insert = 0;
+    if (flush == Z_FINISH) {
+        FLUSH_BLOCK(s, 1);
+        return finish_done;
+    }
+    if (s->last_lit)
+        FLUSH_BLOCK(s, 0);
+    return block_done;
+}
+
+/* ===========================================================================
+ * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
+ */
+local block_state deflate_huff(s, flush)
+    deflate_state *s;
+    int flush;
+{
+    int bflush;             /* set if current block must be flushed */
+
+    for (;;) {
+        /* Make sure that we have a literal to write. */
+        if (s->lookahead == 0) {
+            fill_window(s);
+            if (s->lookahead == 0) {
+                if (flush == Z_NO_FLUSH)
+                    return need_more;
+                break;      /* flush the current block */
+            }
+        }
+
+        /* Output a literal byte */
+        s->match_length = 0;
+        Tracevv((stderr,"%c", s->window[s->strstart]));
+        _tr_tally_lit (s, s->window[s->strstart], bflush);
+        s->lookahead--;
+        s->strstart++;
+        if (bflush) FLUSH_BLOCK(s, 0);
+    }
+    s->insert = 0;
+    if (flush == Z_FINISH) {
+        FLUSH_BLOCK(s, 1);
+        return finish_done;
+    }
+    if (s->last_lit)
+        FLUSH_BLOCK(s, 0);
+    return block_done;
 }
-#endif
index 05a5ab3a2c1251aa0b90aee68dfbd3ddc5d17838..ce0299edd19168b97e38667479bd1b5e769a63d0 100644 (file)
@@ -1,5 +1,5 @@
 /* deflate.h -- internal compression state
- * Copyright (C) 1995-2004 Jean-loup Gailly
+ * Copyright (C) 1995-2012 Jean-loup Gailly
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -48,6 +48,9 @@
 #define MAX_BITS 15
 /* All codes must not exceed MAX_BITS bits */
 
+#define Buf_size 16
+/* size of bit buffer in bi_buf */
+
 #define INIT_STATE    42
 #define EXTRA_STATE   69
 #define NAME_STATE    73
@@ -101,7 +104,7 @@ typedef struct internal_state {
     int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */
     gz_headerp  gzhead;  /* gzip header information to write */
     uInt   gzindex;      /* where in extra, name, or comment */
-    Byte  method;        /* STORED (for zip only) or DEFLATED */
+    Byte  method;        /* can only be DEFLATED */
     int   last_flush;    /* value of flush param for previous deflate call */
 
                 /* used by deflate.c: */
@@ -188,7 +191,7 @@ typedef struct internal_state {
     int nice_match; /* Stop searching when current match exceeds this */
 
                 /* used by trees.c: */
-    /* Didn't use ct_data typedef below to supress compiler warning */
+    /* Didn't use ct_data typedef below to suppress compiler warning */
     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
@@ -244,7 +247,7 @@ typedef struct internal_state {
     ulg opt_len;        /* bit length of current block with optimal trees */
     ulg static_len;     /* bit length of current block with static trees */
     uInt matches;       /* number of string matches in current block */
-    int last_eob_len;   /* bit length of EOB code for last block */
+    uInt insert;        /* bytes at end of window left to insert */
 
 #ifdef DEBUG
     ulg compressed_len; /* total bit length of compressed file mod 2^32 */
@@ -260,6 +263,13 @@ typedef struct internal_state {
      * are always zero.
      */
 
+    ulg high_water;
+    /* High water mark offset in window for initialized bytes -- bytes above
+     * this are set to zero in order to avoid memory check warnings when
+     * longest match routines access bytes past the input.  This is then
+     * updated to the new high water mark.
+     */
+
 } FAR deflate_state;
 
 /* Output a byte on the stream.
@@ -278,14 +288,19 @@ typedef struct internal_state {
  * distances are limited to MAX_DIST instead of WSIZE.
  */
 
+#define WIN_INIT MAX_MATCH
+/* Number of bytes after end of data in window to initialize in order to avoid
+   memory checker errors from longest match routines */
+
         /* in trees.c */
-void _tr_init         OF((deflate_state *s));
-int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
-void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
-                          int eof));
-void _tr_align        OF((deflate_state *s));
-void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
-                          int eof));
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
+                        ulg stored_len, int last));
+void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
+                        ulg stored_len, int last));
 
 #define d_code(dist) \
    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
@@ -298,11 +313,11 @@ void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
 /* Inline versions of _tr_tally for speed: */
 
 #if defined(GEN_TREES_H) || !defined(STDC)
-  extern uch _length_code[];
-  extern uch _dist_code[];
+  extern uch ZLIB_INTERNAL _length_code[];
+  extern uch ZLIB_INTERNAL _dist_code[];
 #else
-  extern const uch _length_code[];
-  extern const uch _dist_code[];
+  extern const uch ZLIB_INTERNAL _length_code[];
+  extern const uch ZLIB_INTERNAL _dist_code[];
 #endif
 
 # define _tr_tally_lit(s, c, flush) \
diff --git a/zlib/gzclose.c b/zlib/gzclose.c
new file mode 100644 (file)
index 0000000..caeb99a
--- /dev/null
@@ -0,0 +1,25 @@
+/* gzclose.c -- zlib gzclose() function
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* gzclose() is in a separate file so that it is linked in only if it is used.
+   That way the other gzclose functions can be used instead to avoid linking in
+   unneeded compression or decompression routines. */
+int ZEXPORT gzclose(file)
+    gzFile file;
+{
+#ifndef NO_GZCOMPRESS
+    gz_statep state;
+
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
+#else
+    return gzclose_r(file);
+#endif
+}
diff --git a/zlib/gzguts.h b/zlib/gzguts.h
new file mode 100644 (file)
index 0000000..d87659d
--- /dev/null
@@ -0,0 +1,209 @@
+/* gzguts.h -- zlib internal header definitions for gz* operations
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#ifdef _LARGEFILE64_SOURCE
+#  ifndef _LARGEFILE_SOURCE
+#    define _LARGEFILE_SOURCE 1
+#  endif
+#  ifdef _FILE_OFFSET_BITS
+#    undef _FILE_OFFSET_BITS
+#  endif
+#endif
+
+#ifdef HAVE_HIDDEN
+#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+#  define ZLIB_INTERNAL
+#endif
+
+#include <stdio.h>
+#include "zlib.h"
+#ifdef STDC
+#  include <string.h>
+#  include <stdlib.h>
+#  include <limits.h>
+#endif
+#include <fcntl.h>
+
+#ifdef _WIN32
+#  include <stddef.h>
+#endif
+
+#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
+#  include <io.h>
+#endif
+
+#ifdef WINAPI_FAMILY
+#  define open _open
+#  define read _read
+#  define write _write
+#  define close _close
+#endif
+
+#ifdef NO_DEFLATE       /* for compatibility with old definition */
+#  define NO_GZCOMPRESS
+#endif
+
+#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
+#  ifndef HAVE_VSNPRINTF
+#    define HAVE_VSNPRINTF
+#  endif
+#endif
+
+#if defined(__CYGWIN__)
+#  ifndef HAVE_VSNPRINTF
+#    define HAVE_VSNPRINTF
+#  endif
+#endif
+
+#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
+#  ifndef HAVE_VSNPRINTF
+#    define HAVE_VSNPRINTF
+#  endif
+#endif
+
+#ifndef HAVE_VSNPRINTF
+#  ifdef MSDOS
+/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
+   but for now we just assume it doesn't. */
+#    define NO_vsnprintf
+#  endif
+#  ifdef __TURBOC__
+#    define NO_vsnprintf
+#  endif
+#  ifdef WIN32
+/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
+#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
+#      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
+#         define vsnprintf _vsnprintf
+#      endif
+#    endif
+#  endif
+#  ifdef __SASC
+#    define NO_vsnprintf
+#  endif
+#  ifdef VMS
+#    define NO_vsnprintf
+#  endif
+#  ifdef __OS400__
+#    define NO_vsnprintf
+#  endif
+#  ifdef __MVS__
+#    define NO_vsnprintf
+#  endif
+#endif
+
+/* unlike snprintf (which is required in C99, yet still not supported by
+   Microsoft more than a decade later!), _snprintf does not guarantee null
+   termination of the result -- however this is only used in gzlib.c where
+   the result is assured to fit in the space provided */
+#ifdef _MSC_VER
+#  define snprintf _snprintf
+#endif
+
+#ifndef local
+#  define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+/* gz* functions always use library allocation functions */
+#ifndef STDC
+  extern voidp  malloc OF((uInt size));
+  extern void   free   OF((voidpf ptr));
+#endif
+
+/* get errno and strerror definition */
+#if defined UNDER_CE
+#  include <windows.h>
+#  define zstrerror() gz_strwinerror((DWORD)GetLastError())
+#else
+#  ifndef NO_STRERROR
+#    include <errno.h>
+#    define zstrerror() strerror(errno)
+#  else
+#    define zstrerror() "stdio error (consult errno)"
+#  endif
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
+    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+#endif
+
+/* default memLevel */
+#if MAX_MEM_LEVEL >= 8
+#  define DEF_MEM_LEVEL 8
+#else
+#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
+#endif
+
+/* default i/o buffer size -- double this for output when reading (this and
+   twice this must be able to fit in an unsigned type) */
+#define GZBUFSIZE 8192
+
+/* gzip modes, also provide a little integrity check on the passed structure */
+#define GZ_NONE 0
+#define GZ_READ 7247
+#define GZ_WRITE 31153
+#define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
+
+/* values for gz_state how */
+#define LOOK 0      /* look for a gzip header */
+#define COPY 1      /* copy input directly */
+#define GZIP 2      /* decompress a gzip stream */
+
+/* internal gzip file state data structure */
+typedef struct {
+        /* exposed contents for gzgetc() macro */
+    struct gzFile_s x;      /* "x" for exposed */
+                            /* x.have: number of bytes available at x.next */
+                            /* x.next: next output data to deliver or write */
+                            /* x.pos: current position in uncompressed data */
+        /* used for both reading and writing */
+    int mode;               /* see gzip modes above */
+    int fd;                 /* file descriptor */
+    char *path;             /* path or fd for error messages */
+    unsigned size;          /* buffer size, zero if not allocated yet */
+    unsigned want;          /* requested buffer size, default is GZBUFSIZE */
+    unsigned char *in;      /* input buffer */
+    unsigned char *out;     /* output buffer (double-sized when reading) */
+    int direct;             /* 0 if processing gzip, 1 if transparent */
+        /* just for reading */
+    int how;                /* 0: get header, 1: copy, 2: decompress */
+    z_off64_t start;        /* where the gzip data started, for rewinding */
+    int eof;                /* true if end of input file reached */
+    int past;               /* true if read requested past end */
+        /* just for writing */
+    int level;              /* compression level */
+    int strategy;           /* compression strategy */
+        /* seek request */
+    z_off64_t skip;         /* amount to skip (already rewound if backwards) */
+    int seek;               /* true if seek request pending */
+        /* error information */
+    int err;                /* error code */
+    char *msg;              /* error message */
+        /* zlib inflate or deflate stream */
+    z_stream strm;          /* stream structure in-place (not a pointer) */
+} gz_state;
+typedef gz_state FAR *gz_statep;
+
+/* shared functions */
+void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
+#if defined UNDER_CE
+char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
+#endif
+
+/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
+   value -- needed when comparing unsigned to z_off64_t, which is signed
+   (possible z_off64_t types off_t, off64_t, and long are all signed) */
+#ifdef INT_MAX
+#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
+#else
+unsigned ZLIB_INTERNAL gz_intmax OF((void));
+#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
+#endif
diff --git a/zlib/gzio.c b/zlib/gzio.c
deleted file mode 100644 (file)
index 7e90f49..0000000
+++ /dev/null
@@ -1,1026 +0,0 @@
-/* gzio.c -- IO on .gz files
- * Copyright (C) 1995-2005 Jean-loup Gailly.
- * For conditions of distribution and use, see copyright notice in zlib.h
- *
- * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
- */
-
-/* @(#) $Id$ */
-
-#include <stdio.h>
-
-#include "zutil.h"
-
-#ifdef NO_DEFLATE       /* for compatibility with old definition */
-#  define NO_GZCOMPRESS
-#endif
-
-#ifndef NO_DUMMY_DECL
-struct internal_state {int dummy;}; /* for buggy compilers */
-#endif
-
-#ifndef Z_BUFSIZE
-#  ifdef MAXSEG_64K
-#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
-#  else
-#    define Z_BUFSIZE 16384
-#  endif
-#endif
-#ifndef Z_PRINTF_BUFSIZE
-#  define Z_PRINTF_BUFSIZE 4096
-#endif
-
-#ifdef __MVS__
-#  pragma map (fdopen , "\174\174FDOPEN")
-   FILE *fdopen(int, const char *);
-#endif
-
-#ifndef STDC
-extern voidp  malloc OF((uInt size));
-extern void   free   OF((voidpf ptr));
-#endif
-
-#define ALLOC(size) malloc(size)
-#define TRYFREE(p) {if (p) free(p);}
-
-static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
-
-/* gzip flag byte */
-#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
-#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
-#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
-#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
-#define COMMENT      0x10 /* bit 4 set: file comment present */
-#define RESERVED     0xE0 /* bits 5..7: reserved */
-
-typedef struct gz_stream {
-    z_stream stream;
-    int      z_err;   /* error code for last stream operation */
-    int      z_eof;   /* set if end of input file */
-    FILE     *file;   /* .gz file */
-    Byte     *inbuf;  /* input buffer */
-    Byte     *outbuf; /* output buffer */
-    uLong    crc;     /* crc32 of uncompressed data */
-    char     *msg;    /* error message */
-    char     *path;   /* path name for debugging only */
-    int      transparent; /* 1 if input file is not a .gz file */
-    char     mode;    /* 'w' or 'r' */
-    z_off_t  start;   /* start of compressed data in file (header skipped) */
-    z_off_t  in;      /* bytes into deflate or inflate */
-    z_off_t  out;     /* bytes out of deflate or inflate */
-    int      back;    /* one character push-back */
-    int      last;    /* true if push-back is last character */
-} gz_stream;
-
-
-local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
-local int do_flush        OF((gzFile file, int flush));
-local int    get_byte     OF((gz_stream *s));
-local void   check_header OF((gz_stream *s));
-local int    destroy      OF((gz_stream *s));
-local void   putLong      OF((FILE *file, uLong x));
-local uLong  getLong      OF((gz_stream *s));
-
-/* ===========================================================================
-     Opens a gzip (.gz) file for reading or writing. The mode parameter
-   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
-   or path name (if fd == -1).
-     gz_open returns NULL if the file could not be opened or if there was
-   insufficient memory to allocate the (de)compression state; errno
-   can be checked to distinguish the two cases (if errno is zero, the
-   zlib error is Z_MEM_ERROR).
-*/
-local gzFile gz_open (path, mode, fd)
-    const char *path;
-    const char *mode;
-    int  fd;
-{
-    int err;
-    int level = Z_DEFAULT_COMPRESSION; /* compression level */
-    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
-    char *p = (char*)mode;
-    gz_stream *s;
-    char fmode[80]; /* copy of mode, without the compression level */
-    char *m = fmode;
-
-    if (!path || !mode) return Z_NULL;
-
-    s = (gz_stream *)ALLOC(sizeof(gz_stream));
-    if (!s) return Z_NULL;
-
-    s->stream.zalloc = (alloc_func)0;
-    s->stream.zfree = (free_func)0;
-    s->stream.opaque = (voidpf)0;
-    s->stream.next_in = s->inbuf = Z_NULL;
-    s->stream.next_out = s->outbuf = Z_NULL;
-    s->stream.avail_in = s->stream.avail_out = 0;
-    s->file = NULL;
-    s->z_err = Z_OK;
-    s->z_eof = 0;
-    s->in = 0;
-    s->out = 0;
-    s->back = EOF;
-    s->crc = crc32(0L, Z_NULL, 0);
-    s->msg = NULL;
-    s->transparent = 0;
-
-    s->path = (char*)ALLOC(strlen(path)+1);
-    if (s->path == NULL) {
-        return destroy(s), (gzFile)Z_NULL;
-    }
-    strcpy(s->path, path); /* do this early for debugging */
-
-    s->mode = '\0';
-    do {
-        if (*p == 'r') s->mode = 'r';
-        if (*p == 'w' || *p == 'a') s->mode = 'w';
-        if (*p >= '0' && *p <= '9') {
-            level = *p - '0';
-        } else if (*p == 'f') {
-          strategy = Z_FILTERED;
-        } else if (*p == 'h') {
-          strategy = Z_HUFFMAN_ONLY;
-        } else if (*p == 'R') {
-          strategy = Z_RLE;
-        } else {
-            *m++ = *p; /* copy the mode */
-        }
-    } while (*p++ && m != fmode + sizeof(fmode));
-    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        err = Z_STREAM_ERROR;
-#else
-        err = deflateInit2(&(s->stream), level,
-                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
-        /* windowBits is passed < 0 to suppress zlib header */
-
-        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
-#endif
-        if (err != Z_OK || s->outbuf == Z_NULL) {
-            return destroy(s), (gzFile)Z_NULL;
-        }
-    } else {
-        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
-
-        err = inflateInit2(&(s->stream), -MAX_WBITS);
-        /* windowBits is passed < 0 to tell that there is no zlib header.
-         * Note that in this case inflate *requires* an extra "dummy" byte
-         * after the compressed stream in order to complete decompression and
-         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
-         * present after the compressed stream.
-         */
-        if (err != Z_OK || s->inbuf == Z_NULL) {
-            return destroy(s), (gzFile)Z_NULL;
-        }
-    }
-    s->stream.avail_out = Z_BUFSIZE;
-
-    errno = 0;
-    s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
-
-    if (s->file == NULL) {
-        return destroy(s), (gzFile)Z_NULL;
-    }
-    if (s->mode == 'w') {
-        /* Write a very simple .gz header:
-         */
-        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
-             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
-        s->start = 10L;
-        /* We use 10L instead of ftell(s->file) to because ftell causes an
-         * fflush on some systems. This version of the library doesn't use
-         * start anyway in write mode, so this initialization is not
-         * necessary.
-         */
-    } else {
-        check_header(s); /* skip the .gz header */
-        s->start = ftell(s->file) - s->stream.avail_in;
-    }
-
-    return (gzFile)s;
-}
-
-/* ===========================================================================
-     Opens a gzip (.gz) file for reading or writing.
-*/
-gzFile ZEXPORT gzopen (path, mode)
-    const char *path;
-    const char *mode;
-{
-    return gz_open (path, mode, -1);
-}
-
-/* ===========================================================================
-     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
-   to mimic the behavio(u)r of fdopen.
-*/
-gzFile ZEXPORT gzdopen (fd, mode)
-    int fd;
-    const char *mode;
-{
-    char name[46];      /* allow for up to 128-bit integers */
-
-    if (fd < 0) return (gzFile)Z_NULL;
-    sprintf(name, "<fd:%d>", fd); /* for debugging */
-
-    return gz_open (name, mode, fd);
-}
-
-/* ===========================================================================
- * Update the compression level and strategy
- */
-int ZEXPORT gzsetparams (file, level, strategy)
-    gzFile file;
-    int level;
-    int strategy;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    /* Make room to allow flushing */
-    if (s->stream.avail_out == 0) {
-
-        s->stream.next_out = s->outbuf;
-        if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
-            s->z_err = Z_ERRNO;
-        }
-        s->stream.avail_out = Z_BUFSIZE;
-    }
-
-    return deflateParams (&(s->stream), level, strategy);
-}
-
-/* ===========================================================================
-     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
-   for end of file.
-   IN assertion: the stream s has been sucessfully opened for reading.
-*/
-local int get_byte(s)
-    gz_stream *s;
-{
-    if (s->z_eof) return EOF;
-    if (s->stream.avail_in == 0) {
-        errno = 0;
-        s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
-        if (s->stream.avail_in == 0) {
-            s->z_eof = 1;
-            if (ferror(s->file)) s->z_err = Z_ERRNO;
-            return EOF;
-        }
-        s->stream.next_in = s->inbuf;
-    }
-    s->stream.avail_in--;
-    return *(s->stream.next_in)++;
-}
-
-/* ===========================================================================
-      Check the gzip header of a gz_stream opened for reading. Set the stream
-    mode to transparent if the gzip magic header is not present; set s->err
-    to Z_DATA_ERROR if the magic header is present but the rest of the header
-    is incorrect.
-    IN assertion: the stream s has already been created sucessfully;
-       s->stream.avail_in is zero for the first time, but may be non-zero
-       for concatenated .gz files.
-*/
-local void check_header(s)
-    gz_stream *s;
-{
-    int method; /* method byte */
-    int flags;  /* flags byte */
-    uInt len;
-    int c;
-
-    /* Assure two bytes in the buffer so we can peek ahead -- handle case
-       where first byte of header is at the end of the buffer after the last
-       gzip segment */
-    len = s->stream.avail_in;
-    if (len < 2) {
-        if (len) s->inbuf[0] = s->stream.next_in[0];
-        errno = 0;
-        len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
-        if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
-        s->stream.avail_in += len;
-        s->stream.next_in = s->inbuf;
-        if (s->stream.avail_in < 2) {
-            s->transparent = s->stream.avail_in;
-            return;
-        }
-    }
-
-    /* Peek ahead to check the gzip magic header */
-    if (s->stream.next_in[0] != gz_magic[0] ||
-        s->stream.next_in[1] != gz_magic[1]) {
-        s->transparent = 1;
-        return;
-    }
-    s->stream.avail_in -= 2;
-    s->stream.next_in += 2;
-
-    /* Check the rest of the gzip header */
-    method = get_byte(s);
-    flags = get_byte(s);
-    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
-        s->z_err = Z_DATA_ERROR;
-        return;
-    }
-
-    /* Discard time, xflags and OS code: */
-    for (len = 0; len < 6; len++) (void)get_byte(s);
-
-    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
-        len  =  (uInt)get_byte(s);
-        len += ((uInt)get_byte(s))<<8;
-        /* len is garbage if EOF but the loop below will quit anyway */
-        while (len-- != 0 && get_byte(s) != EOF) ;
-    }
-    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
-    }
-    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
-    }
-    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
-        for (len = 0; len < 2; len++) (void)get_byte(s);
-    }
-    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
-}
-
- /* ===========================================================================
- * Cleanup then free the given gz_stream. Return a zlib error code.
-   Try freeing in the reverse order of allocations.
- */
-local int destroy (s)
-    gz_stream *s;
-{
-    int err = Z_OK;
-
-    if (!s) return Z_STREAM_ERROR;
-
-    TRYFREE(s->msg);
-
-    if (s->stream.state != NULL) {
-        if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-            err = Z_STREAM_ERROR;
-#else
-            err = deflateEnd(&(s->stream));
-#endif
-        } else if (s->mode == 'r') {
-            err = inflateEnd(&(s->stream));
-        }
-    }
-    if (s->file != NULL && fclose(s->file)) {
-#ifdef ESPIPE
-        if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
-#endif
-            err = Z_ERRNO;
-    }
-    if (s->z_err < 0) err = s->z_err;
-
-    TRYFREE(s->inbuf);
-    TRYFREE(s->outbuf);
-    TRYFREE(s->path);
-    TRYFREE(s);
-    return err;
-}
-
-/* ===========================================================================
-     Reads the given number of uncompressed bytes from the compressed file.
-   gzread returns the number of bytes actually read (0 for end of file).
-*/
-int ZEXPORT gzread (file, buf, len)
-    gzFile file;
-    voidp buf;
-    unsigned len;
-{
-    gz_stream *s = (gz_stream*)file;
-    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
-    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
-
-    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
-
-    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
-    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
-
-    next_out = (Byte*)buf;
-    s->stream.next_out = (Bytef*)buf;
-    s->stream.avail_out = len;
-
-    if (s->stream.avail_out && s->back != EOF) {
-        *next_out++ = s->back;
-        s->stream.next_out++;
-        s->stream.avail_out--;
-        s->back = EOF;
-        s->out++;
-        start++;
-        if (s->last) {
-            s->z_err = Z_STREAM_END;
-            return 1;
-        }
-    }
-
-    while (s->stream.avail_out != 0) {
-
-        if (s->transparent) {
-            /* Copy first the lookahead bytes: */
-            uInt n = s->stream.avail_in;
-            if (n > s->stream.avail_out) n = s->stream.avail_out;
-            if (n > 0) {
-                zmemcpy(s->stream.next_out, s->stream.next_in, n);
-                next_out += n;
-                s->stream.next_out = next_out;
-                s->stream.next_in   += n;
-                s->stream.avail_out -= n;
-                s->stream.avail_in  -= n;
-            }
-            if (s->stream.avail_out > 0) {
-                s->stream.avail_out -=
-                    (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
-            }
-            len -= s->stream.avail_out;
-            s->in  += len;
-            s->out += len;
-            if (len == 0) s->z_eof = 1;
-            return (int)len;
-        }
-        if (s->stream.avail_in == 0 && !s->z_eof) {
-
-            errno = 0;
-            s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
-            if (s->stream.avail_in == 0) {
-                s->z_eof = 1;
-                if (ferror(s->file)) {
-                    s->z_err = Z_ERRNO;
-                    break;
-                }
-            }
-            s->stream.next_in = s->inbuf;
-        }
-        s->in += s->stream.avail_in;
-        s->out += s->stream.avail_out;
-        s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
-        s->in -= s->stream.avail_in;
-        s->out -= s->stream.avail_out;
-
-        if (s->z_err == Z_STREAM_END) {
-            /* Check CRC and original size */
-            s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
-            start = s->stream.next_out;
-
-            if (getLong(s) != s->crc) {
-                s->z_err = Z_DATA_ERROR;
-            } else {
-                (void)getLong(s);
-                /* The uncompressed length returned by above getlong() may be
-                 * different from s->out in case of concatenated .gz files.
-                 * Check for such files:
-                 */
-                check_header(s);
-                if (s->z_err == Z_OK) {
-                    inflateReset(&(s->stream));
-                    s->crc = crc32(0L, Z_NULL, 0);
-                }
-            }
-        }
-        if (s->z_err != Z_OK || s->z_eof) break;
-    }
-    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
-
-    if (len == s->stream.avail_out &&
-        (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
-        return -1;
-    return (int)(len - s->stream.avail_out);
-}
-
-
-/* ===========================================================================
-      Reads one byte from the compressed file. gzgetc returns this byte
-   or -1 in case of end of file or error.
-*/
-int ZEXPORT gzgetc(file)
-    gzFile file;
-{
-    unsigned char c;
-
-    return gzread(file, &c, 1) == 1 ? c : -1;
-}
-
-
-/* ===========================================================================
-      Push one byte back onto the stream.
-*/
-int ZEXPORT gzungetc(c, file)
-    int c;
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
-    s->back = c;
-    s->out--;
-    s->last = (s->z_err == Z_STREAM_END);
-    if (s->last) s->z_err = Z_OK;
-    s->z_eof = 0;
-    return c;
-}
-
-
-/* ===========================================================================
-      Reads bytes from the compressed file until len-1 characters are
-   read, or a newline character is read and transferred to buf, or an
-   end-of-file condition is encountered.  The string is then terminated
-   with a null character.
-      gzgets returns buf, or Z_NULL in case of error.
-
-      The current implementation is not optimized at all.
-*/
-char * ZEXPORT gzgets(file, buf, len)
-    gzFile file;
-    char *buf;
-    int len;
-{
-    char *b = buf;
-    if (buf == Z_NULL || len <= 0) return Z_NULL;
-
-    while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
-    *buf = '\0';
-    return b == buf && len > 0 ? Z_NULL : b;
-}
-
-
-#ifndef NO_GZCOMPRESS
-/* ===========================================================================
-     Writes the given number of uncompressed bytes into the compressed file.
-   gzwrite returns the number of bytes actually written (0 in case of error).
-*/
-int ZEXPORT gzwrite (file, buf, len)
-    gzFile file;
-    voidpc buf;
-    unsigned len;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    s->stream.next_in = (Bytef*)buf;
-    s->stream.avail_in = len;
-
-    while (s->stream.avail_in != 0) {
-
-        if (s->stream.avail_out == 0) {
-
-            s->stream.next_out = s->outbuf;
-            if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
-                s->z_err = Z_ERRNO;
-                break;
-            }
-            s->stream.avail_out = Z_BUFSIZE;
-        }
-        s->in += s->stream.avail_in;
-        s->out += s->stream.avail_out;
-        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
-        s->in -= s->stream.avail_in;
-        s->out -= s->stream.avail_out;
-        if (s->z_err != Z_OK) break;
-    }
-    s->crc = crc32(s->crc, (const Bytef *)buf, len);
-
-    return (int)(len - s->stream.avail_in);
-}
-
-
-/* ===========================================================================
-     Converts, formats, and writes the args to the compressed file under
-   control of the format string, as in fprintf. gzprintf returns the number of
-   uncompressed bytes actually written (0 in case of error).
-*/
-#ifdef STDC
-#include <stdarg.h>
-
-int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
-{
-    char buf[Z_PRINTF_BUFSIZE];
-    va_list va;
-    int len;
-
-    buf[sizeof(buf) - 1] = 0;
-    va_start(va, format);
-#ifdef NO_vsnprintf
-#  ifdef HAS_vsprintf_void
-    (void)vsprintf(buf, format, va);
-    va_end(va);
-    for (len = 0; len < sizeof(buf); len++)
-        if (buf[len] == 0) break;
-#  else
-    len = vsprintf(buf, format, va);
-    va_end(va);
-#  endif
-#else
-#  ifdef HAS_vsnprintf_void
-    (void)vsnprintf(buf, sizeof(buf), format, va);
-    va_end(va);
-    len = strlen(buf);
-#  else
-    len = vsnprintf(buf, sizeof(buf), format, va);
-    va_end(va);
-#  endif
-#endif
-    if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
-        return 0;
-    return gzwrite(file, buf, (unsigned)len);
-}
-#else /* not ANSI C */
-
-int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
-                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
-    gzFile file;
-    const char *format;
-    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
-        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
-{
-    char buf[Z_PRINTF_BUFSIZE];
-    int len;
-
-    buf[sizeof(buf) - 1] = 0;
-#ifdef NO_snprintf
-#  ifdef HAS_sprintf_void
-    sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
-            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-    for (len = 0; len < sizeof(buf); len++)
-        if (buf[len] == 0) break;
-#  else
-    len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
-                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-#  endif
-#else
-#  ifdef HAS_snprintf_void
-    snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
-             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-    len = strlen(buf);
-#  else
-    len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
-                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-#  endif
-#endif
-    if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
-        return 0;
-    return gzwrite(file, buf, len);
-}
-#endif
-
-/* ===========================================================================
-      Writes c, converted to an unsigned char, into the compressed file.
-   gzputc returns the value that was written, or -1 in case of error.
-*/
-int ZEXPORT gzputc(file, c)
-    gzFile file;
-    int c;
-{
-    unsigned char cc = (unsigned char) c; /* required for big endian systems */
-
-    return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
-}
-
-
-/* ===========================================================================
-      Writes the given null-terminated string to the compressed file, excluding
-   the terminating null character.
-      gzputs returns the number of characters written, or -1 in case of error.
-*/
-int ZEXPORT gzputs(file, s)
-    gzFile file;
-    const char *s;
-{
-    return gzwrite(file, (char*)s, (unsigned)strlen(s));
-}
-
-
-/* ===========================================================================
-     Flushes all pending output into the compressed file. The parameter
-   flush is as in the deflate() function.
-*/
-local int do_flush (file, flush)
-    gzFile file;
-    int flush;
-{
-    uInt len;
-    int done = 0;
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    s->stream.avail_in = 0; /* should be zero already anyway */
-
-    for (;;) {
-        len = Z_BUFSIZE - s->stream.avail_out;
-
-        if (len != 0) {
-            if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
-                s->z_err = Z_ERRNO;
-                return Z_ERRNO;
-            }
-            s->stream.next_out = s->outbuf;
-            s->stream.avail_out = Z_BUFSIZE;
-        }
-        if (done) break;
-        s->out += s->stream.avail_out;
-        s->z_err = deflate(&(s->stream), flush);
-        s->out -= s->stream.avail_out;
-
-        /* Ignore the second of two consecutive flushes: */
-        if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
-
-        /* deflate has finished flushing only when it hasn't used up
-         * all the available space in the output buffer:
-         */
-        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
-
-        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
-    }
-    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
-}
-
-int ZEXPORT gzflush (file, flush)
-     gzFile file;
-     int flush;
-{
-    gz_stream *s = (gz_stream*)file;
-    int err = do_flush (file, flush);
-
-    if (err) return err;
-    fflush(s->file);
-    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
-}
-#endif /* NO_GZCOMPRESS */
-
-/* ===========================================================================
-      Sets the starting position for the next gzread or gzwrite on the given
-   compressed file. The offset represents a number of bytes in the
-      gzseek returns the resulting offset location as measured in bytes from
-   the beginning of the uncompressed stream, or -1 in case of error.
-      SEEK_END is not implemented, returns error.
-      In this version of the library, gzseek can be extremely slow.
-*/
-z_off_t ZEXPORT gzseek (file, offset, whence)
-    gzFile file;
-    z_off_t offset;
-    int whence;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || whence == SEEK_END ||
-        s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
-        return -1L;
-    }
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        return -1L;
-#else
-        if (whence == SEEK_SET) {
-            offset -= s->in;
-        }
-        if (offset < 0) return -1L;
-
-        /* At this point, offset is the number of zero bytes to write. */
-        if (s->inbuf == Z_NULL) {
-            s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
-            if (s->inbuf == Z_NULL) return -1L;
-            zmemzero(s->inbuf, Z_BUFSIZE);
-        }
-        while (offset > 0)  {
-            uInt size = Z_BUFSIZE;
-            if (offset < Z_BUFSIZE) size = (uInt)offset;
-
-            size = gzwrite(file, s->inbuf, size);
-            if (size == 0) return -1L;
-
-            offset -= size;
-        }
-        return s->in;
-#endif
-    }
-    /* Rest of function is for reading only */
-
-    /* compute absolute position */
-    if (whence == SEEK_CUR) {
-        offset += s->out;
-    }
-    if (offset < 0) return -1L;
-
-    if (s->transparent) {
-        /* map to fseek */
-        s->back = EOF;
-        s->stream.avail_in = 0;
-        s->stream.next_in = s->inbuf;
-        if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
-
-        s->in = s->out = offset;
-        return offset;
-    }
-
-    /* For a negative seek, rewind and use positive seek */
-    if (offset >= s->out) {
-        offset -= s->out;
-    } else if (gzrewind(file) < 0) {
-        return -1L;
-    }
-    /* offset is now the number of bytes to skip. */
-
-    if (offset != 0 && s->outbuf == Z_NULL) {
-        s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
-        if (s->outbuf == Z_NULL) return -1L;
-    }
-    if (offset && s->back != EOF) {
-        s->back = EOF;
-        s->out++;
-        offset--;
-        if (s->last) s->z_err = Z_STREAM_END;
-    }
-    while (offset > 0)  {
-        int size = Z_BUFSIZE;
-        if (offset < Z_BUFSIZE) size = (int)offset;
-
-        size = gzread(file, s->outbuf, (uInt)size);
-        if (size <= 0) return -1L;
-        offset -= size;
-    }
-    return s->out;
-}
-
-/* ===========================================================================
-     Rewinds input file.
-*/
-int ZEXPORT gzrewind (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r') return -1;
-
-    s->z_err = Z_OK;
-    s->z_eof = 0;
-    s->back = EOF;
-    s->stream.avail_in = 0;
-    s->stream.next_in = s->inbuf;
-    s->crc = crc32(0L, Z_NULL, 0);
-    if (!s->transparent) (void)inflateReset(&s->stream);
-    s->in = 0;
-    s->out = 0;
-    return fseek(s->file, s->start, SEEK_SET);
-}
-
-/* ===========================================================================
-     Returns the starting position for the next gzread or gzwrite on the
-   given compressed file. This position represents a number of bytes in the
-   uncompressed data stream.
-*/
-z_off_t ZEXPORT gztell (file)
-    gzFile file;
-{
-    return gzseek(file, 0L, SEEK_CUR);
-}
-
-/* ===========================================================================
-     Returns 1 when EOF has previously been detected reading the given
-   input stream, otherwise zero.
-*/
-int ZEXPORT gzeof (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    /* With concatenated compressed files that can have embedded
-     * crc trailers, z_eof is no longer the only/best indicator of EOF
-     * on a gz_stream. Handle end-of-stream error explicitly here.
-     */
-    if (s == NULL || s->mode != 'r') return 0;
-    if (s->z_eof) return 1;
-    return s->z_err == Z_STREAM_END;
-}
-
-/* ===========================================================================
-     Returns 1 if reading and doing so transparently, otherwise zero.
-*/
-int ZEXPORT gzdirect (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r') return 0;
-    return s->transparent;
-}
-
-/* ===========================================================================
-   Outputs a long in LSB order to the given file
-*/
-local void putLong (file, x)
-    FILE *file;
-    uLong x;
-{
-    int n;
-    for (n = 0; n < 4; n++) {
-        fputc((int)(x & 0xff), file);
-        x >>= 8;
-    }
-}
-
-/* ===========================================================================
-   Reads a long in LSB order from the given gz_stream. Sets z_err in case
-   of error.
-*/
-local uLong getLong (s)
-    gz_stream *s;
-{
-    uLong x = (uLong)get_byte(s);
-    int c;
-
-    x += ((uLong)get_byte(s))<<8;
-    x += ((uLong)get_byte(s))<<16;
-    c = get_byte(s);
-    if (c == EOF) s->z_err = Z_DATA_ERROR;
-    x += ((uLong)c)<<24;
-    return x;
-}
-
-/* ===========================================================================
-     Flushes all pending output if necessary, closes the compressed file
-   and deallocates all the (de)compression state.
-*/
-int ZEXPORT gzclose (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) return Z_STREAM_ERROR;
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        return Z_STREAM_ERROR;
-#else
-        if (do_flush (file, Z_FINISH) != Z_OK)
-            return destroy((gz_stream*)file);
-
-        putLong (s->file, s->crc);
-        putLong (s->file, (uLong)(s->in & 0xffffffff));
-#endif
-    }
-    return destroy((gz_stream*)file);
-}
-
-#ifdef STDC
-#  define zstrerror(errnum) strerror(errnum)
-#else
-#  define zstrerror(errnum) ""
-#endif
-
-/* ===========================================================================
-     Returns the error message for the last error which occurred on the
-   given compressed file. errnum is set to zlib error number. If an
-   error occurred in the file system and not in the compression library,
-   errnum is set to Z_ERRNO and the application may consult errno
-   to get the exact error code.
-*/
-const char * ZEXPORT gzerror (file, errnum)
-    gzFile file;
-    int *errnum;
-{
-    char *m;
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) {
-        *errnum = Z_STREAM_ERROR;
-        return (const char*)ERR_MSG(Z_STREAM_ERROR);
-    }
-    *errnum = s->z_err;
-    if (*errnum == Z_OK) return (const char*)"";
-
-    m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
-
-    if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
-
-    TRYFREE(s->msg);
-    s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
-    if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
-    strcpy(s->msg, s->path);
-    strcat(s->msg, ": ");
-    strcat(s->msg, m);
-    return (const char*)s->msg;
-}
-
-/* ===========================================================================
-     Clear the error and end-of-file flags, and do the same for the real file.
-*/
-void ZEXPORT gzclearerr (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) return;
-    if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
-    s->z_eof = 0;
-    clearerr(s->file);
-}
diff --git a/zlib/gzlib.c b/zlib/gzlib.c
new file mode 100644 (file)
index 0000000..fae202e
--- /dev/null
@@ -0,0 +1,634 @@
+/* gzlib.c -- zlib functions common to reading and writing gzip files
+ * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+#if defined(_WIN32) && !defined(__BORLANDC__)
+#  define LSEEK _lseeki64
+#else
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+#  define LSEEK lseek64
+#else
+#  define LSEEK lseek
+#endif
+#endif
+
+/* Local functions */
+local void gz_reset OF((gz_statep));
+local gzFile gz_open OF((const void *, int, const char *));
+
+#if defined UNDER_CE
+
+/* Map the Windows error number in ERROR to a locale-dependent error message
+   string and return a pointer to it.  Typically, the values for ERROR come
+   from GetLastError.
+
+   The string pointed to shall not be modified by the application, but may be
+   overwritten by a subsequent call to gz_strwinerror
+
+   The gz_strwinerror function does not change the current setting of
+   GetLastError. */
+char ZLIB_INTERNAL *gz_strwinerror (error)
+     DWORD error;
+{
+    static char buf[1024];
+
+    wchar_t *msgbuf;
+    DWORD lasterr = GetLastError();
+    DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+        | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+        NULL,
+        error,
+        0, /* Default language */
+        (LPVOID)&msgbuf,
+        0,
+        NULL);
+    if (chars != 0) {
+        /* If there is an \r\n appended, zap it.  */
+        if (chars >= 2
+            && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+            chars -= 2;
+            msgbuf[chars] = 0;
+        }
+
+        if (chars > sizeof (buf) - 1) {
+            chars = sizeof (buf) - 1;
+            msgbuf[chars] = 0;
+        }
+
+        wcstombs(buf, msgbuf, chars + 1);
+        LocalFree(msgbuf);
+    }
+    else {
+        sprintf(buf, "unknown win32 error (%ld)", error);
+    }
+
+    SetLastError(lasterr);
+    return buf;
+}
+
+#endif /* UNDER_CE */
+
+/* Reset gzip file state */
+local void gz_reset(state)
+    gz_statep state;
+{
+    state->x.have = 0;              /* no output data available */
+    if (state->mode == GZ_READ) {   /* for reading ... */
+        state->eof = 0;             /* not at end of file */
+        state->past = 0;            /* have not read past end yet */
+        state->how = LOOK;          /* look for gzip header */
+    }
+    state->seek = 0;                /* no seek request pending */
+    gz_error(state, Z_OK, NULL);    /* clear error */
+    state->x.pos = 0;               /* no uncompressed data yet */
+    state->strm.avail_in = 0;       /* no input data yet */
+}
+
+/* Open a gzip file either by name or file descriptor. */
+local gzFile gz_open(path, fd, mode)
+    const void *path;
+    int fd;
+    const char *mode;
+{
+    gz_statep state;
+    size_t len;
+    int oflag;
+#ifdef O_CLOEXEC
+    int cloexec = 0;
+#endif
+#ifdef O_EXCL
+    int exclusive = 0;
+#endif
+
+    /* check input */
+    if (path == NULL)
+        return NULL;
+
+    /* allocate gzFile structure to return */
+    state = (gz_statep)malloc(sizeof(gz_state));
+    if (state == NULL)
+        return NULL;
+    state->size = 0;            /* no buffers allocated yet */
+    state->want = GZBUFSIZE;    /* requested buffer size */
+    state->msg = NULL;          /* no error message yet */
+
+    /* interpret mode */
+    state->mode = GZ_NONE;
+    state->level = Z_DEFAULT_COMPRESSION;
+    state->strategy = Z_DEFAULT_STRATEGY;
+    state->direct = 0;
+    while (*mode) {
+        if (*mode >= '0' && *mode <= '9')
+            state->level = *mode - '0';
+        else
+            switch (*mode) {
+            case 'r':
+                state->mode = GZ_READ;
+                break;
+#ifndef NO_GZCOMPRESS
+            case 'w':
+                state->mode = GZ_WRITE;
+                break;
+            case 'a':
+                state->mode = GZ_APPEND;
+                break;
+#endif
+            case '+':       /* can't read and write at the same time */
+                free(state);
+                return NULL;
+            case 'b':       /* ignore -- will request binary anyway */
+                break;
+#ifdef O_CLOEXEC
+            case 'e':
+                cloexec = 1;
+                break;
+#endif
+#ifdef O_EXCL
+            case 'x':
+                exclusive = 1;
+                break;
+#endif
+            case 'f':
+                state->strategy = Z_FILTERED;
+                break;
+            case 'h':
+                state->strategy = Z_HUFFMAN_ONLY;
+                break;
+            case 'R':
+                state->strategy = Z_RLE;
+                break;
+            case 'F':
+                state->strategy = Z_FIXED;
+                break;
+            case 'T':
+                state->direct = 1;
+                break;
+            default:        /* could consider as an error, but just ignore */
+                ;
+            }
+        mode++;
+    }
+
+    /* must provide an "r", "w", or "a" */
+    if (state->mode == GZ_NONE) {
+        free(state);
+        return NULL;
+    }
+
+    /* can't force transparent read */
+    if (state->mode == GZ_READ) {
+        if (state->direct) {
+            free(state);
+            return NULL;
+        }
+        state->direct = 1;      /* for empty file */
+    }
+
+    /* save the path name for error messages */
+#ifdef _WIN32
+    if (fd == -2) {
+        len = wcstombs(NULL, path, 0);
+        if (len == (size_t)-1)
+            len = 0;
+    }
+    else
+#endif
+        len = strlen((const char *)path);
+    state->path = (char *)malloc(len + 1);
+    if (state->path == NULL) {
+        free(state);
+        return NULL;
+    }
+#ifdef _WIN32
+    if (fd == -2)
+        if (len)
+            wcstombs(state->path, path, len + 1);
+        else
+            *(state->path) = 0;
+    else
+#endif
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+        snprintf(state->path, len + 1, "%s", (const char *)path);
+#else
+        strcpy(state->path, path);
+#endif
+
+    /* compute the flags for open() */
+    oflag =
+#ifdef O_LARGEFILE
+        O_LARGEFILE |
+#endif
+#ifdef O_BINARY
+        O_BINARY |
+#endif
+#ifdef O_CLOEXEC
+        (cloexec ? O_CLOEXEC : 0) |
+#endif
+        (state->mode == GZ_READ ?
+         O_RDONLY :
+         (O_WRONLY | O_CREAT |
+#ifdef O_EXCL
+          (exclusive ? O_EXCL : 0) |
+#endif
+          (state->mode == GZ_WRITE ?
+           O_TRUNC :
+           O_APPEND)));
+
+    /* open the file with the appropriate flags (or just use fd) */
+    state->fd = fd > -1 ? fd : (
+#ifdef _WIN32
+        fd == -2 ? _wopen(path, oflag, 0666) :
+#endif
+        open((const char *)path, oflag, 0666));
+    if (state->fd == -1) {
+        free(state->path);
+        free(state);
+        return NULL;
+    }
+    if (state->mode == GZ_APPEND)
+        state->mode = GZ_WRITE;         /* simplify later checks */
+
+    /* save the current position for rewinding (only if reading) */
+    if (state->mode == GZ_READ) {
+        state->start = LSEEK(state->fd, 0, SEEK_CUR);
+        if (state->start == -1) state->start = 0;
+    }
+
+    /* initialize stream */
+    gz_reset(state);
+
+    /* return stream */
+    return (gzFile)state;
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen(path, mode)
+    const char *path;
+    const char *mode;
+{
+    return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen64(path, mode)
+    const char *path;
+    const char *mode;
+{
+    return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzdopen(fd, mode)
+    int fd;
+    const char *mode;
+{
+    char *path;         /* identifier for error messages */
+    gzFile gz;
+
+    if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
+        return NULL;
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+    snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
+#else
+    sprintf(path, "<fd:%d>", fd);   /* for debugging */
+#endif
+    gz = gz_open(path, fd, mode);
+    free(path);
+    return gz;
+}
+
+/* -- see zlib.h -- */
+#ifdef _WIN32
+gzFile ZEXPORT gzopen_w(path, mode)
+    const wchar_t *path;
+    const char *mode;
+{
+    return gz_open(path, -2, mode);
+}
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzbuffer(file, size)
+    gzFile file;
+    unsigned size;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* make sure we haven't already allocated memory */
+    if (state->size != 0)
+        return -1;
+
+    /* check and set requested size */
+    if (size < 2)
+        size = 2;               /* need two bytes to check magic header */
+    state->want = size;
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzrewind(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ ||
+            (state->err != Z_OK && state->err != Z_BUF_ERROR))
+        return -1;
+
+    /* back up and start over */
+    if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
+        return -1;
+    gz_reset(state);
+    return 0;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzseek64(file, offset, whence)
+    gzFile file;
+    z_off64_t offset;
+    int whence;
+{
+    unsigned n;
+    z_off64_t ret;
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* check that there's no error */
+    if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+        return -1;
+
+    /* can only seek from start or relative to current position */
+    if (whence != SEEK_SET && whence != SEEK_CUR)
+        return -1;
+
+    /* normalize offset to a SEEK_CUR specification */
+    if (whence == SEEK_SET)
+        offset -= state->x.pos;
+    else if (state->seek)
+        offset += state->skip;
+    state->seek = 0;
+
+    /* if within raw area while reading, just go there */
+    if (state->mode == GZ_READ && state->how == COPY &&
+            state->x.pos + offset >= 0) {
+        ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
+        if (ret == -1)
+            return -1;
+        state->x.have = 0;
+        state->eof = 0;
+        state->past = 0;
+        state->seek = 0;
+        gz_error(state, Z_OK, NULL);
+        state->strm.avail_in = 0;
+        state->x.pos += offset;
+        return state->x.pos;
+    }
+
+    /* calculate skip amount, rewinding if needed for back seek when reading */
+    if (offset < 0) {
+        if (state->mode != GZ_READ)         /* writing -- can't go backwards */
+            return -1;
+        offset += state->x.pos;
+        if (offset < 0)                     /* before start of file! */
+            return -1;
+        if (gzrewind(file) == -1)           /* rewind, then skip to offset */
+            return -1;
+    }
+
+    /* if reading, skip what's in output buffer (one less gzgetc() check) */
+    if (state->mode == GZ_READ) {
+        n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
+            (unsigned)offset : state->x.have;
+        state->x.have -= n;
+        state->x.next += n;
+        state->x.pos += n;
+        offset -= n;
+    }
+
+    /* request skip (if not zero) */
+    if (offset) {
+        state->seek = 1;
+        state->skip = offset;
+    }
+    return state->x.pos + offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzseek(file, offset, whence)
+    gzFile file;
+    z_off_t offset;
+    int whence;
+{
+    z_off64_t ret;
+
+    ret = gzseek64(file, (z_off64_t)offset, whence);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gztell64(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* return position */
+    return state->x.pos + (state->seek ? state->skip : 0);
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gztell(file)
+    gzFile file;
+{
+    z_off64_t ret;
+
+    ret = gztell64(file);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzoffset64(file)
+    gzFile file;
+{
+    z_off64_t offset;
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* compute and return effective offset in file */
+    offset = LSEEK(state->fd, 0, SEEK_CUR);
+    if (offset == -1)
+        return -1;
+    if (state->mode == GZ_READ)             /* reading */
+        offset -= state->strm.avail_in;     /* don't count buffered input */
+    return offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzoffset(file)
+    gzFile file;
+{
+    z_off64_t ret;
+
+    ret = gzoffset64(file);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzeof(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return 0;
+
+    /* return end-of-file state */
+    return state->mode == GZ_READ ? state->past : 0;
+}
+
+/* -- see zlib.h -- */
+const char * ZEXPORT gzerror(file, errnum)
+    gzFile file;
+    int *errnum;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return NULL;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return NULL;
+
+    /* return error information */
+    if (errnum != NULL)
+        *errnum = state->err;
+    return state->err == Z_MEM_ERROR ? "out of memory" :
+                                       (state->msg == NULL ? "" : state->msg);
+}
+
+/* -- see zlib.h -- */
+void ZEXPORT gzclearerr(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return;
+
+    /* clear error and end-of-file */
+    if (state->mode == GZ_READ) {
+        state->eof = 0;
+        state->past = 0;
+    }
+    gz_error(state, Z_OK, NULL);
+}
+
+/* Create an error message in allocated memory and set state->err and
+   state->msg accordingly.  Free any previous error message already there.  Do
+   not try to free or allocate space if the error is Z_MEM_ERROR (out of
+   memory).  Simply save the error message as a static string.  If there is an
+   allocation failure constructing the error message, then convert the error to
+   out of memory. */
+void ZLIB_INTERNAL gz_error(state, err, msg)
+    gz_statep state;
+    int err;
+    const char *msg;
+{
+    /* free previously allocated message and clear */
+    if (state->msg != NULL) {
+        if (state->err != Z_MEM_ERROR)
+            free(state->msg);
+        state->msg = NULL;
+    }
+
+    /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
+    if (err != Z_OK && err != Z_BUF_ERROR)
+        state->x.have = 0;
+
+    /* set error code, and if no message, then done */
+    state->err = err;
+    if (msg == NULL)
+        return;
+
+    /* for an out of memory error, return literal string when requested */
+    if (err == Z_MEM_ERROR)
+        return;
+
+    /* construct error message with path */
+    if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
+            NULL) {
+        state->err = Z_MEM_ERROR;
+        return;
+    }
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+    snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
+             "%s%s%s", state->path, ": ", msg);
+#else
+    strcpy(state->msg, state->path);
+    strcat(state->msg, ": ");
+    strcat(state->msg, msg);
+#endif
+    return;
+}
+
+#ifndef INT_MAX
+/* portably return maximum value for an int (when limits.h presumed not
+   available) -- we need to do this to cover cases where 2's complement not
+   used, since C standard permits 1's complement and sign-bit representations,
+   otherwise we could just use ((unsigned)-1) >> 1 */
+unsigned ZLIB_INTERNAL gz_intmax()
+{
+    unsigned p, q;
+
+    p = 1;
+    do {
+        q = p;
+        p <<= 1;
+        p++;
+    } while (p > q);
+    return q >> 1;
+}
+#endif
diff --git a/zlib/gzread.c b/zlib/gzread.c
new file mode 100644 (file)
index 0000000..bf4538e
--- /dev/null
@@ -0,0 +1,594 @@
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
+local int gz_avail OF((gz_statep));
+local int gz_look OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_fetch OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+
+/* Use read() to load a buffer -- return -1 on error, otherwise 0.  Read from
+   state->fd, and update state->eof, state->err, and state->msg as appropriate.
+   This function needs to loop on read(), since read() is not guaranteed to
+   read the number of bytes requested, depending on the type of descriptor. */
+local int gz_load(state, buf, len, have)
+    gz_statep state;
+    unsigned char *buf;
+    unsigned len;
+    unsigned *have;
+{
+    int ret;
+
+    *have = 0;
+    do {
+        ret = read(state->fd, buf + *have, len - *have);
+        if (ret <= 0)
+            break;
+        *have += ret;
+    } while (*have < len);
+    if (ret < 0) {
+        gz_error(state, Z_ERRNO, zstrerror());
+        return -1;
+    }
+    if (ret == 0)
+        state->eof = 1;
+    return 0;
+}
+
+/* Load up input buffer and set eof flag if last data loaded -- return -1 on
+   error, 0 otherwise.  Note that the eof flag is set when the end of the input
+   file is reached, even though there may be unused data in the buffer.  Once
+   that data has been used, no more attempts will be made to read the file.
+   If strm->avail_in != 0, then the current data is moved to the beginning of
+   the input buffer, and then the remainder of the buffer is loaded with the
+   available data from the input file. */
+local int gz_avail(state)
+    gz_statep state;
+{
+    unsigned got;
+    z_streamp strm = &(state->strm);
+
+    if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+        return -1;
+    if (state->eof == 0) {
+        if (strm->avail_in) {       /* copy what's there to the start */
+            unsigned char *p = state->in;
+            unsigned const char *q = strm->next_in;
+            unsigned n = strm->avail_in;
+            do {
+                *p++ = *q++;
+            } while (--n);
+        }
+        if (gz_load(state, state->in + strm->avail_in,
+                    state->size - strm->avail_in, &got) == -1)
+            return -1;
+        strm->avail_in += got;
+        strm->next_in = state->in;
+    }
+    return 0;
+}
+
+/* Look for gzip header, set up for inflate or copy.  state->x.have must be 0.
+   If this is the first time in, allocate required memory.  state->how will be
+   left unchanged if there is no more input data available, will be set to COPY
+   if there is no gzip header and direct copying will be performed, or it will
+   be set to GZIP for decompression.  If direct copying, then leftover input
+   data from the input buffer will be copied to the output buffer.  In that
+   case, all further file reads will be directly to either the output buffer or
+   a user buffer.  If decompressing, the inflate state will be initialized.
+   gz_look() will return 0 on success or -1 on failure. */
+local int gz_look(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    /* allocate read buffers and inflate memory */
+    if (state->size == 0) {
+        /* allocate buffers */
+        state->in = (unsigned char *)malloc(state->want);
+        state->out = (unsigned char *)malloc(state->want << 1);
+        if (state->in == NULL || state->out == NULL) {
+            if (state->out != NULL)
+                free(state->out);
+            if (state->in != NULL)
+                free(state->in);
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+        state->size = state->want;
+
+        /* allocate inflate memory */
+        state->strm.zalloc = Z_NULL;
+        state->strm.zfree = Z_NULL;
+        state->strm.opaque = Z_NULL;
+        state->strm.avail_in = 0;
+        state->strm.next_in = Z_NULL;
+        if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) {    /* gunzip */
+            free(state->out);
+            free(state->in);
+            state->size = 0;
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+    }
+
+    /* get at least the magic bytes in the input buffer */
+    if (strm->avail_in < 2) {
+        if (gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in == 0)
+            return 0;
+    }
+
+    /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
+       a logical dilemma here when considering the case of a partially written
+       gzip file, to wit, if a single 31 byte is written, then we cannot tell
+       whether this is a single-byte file, or just a partially written gzip
+       file -- for here we assume that if a gzip file is being written, then
+       the header will be written in a single operation, so that reading a
+       single byte is sufficient indication that it is not a gzip file) */
+    if (strm->avail_in > 1 &&
+            strm->next_in[0] == 31 && strm->next_in[1] == 139) {
+        inflateReset(strm);
+        state->how = GZIP;
+        state->direct = 0;
+        return 0;
+    }
+
+    /* no gzip header -- if we were decoding gzip before, then this is trailing
+       garbage.  Ignore the trailing garbage and finish. */
+    if (state->direct == 0) {
+        strm->avail_in = 0;
+        state->eof = 1;
+        state->x.have = 0;
+        return 0;
+    }
+
+    /* doing raw i/o, copy any leftover input to output -- this assumes that
+       the output buffer is larger than the input buffer, which also assures
+       space for gzungetc() */
+    state->x.next = state->out;
+    if (strm->avail_in) {
+        memcpy(state->x.next, strm->next_in, strm->avail_in);
+        state->x.have = strm->avail_in;
+        strm->avail_in = 0;
+    }
+    state->how = COPY;
+    state->direct = 1;
+    return 0;
+}
+
+/* Decompress from input to the provided next_out and avail_out in the state.
+   On return, state->x.have and state->x.next point to the just decompressed
+   data.  If the gzip stream completes, state->how is reset to LOOK to look for
+   the next gzip stream or raw data, once state->x.have is depleted.  Returns 0
+   on success, -1 on failure. */
+local int gz_decomp(state)
+    gz_statep state;
+{
+    int ret = Z_OK;
+    unsigned had;
+    z_streamp strm = &(state->strm);
+
+    /* fill output buffer up to end of deflate stream */
+    had = strm->avail_out;
+    do {
+        /* get more input for inflate() */
+        if (strm->avail_in == 0 && gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in == 0) {
+            gz_error(state, Z_BUF_ERROR, "unexpected end of file");
+            break;
+        }
+
+        /* decompress and handle errors */
+        ret = inflate(strm, Z_NO_FLUSH);
+        if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
+            gz_error(state, Z_STREAM_ERROR,
+                     "internal error: inflate stream corrupt");
+            return -1;
+        }
+        if (ret == Z_MEM_ERROR) {
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+        if (ret == Z_DATA_ERROR) {              /* deflate stream invalid */
+            gz_error(state, Z_DATA_ERROR,
+                     strm->msg == NULL ? "compressed data error" : strm->msg);
+            return -1;
+        }
+    } while (strm->avail_out && ret != Z_STREAM_END);
+
+    /* update available output */
+    state->x.have = had - strm->avail_out;
+    state->x.next = strm->next_out - state->x.have;
+
+    /* if the gzip stream completed successfully, look for another */
+    if (ret == Z_STREAM_END)
+        state->how = LOOK;
+
+    /* good decompression */
+    return 0;
+}
+
+/* Fetch data and put it in the output buffer.  Assumes state->x.have is 0.
+   Data is either copied from the input file or decompressed from the input
+   file depending on state->how.  If state->how is LOOK, then a gzip header is
+   looked for to determine whether to copy or decompress.  Returns -1 on error,
+   otherwise 0.  gz_fetch() will leave state->how as COPY or GZIP unless the
+   end of the input file has been reached and all data has been processed.  */
+local int gz_fetch(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    do {
+        switch(state->how) {
+        case LOOK:      /* -> LOOK, COPY (only if never GZIP), or GZIP */
+            if (gz_look(state) == -1)
+                return -1;
+            if (state->how == LOOK)
+                return 0;
+            break;
+        case COPY:      /* -> COPY */
+            if (gz_load(state, state->out, state->size << 1, &(state->x.have))
+                    == -1)
+                return -1;
+            state->x.next = state->out;
+            return 0;
+        case GZIP:      /* -> GZIP or LOOK (if end of gzip stream) */
+            strm->avail_out = state->size << 1;
+            strm->next_out = state->out;
+            if (gz_decomp(state) == -1)
+                return -1;
+        }
+    } while (state->x.have == 0 && (!state->eof || strm->avail_in));
+    return 0;
+}
+
+/* Skip len uncompressed bytes of output.  Return -1 on error, 0 on success. */
+local int gz_skip(state, len)
+    gz_statep state;
+    z_off64_t len;
+{
+    unsigned n;
+
+    /* skip over len bytes or reach end-of-file, whichever comes first */
+    while (len)
+        /* skip over whatever is in output buffer */
+        if (state->x.have) {
+            n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
+                (unsigned)len : state->x.have;
+            state->x.have -= n;
+            state->x.next += n;
+            state->x.pos += n;
+            len -= n;
+        }
+
+        /* output buffer empty -- return if we're at the end of the input */
+        else if (state->eof && state->strm.avail_in == 0)
+            break;
+
+        /* need more data to skip -- load up output buffer */
+        else {
+            /* get more output, looking for header if required */
+            if (gz_fetch(state) == -1)
+                return -1;
+        }
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzread(file, buf, len)
+    gzFile file;
+    voidp buf;
+    unsigned len;
+{
+    unsigned got, n;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're reading and that there's no (serious) error */
+    if (state->mode != GZ_READ ||
+            (state->err != Z_OK && state->err != Z_BUF_ERROR))
+        return -1;
+
+    /* since an int is returned, make sure len fits in one, otherwise return
+       with an error (this avoids the flaw in the interface) */
+    if ((int)len < 0) {
+        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+        return -1;
+    }
+
+    /* if len is zero, avoid unnecessary operations */
+    if (len == 0)
+        return 0;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* get len bytes to buf, or less than len if at the end */
+    got = 0;
+    do {
+        /* first just try copying data from the output buffer */
+        if (state->x.have) {
+            n = state->x.have > len ? len : state->x.have;
+            memcpy(buf, state->x.next, n);
+            state->x.next += n;
+            state->x.have -= n;
+        }
+
+        /* output buffer empty -- return if we're at the end of the input */
+        else if (state->eof && strm->avail_in == 0) {
+            state->past = 1;        /* tried to read past end */
+            break;
+        }
+
+        /* need output data -- for small len or new stream load up our output
+           buffer */
+        else if (state->how == LOOK || len < (state->size << 1)) {
+            /* get more output, looking for header if required */
+            if (gz_fetch(state) == -1)
+                return -1;
+            continue;       /* no progress yet -- go back to copy above */
+            /* the copy above assures that we will leave with space in the
+               output buffer, allowing at least one gzungetc() to succeed */
+        }
+
+        /* large len -- read directly into user buffer */
+        else if (state->how == COPY) {      /* read directly */
+            if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
+                return -1;
+        }
+
+        /* large len -- decompress directly into user buffer */
+        else {  /* state->how == GZIP */
+            strm->avail_out = len;
+            strm->next_out = (unsigned char *)buf;
+            if (gz_decomp(state) == -1)
+                return -1;
+            n = state->x.have;
+            state->x.have = 0;
+        }
+
+        /* update progress */
+        len -= n;
+        buf = (char *)buf + n;
+        got += n;
+        state->x.pos += n;
+    } while (len);
+
+    /* return number of bytes read into user buffer (will fit in int) */
+    return (int)got;
+}
+
+/* -- see zlib.h -- */
+#ifdef Z_PREFIX_SET
+#  undef z_gzgetc
+#else
+#  undef gzgetc
+#endif
+int ZEXPORT gzgetc(file)
+    gzFile file;
+{
+    int ret;
+    unsigned char buf[1];
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no (serious) error */
+    if (state->mode != GZ_READ ||
+        (state->err != Z_OK && state->err != Z_BUF_ERROR))
+        return -1;
+
+    /* try output buffer (no need to check for skip request) */
+    if (state->x.have) {
+        state->x.have--;
+        state->x.pos++;
+        return *(state->x.next)++;
+    }
+
+    /* nothing there -- try gzread() */
+    ret = gzread(file, buf, 1);
+    return ret < 1 ? -1 : buf[0];
+}
+
+int ZEXPORT gzgetc_(file)
+gzFile file;
+{
+    return gzgetc(file);
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzungetc(c, file)
+    int c;
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no (serious) error */
+    if (state->mode != GZ_READ ||
+        (state->err != Z_OK && state->err != Z_BUF_ERROR))
+        return -1;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* can't push EOF */
+    if (c < 0)
+        return -1;
+
+    /* if output buffer empty, put byte at end (allows more pushing) */
+    if (state->x.have == 0) {
+        state->x.have = 1;
+        state->x.next = state->out + (state->size << 1) - 1;
+        state->x.next[0] = c;
+        state->x.pos--;
+        state->past = 0;
+        return c;
+    }
+
+    /* if no room, give up (must have already done a gzungetc()) */
+    if (state->x.have == (state->size << 1)) {
+        gz_error(state, Z_DATA_ERROR, "out of room to push characters");
+        return -1;
+    }
+
+    /* slide output data if needed and insert byte before existing data */
+    if (state->x.next == state->out) {
+        unsigned char *src = state->out + state->x.have;
+        unsigned char *dest = state->out + (state->size << 1);
+        while (src > state->out)
+            *--dest = *--src;
+        state->x.next = dest;
+    }
+    state->x.have++;
+    state->x.next--;
+    state->x.next[0] = c;
+    state->x.pos--;
+    state->past = 0;
+    return c;
+}
+
+/* -- see zlib.h -- */
+char * ZEXPORT gzgets(file, buf, len)
+    gzFile file;
+    char *buf;
+    int len;
+{
+    unsigned left, n;
+    char *str;
+    unsigned char *eol;
+    gz_statep state;
+
+    /* check parameters and get internal structure */
+    if (file == NULL || buf == NULL || len < 1)
+        return NULL;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no (serious) error */
+    if (state->mode != GZ_READ ||
+        (state->err != Z_OK && state->err != Z_BUF_ERROR))
+        return NULL;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return NULL;
+    }
+
+    /* copy output bytes up to new line or len - 1, whichever comes first --
+       append a terminating zero to the string (we don't check for a zero in
+       the contents, let the user worry about that) */
+    str = buf;
+    left = (unsigned)len - 1;
+    if (left) do {
+        /* assure that something is in the output buffer */
+        if (state->x.have == 0 && gz_fetch(state) == -1)
+            return NULL;                /* error */
+        if (state->x.have == 0) {       /* end of file */
+            state->past = 1;            /* read past end */
+            break;                      /* return what we have */
+        }
+
+        /* look for end-of-line in current output buffer */
+        n = state->x.have > left ? left : state->x.have;
+        eol = (unsigned char *)memchr(state->x.next, '\n', n);
+        if (eol != NULL)
+            n = (unsigned)(eol - state->x.next) + 1;
+
+        /* copy through end-of-line, or remainder if not found */
+        memcpy(buf, state->x.next, n);
+        state->x.have -= n;
+        state->x.next += n;
+        state->x.pos += n;
+        left -= n;
+        buf += n;
+    } while (left && eol == NULL);
+
+    /* return terminated string, or if nothing, end of file */
+    if (buf == str)
+        return NULL;
+    buf[0] = 0;
+    return str;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzdirect(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+
+    /* if the state is not known, but we can find out, then do so (this is
+       mainly for right after a gzopen() or gzdopen()) */
+    if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
+        (void)gz_look(state);
+
+    /* return 1 if transparent, 0 if processing a gzip stream */
+    return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(file)
+    gzFile file;
+{
+    int ret, err;
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    /* check that we're reading */
+    if (state->mode != GZ_READ)
+        return Z_STREAM_ERROR;
+
+    /* free memory and close file */
+    if (state->size) {
+        inflateEnd(&(state->strm));
+        free(state->out);
+        free(state->in);
+    }
+    err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
+    gz_error(state, Z_OK, NULL);
+    free(state->path);
+    ret = close(state->fd);
+    free(state);
+    return ret ? Z_ERRNO : err;
+}
diff --git a/zlib/gzwrite.c b/zlib/gzwrite.c
new file mode 100644 (file)
index 0000000..aa767fb
--- /dev/null
@@ -0,0 +1,577 @@
+/* gzwrite.c -- zlib functions for writing gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_init OF((gz_statep));
+local int gz_comp OF((gz_statep, int));
+local int gz_zero OF((gz_statep, z_off64_t));
+
+/* Initialize state for writing a gzip file.  Mark initialization by setting
+   state->size to non-zero.  Return -1 on failure or 0 on success. */
+local int gz_init(state)
+    gz_statep state;
+{
+    int ret;
+    z_streamp strm = &(state->strm);
+
+    /* allocate input buffer */
+    state->in = (unsigned char *)malloc(state->want);
+    if (state->in == NULL) {
+        gz_error(state, Z_MEM_ERROR, "out of memory");
+        return -1;
+    }
+
+    /* only need output buffer and deflate state if compressing */
+    if (!state->direct) {
+        /* allocate output buffer */
+        state->out = (unsigned char *)malloc(state->want);
+        if (state->out == NULL) {
+            free(state->in);
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+
+        /* allocate deflate memory, set up for gzip compression */
+        strm->zalloc = Z_NULL;
+        strm->zfree = Z_NULL;
+        strm->opaque = Z_NULL;
+        ret = deflateInit2(strm, state->level, Z_DEFLATED,
+                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
+        if (ret != Z_OK) {
+            free(state->out);
+            free(state->in);
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+    }
+
+    /* mark state as initialized */
+    state->size = state->want;
+
+    /* initialize write buffer if compressing */
+    if (!state->direct) {
+        strm->avail_out = state->size;
+        strm->next_out = state->out;
+        state->x.next = strm->next_out;
+    }
+    return 0;
+}
+
+/* Compress whatever is at avail_in and next_in and write to the output file.
+   Return -1 if there is an error writing to the output file, otherwise 0.
+   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
+   then the deflate() state is reset to start a new gzip stream.  If gz->direct
+   is true, then simply write to the output file without compressing, and
+   ignore flush. */
+local int gz_comp(state, flush)
+    gz_statep state;
+    int flush;
+{
+    int ret, got;
+    unsigned have;
+    z_streamp strm = &(state->strm);
+
+    /* allocate memory if this is the first time through */
+    if (state->size == 0 && gz_init(state) == -1)
+        return -1;
+
+    /* write directly if requested */
+    if (state->direct) {
+        got = write(state->fd, strm->next_in, strm->avail_in);
+        if (got < 0 || (unsigned)got != strm->avail_in) {
+            gz_error(state, Z_ERRNO, zstrerror());
+            return -1;
+        }
+        strm->avail_in = 0;
+        return 0;
+    }
+
+    /* run deflate() on provided input until it produces no more output */
+    ret = Z_OK;
+    do {
+        /* write out current buffer contents if full, or if flushing, but if
+           doing Z_FINISH then don't write until we get to Z_STREAM_END */
+        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
+            (flush != Z_FINISH || ret == Z_STREAM_END))) {
+            have = (unsigned)(strm->next_out - state->x.next);
+            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
+                         (unsigned)got != have)) {
+                gz_error(state, Z_ERRNO, zstrerror());
+                return -1;
+            }
+            if (strm->avail_out == 0) {
+                strm->avail_out = state->size;
+                strm->next_out = state->out;
+            }
+            state->x.next = strm->next_out;
+        }
+
+        /* compress */
+        have = strm->avail_out;
+        ret = deflate(strm, flush);
+        if (ret == Z_STREAM_ERROR) {
+            gz_error(state, Z_STREAM_ERROR,
+                      "internal error: deflate stream corrupt");
+            return -1;
+        }
+        have -= strm->avail_out;
+    } while (have);
+
+    /* if that completed a deflate stream, allow another to start */
+    if (flush == Z_FINISH)
+        deflateReset(strm);
+
+    /* all done, no errors */
+    return 0;
+}
+
+/* Compress len zeros to output.  Return -1 on error, 0 on success. */
+local int gz_zero(state, len)
+    gz_statep state;
+    z_off64_t len;
+{
+    int first;
+    unsigned n;
+    z_streamp strm = &(state->strm);
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return -1;
+
+    /* compress len zeros (len guaranteed > 0) */
+    first = 1;
+    while (len) {
+        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
+            (unsigned)len : state->size;
+        if (first) {
+            memset(state->in, 0, n);
+            first = 0;
+        }
+        strm->avail_in = n;
+        strm->next_in = state->in;
+        state->x.pos += n;
+        if (gz_comp(state, Z_NO_FLUSH) == -1)
+            return -1;
+        len -= n;
+    }
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzwrite(file, buf, len)
+    gzFile file;
+    voidpc buf;
+    unsigned len;
+{
+    unsigned put = len;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* since an int is returned, make sure len fits in one, otherwise return
+       with an error (this avoids the flaw in the interface) */
+    if ((int)len < 0) {
+        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+        return 0;
+    }
+
+    /* if len is zero, avoid unnecessary operations */
+    if (len == 0)
+        return 0;
+
+    /* allocate memory if this is the first time through */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* for small len, copy to input buffer, otherwise compress directly */
+    if (len < state->size) {
+        /* copy to input buffer, compress when full */
+        do {
+            unsigned have, copy;
+
+            if (strm->avail_in == 0)
+                strm->next_in = state->in;
+            have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
+            copy = state->size - have;
+            if (copy > len)
+                copy = len;
+            memcpy(state->in + have, buf, copy);
+            strm->avail_in += copy;
+            state->x.pos += copy;
+            buf = (const char *)buf + copy;
+            len -= copy;
+            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
+                return 0;
+        } while (len);
+    }
+    else {
+        /* consume whatever's left in the input buffer */
+        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+            return 0;
+
+        /* directly compress user buffer to file */
+        strm->avail_in = len;
+        strm->next_in = (z_const Bytef *)buf;
+        state->x.pos += len;
+        if (gz_comp(state, Z_NO_FLUSH) == -1)
+            return 0;
+    }
+
+    /* input was all buffered or compressed (put will fit in int) */
+    return (int)put;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputc(file, c)
+    gzFile file;
+    int c;
+{
+    unsigned have;
+    unsigned char buf[1];
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return -1;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* try writing to input buffer for speed (state->size == 0 if buffer not
+       initialized) */
+    if (state->size) {
+        if (strm->avail_in == 0)
+            strm->next_in = state->in;
+        have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
+        if (have < state->size) {
+            state->in[have] = c;
+            strm->avail_in++;
+            state->x.pos++;
+            return c & 0xff;
+        }
+    }
+
+    /* no room in buffer or not initialized, use gz_write() */
+    buf[0] = c;
+    if (gzwrite(file, buf, 1) != 1)
+        return -1;
+    return c & 0xff;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputs(file, str)
+    gzFile file;
+    const char *str;
+{
+    int ret;
+    unsigned len;
+
+    /* write string */
+    len = (unsigned)strlen(str);
+    ret = gzwrite(file, str, len);
+    return ret == 0 && len != 0 ? -1 : ret;
+}
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#include <stdarg.h>
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
+{
+    int size, len;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* make sure we have some buffer space */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return 0;
+
+    /* do the printf() into the input buffer, put length in len */
+    size = (int)(state->size);
+    state->in[size - 1] = 0;
+#ifdef NO_vsnprintf
+#  ifdef HAS_vsprintf_void
+    (void)vsprintf((char *)(state->in), format, va);
+    for (len = 0; len < size; len++)
+        if (state->in[len] == 0) break;
+#  else
+    len = vsprintf((char *)(state->in), format, va);
+#  endif
+#else
+#  ifdef HAS_vsnprintf_void
+    (void)vsnprintf((char *)(state->in), size, format, va);
+    len = strlen((char *)(state->in));
+#  else
+    len = vsnprintf((char *)(state->in), size, format, va);
+#  endif
+#endif
+
+    /* check that printf() results fit in buffer */
+    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+        return 0;
+
+    /* update buffer and position, defer compression until needed */
+    strm->avail_in = (unsigned)len;
+    strm->next_in = state->in;
+    state->x.pos += len;
+    return len;
+}
+
+int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
+{
+    va_list va;
+    int ret;
+
+    va_start(va, format);
+    ret = gzvprintf(file, format, va);
+    va_end(va);
+    return ret;
+}
+
+#else /* !STDC && !Z_HAVE_STDARG_H */
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
+    gzFile file;
+    const char *format;
+    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
+{
+    int size, len;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that can really pass pointer in ints */
+    if (sizeof(int) != sizeof(void *))
+        return 0;
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* make sure we have some buffer space */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return 0;
+
+    /* do the printf() into the input buffer, put length in len */
+    size = (int)(state->size);
+    state->in[size - 1] = 0;
+#ifdef NO_snprintf
+#  ifdef HAS_sprintf_void
+    sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
+            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+    for (len = 0; len < size; len++)
+        if (state->in[len] == 0) break;
+#  else
+    len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
+                  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+#  endif
+#else
+#  ifdef HAS_snprintf_void
+    snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+    len = strlen((char *)(state->in));
+#  else
+    len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
+                   a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
+                   a19, a20);
+#  endif
+#endif
+
+    /* check that printf() results fit in buffer */
+    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+        return 0;
+
+    /* update buffer and position, defer compression until needed */
+    strm->avail_in = (unsigned)len;
+    strm->next_in = state->in;
+    state->x.pos += len;
+    return len;
+}
+
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzflush(file, flush)
+    gzFile file;
+    int flush;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return Z_STREAM_ERROR;
+
+    /* check flush parameter */
+    if (flush < 0 || flush > Z_FINISH)
+        return Z_STREAM_ERROR;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* compress remaining data with requested flush */
+    gz_comp(state, flush);
+    return state->err;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzsetparams(file, level, strategy)
+    gzFile file;
+    int level;
+    int strategy;
+{
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return Z_STREAM_ERROR;
+
+    /* if no change is requested, then do nothing */
+    if (level == state->level && strategy == state->strategy)
+        return Z_OK;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* change compression parameters for subsequent input */
+    if (state->size) {
+        /* flush previous input with previous parameters before changing */
+        if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
+            return state->err;
+        deflateParams(strm, level, strategy);
+    }
+    state->level = level;
+    state->strategy = strategy;
+    return Z_OK;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_w(file)
+    gzFile file;
+{
+    int ret = Z_OK;
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    /* check that we're writing */
+    if (state->mode != GZ_WRITE)
+        return Z_STREAM_ERROR;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            ret = state->err;
+    }
+
+    /* flush, free memory, and close file */
+    if (gz_comp(state, Z_FINISH) == -1)
+        ret = state->err;
+    if (state->size) {
+        if (!state->direct) {
+            (void)deflateEnd(&(state->strm));
+            free(state->out);
+        }
+        free(state->in);
+    }
+    gz_error(state, Z_OK, NULL);
+    free(state->path);
+    if (close(state->fd) == -1)
+        ret = Z_ERRNO;
+    free(state);
+    return ret;
+}
index bbee92ed1e6bd3683d4d497bdf639dd1b05e98bc..bda59ceb6a12b2d7de80ef3c1e7318e54fc979ac 100644 (file)
@@ -1,5 +1,5 @@
 /* inffast.c -- fast decoding
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2008, 2010, 2013 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
       requires strm->avail_out >= 258 for each loop to avoid checking for
       output space.
  */
-void inflate_fast(strm, start)
+void ZLIB_INTERNAL inflate_fast(strm, start)
 z_streamp strm;
 unsigned start;         /* inflate()'s starting value for strm->avail_out */
 {
     struct inflate_state FAR *state;
-    unsigned char FAR *in;      /* local strm->next_in */
-    unsigned char FAR *last;    /* while in < last, enough input available */
+    z_const unsigned char FAR *in;      /* local strm->next_in */
+    z_const unsigned char FAR *last;    /* have enough input while in < last */
     unsigned char FAR *out;     /* local strm->next_out */
     unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
     unsigned char FAR *end;     /* while out < end, enough space available */
@@ -79,7 +79,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
 #endif
     unsigned wsize;             /* window size or zero if not using window */
     unsigned whave;             /* valid bytes in the window */
-    unsigned write;             /* window write index */
+    unsigned wnext;             /* window write index */
     unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
     unsigned long hold;         /* local strm->hold */
     unsigned bits;              /* local strm->bits */
@@ -87,7 +87,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
     code const FAR *dcode;      /* local strm->distcode */
     unsigned lmask;             /* mask for first level of length codes */
     unsigned dmask;             /* mask for first level of distance codes */
-    code this;                  /* retrieved table entry */
+    code here;                  /* retrieved table entry */
     unsigned op;                /* code bits, operation, extra bits, or */
                                 /*  window position, window bytes to copy */
     unsigned len;               /* match length, unused bytes */
@@ -106,7 +106,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
 #endif
     wsize = state->wsize;
     whave = state->whave;
-    write = state->write;
+    wnext = state->wnext;
     window = state->window;
     hold = state->hold;
     bits = state->bits;
@@ -124,20 +124,20 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
             hold += (unsigned long)(PUP(in)) << bits;
             bits += 8;
         }
-        this = lcode[hold & lmask];
+        here = lcode[hold & lmask];
       dolen:
-        op = (unsigned)(this.bits);
+        op = (unsigned)(here.bits);
         hold >>= op;
         bits -= op;
-        op = (unsigned)(this.op);
+        op = (unsigned)(here.op);
         if (op == 0) {                          /* literal */
-            Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
+            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
                     "inflate:         literal '%c'\n" :
-                    "inflate:         literal 0x%02x\n", this.val));
-            PUP(out) = (unsigned char)(this.val);
+                    "inflate:         literal 0x%02x\n", here.val));
+            PUP(out) = (unsigned char)(here.val);
         }
         else if (op & 16) {                     /* length base */
-            len = (unsigned)(this.val);
+            len = (unsigned)(here.val);
             op &= 15;                           /* number of extra bits */
             if (op) {
                 if (bits < op) {
@@ -155,14 +155,14 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 hold += (unsigned long)(PUP(in)) << bits;
                 bits += 8;
             }
-            this = dcode[hold & dmask];
+            here = dcode[hold & dmask];
           dodist:
-            op = (unsigned)(this.bits);
+            op = (unsigned)(here.bits);
             hold >>= op;
             bits -= op;
-            op = (unsigned)(this.op);
+            op = (unsigned)(here.op);
             if (op & 16) {                      /* distance base */
-                dist = (unsigned)(this.val);
+                dist = (unsigned)(here.val);
                 op &= 15;                       /* number of extra bits */
                 if (bits < op) {
                     hold += (unsigned long)(PUP(in)) << bits;
@@ -187,12 +187,34 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 if (dist > op) {                /* see if copy from window */
                     op = dist - op;             /* distance back in window */
                     if (op > whave) {
-                        strm->msg = (char *)"invalid distance too far back";
-                        state->mode = BAD;
-                        break;
+                        if (state->sane) {
+                            strm->msg =
+                                (char *)"invalid distance too far back";
+                            state->mode = BAD;
+                            break;
+                        }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+                        if (len <= op - whave) {
+                            do {
+                                PUP(out) = 0;
+                            } while (--len);
+                            continue;
+                        }
+                        len -= op - whave;
+                        do {
+                            PUP(out) = 0;
+                        } while (--op > whave);
+                        if (op == 0) {
+                            from = out - dist;
+                            do {
+                                PUP(out) = PUP(from);
+                            } while (--len);
+                            continue;
+                        }
+#endif
                     }
                     from = window - OFF;
-                    if (write == 0) {           /* very common case */
+                    if (wnext == 0) {           /* very common case */
                         from += wsize - op;
                         if (op < len) {         /* some from window */
                             len -= op;
@@ -202,17 +224,17 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                             from = out - dist;  /* rest from output */
                         }
                     }
-                    else if (write < op) {      /* wrap around window */
-                        from += wsize + write - op;
-                        op -= write;
+                    else if (wnext < op) {      /* wrap around window */
+                        from += wsize + wnext - op;
+                        op -= wnext;
                         if (op < len) {         /* some from end of window */
                             len -= op;
                             do {
                                 PUP(out) = PUP(from);
                             } while (--op);
                             from = window - OFF;
-                            if (write < len) {  /* some from start of window */
-                                op = write;
+                            if (wnext < len) {  /* some from start of window */
+                                op = wnext;
                                 len -= op;
                                 do {
                                     PUP(out) = PUP(from);
@@ -222,7 +244,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                         }
                     }
                     else {                      /* contiguous in window */
-                        from += write - op;
+                        from += wnext - op;
                         if (op < len) {         /* some from window */
                             len -= op;
                             do {
@@ -259,7 +281,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 }
             }
             else if ((op & 64) == 0) {          /* 2nd level distance code */
-                this = dcode[this.val + (hold & ((1U << op) - 1))];
+                here = dcode[here.val + (hold & ((1U << op) - 1))];
                 goto dodist;
             }
             else {
@@ -269,7 +291,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
             }
         }
         else if ((op & 64) == 0) {              /* 2nd level length code */
-            this = lcode[this.val + (hold & ((1U << op) - 1))];
+            here = lcode[here.val + (hold & ((1U << op) - 1))];
             goto dolen;
         }
         else if (op & 32) {                     /* end-of-block */
@@ -305,7 +327,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
    inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
    - Using bit fields for code structure
    - Different op definition to avoid & for extra bits (do & for table bits)
-   - Three separate decoding do-loops for direct, window, and write == 0
+   - Three separate decoding do-loops for direct, window, and wnext == 0
    - Special case for distance > 1 copies to do overlapped load and store copy
    - Explicit branch predictions (based on measured branch probabilities)
    - Deferring match copy and interspersed it with decoding subsequent codes
index 1e88d2d97b568d37c44800c5aa7e54cfa33d46d3..e5c1aa4ca8cd5244423680865609c71ab68f9ab6 100644 (file)
@@ -1,5 +1,5 @@
 /* inffast.h -- header to use inffast.c
- * Copyright (C) 1995-2003 Mark Adler
+ * Copyright (C) 1995-2003, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -8,4 +8,4 @@
    subject to change. Applications should only use zlib.h.
  */
 
-void inflate_fast OF((z_streamp strm, unsigned start));
+void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
index 75ed4b5978de4be3c44ad48a060b75500f17a0a2..d6283277694802ce7938f537f12990d6eead4924 100644 (file)
@@ -2,9 +2,9 @@
      * Generated automatically by makefixed().
      */
 
-    /* WARNING: this file should *not* be used by applications. It
-       is part of the implementation of the compression library and
-       is subject to change. Applications should only use zlib.h.
+    /* WARNING: this file should *not* be used by applications.
+       It is part of the implementation of this library and is
+       subject to change. Applications should only use zlib.h.
      */
 
     static const code lenfix[512] = {
index 792fdee8e9c72bf3a62ead549577ba5cdd473e61..870f89bb4d3646684bf37e2144c4b83c808ab84d 100644 (file)
@@ -1,5 +1,5 @@
 /* inflate.c -- zlib decompression
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2012 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -45,7 +45,7 @@
  * - Rearrange window copies in inflate_fast() for speed and simplification
  * - Unroll last copy for window match in inflate_fast()
  * - Use local copies of window variables in inflate_fast() for speed
- * - Pull out common write == 0 case for speed in inflate_fast()
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
  * - Make op and len in inflate_fast() unsigned for consistency
  * - Add FAR to lcode and dcode declarations in inflate_fast()
  * - Simplified bad distance check in inflate_fast()
 
 /* function prototypes */
 local void fixedtables OF((struct inflate_state FAR *state));
-local int updatewindow OF((z_streamp strm, unsigned out));
+local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
+                           unsigned copy));
 #ifdef BUILDFIXED
    void makefixed OF((void));
 #endif
-local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
+local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
                               unsigned len));
 
-int ZEXPORT inflateReset(strm)
+int ZEXPORT inflateResetKeep(strm)
 z_streamp strm;
 {
     struct inflate_state FAR *state;
@@ -109,36 +110,71 @@ z_streamp strm;
     state = (struct inflate_state FAR *)strm->state;
     strm->total_in = strm->total_out = state->total = 0;
     strm->msg = Z_NULL;
-    strm->adler = 1;        /* to support ill-conceived Java test suite */
+    if (state->wrap)        /* to support ill-conceived Java test suite */
+        strm->adler = state->wrap & 1;
     state->mode = HEAD;
     state->last = 0;
     state->havedict = 0;
     state->dmax = 32768U;
     state->head = Z_NULL;
-    state->wsize = 0;
-    state->whave = 0;
-    state->write = 0;
     state->hold = 0;
     state->bits = 0;
     state->lencode = state->distcode = state->next = state->codes;
+    state->sane = 1;
+    state->back = -1;
     Tracev((stderr, "inflate: reset\n"));
     return Z_OK;
 }
 
-int ZEXPORT inflatePrime(strm, bits, value)
+int ZEXPORT inflateReset(strm)
 z_streamp strm;
-int bits;
-int value;
 {
     struct inflate_state FAR *state;
 
     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
     state = (struct inflate_state FAR *)strm->state;
-    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
-    value &= (1L << bits) - 1;
-    state->hold += value << state->bits;
-    state->bits += bits;
-    return Z_OK;
+    state->wsize = 0;
+    state->whave = 0;
+    state->wnext = 0;
+    return inflateResetKeep(strm);
+}
+
+int ZEXPORT inflateReset2(strm, windowBits)
+z_streamp strm;
+int windowBits;
+{
+    int wrap;
+    struct inflate_state FAR *state;
+
+    /* get the state */
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+
+    /* extract wrap request from windowBits parameter */
+    if (windowBits < 0) {
+        wrap = 0;
+        windowBits = -windowBits;
+    }
+    else {
+        wrap = (windowBits >> 4) + 1;
+#ifdef GUNZIP
+        if (windowBits < 48)
+            windowBits &= 15;
+#endif
+    }
+
+    /* set number of window bits, free window if different */
+    if (windowBits && (windowBits < 8 || windowBits > 15))
+        return Z_STREAM_ERROR;
+    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
+        ZFREE(strm, state->window);
+        state->window = Z_NULL;
+    }
+
+    /* update state and reset the rest of it */
+    state->wrap = wrap;
+    state->wbits = (unsigned)windowBits;
+    return inflateReset(strm);
 }
 
 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
@@ -147,6 +183,7 @@ int windowBits;
 const char *version;
 int stream_size;
 {
+    int ret;
     struct inflate_state FAR *state;
 
     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
@@ -155,33 +192,31 @@ int stream_size;
     if (strm == Z_NULL) return Z_STREAM_ERROR;
     strm->msg = Z_NULL;                 /* in case we return an error */
     if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+        return Z_STREAM_ERROR;
+#else
         strm->zalloc = zcalloc;
         strm->opaque = (voidpf)0;
+#endif
     }
-    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
+    if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+        return Z_STREAM_ERROR;
+#else
+        strm->zfree = zcfree;
+#endif
     state = (struct inflate_state FAR *)
             ZALLOC(strm, 1, sizeof(struct inflate_state));
     if (state == Z_NULL) return Z_MEM_ERROR;
     Tracev((stderr, "inflate: allocated\n"));
     strm->state = (struct internal_state FAR *)state;
-    if (windowBits < 0) {
-        state->wrap = 0;
-        windowBits = -windowBits;
-    }
-    else {
-        state->wrap = (windowBits >> 4) + 1;
-#ifdef GUNZIP
-        if (windowBits < 48) windowBits &= 15;
-#endif
-    }
-    if (windowBits < 8 || windowBits > 15) {
+    state->window = Z_NULL;
+    ret = inflateReset2(strm, windowBits);
+    if (ret != Z_OK) {
         ZFREE(strm, state);
         strm->state = Z_NULL;
-        return Z_STREAM_ERROR;
     }
-    state->wbits = (unsigned)windowBits;
-    state->window = Z_NULL;
-    return inflateReset(strm);
+    return ret;
 }
 
 int ZEXPORT inflateInit_(strm, version, stream_size)
@@ -192,6 +227,27 @@ int stream_size;
     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
 }
 
+int ZEXPORT inflatePrime(strm, bits, value)
+z_streamp strm;
+int bits;
+int value;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+    if (bits < 0) {
+        state->hold = 0;
+        state->bits = 0;
+        return Z_OK;
+    }
+    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
+    value &= (1L << bits) - 1;
+    state->hold += value << state->bits;
+    state->bits += bits;
+    return Z_OK;
+}
+
 /*
    Return state with length and distance decoding tables and index sizes set to
    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
@@ -286,8 +342,8 @@ void makefixed()
     low = 0;
     for (;;) {
         if ((low % 7) == 0) printf("\n        ");
-        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
-               state.lencode[low].val);
+        printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
+               state.lencode[low].bits, state.lencode[low].val);
         if (++low == size) break;
         putchar(',');
     }
@@ -320,12 +376,13 @@ void makefixed()
    output will fall in the output data, making match copies simpler and faster.
    The advantage may be dependent on the size of the processor's data caches.
  */
-local int updatewindow(strm, out)
+local int updatewindow(strm, end, copy)
 z_streamp strm;
-unsigned out;
+const Bytef *end;
+unsigned copy;
 {
     struct inflate_state FAR *state;
-    unsigned copy, dist;
+    unsigned dist;
 
     state = (struct inflate_state FAR *)strm->state;
 
@@ -340,30 +397,29 @@ unsigned out;
     /* if window not in use yet, initialize */
     if (state->wsize == 0) {
         state->wsize = 1U << state->wbits;
-        state->write = 0;
+        state->wnext = 0;
         state->whave = 0;
     }
 
     /* copy state->wsize or less output bytes into the circular window */
-    copy = out - strm->avail_out;
     if (copy >= state->wsize) {
-        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
-        state->write = 0;
+        zmemcpy(state->window, end - state->wsize, state->wsize);
+        state->wnext = 0;
         state->whave = state->wsize;
     }
     else {
-        dist = state->wsize - state->write;
+        dist = state->wsize - state->wnext;
         if (dist > copy) dist = copy;
-        zmemcpy(state->window + state->write, strm->next_out - copy, dist);
+        zmemcpy(state->window + state->wnext, end - copy, dist);
         copy -= dist;
         if (copy) {
-            zmemcpy(state->window, strm->next_out - copy, copy);
-            state->write = copy;
+            zmemcpy(state->window, end - copy, copy);
+            state->wnext = copy;
             state->whave = state->wsize;
         }
         else {
-            state->write += dist;
-            if (state->write == state->wsize) state->write = 0;
+            state->wnext += dist;
+            if (state->wnext == state->wsize) state->wnext = 0;
             if (state->whave < state->wsize) state->whave += dist;
         }
     }
@@ -464,11 +520,6 @@ unsigned out;
         bits -= bits & 7; \
     } while (0)
 
-/* Reverse the bytes in a 32-bit value */
-#define REVERSE(q) \
-    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
-     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
-
 /*
    inflate() uses a state machine to process as much input data and generate as
    much output data as possible before returning.  The state machine is
@@ -556,7 +607,7 @@ z_streamp strm;
 int flush;
 {
     struct inflate_state FAR *state;
-    unsigned char FAR *next;    /* next input */
+    z_const unsigned char FAR *next;    /* next input */
     unsigned char FAR *put;     /* next output */
     unsigned have, left;        /* available input and output */
     unsigned long hold;         /* bit buffer */
@@ -564,7 +615,7 @@ int flush;
     unsigned in, out;           /* save starting available input and output */
     unsigned copy;              /* number of stored or match bytes to copy */
     unsigned char FAR *from;    /* where to copy match bytes from */
-    code this;                  /* current decoding table entry */
+    code here;                  /* current decoding table entry */
     code last;                  /* parent table entry */
     unsigned len;               /* length to copy for repeats, bits to drop */
     int ret;                    /* return code */
@@ -619,7 +670,9 @@ int flush;
             }
             DROPBITS(4);
             len = BITS(4) + 8;
-            if (len > state->wbits) {
+            if (state->wbits == 0)
+                state->wbits = len;
+            else if (len > state->wbits) {
                 strm->msg = (char *)"invalid window size";
                 state->mode = BAD;
                 break;
@@ -760,7 +813,7 @@ int flush;
 #endif
         case DICTID:
             NEEDBITS(32);
-            strm->adler = state->check = REVERSE(hold);
+            strm->adler = state->check = ZSWAP32(hold);
             INITBITS();
             state->mode = DICT;
         case DICT:
@@ -771,7 +824,7 @@ int flush;
             strm->adler = state->check = adler32(0L, Z_NULL, 0);
             state->mode = TYPE;
         case TYPE:
-            if (flush == Z_BLOCK) goto inf_leave;
+            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
         case TYPEDO:
             if (state->last) {
                 BYTEBITS();
@@ -791,7 +844,11 @@ int flush;
                 fixedtables(state);
                 Tracev((stderr, "inflate:     fixed codes block%s\n",
                         state->last ? " (last)" : ""));
-                state->mode = LEN;              /* decode codes */
+                state->mode = LEN_;             /* decode codes */
+                if (flush == Z_TREES) {
+                    DROPBITS(2);
+                    goto inf_leave;
+                }
                 break;
             case 2:                             /* dynamic block */
                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
@@ -816,6 +873,9 @@ int flush;
             Tracev((stderr, "inflate:       stored length %u\n",
                     state->length));
             INITBITS();
+            state->mode = COPY_;
+            if (flush == Z_TREES) goto inf_leave;
+        case COPY_:
             state->mode = COPY;
         case COPY:
             copy = state->length;
@@ -861,7 +921,7 @@ int flush;
             while (state->have < 19)
                 state->lens[order[state->have++]] = 0;
             state->next = state->codes;
-            state->lencode = (code const FAR *)(state->next);
+            state->lencode = (const code FAR *)(state->next);
             state->lenbits = 7;
             ret = inflate_table(CODES, state->lens, 19, &(state->next),
                                 &(state->lenbits), state->work);
@@ -876,19 +936,18 @@ int flush;
         case CODELENS:
             while (state->have < state->nlen + state->ndist) {
                 for (;;) {
-                    this = state->lencode[BITS(state->lenbits)];
-                    if ((unsigned)(this.bits) <= bits) break;
+                    here = state->lencode[BITS(state->lenbits)];
+                    if ((unsigned)(here.bits) <= bits) break;
                     PULLBYTE();
                 }
-                if (this.val < 16) {
-                    NEEDBITS(this.bits);
-                    DROPBITS(this.bits);
-                    state->lens[state->have++] = this.val;
+                if (here.val < 16) {
+                    DROPBITS(here.bits);
+                    state->lens[state->have++] = here.val;
                 }
                 else {
-                    if (this.val == 16) {
-                        NEEDBITS(this.bits + 2);
-                        DROPBITS(this.bits);
+                    if (here.val == 16) {
+                        NEEDBITS(here.bits + 2);
+                        DROPBITS(here.bits);
                         if (state->have == 0) {
                             strm->msg = (char *)"invalid bit length repeat";
                             state->mode = BAD;
@@ -898,16 +957,16 @@ int flush;
                         copy = 3 + BITS(2);
                         DROPBITS(2);
                     }
-                    else if (this.val == 17) {
-                        NEEDBITS(this.bits + 3);
-                        DROPBITS(this.bits);
+                    else if (here.val == 17) {
+                        NEEDBITS(here.bits + 3);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 3 + BITS(3);
                         DROPBITS(3);
                     }
                     else {
-                        NEEDBITS(this.bits + 7);
-                        DROPBITS(this.bits);
+                        NEEDBITS(here.bits + 7);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 11 + BITS(7);
                         DROPBITS(7);
@@ -925,9 +984,18 @@ int flush;
             /* handle error breaks in while */
             if (state->mode == BAD) break;
 
-            /* build code tables */
+            /* check for end-of-block code (better have one) */
+            if (state->lens[256] == 0) {
+                strm->msg = (char *)"invalid code -- missing end-of-block";
+                state->mode = BAD;
+                break;
+            }
+
+            /* build code tables -- note: do not change the lenbits or distbits
+               values here (9 and 6) without reading the comments in inftrees.h
+               concerning the ENOUGH constants, which depend on those values */
             state->next = state->codes;
-            state->lencode = (code const FAR *)(state->next);
+            state->lencode = (const code FAR *)(state->next);
             state->lenbits = 9;
             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
                                 &(state->lenbits), state->work);
@@ -936,7 +1004,7 @@ int flush;
                 state->mode = BAD;
                 break;
             }
-            state->distcode = (code const FAR *)(state->next);
+            state->distcode = (const code FAR *)(state->next);
             state->distbits = 6;
             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
                             &(state->next), &(state->distbits), state->work);
@@ -946,88 +1014,102 @@ int flush;
                 break;
             }
             Tracev((stderr, "inflate:       codes ok\n"));
+            state->mode = LEN_;
+            if (flush == Z_TREES) goto inf_leave;
+        case LEN_:
             state->mode = LEN;
         case LEN:
             if (have >= 6 && left >= 258) {
                 RESTORE();
                 inflate_fast(strm, out);
                 LOAD();
+                if (state->mode == TYPE)
+                    state->back = -1;
                 break;
             }
+            state->back = 0;
             for (;;) {
-                this = state->lencode[BITS(state->lenbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->lencode[BITS(state->lenbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if (this.op && (this.op & 0xf0) == 0) {
-                last = this;
+            if (here.op && (here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->lencode[last.val +
+                    here = state->lencode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
+                state->back += last.bits;
             }
-            DROPBITS(this.bits);
-            state->length = (unsigned)this.val;
-            if ((int)(this.op) == 0) {
-                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
+            DROPBITS(here.bits);
+            state->back += here.bits;
+            state->length = (unsigned)here.val;
+            if ((int)(here.op) == 0) {
+                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
                         "inflate:         literal '%c'\n" :
-                        "inflate:         literal 0x%02x\n", this.val));
+                        "inflate:         literal 0x%02x\n", here.val));
                 state->mode = LIT;
                 break;
             }
-            if (this.op & 32) {
+            if (here.op & 32) {
                 Tracevv((stderr, "inflate:         end of block\n"));
+                state->back = -1;
                 state->mode = TYPE;
                 break;
             }
-            if (this.op & 64) {
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid literal/length code";
                 state->mode = BAD;
                 break;
             }
-            state->extra = (unsigned)(this.op) & 15;
+            state->extra = (unsigned)(here.op) & 15;
             state->mode = LENEXT;
         case LENEXT:
             if (state->extra) {
                 NEEDBITS(state->extra);
                 state->length += BITS(state->extra);
                 DROPBITS(state->extra);
+                state->back += state->extra;
             }
             Tracevv((stderr, "inflate:         length %u\n", state->length));
+            state->was = state->length;
             state->mode = DIST;
         case DIST:
             for (;;) {
-                this = state->distcode[BITS(state->distbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->distcode[BITS(state->distbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if ((this.op & 0xf0) == 0) {
-                last = this;
+            if ((here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->distcode[last.val +
+                    here = state->distcode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
+                state->back += last.bits;
             }
-            DROPBITS(this.bits);
-            if (this.op & 64) {
+            DROPBITS(here.bits);
+            state->back += here.bits;
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid distance code";
                 state->mode = BAD;
                 break;
             }
-            state->offset = (unsigned)this.val;
-            state->extra = (unsigned)(this.op) & 15;
+            state->offset = (unsigned)here.val;
+            state->extra = (unsigned)(here.op) & 15;
             state->mode = DISTEXT;
         case DISTEXT:
             if (state->extra) {
                 NEEDBITS(state->extra);
                 state->offset += BITS(state->extra);
                 DROPBITS(state->extra);
+                state->back += state->extra;
             }
 #ifdef INFLATE_STRICT
             if (state->offset > state->dmax) {
@@ -1036,11 +1118,6 @@ int flush;
                 break;
             }
 #endif
-            if (state->offset > state->whave + out - left) {
-                strm->msg = (char *)"invalid distance too far back";
-                state->mode = BAD;
-                break;
-            }
             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
             state->mode = MATCH;
         case MATCH:
@@ -1048,12 +1125,32 @@ int flush;
             copy = out - left;
             if (state->offset > copy) {         /* copy from window */
                 copy = state->offset - copy;
-                if (copy > state->write) {
-                    copy -= state->write;
+                if (copy > state->whave) {
+                    if (state->sane) {
+                        strm->msg = (char *)"invalid distance too far back";
+                        state->mode = BAD;
+                        break;
+                    }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+                    Trace((stderr, "inflate.c too far\n"));
+                    copy -= state->whave;
+                    if (copy > state->length) copy = state->length;
+                    if (copy > left) copy = left;
+                    left -= copy;
+                    state->length -= copy;
+                    do {
+                        *put++ = 0;
+                    } while (--copy);
+                    if (state->length == 0) state->mode = LEN;
+                    break;
+#endif
+                }
+                if (copy > state->wnext) {
+                    copy -= state->wnext;
                     from = state->window + (state->wsize - copy);
                 }
                 else
-                    from = state->window + (state->write - copy);
+                    from = state->window + (state->wnext - copy);
                 if (copy > state->length) copy = state->length;
             }
             else {                              /* copy from output */
@@ -1088,7 +1185,7 @@ int flush;
 #ifdef GUNZIP
                      state->flags ? hold :
 #endif
-                     REVERSE(hold)) != state->check) {
+                     ZSWAP32(hold)) != state->check) {
                     strm->msg = (char *)"incorrect data check";
                     state->mode = BAD;
                     break;
@@ -1132,8 +1229,9 @@ int flush;
      */
   inf_leave:
     RESTORE();
-    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
-        if (updatewindow(strm, out)) {
+    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
+            (state->mode < CHECK || flush != Z_FINISH)))
+        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
             state->mode = MEM;
             return Z_MEM_ERROR;
         }
@@ -1146,7 +1244,8 @@ int flush;
         strm->adler = state->check =
             UPDATE(state->check, strm->next_out - out, out);
     strm->data_type = state->bits + (state->last ? 64 : 0) +
-                      (state->mode == TYPE ? 128 : 0);
+                      (state->mode == TYPE ? 128 : 0) +
+                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
         ret = Z_BUF_ERROR;
     return ret;
@@ -1166,13 +1265,37 @@ z_streamp strm;
     return Z_OK;
 }
 
+int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
+z_streamp strm;
+Bytef *dictionary;
+uInt *dictLength;
+{
+    struct inflate_state FAR *state;
+
+    /* check state */
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+
+    /* copy dictionary */
+    if (state->whave && dictionary != Z_NULL) {
+        zmemcpy(dictionary, state->window + state->wnext,
+                state->whave - state->wnext);
+        zmemcpy(dictionary + state->whave - state->wnext,
+                state->window, state->wnext);
+    }
+    if (dictLength != Z_NULL)
+        *dictLength = state->whave;
+    return Z_OK;
+}
+
 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
 z_streamp strm;
 const Bytef *dictionary;
 uInt dictLength;
 {
     struct inflate_state FAR *state;
-    unsigned long id;
+    unsigned long dictid;
+    int ret;
 
     /* check state */
     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
@@ -1180,29 +1303,21 @@ uInt dictLength;
     if (state->wrap != 0 && state->mode != DICT)
         return Z_STREAM_ERROR;
 
-    /* check for correct dictionary id */
+    /* check for correct dictionary identifier */
     if (state->mode == DICT) {
-        id = adler32(0L, Z_NULL, 0);
-        id = adler32(id, dictionary, dictLength);
-        if (id != state->check)
+        dictid = adler32(0L, Z_NULL, 0);
+        dictid = adler32(dictid, dictionary, dictLength);
+        if (dictid != state->check)
             return Z_DATA_ERROR;
     }
 
-    /* copy dictionary to window */
-    if (updatewindow(strm, strm->avail_out)) {
+    /* copy dictionary to window using updatewindow(), which will amend the
+       existing dictionary if appropriate */
+    ret = updatewindow(strm, dictionary + dictLength, dictLength);
+    if (ret) {
         state->mode = MEM;
         return Z_MEM_ERROR;
     }
-    if (dictLength > state->wsize) {
-        zmemcpy(state->window, dictionary + dictLength - state->wsize,
-                state->wsize);
-        state->whave = state->wsize;
-    }
-    else {
-        zmemcpy(state->window + state->wsize - dictLength, dictionary,
-                dictLength);
-        state->whave = dictLength;
-    }
     state->havedict = 1;
     Tracev((stderr, "inflate:   dictionary set\n"));
     return Z_OK;
@@ -1238,7 +1353,7 @@ gz_headerp head;
  */
 local unsigned syncsearch(have, buf, len)
 unsigned FAR *have;
-unsigned char FAR *buf;
+const unsigned char FAR *buf;
 unsigned len;
 {
     unsigned got;
@@ -1350,8 +1465,8 @@ z_streamp source;
     }
 
     /* copy state */
-    zmemcpy(dest, source, sizeof(z_stream));
-    zmemcpy(copy, state, sizeof(struct inflate_state));
+    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
     if (state->lencode >= state->codes &&
         state->lencode <= state->codes + ENOUGH - 1) {
         copy->lencode = copy->codes + (state->lencode - state->codes);
@@ -1366,3 +1481,32 @@ z_streamp source;
     dest->state = (struct internal_state FAR *)copy;
     return Z_OK;
 }
+
+int ZEXPORT inflateUndermine(strm, subvert)
+z_streamp strm;
+int subvert;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+    state->sane = !subvert;
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+    return Z_OK;
+#else
+    state->sane = 1;
+    return Z_DATA_ERROR;
+#endif
+}
+
+long ZEXPORT inflateMark(strm)
+z_streamp strm;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
+    state = (struct inflate_state FAR *)strm->state;
+    return ((long)(state->back) << 16) +
+        (state->mode == COPY ? state->length :
+            (state->mode == MATCH ? state->was - state->length : 0));
+}
index 07bd3e78a7c7e763f2dbb939320b01a9897df019..95f4986d400223bad542e5b34a7e6284a039425e 100644 (file)
@@ -1,5 +1,5 @@
 /* inflate.h -- internal inflate state definition
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2009 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -32,11 +32,13 @@ typedef enum {
         TYPE,       /* i: waiting for type bits, including last-flag bit */
         TYPEDO,     /* i: same, but skip check to exit inflate on new block */
         STORED,     /* i: waiting for stored size (length and complement) */
+        COPY_,      /* i/o: same as COPY below, but only first time in */
         COPY,       /* i/o: waiting for input or output to copy stored block */
         TABLE,      /* i: waiting for dynamic block table lengths */
         LENLENS,    /* i: waiting for code length code lengths */
         CODELENS,   /* i: waiting for length/lit and distance code lengths */
-            LEN,        /* i: waiting for length/lit code */
+            LEN_,       /* i: same as LEN below, but only first time in */
+            LEN,        /* i: waiting for length/lit/eob code */
             LENEXT,     /* i: waiting for length extra bits */
             DIST,       /* i: waiting for distance code */
             DISTEXT,    /* i: waiting for distance extra bits */
@@ -53,19 +55,21 @@ typedef enum {
 /*
     State transitions between above modes -
 
-    (most modes can go to the BAD or MEM mode -- not shown for clarity)
+    (most modes can go to BAD or MEM on error -- not shown for clarity)
 
     Process header:
-        HEAD -> (gzip) or (zlib)
-        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
-        NAME -> COMMENT -> HCRC -> TYPE
+        HEAD -> (gzip) or (zlib) or (raw)
+        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
+                  HCRC -> TYPE
         (zlib) -> DICTID or TYPE
         DICTID -> DICT -> TYPE
+        (raw) -> TYPEDO
     Read deflate blocks:
-            TYPE -> STORED or TABLE or LEN or CHECK
-            STORED -> COPY -> TYPE
-            TABLE -> LENLENS -> CODELENS -> LEN
-    Read deflate codes:
+            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
+            STORED -> COPY_ -> COPY -> TYPE
+            TABLE -> LENLENS -> CODELENS -> LEN_
+            LEN_ -> LEN
+    Read deflate codes in fixed or dynamic block:
                 LEN -> LENEXT or LIT or TYPE
                 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
                 LIT -> LEN
@@ -73,7 +77,7 @@ typedef enum {
         CHECK -> LENGTH -> DONE
  */
 
-/* state maintained between inflate() calls.  Approximately 7K bytes. */
+/* state maintained between inflate() calls.  Approximately 10K bytes. */
 struct inflate_state {
     inflate_mode mode;          /* current inflate mode */
     int last;                   /* true if processing last block */
@@ -88,7 +92,7 @@ struct inflate_state {
     unsigned wbits;             /* log base 2 of requested window size */
     unsigned wsize;             /* window size or zero if not using window */
     unsigned whave;             /* valid bytes in the window */
-    unsigned write;             /* window write index */
+    unsigned wnext;             /* window write index */
     unsigned char FAR *window;  /* allocated sliding window, if needed */
         /* bit accumulator */
     unsigned long hold;         /* input bit accumulator */
@@ -112,4 +116,7 @@ struct inflate_state {
     unsigned short lens[320];   /* temporary storage for code lengths */
     unsigned short work[288];   /* work area for code table building */
     code codes[ENOUGH];         /* space for code tables */
+    int sane;                   /* if false, allow invalid distance too far */
+    int back;                   /* bits back of last unprocessed length/lit */
+    unsigned was;               /* initial length of match */
 };
index 8a9c13ff03d874f03d0ad132c5ab19cfe3a53add..44d89cf24e1c2aa20c3b4f0e68bff0d15ae3a5d7 100644 (file)
@@ -1,5 +1,5 @@
 /* inftrees.c -- generate Huffman trees for efficient decoding
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2013 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -9,7 +9,7 @@
 #define MAXBITS 15
 
 const char inflate_copyright[] =
-   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
+   " inflate 1.2.8 Copyright 1995-2013 Mark Adler ";
 /*
   If you use the zlib library in a product, an acknowledgment is welcome
   in the documentation of your product. If for some reason you cannot
@@ -29,7 +29,7 @@ const char inflate_copyright[] =
    table index bits.  It will differ if the request is greater than the
    longest code or if it is less than the shortest code.
  */
-int inflate_table(type, lens, codes, table, bits, work)
+int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
 codetype type;
 unsigned short FAR *lens;
 unsigned codes;
@@ -50,7 +50,7 @@ unsigned short FAR *work;
     unsigned fill;              /* index for replicating entries */
     unsigned low;               /* low bits for current root entry */
     unsigned mask;              /* mask for low root bits */
-    code this;                  /* table entry for duplication */
+    code here;                  /* table entry for duplication */
     code FAR *next;             /* next available space in table */
     const unsigned short FAR *base;     /* base value table to use */
     const unsigned short FAR *extra;    /* extra bits table to use */
@@ -62,7 +62,7 @@ unsigned short FAR *work;
         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
     static const unsigned short lext[31] = { /* Length codes 257..285 extra */
         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
-        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
+        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
     static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
@@ -115,15 +115,15 @@ unsigned short FAR *work;
         if (count[max] != 0) break;
     if (root > max) root = max;
     if (max == 0) {                     /* no symbols to code at all */
-        this.op = (unsigned char)64;    /* invalid code marker */
-        this.bits = (unsigned char)1;
-        this.val = (unsigned short)0;
-        *(*table)++ = this;             /* make a table to force an error */
-        *(*table)++ = this;
+        here.op = (unsigned char)64;    /* invalid code marker */
+        here.bits = (unsigned char)1;
+        here.val = (unsigned short)0;
+        *(*table)++ = here;             /* make a table to force an error */
+        *(*table)++ = here;
         *bits = 1;
         return 0;     /* no symbols, but wait for decoding to report error */
     }
-    for (min = 1; min <= MAXBITS; min++)
+    for (min = 1; min < max; min++)
         if (count[min] != 0) break;
     if (root < min) root = min;
 
@@ -166,11 +166,10 @@ unsigned short FAR *work;
        entered in the tables.
 
        used keeps track of how many table entries have been allocated from the
-       provided *table space.  It is checked when a LENS table is being made
-       against the space in *table, ENOUGH, minus the maximum space needed by
-       the worst case distance code, MAXD.  This should never happen, but the
-       sufficiency of ENOUGH has not been proven exhaustively, hence the check.
-       This assumes that when type == LENS, bits == 9.
+       provided *table space.  It is checked for LENS and DIST tables against
+       the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
+       the initial root table size constants.  See the comments in inftrees.h
+       for more information.
 
        sym increments through all symbols, and the loop terminates when
        all codes of length max, i.e. all codes, have been processed.  This
@@ -209,24 +208,25 @@ unsigned short FAR *work;
     mask = used - 1;            /* mask for comparing low */
 
     /* check available table space */
-    if (type == LENS && used >= ENOUGH - MAXD)
+    if ((type == LENS && used > ENOUGH_LENS) ||
+        (type == DISTS && used > ENOUGH_DISTS))
         return 1;
 
     /* process all codes and make table entries */
     for (;;) {
         /* create table entry */
-        this.bits = (unsigned char)(len - drop);
+        here.bits = (unsigned char)(len - drop);
         if ((int)(work[sym]) < end) {
-            this.op = (unsigned char)0;
-            this.val = work[sym];
+            here.op = (unsigned char)0;
+            here.val = work[sym];
         }
         else if ((int)(work[sym]) > end) {
-            this.op = (unsigned char)(extra[work[sym]]);
-            this.val = base[work[sym]];
+            here.op = (unsigned char)(extra[work[sym]]);
+            here.val = base[work[sym]];
         }
         else {
-            this.op = (unsigned char)(32 + 64);         /* end of block */
-            this.val = 0;
+            here.op = (unsigned char)(32 + 64);         /* end of block */
+            here.val = 0;
         }
 
         /* replicate for those indices with low len bits equal to huff */
@@ -235,7 +235,7 @@ unsigned short FAR *work;
         min = fill;                 /* save offset to next table */
         do {
             fill -= incr;
-            next[(huff >> drop) + fill] = this;
+            next[(huff >> drop) + fill] = here;
         } while (fill != 0);
 
         /* backwards increment the len-bit code huff */
@@ -277,7 +277,8 @@ unsigned short FAR *work;
 
             /* check for enough space */
             used += 1U << curr;
-            if (type == LENS && used >= ENOUGH - MAXD)
+            if ((type == LENS && used > ENOUGH_LENS) ||
+                (type == DISTS && used > ENOUGH_DISTS))
                 return 1;
 
             /* point entry in root table to sub-table */
@@ -288,38 +289,14 @@ unsigned short FAR *work;
         }
     }
 
-    /*
-       Fill in rest of table for incomplete codes.  This loop is similar to the
-       loop above in incrementing huff for table indices.  It is assumed that
-       len is equal to curr + drop, so there is no loop needed to increment
-       through high index bits.  When the current sub-table is filled, the loop
-       drops back to the root table to fill in any remaining entries there.
-     */
-    this.op = (unsigned char)64;                /* invalid code marker */
-    this.bits = (unsigned char)(len - drop);
-    this.val = (unsigned short)0;
-    while (huff != 0) {
-        /* when done with sub-table, drop back to root table */
-        if (drop != 0 && (huff & mask) != low) {
-            drop = 0;
-            len = root;
-            next = *table;
-            this.bits = (unsigned char)len;
-        }
-
-        /* put invalid code marker in table */
-        next[huff >> drop] = this;
-
-        /* backwards increment the len-bit code huff */
-        incr = 1U << (len - 1);
-        while (huff & incr)
-            incr >>= 1;
-        if (incr != 0) {
-            huff &= incr - 1;
-            huff += incr;
-        }
-        else
-            huff = 0;
+    /* fill in remaining table entry if code is incomplete (guaranteed to have
+       at most one remaining entry, since if the code is incomplete, the
+       maximum code length that was allowed to get this far is one bit) */
+    if (huff != 0) {
+        here.op = (unsigned char)64;            /* invalid code marker */
+        here.bits = (unsigned char)(len - drop);
+        here.val = (unsigned short)0;
+        next[huff] = here;
     }
 
     /* set return parameters */
index b1104c87e76907a2105183fa8a904f01de8356c6..baa53a0b1a199ce6ea4c3f99d0306502ab4fab2c 100644 (file)
@@ -1,5 +1,5 @@
 /* inftrees.h -- header to use inftrees.c
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2005, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -35,21 +35,28 @@ typedef struct {
     01000000 - invalid code
  */
 
-/* Maximum size of dynamic tree.  The maximum found in a long but non-
-   exhaustive search was 1444 code structures (852 for length/literals
-   and 592 for distances, the latter actually the result of an
-   exhaustive search).  The true maximum is not known, but the value
-   below is more than safe. */
-#define ENOUGH 2048
-#define MAXD 592
+/* Maximum size of the dynamic table.  The maximum number of code structures is
+   1444, which is the sum of 852 for literal/length codes and 592 for distance
+   codes.  These values were found by exhaustive searches using the program
+   examples/enough.c found in the zlib distribtution.  The arguments to that
+   program are the number of symbols, the initial root table size, and the
+   maximum bit length of a code.  "enough 286 9 15" for literal/length codes
+   returns returns 852, and "enough 30 6 15" for distance codes returns 592.
+   The initial root table size (9 or 6) is found in the fifth argument of the
+   inflate_table() calls in inflate.c and infback.c.  If the root table size is
+   changed, then these maximum sizes would be need to be recalculated and
+   updated. */
+#define ENOUGH_LENS 852
+#define ENOUGH_DISTS 592
+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
 
-/* Type of code to build for inftable() */
+/* Type of code to build for inflate_table() */
 typedef enum {
     CODES,
     LENS,
     DISTS
 } codetype;
 
-extern int inflate_table OF((codetype type, unsigned short FAR *lens,
+int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
                              unsigned codes, code FAR * FAR *table,
                              unsigned FAR *bits, unsigned short FAR *work));
index 395e4e16814e5e1d863fdbd38790e1508a8e5dcf..1fd7759ef004c66fd920873efb030eda5d1eafe0 100644 (file)
@@ -1,5 +1,6 @@
 /* trees.c -- output deflated data using Huffman coding
- * Copyright (C) 1995-2005 Jean-loup Gailly
+ * Copyright (C) 1995-2012 Jean-loup Gailly
+ * detect_data_type() function provided freely by Cosmin Truta, 2006
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -73,11 +74,6 @@ local const uch bl_order[BL_CODES]
  * probability, to avoid transmitting the lengths for unused bit length codes.
  */
 
-#define Buf_size (8 * 2*sizeof(char))
-/* Number of bits used within bi_buf. (bi_buf might be implemented on
- * more than 16 bits on some systems.)
- */
-
 /* ===========================================================================
  * Local data. These are initialized only once.
  */
@@ -150,9 +146,9 @@ local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
 local int  build_bl_tree  OF((deflate_state *s));
 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
                               int blcodes));
-local void compress_block OF((deflate_state *s, ct_data *ltree,
-                              ct_data *dtree));
-local void set_data_type  OF((deflate_state *s));
+local void compress_block OF((deflate_state *s, const ct_data *ltree,
+                              const ct_data *dtree));
+local int  detect_data_type OF((deflate_state *s));
 local unsigned bi_reverse OF((unsigned value, int length));
 local void bi_windup      OF((deflate_state *s));
 local void bi_flush       OF((deflate_state *s));
@@ -203,12 +199,12 @@ local void send_bits(s, value, length)
      * unused bits in value.
      */
     if (s->bi_valid > (int)Buf_size - length) {
-        s->bi_buf |= (value << s->bi_valid);
+        s->bi_buf |= (ush)value << s->bi_valid;
         put_short(s, s->bi_buf);
         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
         s->bi_valid += length - Buf_size;
     } else {
-        s->bi_buf |= value << s->bi_valid;
+        s->bi_buf |= (ush)value << s->bi_valid;
         s->bi_valid += length;
     }
 }
@@ -218,12 +214,12 @@ local void send_bits(s, value, length)
 { int len = length;\
   if (s->bi_valid > (int)Buf_size - len) {\
     int val = value;\
-    s->bi_buf |= (val << s->bi_valid);\
+    s->bi_buf |= (ush)val << s->bi_valid;\
     put_short(s, s->bi_buf);\
     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
     s->bi_valid += len - Buf_size;\
   } else {\
-    s->bi_buf |= (value) << s->bi_valid;\
+    s->bi_buf |= (ush)(value) << s->bi_valid;\
     s->bi_valid += len;\
   }\
 }
@@ -250,11 +246,13 @@ local void tr_static_init()
     if (static_init_done) return;
 
     /* For some embedded targets, global variables are not initialized: */
+#ifdef NO_INIT_GLOBAL_POINTERS
     static_l_desc.static_tree = static_ltree;
     static_l_desc.extra_bits = extra_lbits;
     static_d_desc.static_tree = static_dtree;
     static_d_desc.extra_bits = extra_dbits;
     static_bl_desc.extra_bits = extra_blbits;
+#endif
 
     /* Initialize the mapping length (0..255) -> length code (0..28) */
     length = 0;
@@ -348,13 +346,14 @@ void gen_trees_header()
                 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
     }
 
-    fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
+    fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
     for (i = 0; i < DIST_CODE_LEN; i++) {
         fprintf(header, "%2u%s", _dist_code[i],
                 SEPARATOR(i, DIST_CODE_LEN-1, 20));
     }
 
-    fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
+    fprintf(header,
+        "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
     for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
         fprintf(header, "%2u%s", _length_code[i],
                 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
@@ -379,7 +378,7 @@ void gen_trees_header()
 /* ===========================================================================
  * Initialize the tree data structures for a new zlib stream.
  */
-void _tr_init(s)
+void ZLIB_INTERNAL _tr_init(s)
     deflate_state *s;
 {
     tr_static_init();
@@ -395,7 +394,6 @@ void _tr_init(s)
 
     s->bi_buf = 0;
     s->bi_valid = 0;
-    s->last_eob_len = 8; /* enough lookahead for inflate */
 #ifdef DEBUG
     s->compressed_len = 0L;
     s->bits_sent = 0L;
@@ -864,13 +862,13 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
 /* ===========================================================================
  * Send a stored block
  */
-void _tr_stored_block(s, buf, stored_len, eof)
+void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
     deflate_state *s;
     charf *buf;       /* input block */
     ulg stored_len;   /* length of input block */
-    int eof;          /* true if this is the last block for a file */
+    int last;         /* one if this is the last block for a file */
 {
-    send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
+    send_bits(s, (STORED_BLOCK<<1)+last, 3);    /* send block type */
 #ifdef DEBUG
     s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
     s->compressed_len += (stored_len + 4) << 3;
@@ -878,18 +876,20 @@ void _tr_stored_block(s, buf, stored_len, eof)
     copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
 }
 
+/* ===========================================================================
+ * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
+ */
+void ZLIB_INTERNAL _tr_flush_bits(s)
+    deflate_state *s;
+{
+    bi_flush(s);
+}
+
 /* ===========================================================================
  * Send one empty static block to give enough lookahead for inflate.
  * This takes 10 bits, of which 7 may remain in the bit buffer.
- * The current inflate code requires 9 bits of lookahead. If the
- * last two codes for the previous block (real code plus EOB) were coded
- * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
- * the last real code. In this case we send two empty static blocks instead
- * of one. (There are no problems if the previous block is stored or fixed.)
- * To simplify the code, we assume the worst case of last real code encoded
- * on one bit only.
  */
-void _tr_align(s)
+void ZLIB_INTERNAL _tr_align(s)
     deflate_state *s;
 {
     send_bits(s, STATIC_TREES<<1, 3);
@@ -898,31 +898,17 @@ void _tr_align(s)
     s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
 #endif
     bi_flush(s);
-    /* Of the 10 bits for the empty block, we have already sent
-     * (10 - bi_valid) bits. The lookahead for the last real code (before
-     * the EOB of the previous block) was thus at least one plus the length
-     * of the EOB plus what we have just sent of the empty static block.
-     */
-    if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
-        send_bits(s, STATIC_TREES<<1, 3);
-        send_code(s, END_BLOCK, static_ltree);
-#ifdef DEBUG
-        s->compressed_len += 10L;
-#endif
-        bi_flush(s);
-    }
-    s->last_eob_len = 7;
 }
 
 /* ===========================================================================
  * Determine the best encoding for the current block: dynamic trees, static
  * trees or store, and output the encoded block to the zip file.
  */
-void _tr_flush_block(s, buf, stored_len, eof)
+void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
     deflate_state *s;
     charf *buf;       /* input block, or NULL if too old */
     ulg stored_len;   /* length of input block */
-    int eof;          /* true if this is the last block for a file */
+    int last;         /* one if this is the last block for a file */
 {
     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
     int max_blindex = 0;  /* index of last bit length code of non zero freq */
@@ -931,8 +917,8 @@ void _tr_flush_block(s, buf, stored_len, eof)
     if (s->level > 0) {
 
         /* Check if the file is binary or text */
-        if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
-            set_data_type(s);
+        if (s->strm->data_type == Z_UNKNOWN)
+            s->strm->data_type = detect_data_type(s);
 
         /* Construct the literal and distance trees */
         build_tree(s, (tree_desc *)(&(s->l_desc)));
@@ -978,23 +964,25 @@ void _tr_flush_block(s, buf, stored_len, eof)
          * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
          * transform a block into a stored block.
          */
-        _tr_stored_block(s, buf, stored_len, eof);
+        _tr_stored_block(s, buf, stored_len, last);
 
 #ifdef FORCE_STATIC
     } else if (static_lenb >= 0) { /* force static trees */
 #else
     } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
 #endif
-        send_bits(s, (STATIC_TREES<<1)+eof, 3);
-        compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
+        send_bits(s, (STATIC_TREES<<1)+last, 3);
+        compress_block(s, (const ct_data *)static_ltree,
+                       (const ct_data *)static_dtree);
 #ifdef DEBUG
         s->compressed_len += 3 + s->static_len;
 #endif
     } else {
-        send_bits(s, (DYN_TREES<<1)+eof, 3);
+        send_bits(s, (DYN_TREES<<1)+last, 3);
         send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
                        max_blindex+1);
-        compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
+        compress_block(s, (const ct_data *)s->dyn_ltree,
+                       (const ct_data *)s->dyn_dtree);
 #ifdef DEBUG
         s->compressed_len += 3 + s->opt_len;
 #endif
@@ -1005,21 +993,21 @@ void _tr_flush_block(s, buf, stored_len, eof)
      */
     init_block(s);
 
-    if (eof) {
+    if (last) {
         bi_windup(s);
 #ifdef DEBUG
         s->compressed_len += 7;  /* align on byte boundary */
 #endif
     }
     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
-           s->compressed_len-7*eof));
+           s->compressed_len-7*last));
 }
 
 /* ===========================================================================
  * Save the match info and tally the frequency counts. Return true if
  * the current block must be flushed.
  */
-int _tr_tally (s, dist, lc)
+int ZLIB_INTERNAL _tr_tally (s, dist, lc)
     deflate_state *s;
     unsigned dist;  /* distance of matched string */
     unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
@@ -1071,8 +1059,8 @@ int _tr_tally (s, dist, lc)
  */
 local void compress_block(s, ltree, dtree)
     deflate_state *s;
-    ct_data *ltree; /* literal tree */
-    ct_data *dtree; /* distance tree */
+    const ct_data *ltree; /* literal tree */
+    const ct_data *dtree; /* distance tree */
 {
     unsigned dist;      /* distance of matched string */
     int lc;             /* match length or unmatched char (if dist == 0) */
@@ -1114,28 +1102,48 @@ local void compress_block(s, ltree, dtree)
     } while (lx < s->last_lit);
 
     send_code(s, END_BLOCK, ltree);
-    s->last_eob_len = ltree[END_BLOCK].Len;
 }
 
 /* ===========================================================================
- * Set the data type to BINARY or TEXT, using a crude approximation:
- * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
- * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
+ * - TEXT if the two conditions below are satisfied:
+ *    a) There are no non-portable control characters belonging to the
+ *       "black list" (0..6, 14..25, 28..31).
+ *    b) There is at least one printable character belonging to the
+ *       "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
+ * - BINARY otherwise.
+ * - The following partially-portable control characters form a
+ *   "gray list" that is ignored in this detection algorithm:
+ *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  * IN assertion: the fields Freq of dyn_ltree are set.
  */
-local void set_data_type(s)
+local int detect_data_type(s)
     deflate_state *s;
 {
+    /* black_mask is the bit mask of black-listed bytes
+     * set bits 0..6, 14..25, and 28..31
+     * 0xf3ffc07f = binary 11110011111111111100000001111111
+     */
+    unsigned long black_mask = 0xf3ffc07fUL;
     int n;
 
-    for (n = 0; n < 9; n++)
+    /* Check for non-textual ("black-listed") bytes. */
+    for (n = 0; n <= 31; n++, black_mask >>= 1)
+        if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
+            return Z_BINARY;
+
+    /* Check for textual ("white-listed") bytes. */
+    if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
+            || s->dyn_ltree[13].Freq != 0)
+        return Z_TEXT;
+    for (n = 32; n < LITERALS; n++)
         if (s->dyn_ltree[n].Freq != 0)
-            break;
-    if (n == 9)
-        for (n = 14; n < 32; n++)
-            if (s->dyn_ltree[n].Freq != 0)
-                break;
-    s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
+            return Z_TEXT;
+
+    /* There are no "black-listed" or "white-listed" bytes:
+     * this stream either is empty or has tolerated ("gray-listed") bytes only.
+     */
+    return Z_BINARY;
 }
 
 /* ===========================================================================
@@ -1201,7 +1209,6 @@ local void copy_block(s, buf, len, header)
     int      header;  /* true if block header must be written */
 {
     bi_windup(s);        /* align on byte boundary */
-    s->last_eob_len = 8; /* enough lookahead for inflate */
 
     if (header) {
         put_short(s, (ush)len);
index 72facf900f7787e26fe05781e08ecf26b42fbd3a..d35639d82a27807e49ea35c334f8bbcf64720f82 100644 (file)
@@ -70,7 +70,7 @@ local const ct_data static_dtree[D_CODES] = {
 {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
 };
 
-const uch _dist_code[DIST_CODE_LEN] = {
+const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
  0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,
  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10,
 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
@@ -99,7 +99,7 @@ const uch _dist_code[DIST_CODE_LEN] = {
 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
 };
 
-const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
+const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
  0,  1,  2,  3,  4,  5,  6,  7,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 12, 12,
 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
index 03a9431c8be2b44f2b1159308454a0f9c628597c..9987a775530c0393e7c27b73b1e854b946d5ee47 100644 (file)
@@ -1,5 +1,5 @@
 /* zconf.h -- configuration of the zlib compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2013 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
 /*
  * If you *really* need a unique prefix for all types and library functions,
  * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ * Even better than compiling with -DZ_PREFIX would be to use configure to set
+ * this permanently in zconf.h using "./configure --zprefix".
  */
-#ifdef Z_PREFIX
-#  define deflateInit_          z_deflateInit_
+#ifdef Z_PREFIX     /* may be set to #if 1 by ./configure */
+#  define Z_PREFIX_SET
+
+/* all linked symbols */
+#  define _dist_code            z__dist_code
+#  define _length_code          z__length_code
+#  define _tr_align             z__tr_align
+#  define _tr_flush_bits        z__tr_flush_bits
+#  define _tr_flush_block       z__tr_flush_block
+#  define _tr_init              z__tr_init
+#  define _tr_stored_block      z__tr_stored_block
+#  define _tr_tally             z__tr_tally
+#  define adler32               z_adler32
+#  define adler32_combine       z_adler32_combine
+#  define adler32_combine64     z_adler32_combine64
+#  ifndef Z_SOLO
+#    define compress              z_compress
+#    define compress2             z_compress2
+#    define compressBound         z_compressBound
+#  endif
+#  define crc32                 z_crc32
+#  define crc32_combine         z_crc32_combine
+#  define crc32_combine64       z_crc32_combine64
 #  define deflate               z_deflate
+#  define deflateBound          z_deflateBound
+#  define deflateCopy           z_deflateCopy
 #  define deflateEnd            z_deflateEnd
-#  define inflateInit_          z_inflateInit_
-#  define inflate               z_inflate
-#  define inflateEnd            z_inflateEnd
 #  define deflateInit2_         z_deflateInit2_
-#  define deflateSetDictionary  z_deflateSetDictionary
-#  define deflateCopy           z_deflateCopy
-#  define deflateReset          z_deflateReset
+#  define deflateInit_          z_deflateInit_
 #  define deflateParams         z_deflateParams
-#  define deflateBound          z_deflateBound
+#  define deflatePending        z_deflatePending
 #  define deflatePrime          z_deflatePrime
+#  define deflateReset          z_deflateReset
+#  define deflateResetKeep      z_deflateResetKeep
+#  define deflateSetDictionary  z_deflateSetDictionary
+#  define deflateSetHeader      z_deflateSetHeader
+#  define deflateTune           z_deflateTune
+#  define deflate_copyright     z_deflate_copyright
+#  define get_crc_table         z_get_crc_table
+#  ifndef Z_SOLO
+#    define gz_error              z_gz_error
+#    define gz_intmax             z_gz_intmax
+#    define gz_strwinerror        z_gz_strwinerror
+#    define gzbuffer              z_gzbuffer
+#    define gzclearerr            z_gzclearerr
+#    define gzclose               z_gzclose
+#    define gzclose_r             z_gzclose_r
+#    define gzclose_w             z_gzclose_w
+#    define gzdirect              z_gzdirect
+#    define gzdopen               z_gzdopen
+#    define gzeof                 z_gzeof
+#    define gzerror               z_gzerror
+#    define gzflush               z_gzflush
+#    define gzgetc                z_gzgetc
+#    define gzgetc_               z_gzgetc_
+#    define gzgets                z_gzgets
+#    define gzoffset              z_gzoffset
+#    define gzoffset64            z_gzoffset64
+#    define gzopen                z_gzopen
+#    define gzopen64              z_gzopen64
+#    ifdef _WIN32
+#      define gzopen_w              z_gzopen_w
+#    endif
+#    define gzprintf              z_gzprintf
+#    define gzvprintf             z_gzvprintf
+#    define gzputc                z_gzputc
+#    define gzputs                z_gzputs
+#    define gzread                z_gzread
+#    define gzrewind              z_gzrewind
+#    define gzseek                z_gzseek
+#    define gzseek64              z_gzseek64
+#    define gzsetparams           z_gzsetparams
+#    define gztell                z_gztell
+#    define gztell64              z_gztell64
+#    define gzungetc              z_gzungetc
+#    define gzwrite               z_gzwrite
+#  endif
+#  define inflate               z_inflate
+#  define inflateBack           z_inflateBack
+#  define inflateBackEnd        z_inflateBackEnd
+#  define inflateBackInit_      z_inflateBackInit_
+#  define inflateCopy           z_inflateCopy
+#  define inflateEnd            z_inflateEnd
+#  define inflateGetHeader      z_inflateGetHeader
 #  define inflateInit2_         z_inflateInit2_
+#  define inflateInit_          z_inflateInit_
+#  define inflateMark           z_inflateMark
+#  define inflatePrime          z_inflatePrime
+#  define inflateReset          z_inflateReset
+#  define inflateReset2         z_inflateReset2
 #  define inflateSetDictionary  z_inflateSetDictionary
+#  define inflateGetDictionary  z_inflateGetDictionary
 #  define inflateSync           z_inflateSync
 #  define inflateSyncPoint      z_inflateSyncPoint
-#  define inflateCopy           z_inflateCopy
-#  define inflateReset          z_inflateReset
-#  define inflateBack           z_inflateBack
-#  define inflateBackEnd        z_inflateBackEnd
-#  define compress              z_compress
-#  define compress2             z_compress2
-#  define compressBound         z_compressBound
-#  define uncompress            z_uncompress
-#  define adler32               z_adler32
-#  define crc32                 z_crc32
-#  define get_crc_table         z_get_crc_table
+#  define inflateUndermine      z_inflateUndermine
+#  define inflateResetKeep      z_inflateResetKeep
+#  define inflate_copyright     z_inflate_copyright
+#  define inflate_fast          z_inflate_fast
+#  define inflate_table         z_inflate_table
+#  ifndef Z_SOLO
+#    define uncompress            z_uncompress
+#  endif
 #  define zError                z_zError
+#  ifndef Z_SOLO
+#    define zcalloc               z_zcalloc
+#    define zcfree                z_zcfree
+#  endif
+#  define zlibCompileFlags      z_zlibCompileFlags
+#  define zlibVersion           z_zlibVersion
 
+/* all zlib typedefs in zlib.h and zconf.h */
+#  define Byte                  z_Byte
+#  define Bytef                 z_Bytef
 #  define alloc_func            z_alloc_func
+#  define charf                 z_charf
 #  define free_func             z_free_func
+#  ifndef Z_SOLO
+#    define gzFile                z_gzFile
+#  endif
+#  define gz_header             z_gz_header
+#  define gz_headerp            z_gz_headerp
 #  define in_func               z_in_func
+#  define intf                  z_intf
 #  define out_func              z_out_func
-#  define Byte                  z_Byte
 #  define uInt                  z_uInt
-#  define uLong                 z_uLong
-#  define Bytef                 z_Bytef
-#  define charf                 z_charf
-#  define intf                  z_intf
 #  define uIntf                 z_uIntf
+#  define uLong                 z_uLong
 #  define uLongf                z_uLongf
-#  define voidpf                z_voidpf
 #  define voidp                 z_voidp
+#  define voidpc                z_voidpc
+#  define voidpf                z_voidpf
+
+/* all zlib structs in zlib.h and zconf.h */
+#  define gz_header_s           z_gz_header_s
+#  define internal_state        z_internal_state
+
 #endif
 
 #if defined(__MSDOS__) && !defined(MSDOS)
 #  endif
 #endif
 
+#if defined(ZLIB_CONST) && !defined(z_const)
+#  define z_const const
+#else
+#  define z_const
+#endif
+
 /* Some Mac compilers merge all .h files incorrectly: */
 #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
 #  define NO_DUMMY_DECL
 #  endif
 #endif
 
+#ifndef Z_ARG /* function prototypes for stdarg */
+#  if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#    define Z_ARG(args)  args
+#  else
+#    define Z_ARG(args)  ()
+#  endif
+#endif
+
 /* The following definitions for FAR are needed only for MSDOS mixed
  * model programming (small or medium model with some far allocations).
  * This was tested only with MSC; for other MSDOS compilers you may have
@@ -284,49 +391,121 @@ typedef uLong FAR uLongf;
    typedef Byte       *voidp;
 #endif
 
-#if 0           /* HAVE_UNISTD_H -- this line is updated by ./configure */
-#  include <sys/types.h> /* for off_t */
-#  include <unistd.h>    /* for SEEK_* and off_t */
-#  ifdef VMS
-#    include <unixio.h>   /* for off_t */
+#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC)
+#  include <limits.h>
+#  if (UINT_MAX == 0xffffffffUL)
+#    define Z_U4 unsigned
+#  elif (ULONG_MAX == 0xffffffffUL)
+#    define Z_U4 unsigned long
+#  elif (USHRT_MAX == 0xffffffffUL)
+#    define Z_U4 unsigned short
+#  endif
+#endif
+
+#ifdef Z_U4
+   typedef Z_U4 z_crc_t;
+#else
+   typedef unsigned long z_crc_t;
+#endif
+
+#ifdef HAVE_UNISTD_H    /* may be set to #if 1 by ./configure */
+#  define Z_HAVE_UNISTD_H
+#endif
+
+#ifdef HAVE_STDARG_H    /* may be set to #if 1 by ./configure */
+#  define Z_HAVE_STDARG_H
+#endif
+
+#ifdef STDC
+#  ifndef Z_SOLO
+#    include <sys/types.h>      /* for off_t */
+#  endif
+#endif
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#  ifndef Z_SOLO
+#    include <stdarg.h>         /* for va_list */
 #  endif
-#  define z_off_t off_t
 #endif
-#ifndef SEEK_SET
+
+#ifdef _WIN32
+#  ifndef Z_SOLO
+#    include <stddef.h>         /* for wchar_t */
+#  endif
+#endif
+
+/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
+ * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
+ * though the former does not conform to the LFS document), but considering
+ * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
+ * equivalently requesting no 64-bit operations
+ */
+#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
+#  undef _LARGEFILE64_SOURCE
+#endif
+
+#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H)
+#  define Z_HAVE_UNISTD_H
+#endif
+#ifndef Z_SOLO
+#  if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
+#    include <unistd.h>         /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
+#    ifdef VMS
+#      include <unixio.h>       /* for off_t */
+#    endif
+#    ifndef z_off_t
+#      define z_off_t off_t
+#    endif
+#  endif
+#endif
+
+#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
+#  define Z_LFS64
+#endif
+
+#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
+#  define Z_LARGE64
+#endif
+
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
+#  define Z_WANT64
+#endif
+
+#if !defined(SEEK_SET) && !defined(Z_SOLO)
 #  define SEEK_SET        0       /* Seek from beginning of file.  */
 #  define SEEK_CUR        1       /* Seek from current position.  */
 #  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
 #endif
+
 #ifndef z_off_t
 #  define z_off_t long
 #endif
 
-#if defined(__OS400__)
-#  define NO_vsnprintf
-#endif
-
-#if defined(__MVS__)
-#  define NO_vsnprintf
-#  ifdef FAR
-#    undef FAR
+#if !defined(_WIN32) && defined(Z_LARGE64)
+#  define z_off64_t off64_t
+#else
+#  if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO)
+#    define z_off64_t __int64
+#  else
+#    define z_off64_t z_off_t
 #  endif
 #endif
 
 /* MVS linker does not support external names larger than 8 bytes */
 #if defined(__MVS__)
-#   pragma map(deflateInit_,"DEIN")
-#   pragma map(deflateInit2_,"DEIN2")
-#   pragma map(deflateEnd,"DEEND")
-#   pragma map(deflateBound,"DEBND")
-#   pragma map(inflateInit_,"ININ")
-#   pragma map(inflateInit2_,"ININ2")
-#   pragma map(inflateEnd,"INEND")
-#   pragma map(inflateSync,"INSY")
-#   pragma map(inflateSetDictionary,"INSEDI")
-#   pragma map(compressBound,"CMBND")
-#   pragma map(inflate_table,"INTABL")
-#   pragma map(inflate_fast,"INFA")
-#   pragma map(inflate_copyright,"INCOPY")
+  #pragma map(deflateInit_,"DEIN")
+  #pragma map(deflateInit2_,"DEIN2")
+  #pragma map(deflateEnd,"DEEND")
+  #pragma map(deflateBound,"DEBND")
+  #pragma map(inflateInit_,"ININ")
+  #pragma map(inflateInit2_,"ININ2")
+  #pragma map(inflateEnd,"INEND")
+  #pragma map(inflateSync,"INSY")
+  #pragma map(inflateSetDictionary,"INSEDI")
+  #pragma map(compressBound,"CMBND")
+  #pragma map(inflate_table,"INTABL")
+  #pragma map(inflate_fast,"INFA")
+  #pragma map(inflate_copyright,"INCOPY")
 #endif
 
 #endif /* ZCONF_H */
index 022817927ce3d6b1abe5ac57bff70e7de5291ae0..3e0c7672ac51d93782f020bba32eb1207617e70a 100644 (file)
@@ -1,7 +1,7 @@
 /* zlib.h -- interface of the 'zlib' general purpose compression library
-  version 1.2.3, July 18th, 2005
+  version 1.2.8, April 28th, 2013
 
-  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
+  Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -24,8 +24,8 @@
 
 
   The data format used by the zlib library is described by RFCs (Request for
-  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
-  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
+  Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
+  (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
 */
 
 #ifndef ZLIB_H
 extern "C" {
 #endif
 
-#define ZLIB_VERSION "1.2.3"
-#define ZLIB_VERNUM 0x1230
+#define ZLIB_VERSION "1.2.8"
+#define ZLIB_VERNUM 0x1280
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 8
+#define ZLIB_VER_SUBREVISION 0
 
 /*
-     The 'zlib' compression library provides in-memory compression and
-  decompression functions, including integrity checks of the uncompressed
-  data.  This version of the library supports only one compression method
-  (deflation) but other algorithms will be added later and will have the same
-  stream interface.
-
-     Compression can be done in a single step if the buffers are large
-  enough (for example if an input file is mmap'ed), or can be done by
-  repeated calls of the compression function.  In the latter case, the
-  application must provide more input and/or consume the output
+    The 'zlib' compression library provides in-memory compression and
+  decompression functions, including integrity checks of the uncompressed data.
+  This version of the library supports only one compression method (deflation)
+  but other algorithms will be added later and will have the same stream
+  interface.
+
+    Compression can be done in a single step if the buffers are large enough,
+  or can be done by repeated calls of the compression function.  In the latter
+  case, the application must provide more input and/or consume the output
   (providing more output space) before each call.
 
-     The compressed data format used by default by the in-memory functions is
+    The compressed data format used by default by the in-memory functions is
   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
   around a deflate stream, which is itself documented in RFC 1951.
 
-     The library also supports reading and writing files in gzip (.gz) format
+    The library also supports reading and writing files in gzip (.gz) format
   with an interface similar to that of stdio using the functions that start
   with "gz".  The gzip format is different from the zlib format.  gzip is a
   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
 
-     This library can optionally read and write gzip streams in memory as well.
+    This library can optionally read and write gzip streams in memory as well.
 
-     The zlib format was designed to be compact and fast for use in memory
+    The zlib format was designed to be compact and fast for use in memory
   and on communications channels.  The gzip format was designed for single-
   file compression on file systems, has a larger header than zlib to maintain
   directory information, and uses a different, slower check method than zlib.
 
-     The library does not install any signal handler. The decoder checks
-  the consistency of the compressed data, so the library should never
-  crash even in case of corrupted input.
+    The library does not install any signal handler.  The decoder checks
+  the consistency of the compressed data, so the library should never crash
+  even in case of corrupted input.
 */
 
 typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
@@ -80,15 +83,15 @@ typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
 struct internal_state;
 
 typedef struct z_stream_s {
-    Bytef    *next_in;  /* next input byte */
+    z_const Bytef *next_in;     /* next input byte */
     uInt     avail_in;  /* number of bytes available at next_in */
-    uLong    total_in;  /* total nb of input bytes read so far */
+    uLong    total_in;  /* total number of input bytes read so far */
 
     Bytef    *next_out; /* next output byte should be put there */
     uInt     avail_out; /* remaining free space at next_out */
-    uLong    total_out; /* total nb of bytes output so far */
+    uLong    total_out; /* total number of bytes output so far */
 
-    char     *msg;      /* last error message, NULL if no error */
+    z_const char *msg;  /* last error message, NULL if no error */
     struct internal_state FAR *state; /* not visible by applications */
 
     alloc_func zalloc;  /* used to allocate the internal state */
@@ -126,45 +129,45 @@ typedef struct gz_header_s {
 typedef gz_header FAR *gz_headerp;
 
 /*
-   The application must update next_in and avail_in when avail_in has
-   dropped to zero. It must update next_out and avail_out when avail_out
-   has dropped to zero. The application must initialize zalloc, zfree and
-   opaque before calling the init function. All other fields are set by the
-   compression library and must not be updated by the application.
-
-   The opaque value provided by the application will be passed as the first
-   parameter for calls of zalloc and zfree. This can be useful for custom
-   memory management. The compression library attaches no meaning to the
+     The application must update next_in and avail_in when avail_in has dropped
+   to zero.  It must update next_out and avail_out when avail_out has dropped
+   to zero.  The application must initialize zalloc, zfree and opaque before
+   calling the init function.  All other fields are set by the compression
+   library and must not be updated by the application.
+
+     The opaque value provided by the application will be passed as the first
+   parameter for calls of zalloc and zfree.  This can be useful for custom
+   memory management.  The compression library attaches no meaning to the
    opaque value.
 
-   zalloc must return Z_NULL if there is not enough memory for the object.
+     zalloc must return Z_NULL if there is not enough memory for the object.
    If zlib is used in a multi-threaded application, zalloc and zfree must be
    thread safe.
 
-   On 16-bit systems, the functions zalloc and zfree must be able to allocate
-   exactly 65536 bytes, but will not be required to allocate more than this
-   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
-   pointers returned by zalloc for objects of exactly 65536 bytes *must*
-   have their offset normalized to zero. The default allocation function
-   provided by this library ensures this (see zutil.c). To reduce memory
-   requirements and avoid any allocation of 64K objects, at the expense of
-   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
-
-   The fields total_in and total_out can be used for statistics or
-   progress reports. After compression, total_in holds the total size of
-   the uncompressed data and may be saved for use in the decompressor
-   (particularly if the decompressor wants to decompress everything in
-   a single step).
+     On 16-bit systems, the functions zalloc and zfree must be able to allocate
+   exactly 65536 bytes, but will not be required to allocate more than this if
+   the symbol MAXSEG_64K is defined (see zconf.h).  WARNING: On MSDOS, pointers
+   returned by zalloc for objects of exactly 65536 bytes *must* have their
+   offset normalized to zero.  The default allocation function provided by this
+   library ensures this (see zutil.c).  To reduce memory requirements and avoid
+   any allocation of 64K objects, at the expense of compression ratio, compile
+   the library with -DMAX_WBITS=14 (see zconf.h).
+
+     The fields total_in and total_out can be used for statistics or progress
+   reports.  After compression, total_in holds the total size of the
+   uncompressed data and may be saved for use in the decompressor (particularly
+   if the decompressor wants to decompress everything in a single step).
 */
 
                         /* constants */
 
 #define Z_NO_FLUSH      0
-#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
+#define Z_PARTIAL_FLUSH 1
 #define Z_SYNC_FLUSH    2
 #define Z_FULL_FLUSH    3
 #define Z_FINISH        4
 #define Z_BLOCK         5
+#define Z_TREES         6
 /* Allowed flush values; see deflate() and inflate() below for details */
 
 #define Z_OK            0
@@ -176,8 +179,8 @@ typedef gz_header FAR *gz_headerp;
 #define Z_MEM_ERROR    (-4)
 #define Z_BUF_ERROR    (-5)
 #define Z_VERSION_ERROR (-6)
-/* Return codes for the compression/decompression functions. Negative
- * values are errors, positive values are used for special but normal events.
+/* Return codes for the compression/decompression functions. Negative values
+ * are errors, positive values are used for special but normal events.
  */
 
 #define Z_NO_COMPRESSION         0
@@ -207,119 +210,141 @@ typedef gz_header FAR *gz_headerp;
 #define zlib_version zlibVersion()
 /* for compatibility with versions < 1.0.2 */
 
+
                         /* basic functions */
 
 ZEXTERN const char * ZEXPORT zlibVersion OF((void));
 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
-   If the first character differs, the library code actually used is
-   not compatible with the zlib.h header file used by the application.
-   This check is automatically made by deflateInit and inflateInit.
+   If the first character differs, the library code actually used is not
+   compatible with the zlib.h header file used by the application.  This check
+   is automatically made by deflateInit and inflateInit.
  */
 
 /*
 ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
 
-     Initializes the internal stream state for compression. The fields
-   zalloc, zfree and opaque must be initialized before by the caller.
-   If zalloc and zfree are set to Z_NULL, deflateInit updates them to
-   use default allocation functions.
+     Initializes the internal stream state for compression.  The fields
+   zalloc, zfree and opaque must be initialized before by the caller.  If
+   zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
+   allocation functions.
 
      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
-   1 gives best speed, 9 gives best compression, 0 gives no compression at
-   all (the input data is simply copied a block at a time).
-   Z_DEFAULT_COMPRESSION requests a default compromise between speed and
-   compression (currently equivalent to level 6).
+   1 gives best speed, 9 gives best compression, 0 gives no compression at all
+   (the input data is simply copied a block at a time).  Z_DEFAULT_COMPRESSION
+   requests a default compromise between speed and compression (currently
+   equivalent to level 6).
 
-     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
-   enough memory, Z_STREAM_ERROR if level is not a valid compression level,
+     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if level is not a valid compression level, or
    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
-   with the version assumed by the caller (ZLIB_VERSION).
-   msg is set to null if there is no error message.  deflateInit does not
-   perform any compression: this will be done by deflate().
+   with the version assumed by the caller (ZLIB_VERSION).  msg is set to null
+   if there is no error message.  deflateInit does not perform any compression:
+   this will be done by deflate().
 */
 
 
 ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
 /*
     deflate compresses as much data as possible, and stops when the input
-  buffer becomes empty or the output buffer becomes full. It may introduce some
-  output latency (reading input without producing any output) except when
+  buffer becomes empty or the output buffer becomes full.  It may introduce
+  some output latency (reading input without producing any output) except when
   forced to flush.
 
-    The detailed semantics are as follows. deflate performs one or both of the
+    The detailed semantics are as follows.  deflate performs one or both of the
   following actions:
 
   - Compress more input starting at next_in and update next_in and avail_in
-    accordingly. If not all input can be processed (because there is not
+    accordingly.  If not all input can be processed (because there is not
     enough room in the output buffer), next_in and avail_in are updated and
     processing will resume at this point for the next call of deflate().
 
   - Provide more output starting at next_out and update next_out and avail_out
-    accordingly. This action is forced if the parameter flush is non zero.
+    accordingly.  This action is forced if the parameter flush is non zero.
     Forcing flush frequently degrades the compression ratio, so this parameter
-    should be set only when necessary (in interactive applications).
-    Some output may be provided even if flush is not set.
-
-  Before the call of deflate(), the application should ensure that at least
-  one of the actions is possible, by providing more input and/or consuming
-  more output, and updating avail_in or avail_out accordingly; avail_out
-  should never be zero before the call. The application can consume the
-  compressed output when it wants, for example when the output buffer is full
-  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
-  and with zero avail_out, it must be called again after making room in the
-  output buffer because there might be more output pending.
+    should be set only when necessary (in interactive applications).  Some
+    output may be provided even if flush is not set.
+
+    Before the call of deflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming more
+  output, and updating avail_in or avail_out accordingly; avail_out should
+  never be zero before the call.  The application can consume the compressed
+  output when it wants, for example when the output buffer is full (avail_out
+  == 0), or after each call of deflate().  If deflate returns Z_OK and with
+  zero avail_out, it must be called again after making room in the output
+  buffer because there might be more output pending.
 
     Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
-  decide how much data to accumualte before producing output, in order to
+  decide how much data to accumulate before producing output, in order to
   maximize compression.
 
     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
   flushed to the output buffer and the output is aligned on a byte boundary, so
-  that the decompressor can get all input data available so far. (In particular
-  avail_in is zero after the call if enough output space has been provided
-  before the call.)  Flushing may degrade compression for some compression
-  algorithms and so it should be used only when necessary.
+  that the decompressor can get all input data available so far.  (In
+  particular avail_in is zero after the call if enough output space has been
+  provided before the call.) Flushing may degrade compression for some
+  compression algorithms and so it should be used only when necessary.  This
+  completes the current deflate block and follows it with an empty stored block
+  that is three bits plus filler bits to the next byte, followed by four bytes
+  (00 00 ff ff).
+
+    If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
+  output buffer, but the output is not aligned to a byte boundary.  All of the
+  input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
+  This completes the current deflate block and follows it with an empty fixed
+  codes block that is 10 bits long.  This assures that enough bytes are output
+  in order for the decompressor to finish the block before the empty fixed code
+  block.
+
+    If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
+  for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
+  seven bits of the current block are held to be written as the next byte after
+  the next deflate block is completed.  In this case, the decompressor may not
+  be provided enough bits at this point in order to complete decompression of
+  the data provided so far to the compressor.  It may need to wait for the next
+  block to be emitted.  This is for advanced applications that need to control
+  the emission of deflate blocks.
 
     If flush is set to Z_FULL_FLUSH, all output is flushed as with
   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
   restart from this point if previous compressed data has been damaged or if
-  random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+  random access is desired.  Using Z_FULL_FLUSH too often can seriously degrade
   compression.
 
     If deflate returns with avail_out == 0, this function must be called again
   with the same value of the flush parameter and more output space (updated
   avail_out), until the flush is complete (deflate returns with non-zero
-  avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+  avail_out).  In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
   avail_out is greater than six to avoid repeated flush markers due to
   avail_out == 0 on return.
 
     If the parameter flush is set to Z_FINISH, pending input is processed,
-  pending output is flushed and deflate returns with Z_STREAM_END if there
-  was enough output space; if deflate returns with Z_OK, this function must be
+  pending output is flushed and deflate returns with Z_STREAM_END if there was
+  enough output space; if deflate returns with Z_OK, this function must be
   called again with Z_FINISH and more output space (updated avail_out) but no
-  more input data, until it returns with Z_STREAM_END or an error. After
-  deflate has returned Z_STREAM_END, the only possible operations on the
-  stream are deflateReset or deflateEnd.
+  more input data, until it returns with Z_STREAM_END or an error.  After
+  deflate has returned Z_STREAM_END, the only possible operations on the stream
+  are deflateReset or deflateEnd.
 
     Z_FINISH can be used immediately after deflateInit if all the compression
-  is to be done in a single step. In this case, avail_out must be at least
-  the value returned by deflateBound (see below). If deflate does not return
-  Z_STREAM_END, then it must be called again as described above.
+  is to be done in a single step.  In this case, avail_out must be at least the
+  value returned by deflateBound (see below).  Then deflate is guaranteed to
+  return Z_STREAM_END.  If not enough output space is provided, deflate will
+  not return Z_STREAM_END, and it must be called again as described above.
 
     deflate() sets strm->adler to the adler32 checksum of all input read
   so far (that is, total_in bytes).
 
     deflate() may update strm->data_type if it can make a good guess about
-  the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
-  binary. This field is only for information purposes and does not affect
-  the compression algorithm in any manner.
+  the input data type (Z_BINARY or Z_TEXT).  In doubt, the data is considered
+  binary.  This field is only for information purposes and does not affect the
+  compression algorithm in any manner.
 
     deflate() returns Z_OK if some progress has been made (more input
   processed or more output produced), Z_STREAM_END if all input has been
   consumed and all output has been produced (only when flush is set to
   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
-  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
-  (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
+  if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
+  (for example avail_in or avail_out was zero).  Note that Z_BUF_ERROR is not
   fatal, and deflate() can be called again with more input and more output
   space to continue compressing.
 */
@@ -328,13 +353,13 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
 ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
 /*
      All dynamically allocated data structures for this stream are freed.
-   This function discards any unprocessed input and does not flush any
-   pending output.
+   This function discards any unprocessed input and does not flush any pending
+   output.
 
      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
-   prematurely (some input or output was discarded). In the error case,
-   msg may be set but then points to a static string (which must not be
+   prematurely (some input or output was discarded).  In the error case, msg
+   may be set but then points to a static string (which must not be
    deallocated).
 */
 
@@ -342,10 +367,10 @@ ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
 /*
 ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
 
-     Initializes the internal stream state for decompression. The fields
+     Initializes the internal stream state for decompression.  The fields
    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
-   the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
-   value depends on the compression method), inflateInit determines the
+   the caller.  If next_in is not Z_NULL and avail_in is large enough (the
+   exact value depends on the compression method), inflateInit determines the
    compression method from the zlib header and allocates all data structures
    accordingly; otherwise the allocation will be deferred to the first call of
    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
@@ -353,95 +378,116 @@ ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
 
      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
-   version assumed by the caller.  msg is set to null if there is no error
-   message. inflateInit does not perform any decompression apart from reading
-   the zlib header if present: this will be done by inflate().  (So next_in and
-   avail_in may be modified, but next_out and avail_out are unchanged.)
+   version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+   invalid, such as a null pointer to the structure.  msg is set to null if
+   there is no error message.  inflateInit does not perform any decompression
+   apart from possibly reading the zlib header if present: actual decompression
+   will be done by inflate().  (So next_in and avail_in may be modified, but
+   next_out and avail_out are unused and unchanged.) The current implementation
+   of inflateInit() does not process any header information -- that is deferred
+   until inflate() is called.
 */
 
 
 ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
 /*
     inflate decompresses as much data as possible, and stops when the input
-  buffer becomes empty or the output buffer becomes full. It may introduce
+  buffer becomes empty or the output buffer becomes full.  It may introduce
   some output latency (reading input without producing any output) except when
   forced to flush.
 
-  The detailed semantics are as follows. inflate performs one or both of the
+  The detailed semantics are as follows.  inflate performs one or both of the
   following actions:
 
   - Decompress more input starting at next_in and update next_in and avail_in
-    accordingly. If not all input can be processed (because there is not
-    enough room in the output buffer), next_in is updated and processing
-    will resume at this point for the next call of inflate().
+    accordingly.  If not all input can be processed (because there is not
+    enough room in the output buffer), next_in is updated and processing will
+    resume at this point for the next call of inflate().
 
   - Provide more output starting at next_out and update next_out and avail_out
-    accordingly.  inflate() provides as much output as possible, until there
-    is no more input data or no more space in the output buffer (see below
-    about the flush parameter).
-
-  Before the call of inflate(), the application should ensure that at least
-  one of the actions is possible, by providing more input and/or consuming
-  more output, and updating the next_* and avail_* values accordingly.
-  The application can consume the uncompressed output when it wants, for
-  example when the output buffer is full (avail_out == 0), or after each
-  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
-  must be called again after making room in the output buffer because there
-  might be more output pending.
-
-    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
-  Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
-  output as possible to the output buffer. Z_BLOCK requests that inflate() stop
-  if and when it gets to the next deflate block boundary. When decoding the
-  zlib or gzip format, this will cause inflate() to return immediately after
-  the header and before the first block. When doing a raw inflate, inflate()
-  will go ahead and process the first block, and will return when it gets to
-  the end of that block, or when it runs out of data.
+    accordingly.  inflate() provides as much output as possible, until there is
+    no more input data or no more space in the output buffer (see below about
+    the flush parameter).
+
+    Before the call of inflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming more
+  output, and updating the next_* and avail_* values accordingly.  The
+  application can consume the uncompressed output when it wants, for example
+  when the output buffer is full (avail_out == 0), or after each call of
+  inflate().  If inflate returns Z_OK and with zero avail_out, it must be
+  called again after making room in the output buffer because there might be
+  more output pending.
+
+    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
+  Z_BLOCK, or Z_TREES.  Z_SYNC_FLUSH requests that inflate() flush as much
+  output as possible to the output buffer.  Z_BLOCK requests that inflate()
+  stop if and when it gets to the next deflate block boundary.  When decoding
+  the zlib or gzip format, this will cause inflate() to return immediately
+  after the header and before the first block.  When doing a raw inflate,
+  inflate() will go ahead and process the first block, and will return when it
+  gets to the end of that block, or when it runs out of data.
 
     The Z_BLOCK option assists in appending to or combining deflate streams.
   Also to assist in this, on return inflate() will set strm->data_type to the
-  number of unused bits in the last byte taken from strm->next_in, plus 64
-  if inflate() is currently decoding the last block in the deflate stream,
-  plus 128 if inflate() returned immediately after decoding an end-of-block
-  code or decoding the complete header up to just before the first byte of the
-  deflate stream. The end-of-block will not be indicated until all of the
-  uncompressed data from that block has been written to strm->next_out.  The
-  number of unused bits may in general be greater than seven, except when
-  bit 7 of data_type is set, in which case the number of unused bits will be
-  less than eight.
+  number of unused bits in the last byte taken from strm->next_in, plus 64 if
+  inflate() is currently decoding the last block in the deflate stream, plus
+  128 if inflate() returned immediately after decoding an end-of-block code or
+  decoding the complete header up to just before the first byte of the deflate
+  stream.  The end-of-block will not be indicated until all of the uncompressed
+  data from that block has been written to strm->next_out.  The number of
+  unused bits may in general be greater than seven, except when bit 7 of
+  data_type is set, in which case the number of unused bits will be less than
+  eight.  data_type is set as noted here every time inflate() returns for all
+  flush options, and so can be used to determine the amount of currently
+  consumed input in bits.
+
+    The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
+  end of each deflate block header is reached, before any actual data in that
+  block is decoded.  This allows the caller to determine the length of the
+  deflate block header for later use in random access within a deflate block.
+  256 is added to the value of strm->data_type when inflate() returns
+  immediately after reaching the end of the deflate block header.
 
     inflate() should normally be called until it returns Z_STREAM_END or an
-  error. However if all decompression is to be performed in a single step
-  (a single call of inflate), the parameter flush should be set to
-  Z_FINISH. In this case all pending input is processed and all pending
-  output is flushed; avail_out must be large enough to hold all the
-  uncompressed data. (The size of the uncompressed data may have been saved
-  by the compressor for this purpose.) The next operation on this stream must
-  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
-  is never required, but can be used to inform inflate that a faster approach
-  may be used for the single inflate() call.
+  error.  However if all decompression is to be performed in a single step (a
+  single call of inflate), the parameter flush should be set to Z_FINISH.  In
+  this case all pending input is processed and all pending output is flushed;
+  avail_out must be large enough to hold all of the uncompressed data for the
+  operation to complete.  (The size of the uncompressed data may have been
+  saved by the compressor for this purpose.) The use of Z_FINISH is not
+  required to perform an inflation in one step.  However it may be used to
+  inform inflate that a faster approach can be used for the single inflate()
+  call.  Z_FINISH also informs inflate to not maintain a sliding window if the
+  stream completes, which reduces inflate's memory footprint.  If the stream
+  does not complete, either because not all of the stream is provided or not
+  enough output space is provided, then a sliding window will be allocated and
+  inflate() can be called again to continue the operation as if Z_NO_FLUSH had
+  been used.
 
      In this implementation, inflate() always flushes as much output as
   possible to the output buffer, and always uses the faster approach on the
-  first call. So the only effect of the flush parameter in this implementation
-  is on the return value of inflate(), as noted below, or when it returns early
-  because Z_BLOCK is used.
+  first call.  So the effects of the flush parameter in this implementation are
+  on the return value of inflate() as noted below, when inflate() returns early
+  when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
+  memory for a sliding window when Z_FINISH is used.
 
      If a preset dictionary is needed after this call (see inflateSetDictionary
-  below), inflate sets strm->adler to the adler32 checksum of the dictionary
+  below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
-  strm->adler to the adler32 checksum of all output produced so far (that is,
+  strm->adler to the Adler-32 checksum of all output produced so far (that is,
   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
-  below. At the end of the stream, inflate() checks that its computed adler32
+  below.  At the end of the stream, inflate() checks that its computed adler32
   checksum is equal to that saved by the compressor and returns Z_STREAM_END
   only if the checksum is correct.
 
-    inflate() will decompress and check either zlib-wrapped or gzip-wrapped
-  deflate data.  The header type is detected automatically.  Any information
-  contained in the gzip header is not retained, so applications that need that
-  information should instead use raw inflate, see inflateInit2() below, or
-  inflateBack() and perform their own processing of the gzip header and
-  trailer.
+    inflate() can decompress and check either zlib-wrapped or gzip-wrapped
+  deflate data.  The header type is detected automatically, if requested when
+  initializing with inflateInit2().  Any information contained in the gzip
+  header is not retained, so applications that need that information should
+  instead use raw inflate, see inflateInit2() below, or inflateBack() and
+  perform their own processing of the gzip header and trailer.  When processing
+  gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
+  producted so far.  The CRC-32 is checked against the gzip trailer.
 
     inflate() returns Z_OK if some progress has been made (more input processed
   or more output produced), Z_STREAM_END if the end of the compressed data has
@@ -449,27 +495,28 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
   corrupted (input stream not conforming to the zlib format or incorrect check
   value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
-  if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
+  next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
   Z_BUF_ERROR if no progress is possible or if there was not enough room in the
-  output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+  output buffer when Z_FINISH is used.  Note that Z_BUF_ERROR is not fatal, and
   inflate() can be called again with more input and more output space to
-  continue decompressing. If Z_DATA_ERROR is returned, the application may then
-  call inflateSync() to look for a good compression block if a partial recovery
-  of the data is desired.
+  continue decompressing.  If Z_DATA_ERROR is returned, the application may
+  then call inflateSync() to look for a good compression block if a partial
+  recovery of the data is desired.
 */
 
 
 ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
 /*
      All dynamically allocated data structures for this stream are freed.
-   This function discards any unprocessed input and does not flush any
-   pending output.
+   This function discards any unprocessed input and does not flush any pending
+   output.
 
      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
-   was inconsistent. In the error case, msg may be set but then points to a
+   was inconsistent.  In the error case, msg may be set but then points to a
    static string (which must not be deallocated).
 */
 
+
                         /* Advanced functions */
 
 /*
@@ -484,55 +531,57 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
                                      int  memLevel,
                                      int  strategy));
 
-     This is another version of deflateInit with more compression options. The
-   fields next_in, zalloc, zfree and opaque must be initialized before by
-   the caller.
+     This is another version of deflateInit with more compression options.  The
+   fields next_in, zalloc, zfree and opaque must be initialized before by the
+   caller.
 
-     The method parameter is the compression method. It must be Z_DEFLATED in
+     The method parameter is the compression method.  It must be Z_DEFLATED in
    this version of the library.
 
      The windowBits parameter is the base two logarithm of the window size
-   (the size of the history buffer). It should be in the range 8..15 for this
-   version of the library. Larger values of this parameter result in better
-   compression at the expense of memory usage. The default value is 15 if
+   (the size of the history buffer).  It should be in the range 8..15 for this
+   version of the library.  Larger values of this parameter result in better
+   compression at the expense of memory usage.  The default value is 15 if
    deflateInit is used instead.
 
-     windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
-   determines the window size. deflate() will then generate raw deflate data
+     windowBits can also be -8..-15 for raw deflate.  In this case, -windowBits
+   determines the window size.  deflate() will then generate raw deflate data
    with no zlib header or trailer, and will not compute an adler32 check value.
 
-     windowBits can also be greater than 15 for optional gzip encoding. Add
+     windowBits can also be greater than 15 for optional gzip encoding.  Add
    16 to windowBits to write a simple gzip header and trailer around the
-   compressed data instead of a zlib wrapper. The gzip header will have no
-   file name, no extra data, no comment, no modification time (set to zero),
-   no header crc, and the operating system will be set to 255 (unknown).  If a
+   compressed data instead of a zlib wrapper.  The gzip header will have no
+   file name, no extra data, no comment, no modification time (set to zero), no
+   header crc, and the operating system will be set to 255 (unknown).  If a
    gzip stream is being written, strm->adler is a crc32 instead of an adler32.
 
      The memLevel parameter specifies how much memory should be allocated
-   for the internal compression state. memLevel=1 uses minimum memory but
-   is slow and reduces compression ratio; memLevel=9 uses maximum memory
-   for optimal speed. The default value is 8. See zconf.h for total memory
-   usage as a function of windowBits and memLevel.
+   for the internal compression state.  memLevel=1 uses minimum memory but is
+   slow and reduces compression ratio; memLevel=9 uses maximum memory for
+   optimal speed.  The default value is 8.  See zconf.h for total memory usage
+   as a function of windowBits and memLevel.
 
-     The strategy parameter is used to tune the compression algorithm. Use the
+     The strategy parameter is used to tune the compression algorithm.  Use the
    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
    filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
    string match), or Z_RLE to limit match distances to one (run-length
-   encoding). Filtered data consists mostly of small values with a somewhat
-   random distribution. In this case, the compression algorithm is tuned to
-   compress them better. The effect of Z_FILTERED is to force more Huffman
+   encoding).  Filtered data consists mostly of small values with a somewhat
+   random distribution.  In this case, the compression algorithm is tuned to
+   compress them better.  The effect of Z_FILTERED is to force more Huffman
    coding and less string matching; it is somewhat intermediate between
-   Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
-   Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
-   parameter only affects the compression ratio but not the correctness of the
-   compressed output even if it is not set appropriately.  Z_FIXED prevents the
-   use of dynamic Huffman codes, allowing for a simpler decoder for special
-   applications.
-
-      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
-   memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
-   method). msg is set to null if there is no error message.  deflateInit2 does
-   not perform any compression: this will be done by deflate().
+   Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY.  Z_RLE is designed to be almost as
+   fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data.  The
+   strategy parameter only affects the compression ratio but not the
+   correctness of the compressed output even if it is not set appropriately.
+   Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
+   decoder for special applications.
+
+     deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
+   method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
+   incompatible with the version assumed by the caller (ZLIB_VERSION).  msg is
+   set to null if there is no error message.  deflateInit2 does not perform any
+   compression: this will be done by deflate().
 */
 
 ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
@@ -540,38 +589,43 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
                                              uInt  dictLength));
 /*
      Initializes the compression dictionary from the given byte sequence
-   without producing any compressed output. This function must be called
-   immediately after deflateInit, deflateInit2 or deflateReset, before any
-   call of deflate. The compressor and decompressor must use exactly the same
-   dictionary (see inflateSetDictionary).
+   without producing any compressed output.  When using the zlib format, this
+   function must be called immediately after deflateInit, deflateInit2 or
+   deflateReset, and before any call of deflate.  When doing raw deflate, this
+   function must be called either before any call of deflate, or immediately
+   after the completion of a deflate block, i.e. after all input has been
+   consumed and all output has been delivered when using any of the flush
+   options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH.  The
+   compressor and decompressor must use exactly the same dictionary (see
+   inflateSetDictionary).
 
      The dictionary should consist of strings (byte sequences) that are likely
    to be encountered later in the data to be compressed, with the most commonly
-   used strings preferably put towards the end of the dictionary. Using a
+   used strings preferably put towards the end of the dictionary.  Using a
    dictionary is most useful when the data to be compressed is short and can be
    predicted with good accuracy; the data can then be compressed better than
    with the default empty dictionary.
 
      Depending on the size of the compression data structures selected by
    deflateInit or deflateInit2, a part of the dictionary may in effect be
-   discarded, for example if the dictionary is larger than the window size in
-   deflate or deflate2. Thus the strings most likely to be useful should be
-   put at the end of the dictionary, not at the front. In addition, the
-   current implementation of deflate will use at most the window size minus
-   262 bytes of the provided dictionary.
+   discarded, for example if the dictionary is larger than the window size
+   provided in deflateInit or deflateInit2.  Thus the strings most likely to be
+   useful should be put at the end of the dictionary, not at the front.  In
+   addition, the current implementation of deflate will use at most the window
+   size minus 262 bytes of the provided dictionary.
 
      Upon return of this function, strm->adler is set to the adler32 value
    of the dictionary; the decompressor may later use this value to determine
-   which dictionary has been used by the compressor. (The adler32 value
+   which dictionary has been used by the compressor.  (The adler32 value
    applies to the whole dictionary even if only a subset of the dictionary is
    actually used by the compressor.) If a raw deflate was requested, then the
    adler32 value is not computed and strm->adler is not set.
 
      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
-   parameter is invalid (such as NULL dictionary) or the stream state is
+   parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
    inconsistent (for example if deflate has already been called for this stream
-   or if the compression method is bsort). deflateSetDictionary does not
-   perform any compression: this will be done by deflate().
+   or if not at a block boundary for raw deflate).  deflateSetDictionary does
+   not perform any compression: this will be done by deflate().
 */
 
 ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
@@ -581,26 +635,26 @@ ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
 
      This function can be useful when several compression strategies will be
    tried, for example when there are several ways of pre-processing the input
-   data with a filter. The streams that will be discarded should then be freed
+   data with a filter.  The streams that will be discarded should then be freed
    by calling deflateEnd.  Note that deflateCopy duplicates the internal
-   compression state which can be quite large, so this strategy is slow and
-   can consume lots of memory.
+   compression state which can be quite large, so this strategy is slow and can
+   consume lots of memory.
 
      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
-   (such as zalloc being NULL). msg is left unchanged in both source and
+   (such as zalloc being Z_NULL).  msg is left unchanged in both source and
    destination.
 */
 
 ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
 /*
      This function is equivalent to deflateEnd followed by deflateInit,
-   but does not free and reallocate all the internal compression state.
-   The stream will keep the same compression level and any other attributes
-   that may have been set by deflateInit2.
+   but does not free and reallocate all the internal compression state.  The
+   stream will keep the same compression level and any other attributes that
+   may have been set by deflateInit2.
 
-      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
-   stream state was inconsistent (such as zalloc or state being NULL).
+     deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL).
 */
 
 ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
@@ -610,18 +664,18 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
      Dynamically update the compression level and compression strategy.  The
    interpretation of level and strategy is as in deflateInit2.  This can be
    used to switch between compression and straight copy of the input data, or
-   to switch to a different kind of input data requiring a different
-   strategy. If the compression level is changed, the input available so far
-   is compressed with the old level (and may be flushed); the new level will
-   take effect only at the next call of deflate().
+   to switch to a different kind of input data requiring a different strategy.
+   If the compression level is changed, the input available so far is
+   compressed with the old level (and may be flushed); the new level will take
+   effect only at the next call of deflate().
 
      Before the call of deflateParams, the stream state must be set as for
-   a call of deflate(), since the currently available input may have to
-   be compressed and flushed. In particular, strm->avail_out must be non-zero.
+   a call of deflate(), since the currently available input may have to be
+   compressed and flushed.  In particular, strm->avail_out must be non-zero.
 
      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
-   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
-   if strm->avail_out was zero.
+   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if
+   strm->avail_out was zero.
 */
 
 ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
@@ -645,31 +699,53 @@ ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
                                        uLong sourceLen));
 /*
      deflateBound() returns an upper bound on the compressed size after
-   deflation of sourceLen bytes.  It must be called after deflateInit()
-   or deflateInit2().  This would be used to allocate an output buffer
-   for deflation in a single pass, and so would be called before deflate().
+   deflation of sourceLen bytes.  It must be called after deflateInit() or
+   deflateInit2(), and after deflateSetHeader(), if used.  This would be used
+   to allocate an output buffer for deflation in a single pass, and so would be
+   called before deflate().  If that first deflate() call is provided the
+   sourceLen input bytes, an output buffer allocated to the size returned by
+   deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed
+   to return Z_STREAM_END.  Note that it is possible for the compressed size to
+   be larger than the value returned by deflateBound() if flush options other
+   than Z_FINISH or Z_NO_FLUSH are used.
 */
 
+ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm,
+                                       unsigned *pending,
+                                       int *bits));
+/*
+     deflatePending() returns the number of bytes and bits of output that have
+   been generated, but not yet provided in the available output.  The bytes not
+   provided would be due to the available output space having being consumed.
+   The number of bits of output not provided are between 0 and 7, where they
+   await more bits to join them in order to fill out a full byte.  If pending
+   or bits are Z_NULL, then those values are not set.
+
+     deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent.
+ */
+
 ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
                                      int bits,
                                      int value));
 /*
      deflatePrime() inserts bits in the deflate output stream.  The intent
-  is that this function is used to start off the deflate output with the
-  bits leftover from a previous deflate stream when appending to it.  As such,
-  this function can only be used for raw deflate, and must be used before the
-  first deflate() call after a deflateInit2() or deflateReset().  bits must be
-  less than or equal to 16, and that many of the least significant bits of
-  value will be inserted in the output.
-
-      deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
-   stream state was inconsistent.
+   is that this function is used to start off the deflate output with the bits
+   leftover from a previous deflate stream when appending to it.  As such, this
+   function can only be used for raw deflate, and must be used before the first
+   deflate() call after a deflateInit2() or deflateReset().  bits must be less
+   than or equal to 16, and that many of the least significant bits of value
+   will be inserted in the output.
+
+     deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
+   room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
+   source stream state was inconsistent.
 */
 
 ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
                                          gz_headerp head));
 /*
-      deflateSetHeader() provides gzip header information for when a gzip
+     deflateSetHeader() provides gzip header information for when a gzip
    stream is requested by deflateInit2().  deflateSetHeader() may be called
    after deflateInit2() or deflateReset() and before the first call of
    deflate().  The text, time, os, extra field, name, and comment information
@@ -682,11 +758,11 @@ ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
    1.3.x) do not support header crc's, and will report that it is a "multi-part
    gzip file" and give up.
 
-      If deflateSetHeader is not used, the default gzip header has text false,
+     If deflateSetHeader is not used, the default gzip header has text false,
    the time set to zero, and os set to 255, with no extra, name, or comment
    fields.  The gzip header is returned to the default state by deflateReset().
 
-      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+     deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
@@ -694,43 +770,50 @@ ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
 ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
                                      int  windowBits));
 
-     This is another version of inflateInit with an extra parameter. The
+     This is another version of inflateInit with an extra parameter.  The
    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
    before by the caller.
 
      The windowBits parameter is the base two logarithm of the maximum window
    size (the size of the history buffer).  It should be in the range 8..15 for
-   this version of the library. The default value is 15 if inflateInit is used
-   instead. windowBits must be greater than or equal to the windowBits value
+   this version of the library.  The default value is 15 if inflateInit is used
+   instead.  windowBits must be greater than or equal to the windowBits value
    provided to deflateInit2() while compressing, or it must be equal to 15 if
-   deflateInit2() was not used. If a compressed stream with a larger window
+   deflateInit2() was not used.  If a compressed stream with a larger window
    size is given as input, inflate() will return with the error code
    Z_DATA_ERROR instead of trying to allocate a larger window.
 
-     windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
-   determines the window size. inflate() will then process raw deflate data,
+     windowBits can also be zero to request that inflate use the window size in
+   the zlib header of the compressed stream.
+
+     windowBits can also be -8..-15 for raw inflate.  In this case, -windowBits
+   determines the window size.  inflate() will then process raw deflate data,
    not looking for a zlib or gzip header, not generating a check value, and not
-   looking for any check values for comparison at the end of the stream. This
+   looking for any check values for comparison at the end of the stream.  This
    is for use with other formats that use the deflate compressed data format
-   such as zip.  Those formats provide their own check values. If a custom
+   such as zip.  Those formats provide their own check values.  If a custom
    format is developed using the raw deflate format for compressed data, it is
    recommended that a check value such as an adler32 or a crc32 be applied to
    the uncompressed data as is done in the zlib, gzip, and zip formats.  For
-   most applications, the zlib format should be used as is. Note that comments
+   most applications, the zlib format should be used as is.  Note that comments
    above on the use in deflateInit2() applies to the magnitude of windowBits.
 
-     windowBits can also be greater than 15 for optional gzip decoding. Add
+     windowBits can also be greater than 15 for optional gzip decoding.  Add
    32 to windowBits to enable zlib and gzip decoding with automatic header
    detection, or add 16 to decode only the gzip format (the zlib format will
-   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is
-   crc32 instead of an adler32.
+   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is a
+   crc32 instead of an adler32.
 
      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
-   memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
-   is set to null if there is no error message.  inflateInit2 does not perform
-   any decompression apart from reading the zlib header if present: this will
-   be done by inflate(). (So next_in and avail_in may be modified, but next_out
-   and avail_out are unchanged.)
+   memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+   version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+   invalid, such as a null pointer to the structure.  msg is set to null if
+   there is no error message.  inflateInit2 does not perform any decompression
+   apart from possibly reading the zlib header if present: actual decompression
+   will be done by inflate().  (So next_in and avail_in may be modified, but
+   next_out and avail_out are unused and unchanged.) The current implementation
+   of inflateInit2() does not process any header information -- that is
+   deferred until inflate() is called.
 */
 
 ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
@@ -738,36 +821,56 @@ ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
                                              uInt  dictLength));
 /*
      Initializes the decompression dictionary from the given uncompressed byte
-   sequence. This function must be called immediately after a call of inflate,
-   if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+   sequence.  This function must be called immediately after a call of inflate,
+   if that call returned Z_NEED_DICT.  The dictionary chosen by the compressor
    can be determined from the adler32 value returned by that call of inflate.
    The compressor and decompressor must use exactly the same dictionary (see
-   deflateSetDictionary).  For raw inflate, this function can be called
-   immediately after inflateInit2() or inflateReset() and before any call of
-   inflate() to set the dictionary.  The application must insure that the
-   dictionary that was used for compression is provided.
+   deflateSetDictionary).  For raw inflate, this function can be called at any
+   time to set the dictionary.  If the provided dictionary is smaller than the
+   window and there is already data in the window, then the provided dictionary
+   will amend what's there.  The application must insure that the dictionary
+   that was used for compression is provided.
 
      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
-   parameter is invalid (such as NULL dictionary) or the stream state is
+   parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
-   expected one (incorrect adler32 value). inflateSetDictionary does not
+   expected one (incorrect adler32 value).  inflateSetDictionary does not
    perform any decompression: this will be done by subsequent calls of
    inflate().
 */
 
-ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
+                                             Bytef *dictionary,
+                                             uInt  *dictLength));
 /*
-    Skips invalid compressed data until a full flush point (see above the
-  description of deflate with Z_FULL_FLUSH) can be found, or until all
-  available input is skipped. No output is provided.
+     Returns the sliding dictionary being maintained by inflate.  dictLength is
+   set to the number of bytes in the dictionary, and that many bytes are copied
+   to dictionary.  dictionary must have enough space, where 32768 bytes is
+   always enough.  If inflateGetDictionary() is called with dictionary equal to
+   Z_NULL, then only the dictionary length is returned, and nothing is copied.
+   Similary, if dictLength is Z_NULL, then it is not set.
+
+     inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
+   stream state is inconsistent.
+*/
 
-    inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
-  if no more input was provided, Z_DATA_ERROR if no flush point has been found,
-  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
-  case, the application may save the current current value of total_in which
-  indicates where valid compressed data was found. In the error case, the
-  application may repeatedly call inflateSync, providing more input each time,
-  until success or end of the input data.
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+/*
+     Skips invalid compressed data until a possible full flush point (see above
+   for the description of deflate with Z_FULL_FLUSH) can be found, or until all
+   available input is skipped.  No output is provided.
+
+     inflateSync searches for a 00 00 FF FF pattern in the compressed data.
+   All full flush points have this pattern, but not all occurrences of this
+   pattern are full flush points.
+
+     inflateSync returns Z_OK if a possible full flush point has been found,
+   Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
+   has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
+   In the success case, the application may save the current current value of
+   total_in which indicates where valid compressed data was found.  In the
+   error case, the application may repeatedly call inflateSync, providing more
+   input each time, until success or end of the input data.
 */
 
 ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
@@ -782,18 +885,30 @@ ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
 
      inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
-   (such as zalloc being NULL). msg is left unchanged in both source and
+   (such as zalloc being Z_NULL).  msg is left unchanged in both source and
    destination.
 */
 
 ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
 /*
      This function is equivalent to inflateEnd followed by inflateInit,
-   but does not free and reallocate all the internal decompression state.
-   The stream will keep attributes that may have been set by inflateInit2.
+   but does not free and reallocate all the internal decompression state.  The
+   stream will keep attributes that may have been set by inflateInit2.
 
-      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
-   stream state was inconsistent (such as zalloc or state being NULL).
+     inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+                                      int windowBits));
+/*
+     This function is the same as inflateReset, but it also permits changing
+   the wrap and window size requests.  The windowBits parameter is interpreted
+   the same as it is for inflateInit2.
+
+     inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL), or if
+   the windowBits parameter is invalid.
 */
 
 ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
@@ -801,54 +916,87 @@ ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
                                      int value));
 /*
      This function inserts bits in the inflate input stream.  The intent is
-  that this function is used to start inflating at a bit position in the
-  middle of a byte.  The provided bits will be used before any bytes are used
-  from next_in.  This function should only be used with raw inflate, and
-  should be used before the first inflate() call after inflateInit2() or
-  inflateReset().  bits must be less than or equal to 16, and that many of the
-  least significant bits of value will be inserted in the input.
-
-      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+   that this function is used to start inflating at a bit position in the
+   middle of a byte.  The provided bits will be used before any bytes are used
+   from next_in.  This function should only be used with raw inflate, and
+   should be used before the first inflate() call after inflateInit2() or
+   inflateReset().  bits must be less than or equal to 16, and that many of the
+   least significant bits of value will be inserted in the input.
+
+     If bits is negative, then the input stream bit buffer is emptied.  Then
+   inflatePrime() can be called again to put bits in the buffer.  This is used
+   to clear out bits leftover after feeding inflate a block description prior
+   to feeding inflate codes.
+
+     inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+/*
+     This function returns two values, one in the lower 16 bits of the return
+   value, and the other in the remaining upper bits, obtained by shifting the
+   return value down 16 bits.  If the upper value is -1 and the lower value is
+   zero, then inflate() is currently decoding information outside of a block.
+   If the upper value is -1 and the lower value is non-zero, then inflate is in
+   the middle of a stored block, with the lower value equaling the number of
+   bytes from the input remaining to copy.  If the upper value is not -1, then
+   it is the number of bits back from the current bit position in the input of
+   the code (literal or length/distance pair) currently being processed.  In
+   that case the lower value is the number of bytes already emitted for that
+   code.
+
+     A code is being processed if inflate is waiting for more input to complete
+   decoding of the code, or if it has completed decoding but is waiting for
+   more output space to write the literal or match data.
+
+     inflateMark() is used to mark locations in the input data for random
+   access, which may be at bit positions, and to note those cases where the
+   output of a code may span boundaries of random access blocks.  The current
+   location in the input stream can be determined from avail_in and data_type
+   as noted in the description for the Z_BLOCK flush parameter for inflate.
+
+     inflateMark returns the value noted above or -1 << 16 if the provided
+   source stream state was inconsistent.
+*/
+
 ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
                                          gz_headerp head));
 /*
-      inflateGetHeader() requests that gzip header information be stored in the
+     inflateGetHeader() requests that gzip header information be stored in the
    provided gz_header structure.  inflateGetHeader() may be called after
    inflateInit2() or inflateReset(), and before the first call of inflate().
    As inflate() processes the gzip stream, head->done is zero until the header
    is completed, at which time head->done is set to one.  If a zlib stream is
    being decoded, then head->done is set to -1 to indicate that there will be
-   no gzip header information forthcoming.  Note that Z_BLOCK can be used to
-   force inflate() to return immediately after header processing is complete
-   and before any actual data is decompressed.
+   no gzip header information forthcoming.  Note that Z_BLOCK or Z_TREES can be
+   used to force inflate() to return immediately after header processing is
+   complete and before any actual data is decompressed.
 
-      The text, time, xflags, and os fields are filled in with the gzip header
+     The text, time, xflags, and os fields are filled in with the gzip header
    contents.  hcrc is set to true if there is a header CRC.  (The header CRC
-   was valid if done is set to one.)  If extra is not Z_NULL, then extra_max
+   was valid if done is set to one.) If extra is not Z_NULL, then extra_max
    contains the maximum number of bytes to write to extra.  Once done is true,
    extra_len contains the actual extra field length, and extra contains the
    extra field, or that field truncated if extra_max is less than extra_len.
    If name is not Z_NULL, then up to name_max characters are written there,
    terminated with a zero unless the length is greater than name_max.  If
    comment is not Z_NULL, then up to comm_max characters are written there,
-   terminated with a zero unless the length is greater than comm_max.  When
-   any of extra, name, or comment are not Z_NULL and the respective field is
-   not present in the header, then that field is set to Z_NULL to signal its
+   terminated with a zero unless the length is greater than comm_max.  When any
+   of extra, name, or comment are not Z_NULL and the respective field is not
+   present in the header, then that field is set to Z_NULL to signal its
    absence.  This allows the use of deflateSetHeader() with the returned
    structure to duplicate the header.  However if those fields are set to
    allocated memory, then the application will need to save those pointers
    elsewhere so that they can be eventually freed.
 
-      If inflateGetHeader is not used, then the header information is simply
+     If inflateGetHeader is not used, then the header information is simply
    discarded.  The header is always checked for validity, including the header
    CRC if present.  inflateReset() will reset the process to discard the header
    information.  The application would need to call inflateGetHeader() again to
    retrieve the header from the next gzip stream.
 
-      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+     inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
@@ -869,12 +1017,13 @@ ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
      See inflateBack() for the usage of these routines.
 
      inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
-   the paramaters are invalid, Z_MEM_ERROR if the internal state could not
-   be allocated, or Z_VERSION_ERROR if the version of the library does not
-   match the version of the header file.
+   the parameters are invalid, Z_MEM_ERROR if the internal state could not be
+   allocated, or Z_VERSION_ERROR if the version of the library does not match
+   the version of the header file.
 */
 
-typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
+typedef unsigned (*in_func) OF((void FAR *,
+                                z_const unsigned char FAR * FAR *));
 typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
 
 ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
@@ -882,24 +1031,25 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
                                     out_func out, void FAR *out_desc));
 /*
      inflateBack() does a raw inflate with a single call using a call-back
-   interface for input and output.  This is more efficient than inflate() for
-   file i/o applications in that it avoids copying between the output and the
-   sliding window by simply making the window itself the output buffer.  This
-   function trusts the application to not change the output buffer passed by
-   the output function, at least until inflateBack() returns.
+   interface for input and output.  This is potentially more efficient than
+   inflate() for file i/o applications, in that it avoids copying between the
+   output and the sliding window by simply making the window itself the output
+   buffer.  inflate() can be faster on modern CPUs when used with large
+   buffers.  inflateBack() trusts the application to not change the output
+   buffer passed by the output function, at least until inflateBack() returns.
 
      inflateBackInit() must be called first to allocate the internal state
    and to initialize the state with the user-provided window buffer.
    inflateBack() may then be used multiple times to inflate a complete, raw
-   deflate stream with each call.  inflateBackEnd() is then called to free
-   the allocated state.
+   deflate stream with each call.  inflateBackEnd() is then called to free the
+   allocated state.
 
      A raw deflate stream is one with no zlib or gzip header or trailer.
    This routine would normally be used in a utility that reads zip or gzip
    files and writes out uncompressed files.  The utility would decode the
-   header and process the trailer on its own, hence this routine expects
-   only the raw deflate stream to decompress.  This is different from the
-   normal behavior of inflate(), which expects either a zlib or gzip header and
+   header and process the trailer on its own, hence this routine expects only
+   the raw deflate stream to decompress.  This is different from the normal
+   behavior of inflate(), which expects either a zlib or gzip header and
    trailer around the deflate stream.
 
      inflateBack() uses two subroutines supplied by the caller that are then
@@ -925,7 +1075,7 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
    calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
    immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
    must also be initialized, and then if strm->avail_in is not zero, input will
-   initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+   initially be taken from strm->next_in[0 ..  strm->avail_in - 1].
 
      The in_desc and out_desc parameters of inflateBack() is passed as the
    first parameter of in() and out() respectively when they are called.  These
@@ -935,15 +1085,15 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
      On return, inflateBack() will set strm->next_in and strm->avail_in to
    pass back any unused input that was provided by the last in() call.  The
    return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
-   if in() or out() returned an error, Z_DATA_ERROR if there was a format
-   error in the deflate stream (in which case strm->msg is set to indicate the
-   nature of the error), or Z_STREAM_ERROR if the stream was not properly
-   initialized.  In the case of Z_BUF_ERROR, an input or output error can be
-   distinguished using strm->next_in which will be Z_NULL only if in() returned
-   an error.  If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
-   out() returning non-zero.  (in() will always be called before out(), so
-   strm->next_in is assured to be defined if out() returns non-zero.)  Note
-   that inflateBack() cannot return Z_OK.
+   if in() or out() returned an error, Z_DATA_ERROR if there was a format error
+   in the deflate stream (in which case strm->msg is set to indicate the nature
+   of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
+   In the case of Z_BUF_ERROR, an input or output error can be distinguished
+   using strm->next_in which will be Z_NULL only if in() returned an error.  If
+   strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
+   non-zero.  (in() will always be called before out(), so strm->next_in is
+   assured to be defined if out() returns non-zero.) Note that inflateBack()
+   cannot return Z_OK.
 */
 
 ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
@@ -995,27 +1145,27 @@ ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
      27-31: 0 (reserved)
  */
 
+#ifndef Z_SOLO
 
                         /* utility functions */
 
 /*
-     The following utility functions are implemented on top of the
-   basic stream-oriented functions. To simplify the interface, some
-   default options are assumed (compression level and memory usage,
-   standard memory allocation functions). The source code of these
-   utility functions can easily be modified if you need special options.
+     The following utility functions are implemented on top of the basic
+   stream-oriented functions.  To simplify the interface, some default options
+   are assumed (compression level and memory usage, standard memory allocation
+   functions).  The source code of these utility functions can be modified if
+   you need special options.
 */
 
 ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
                                  const Bytef *source, uLong sourceLen));
 /*
      Compresses the source buffer into the destination buffer.  sourceLen is
-   the byte length of the source buffer. Upon entry, destLen is the total
-   size of the destination buffer, which must be at least the value returned
-   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   the byte length of the source buffer.  Upon entry, destLen is the total size
+   of the destination buffer, which must be at least the value returned by
+   compressBound(sourceLen).  Upon exit, destLen is the actual size of the
    compressed buffer.
-     This function can be used to compress a whole file at once if the
-   input file is mmap'ed.
+
      compress returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_BUF_ERROR if there was not enough room in the output
    buffer.
@@ -1025,11 +1175,11 @@ ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
                                   const Bytef *source, uLong sourceLen,
                                   int level));
 /*
-     Compresses the source buffer into the destination buffer. The level
+     Compresses the source buffer into the destination buffer.  The level
    parameter has the same meaning as in deflateInit.  sourceLen is the byte
-   length of the source buffer. Upon entry, destLen is the total size of the
+   length of the source buffer.  Upon entry, destLen is the total size of the
    destination buffer, which must be at least the value returned by
-   compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   compressBound(sourceLen).  Upon exit, destLen is the actual size of the
    compressed buffer.
 
      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
@@ -1040,159 +1190,255 @@ ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
 ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
 /*
      compressBound() returns an upper bound on the compressed size after
-   compress() or compress2() on sourceLen bytes.  It would be used before
-   compress() or compress2() call to allocate the destination buffer.
+   compress() or compress2() on sourceLen bytes.  It would be used before a
+   compress() or compress2() call to allocate the destination buffer.
 */
 
 ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
                                    const Bytef *source, uLong sourceLen));
 /*
      Decompresses the source buffer into the destination buffer.  sourceLen is
-   the byte length of the source buffer. Upon entry, destLen is the total
-   size of the destination buffer, which must be large enough to hold the
-   entire uncompressed data. (The size of the uncompressed data must have
-   been saved previously by the compressor and transmitted to the decompressor
-   by some mechanism outside the scope of this compression library.)
-   Upon exit, destLen is the actual size of the compressed buffer.
-     This function can be used to decompress a whole file at once if the
-   input file is mmap'ed.
+   the byte length of the source buffer.  Upon entry, destLen is the total size
+   of the destination buffer, which must be large enough to hold the entire
+   uncompressed data.  (The size of the uncompressed data must have been saved
+   previously by the compressor and transmitted to the decompressor by some
+   mechanism outside the scope of this compression library.) Upon exit, destLen
+   is the actual size of the uncompressed buffer.
 
      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_BUF_ERROR if there was not enough room in the output
-   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
+   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.  In
+   the case where there is not enough room, uncompress() will fill the output
+   buffer with the uncompressed data up to that point.
 */
 
+                        /* gzip file access functions */
 
-typedef voidp gzFile;
+/*
+     This library supports reading and writing files in gzip (.gz) format with
+   an interface similar to that of stdio, using the functions that start with
+   "gz".  The gzip format is different from the zlib format.  gzip is a gzip
+   wrapper, documented in RFC 1952, wrapped around a deflate stream.
+*/
+
+typedef struct gzFile_s *gzFile;    /* semi-opaque gzip file descriptor */
 
-ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));
 /*
-     Opens a gzip (.gz) file for reading or writing. The mode parameter
-   is as in fopen ("rb" or "wb") but can also include a compression level
-   ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
-   Huffman only compression as in "wb1h", or 'R' for run-length encoding
-   as in "wb1R". (See the description of deflateInit2 for more information
-   about the strategy parameter.)
+ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
+
+     Opens a gzip (.gz) file for reading or writing.  The mode parameter is as
+   in fopen ("rb" or "wb") but can also include a compression level ("wb9") or
+   a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only
+   compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F'
+   for fixed code compression as in "wb9F".  (See the description of
+   deflateInit2 for more information about the strategy parameter.)  'T' will
+   request transparent writing or appending with no compression and not using
+   the gzip format.
+
+     "a" can be used instead of "w" to request that the gzip stream that will
+   be written be appended to the file.  "+" will result in an error, since
+   reading and writing to the same gzip file is not supported.  The addition of
+   "x" when writing will create the file exclusively, which fails if the file
+   already exists.  On systems that support it, the addition of "e" when
+   reading or writing will set the flag to close the file on an execve() call.
+
+     These functions, as well as gzip, will read and decode a sequence of gzip
+   streams in a file.  The append function of gzopen() can be used to create
+   such a file.  (Also see gzflush() for another way to do this.)  When
+   appending, gzopen does not test whether the file begins with a gzip stream,
+   nor does it look for the end of the gzip streams to begin appending.  gzopen
+   will simply append a gzip stream to the existing file.
 
      gzopen can be used to read a file which is not in gzip format; in this
-   case gzread will directly read from the file without decompression.
+   case gzread will directly read from the file without decompression.  When
+   reading, this will be detected automatically by looking for the magic two-
+   byte gzip header.
+
+     gzopen returns NULL if the file could not be opened, if there was
+   insufficient memory to allocate the gzFile state, or if an invalid mode was
+   specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
+   errno can be checked to determine if the reason gzopen failed was that the
+   file could not be opened.
+*/
 
-     gzopen returns NULL if the file could not be opened or if there was
-   insufficient memory to allocate the (de)compression state; errno
-   can be checked to distinguish the two cases (if errno is zero, the
-   zlib error is Z_MEM_ERROR).  */
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
+/*
+     gzdopen associates a gzFile with the file descriptor fd.  File descriptors
+   are obtained from calls like open, dup, creat, pipe or fileno (if the file
+   has been previously opened with fopen).  The mode parameter is as in gzopen.
+
+     The next call of gzclose on the returned gzFile will also close the file
+   descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
+   fd.  If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
+   mode);.  The duplicated descriptor should be saved to avoid a leak, since
+   gzdopen does not close fd if it fails.  If you are using fileno() to get the
+   file descriptor from a FILE *, then you will have to use dup() to avoid
+   double-close()ing the file descriptor.  Both gzclose() and fclose() will
+   close the associated file descriptor, so they need to have different file
+   descriptors.
+
+     gzdopen returns NULL if there was insufficient memory to allocate the
+   gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
+   provided, or '+' was provided), or if fd is -1.  The file descriptor is not
+   used until the next gz* read, write, seek, or close operation, so gzdopen
+   will not detect if fd is invalid (unless fd is -1).
+*/
 
-ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
 /*
-     gzdopen() associates a gzFile with the file descriptor fd.  File
-   descriptors are obtained from calls like open, dup, creat, pipe or
-   fileno (in the file has been previously opened with fopen).
-   The mode parameter is as in gzopen.
-     The next call of gzclose on the returned gzFile will also close the
-   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
-   descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
-     gzdopen returns NULL if there was insufficient memory to allocate
-   the (de)compression state.
+     Set the internal buffer size used by this library's functions.  The
+   default buffer size is 8192 bytes.  This function must be called after
+   gzopen() or gzdopen(), and before any other calls that read or write the
+   file.  The buffer memory allocation is always deferred to the first read or
+   write.  Two buffers are allocated, either both of the specified size when
+   writing, or one of the specified size and the other twice that size when
+   reading.  A larger buffer size of, for example, 64K or 128K bytes will
+   noticeably increase the speed of decompression (reading).
+
+     The new buffer size also affects the maximum length for gzprintf().
+
+     gzbuffer() returns 0 on success, or -1 on failure, such as being called
+   too late.
 */
 
 ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
 /*
-     Dynamically update the compression level or strategy. See the description
+     Dynamically update the compression level or strategy.  See the description
    of deflateInit2 for the meaning of these parameters.
+
      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
    opened for writing.
 */
 
-ZEXTERN int ZEXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
 /*
-     Reads the given number of uncompressed bytes from the compressed file.
-   If the input file was not in gzip format, gzread copies the given number
-   of bytes into the buffer.
-     gzread returns the number of uncompressed bytes actually read (0 for
-   end of file, -1 for error). */
+     Reads the given number of uncompressed bytes from the compressed file.  If
+   the input file is not in gzip format, gzread copies the given number of
+   bytes into the buffer directly from the file.
+
+     After reaching the end of a gzip stream in the input, gzread will continue
+   to read, looking for another gzip stream.  Any number of gzip streams may be
+   concatenated in the input file, and will all be decompressed by gzread().
+   If something other than a gzip stream is encountered after a gzip stream,
+   that remaining trailing garbage is ignored (and no error is returned).
+
+     gzread can be used to read a gzip file that is being concurrently written.
+   Upon reaching the end of the input, gzread will return with the available
+   data.  If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then
+   gzclearerr can be used to clear the end of file indicator in order to permit
+   gzread to be tried again.  Z_OK indicates that a gzip stream was completed
+   on the last gzread.  Z_BUF_ERROR indicates that the input file ended in the
+   middle of a gzip stream.  Note that gzread does not return -1 in the event
+   of an incomplete gzip stream.  This error is deferred until gzclose(), which
+   will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip
+   stream.  Alternatively, gzerror can be used before gzclose to detect this
+   case.
+
+     gzread returns the number of uncompressed bytes actually read, less than
+   len for end of file, or -1 for error.
+*/
 
-ZEXTERN int ZEXPORT    gzwrite OF((gzFile file,
-                                   voidpc buf, unsigned len));
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+                                voidpc buf, unsigned len));
 /*
      Writes the given number of uncompressed bytes into the compressed file.
-   gzwrite returns the number of uncompressed bytes actually written
-   (0 in case of error).
+   gzwrite returns the number of uncompressed bytes written or 0 in case of
+   error.
 */
 
-ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));
+ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
 /*
-     Converts, formats, and writes the args to the compressed file under
-   control of the format string, as in fprintf. gzprintf returns the number of
-   uncompressed bytes actually written (0 in case of error).  The number of
-   uncompressed bytes written is limited to 4095. The caller should assure that
-   this limit is not exceeded. If it is exceeded, then gzprintf() will return
-   return an error (0) with nothing written. In this case, there may also be a
-   buffer overflow with unpredictable consequences, which is possible only if
-   zlib was compiled with the insecure functions sprintf() or vsprintf()
-   because the secure snprintf() or vsnprintf() functions were not available.
+     Converts, formats, and writes the arguments to the compressed file under
+   control of the format string, as in fprintf.  gzprintf returns the number of
+   uncompressed bytes actually written, or 0 in case of error.  The number of
+   uncompressed bytes written is limited to 8191, or one less than the buffer
+   size given to gzbuffer().  The caller should assure that this limit is not
+   exceeded.  If it is exceeded, then gzprintf() will return an error (0) with
+   nothing written.  In this case, there may also be a buffer overflow with
+   unpredictable consequences, which is possible only if zlib was compiled with
+   the insecure functions sprintf() or vsprintf() because the secure snprintf()
+   or vsnprintf() functions were not available.  This can be determined using
+   zlibCompileFlags().
 */
 
 ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
 /*
-      Writes the given null-terminated string to the compressed file, excluding
+     Writes the given null-terminated string to the compressed file, excluding
    the terminating null character.
-      gzputs returns the number of characters written, or -1 in case of error.
+
+     gzputs returns the number of characters written, or -1 in case of error.
 */
 
 ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
 /*
-      Reads bytes from the compressed file until len-1 characters are read, or
-   a newline character is read and transferred to buf, or an end-of-file
-   condition is encountered.  The string is then terminated with a null
-   character.
-      gzgets returns buf, or Z_NULL in case of error.
+     Reads bytes from the compressed file until len-1 characters are read, or a
+   newline character is read and transferred to buf, or an end-of-file
+   condition is encountered.  If any characters are read or if len == 1, the
+   string is terminated with a null character.  If no characters are read due
+   to an end-of-file or len < 1, then the buffer is left untouched.
+
+     gzgets returns buf which is a null-terminated string, or it returns NULL
+   for end-of-file or in case of error.  If there was an error, the contents at
+   buf are indeterminate.
 */
 
-ZEXTERN int ZEXPORT    gzputc OF((gzFile file, int c));
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
 /*
-      Writes c, converted to an unsigned char, into the compressed file.
-   gzputc returns the value that was written, or -1 in case of error.
+     Writes c, converted to an unsigned char, into the compressed file.  gzputc
+   returns the value that was written, or -1 in case of error.
 */
 
-ZEXTERN int ZEXPORT    gzgetc OF((gzFile file));
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
 /*
-      Reads one byte from the compressed file. gzgetc returns this byte
-   or -1 in case of end of file or error.
+     Reads one byte from the compressed file.  gzgetc returns this byte or -1
+   in case of end of file or error.  This is implemented as a macro for speed.
+   As such, it does not do all of the checking the other functions do.  I.e.
+   it does not check to see if file is NULL, nor whether the structure file
+   points to has been clobbered or not.
 */
 
-ZEXTERN int ZEXPORT    gzungetc OF((int c, gzFile file));
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
 /*
-      Push one character back onto the stream to be read again later.
-   Only one character of push-back is allowed.  gzungetc() returns the
-   character pushed, or -1 on failure.  gzungetc() will fail if a
-   character has been pushed but not read yet, or if c is -1. The pushed
-   character will be discarded if the stream is repositioned with gzseek()
-   or gzrewind().
+     Push one character back onto the stream to be read as the first character
+   on the next read.  At least one character of push-back is allowed.
+   gzungetc() returns the character pushed, or -1 on failure.  gzungetc() will
+   fail if c is -1, and may fail if a character has been pushed but not read
+   yet.  If gzungetc is used immediately after gzopen or gzdopen, at least the
+   output buffer size of pushed characters is allowed.  (See gzbuffer above.)
+   The pushed character will be discarded if the stream is repositioned with
+   gzseek() or gzrewind().
 */
 
-ZEXTERN int ZEXPORT    gzflush OF((gzFile file, int flush));
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
 /*
-     Flushes all pending output into the compressed file. The parameter
-   flush is as in the deflate() function. The return value is the zlib
-   error number (see function gzerror below). gzflush returns Z_OK if
-   the flush parameter is Z_FINISH and all output could be flushed.
-     gzflush should be called only when strictly necessary because it can
-   degrade compression.
+     Flushes all pending output into the compressed file.  The parameter flush
+   is as in the deflate() function.  The return value is the zlib error number
+   (see function gzerror below).  gzflush is only permitted when writing.
+
+     If the flush parameter is Z_FINISH, the remaining data is written and the
+   gzip stream is completed in the output.  If gzwrite() is called again, a new
+   gzip stream will be started in the output.  gzread() is able to read such
+   concatented gzip streams.
+
+     gzflush should be called only when strictly necessary because it will
+   degrade compression if called too often.
 */
 
-ZEXTERN z_off_t ZEXPORT    gzseek OF((gzFile file,
-                                      z_off_t offset, int whence));
 /*
-      Sets the starting position for the next gzread or gzwrite on the
-   given compressed file. The offset represents a number of bytes in the
-   uncompressed data stream. The whence parameter is defined as in lseek(2);
+ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
+                                   z_off_t offset, int whence));
+
+     Sets the starting position for the next gzread or gzwrite on the given
+   compressed file.  The offset represents a number of bytes in the
+   uncompressed data stream.  The whence parameter is defined as in lseek(2);
    the value SEEK_END is not supported.
+
      If the file is opened for reading, this function is emulated but can be
-   extremely slow. If the file is opened for writing, only forward seeks are
+   extremely slow.  If the file is opened for writing, only forward seeks are
    supported; gzseek then compresses a sequence of zeroes up to the new
    starting position.
 
-      gzseek returns the resulting offset location as measured in bytes from
+     gzseek returns the resulting offset location as measured in bytes from
    the beginning of the uncompressed stream, or -1 in case of error, in
    particular if the file is opened for writing and the new starting position
    would be before the current position.
@@ -1202,68 +1448,134 @@ ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
 /*
      Rewinds the given file. This function is supported only for reading.
 
-   gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+     gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
 */
 
+/*
 ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
+
+     Returns the starting position for the next gzread or gzwrite on the given
+   compressed file.  This position represents a number of bytes in the
+   uncompressed data stream, and is zero when starting, even if appending or
+   reading a gzip stream from the middle of a file using gzdopen().
+
+     gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
 /*
-     Returns the starting position for the next gzread or gzwrite on the
-   given compressed file. This position represents a number of bytes in the
-   uncompressed data stream.
+ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
 
-   gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+     Returns the current offset in the file being read or written.  This offset
+   includes the count of bytes that precede the gzip stream, for example when
+   appending or when using gzdopen() for reading.  When reading, the offset
+   does not include as yet unused buffered input.  This information can be used
+   for a progress indicator.  On error, gzoffset() returns -1.
 */
 
 ZEXTERN int ZEXPORT gzeof OF((gzFile file));
 /*
-     Returns 1 when EOF has previously been detected reading the given
-   input stream, otherwise zero.
+     Returns true (1) if the end-of-file indicator has been set while reading,
+   false (0) otherwise.  Note that the end-of-file indicator is set only if the
+   read tried to go past the end of the input, but came up short.  Therefore,
+   just like feof(), gzeof() may return false even if there is no more data to
+   read, in the event that the last read request was for the exact number of
+   bytes remaining in the input file.  This will happen if the input file size
+   is an exact multiple of the buffer size.
+
+     If gzeof() returns true, then the read functions will return no more data,
+   unless the end-of-file indicator is reset by gzclearerr() and the input file
+   has grown since the previous end of file was detected.
 */
 
 ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
 /*
-     Returns 1 if file is being read directly without decompression, otherwise
-   zero.
+     Returns true (1) if file is being copied directly while reading, or false
+   (0) if file is a gzip stream being decompressed.
+
+     If the input file is empty, gzdirect() will return true, since the input
+   does not contain a gzip stream.
+
+     If gzdirect() is used immediately after gzopen() or gzdopen() it will
+   cause buffers to be allocated to allow reading the file to determine if it
+   is a gzip file.  Therefore if gzbuffer() is used, it should be called before
+   gzdirect().
+
+     When writing, gzdirect() returns true (1) if transparent writing was
+   requested ("wT" for the gzopen() mode), or false (0) otherwise.  (Note:
+   gzdirect() is not needed when writing.  Transparent writing must be
+   explicitly requested, so the application already knows the answer.  When
+   linking statically, using gzdirect() will include all of the zlib code for
+   gzip file reading and decompression, which may not be desired.)
 */
 
 ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
 /*
-     Flushes all pending output if necessary, closes the compressed file
-   and deallocates all the (de)compression state. The return value is the zlib
-   error number (see function gzerror below).
+     Flushes all pending output if necessary, closes the compressed file and
+   deallocates the (de)compression state.  Note that once file is closed, you
+   cannot call gzerror with file, since its structures have been deallocated.
+   gzclose must not be called more than once on the same file, just as free
+   must not be called more than once on the same allocation.
+
+     gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
+   file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
+   last read ended in the middle of a gzip stream, or Z_OK on success.
+*/
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+/*
+     Same as gzclose(), but gzclose_r() is only for use when reading, and
+   gzclose_w() is only for use when writing or appending.  The advantage to
+   using these instead of gzclose() is that they avoid linking in zlib
+   compression or decompression code that is not used when only reading or only
+   writing respectively.  If gzclose() is used, then both compression and
+   decompression code will be included the application when linking to a static
+   zlib library.
 */
 
 ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
 /*
-     Returns the error message for the last error which occurred on the
-   given compressed file. errnum is set to zlib error number. If an
-   error occurred in the file system and not in the compression library,
-   errnum is set to Z_ERRNO and the application may consult errno
-   to get the exact error code.
+     Returns the error message for the last error which occurred on the given
+   compressed file.  errnum is set to zlib error number.  If an error occurred
+   in the file system and not in the compression library, errnum is set to
+   Z_ERRNO and the application may consult errno to get the exact error code.
+
+     The application must not modify the returned string.  Future calls to
+   this function may invalidate the previously returned string.  If file is
+   closed, then the string previously returned by gzerror will no longer be
+   available.
+
+     gzerror() should be used to distinguish errors from end-of-file for those
+   functions above that do not distinguish those cases in their return values.
 */
 
 ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
 /*
-     Clears the error and end-of-file flags for file. This is analogous to the
-   clearerr() function in stdio. This is useful for continuing to read a gzip
+     Clears the error and end-of-file flags for file.  This is analogous to the
+   clearerr() function in stdio.  This is useful for continuing to read a gzip
    file that is being written concurrently.
 */
 
+#endif /* !Z_SOLO */
+
                         /* checksum functions */
 
 /*
      These functions are not related to compression but are exported
-   anyway because they might be useful in applications using the
-   compression library.
+   anyway because they might be useful in applications using the compression
+   library.
 */
 
 ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
 /*
      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
-   return the updated checksum. If buf is NULL, this function returns
-   the required initial value for the checksum.
-   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
-   much faster. Usage example:
+   return the updated checksum.  If buf is Z_NULL, this function returns the
+   required initial value for the checksum.
+
+     An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+   much faster.
+
+   Usage example:
 
      uLong adler = adler32(0L, Z_NULL, 0);
 
@@ -1273,21 +1585,25 @@ ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
      if (adler != original_adler) error();
 */
 
+/*
 ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
                                           z_off_t len2));
-/*
+
      Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
    and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
    each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
-   seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
+   seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.  Note
+   that the z_off_t type (like off_t) is a signed integer.  If len2 is
+   negative, the result has no meaning or utility.
 */
 
 ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
 /*
      Update a running CRC-32 with the bytes buf[0..len-1] and return the
-   updated CRC-32. If buf is NULL, this function returns the required initial
-   value for the for the crc. Pre- and post-conditioning (one's complement) is
+   updated CRC-32.  If buf is Z_NULL, this function returns the required
+   initial value for the crc.  Pre- and post-conditioning (one's complement) is
    performed within this function so it shouldn't be done by the application.
+
    Usage example:
 
      uLong crc = crc32(0L, Z_NULL, 0);
@@ -1298,9 +1614,9 @@ ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
      if (crc != original_crc) error();
 */
 
+/*
 ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
 
-/*
      Combine two CRC-32 check values into one.  For two sequences of bytes,
    seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
    calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
@@ -1329,26 +1645,121 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
                                          const char *version,
                                          int stream_size));
 #define deflateInit(strm, level) \
-        deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
+        deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
 #define inflateInit(strm) \
-        inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
+        inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
-                      (strategy),           ZLIB_VERSION, sizeof(z_stream))
+                      (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
 #define inflateInit2(strm, windowBits) \
-        inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+        inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+                      (int)sizeof(z_stream))
 #define inflateBackInit(strm, windowBits, window) \
         inflateBackInit_((strm), (windowBits), (window), \
-        ZLIB_VERSION, sizeof(z_stream))
+                      ZLIB_VERSION, (int)sizeof(z_stream))
+
+#ifndef Z_SOLO
+
+/* gzgetc() macro and its supporting function and exposed data structure.  Note
+ * that the real internal state is much larger than the exposed structure.
+ * This abbreviated structure exposes just enough for the gzgetc() macro.  The
+ * user should not mess with these exposed elements, since their names or
+ * behavior could change in the future, perhaps even capriciously.  They can
+ * only be used by the gzgetc() macro.  You have been warned.
+ */
+struct gzFile_s {
+    unsigned have;
+    unsigned char *next;
+    z_off64_t pos;
+};
+ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file));  /* backward compatibility */
+#ifdef Z_PREFIX_SET
+#  undef z_gzgetc
+#  define z_gzgetc(g) \
+          ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
+#else
+#  define gzgetc(g) \
+          ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
+#endif
 
+/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
+ * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
+ * both are true, the application gets the *64 functions, and the regular
+ * functions are changed to 64 bits) -- in case these are set on systems
+ * without large file support, _LFS64_LARGEFILE must also be true
+ */
+#ifdef Z_LARGE64
+   ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+   ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+   ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+   ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+   ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+   ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
+#  ifdef Z_PREFIX_SET
+#    define z_gzopen z_gzopen64
+#    define z_gzseek z_gzseek64
+#    define z_gztell z_gztell64
+#    define z_gzoffset z_gzoffset64
+#    define z_adler32_combine z_adler32_combine64
+#    define z_crc32_combine z_crc32_combine64
+#  else
+#    define gzopen gzopen64
+#    define gzseek gzseek64
+#    define gztell gztell64
+#    define gzoffset gzoffset64
+#    define adler32_combine adler32_combine64
+#    define crc32_combine crc32_combine64
+#  endif
+#  ifndef Z_LARGE64
+     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+     ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
+     ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
+     ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
+     ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+     ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#  endif
+#else
+   ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+   ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+   ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+   ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+   ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+   ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
+
+#else /* Z_SOLO */
 
+   ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+   ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+
+#endif /* !Z_SOLO */
+
+/* hack for buggy compilers */
 #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
-    struct internal_state {int dummy;}; /* hack for buggy compilers */
+    struct internal_state {int dummy;};
 #endif
 
+/* undocumented functions */
 ZEXTERN const char   * ZEXPORT zError           OF((int));
-ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp z));
-ZEXTERN const uLongf * ZEXPORT get_crc_table    OF((void));
+ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp));
+ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table    OF((void));
+ZEXTERN int            ZEXPORT inflateUndermine OF((z_streamp, int));
+ZEXTERN int            ZEXPORT inflateResetKeep OF((z_streamp));
+ZEXTERN int            ZEXPORT deflateResetKeep OF((z_streamp));
+#if defined(_WIN32) && !defined(Z_SOLO)
+ZEXTERN gzFile         ZEXPORT gzopen_w OF((const wchar_t *path,
+                                            const char *mode));
+#endif
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#  ifndef Z_SOLO
+ZEXTERN int            ZEXPORTVA gzvprintf Z_ARG((gzFile file,
+                                                  const char *format,
+                                                  va_list va));
+#  endif
+#endif
 
 #ifdef __cplusplus
 }
index d55f5948a37f181c003dd4572b660727132eaba1..23d2ebef008fdcc00833eba0d9abcd7b9c665531 100644 (file)
@@ -1,17 +1,20 @@
 /* zutil.c -- target dependent utility functions for the compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
 /* @(#) $Id$ */
 
 #include "zutil.h"
+#ifndef Z_SOLO
+#  include "gzguts.h"
+#endif
 
 #ifndef NO_DUMMY_DECL
 struct internal_state      {int dummy;}; /* for buggy compilers */
 #endif
 
-const char * const z_errmsg[10] = {
+z_const char * const z_errmsg[10] = {
 "need dictionary",     /* Z_NEED_DICT       2  */
 "stream end",          /* Z_STREAM_END      1  */
 "",                    /* Z_OK              0  */
@@ -34,25 +37,25 @@ uLong ZEXPORT zlibCompileFlags()
     uLong flags;
 
     flags = 0;
-    switch (sizeof(uInt)) {
+    switch ((int)(sizeof(uInt))) {
     case 2:     break;
     case 4:     flags += 1;     break;
     case 8:     flags += 2;     break;
     default:    flags += 3;
     }
-    switch (sizeof(uLong)) {
+    switch ((int)(sizeof(uLong))) {
     case 2:     break;
     case 4:     flags += 1 << 2;        break;
     case 8:     flags += 2 << 2;        break;
     default:    flags += 3 << 2;
     }
-    switch (sizeof(voidpf)) {
+    switch ((int)(sizeof(voidpf))) {
     case 2:     break;
     case 4:     flags += 1 << 4;        break;
     case 8:     flags += 2 << 4;        break;
     default:    flags += 3 << 4;
     }
-    switch (sizeof(z_off_t)) {
+    switch ((int)(sizeof(z_off_t))) {
     case 2:     break;
     case 4:     flags += 1 << 6;        break;
     case 8:     flags += 2 << 6;        break;
@@ -85,27 +88,27 @@ uLong ZEXPORT zlibCompileFlags()
 #ifdef FASTEST
     flags += 1L << 21;
 #endif
-#ifdef STDC
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
 #  ifdef NO_vsnprintf
-        flags += 1L << 25;
+    flags += 1L << 25;
 #    ifdef HAS_vsprintf_void
-        flags += 1L << 26;
+    flags += 1L << 26;
 #    endif
 #  else
 #    ifdef HAS_vsnprintf_void
-        flags += 1L << 26;
+    flags += 1L << 26;
 #    endif
 #  endif
 #else
-        flags += 1L << 24;
+    flags += 1L << 24;
 #  ifdef NO_snprintf
-        flags += 1L << 25;
+    flags += 1L << 25;
 #    ifdef HAS_sprintf_void
-        flags += 1L << 26;
+    flags += 1L << 26;
 #    endif
 #  else
 #    ifdef HAS_snprintf_void
-        flags += 1L << 26;
+    flags += 1L << 26;
 #    endif
 #  endif
 #endif
@@ -117,9 +120,9 @@ uLong ZEXPORT zlibCompileFlags()
 #  ifndef verbose
 #    define verbose 0
 #  endif
-int z_verbose = verbose;
+int ZLIB_INTERNAL z_verbose = verbose;
 
-void z_error (m)
+void ZLIB_INTERNAL z_error (m)
     char *m;
 {
     fprintf(stderr, "%s\n", m);
@@ -146,7 +149,7 @@ const char * ZEXPORT zError(err)
 
 #ifndef HAVE_MEMCPY
 
-void zmemcpy(dest, source, len)
+void ZLIB_INTERNAL zmemcpy(dest, source, len)
     Bytef* dest;
     const Bytef* source;
     uInt  len;
@@ -157,7 +160,7 @@ void zmemcpy(dest, source, len)
     } while (--len != 0);
 }
 
-int zmemcmp(s1, s2, len)
+int ZLIB_INTERNAL zmemcmp(s1, s2, len)
     const Bytef* s1;
     const Bytef* s2;
     uInt  len;
@@ -170,7 +173,7 @@ int zmemcmp(s1, s2, len)
     return 0;
 }
 
-void zmemzero(dest, len)
+void ZLIB_INTERNAL zmemzero(dest, len)
     Bytef* dest;
     uInt  len;
 {
@@ -181,6 +184,7 @@ void zmemzero(dest, len)
 }
 #endif
 
+#ifndef Z_SOLO
 
 #ifdef SYS16BIT
 
@@ -213,7 +217,7 @@ local ptr_table table[MAX_PTR];
  * a protected system like OS/2. Use Microsoft C instead.
  */
 
-voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
 {
     voidpf buf = opaque; /* just to make some compilers happy */
     ulg bsize = (ulg)items*size;
@@ -237,7 +241,7 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
     return buf;
 }
 
-void  zcfree (voidpf opaque, voidpf ptr)
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
 {
     int n;
     if (*(ush*)&ptr != 0) { /* object < 64K */
@@ -272,13 +276,13 @@ void  zcfree (voidpf opaque, voidpf ptr)
 #  define _hfree   hfree
 #endif
 
-voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
 {
     if (opaque) opaque = 0; /* to make compiler happy */
     return _halloc((long)items, size);
 }
 
-void  zcfree (voidpf opaque, voidpf ptr)
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
 {
     if (opaque) opaque = 0; /* to make compiler happy */
     _hfree(ptr);
@@ -297,7 +301,7 @@ extern voidp  calloc OF((uInt items, uInt size));
 extern void   free   OF((voidpf ptr));
 #endif
 
-voidpf zcalloc (opaque, items, size)
+voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
     voidpf opaque;
     unsigned items;
     unsigned size;
@@ -307,7 +311,7 @@ voidpf zcalloc (opaque, items, size)
                               (voidpf)calloc(items, size);
 }
 
-void  zcfree (opaque, ptr)
+void ZLIB_INTERNAL zcfree (opaque, ptr)
     voidpf opaque;
     voidpf ptr;
 {
@@ -316,3 +320,5 @@ void  zcfree (opaque, ptr)
 }
 
 #endif /* MY_ZCALLOC */
+
+#endif /* !Z_SOLO */
index b7d5eff81b69a3c23daec86c42dff4ba4746b1dd..24ab06b1cf60aeba4ade9ab36ff7ad5f73541960 100644 (file)
@@ -1,5 +1,5 @@
 /* zutil.h -- internal interface and configuration of the compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2013 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
 #ifndef ZUTIL_H
 #define ZUTIL_H
 
-#define ZLIB_INTERNAL
+#ifdef HAVE_HIDDEN
+#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+#  define ZLIB_INTERNAL
+#endif
+
 #include "zlib.h"
 
-#ifdef STDC
-#  ifndef _WIN32_WCE
+#if defined(STDC) && !defined(Z_SOLO)
+#  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
 #    include <stddef.h>
 #  endif
 #  include <string.h>
 #  include <stdlib.h>
 #endif
-#ifdef NO_ERRNO_H
-#   ifdef _WIN32_WCE
-      /* The Microsoft C Run-Time Library for Windows CE doesn't have
-       * errno.  We define it as a global variable to simplify porting.
-       * Its value is always 0 and should not be used.  We rename it to
-       * avoid conflict with other libraries that use the same workaround.
-       */
-#     define errno z_errno
-#   endif
-    extern int errno;
-#else
-#  ifndef _WIN32_WCE
-#    include <errno.h>
-#  endif
+
+#ifdef Z_SOLO
+   typedef long ptrdiff_t;  /* guess -- will be caught if guess is wrong */
 #endif
 
 #ifndef local
@@ -50,13 +44,13 @@ typedef unsigned short ush;
 typedef ush FAR ushf;
 typedef unsigned long  ulg;
 
-extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
+extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 /* (size given to avoid silly warnings with Visual C++) */
 
 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
 
 #define ERR_RETURN(strm,err) \
-  return (strm->msg = (char*)ERR_MSG(err), (err))
+  return (strm->msg = ERR_MSG(err), (err))
 /* To be used only when the state is known to be valid */
 
         /* common constants */
@@ -88,16 +82,18 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 
 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
 #  define OS_CODE  0x00
-#  if defined(__TURBOC__) || defined(__BORLANDC__)
-#    if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
-       /* Allow compilation with ANSI keywords only enabled */
-       void _Cdecl farfree( void *block );
-       void *_Cdecl farmalloc( unsigned long nbytes );
-#    else
-#      include <alloc.h>
+#  ifndef Z_SOLO
+#    if defined(__TURBOC__) || defined(__BORLANDC__)
+#      if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
+         /* Allow compilation with ANSI keywords only enabled */
+         void _Cdecl farfree( void *block );
+         void *_Cdecl farmalloc( unsigned long nbytes );
+#      else
+#        include <alloc.h>
+#      endif
+#    else /* MSC or DJGPP */
+#      include <malloc.h>
 #    endif
-#  else /* MSC or DJGPP */
-#    include <malloc.h>
 #  endif
 #endif
 
@@ -117,18 +113,20 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 
 #ifdef OS2
 #  define OS_CODE  0x06
-#  ifdef M_I86
-     #include <malloc.h>
+#  if defined(M_I86) && !defined(Z_SOLO)
+#    include <malloc.h>
 #  endif
 #endif
 
 #if defined(MACOS) || defined(TARGET_OS_MAC)
 #  define OS_CODE  0x07
-#  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
-#    include <unix.h> /* for fdopen */
-#  else
-#    ifndef fdopen
-#      define fdopen(fd,mode) NULL /* No fdopen() */
+#  ifndef Z_SOLO
+#    if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
+#      include <unix.h> /* for fdopen */
+#    else
+#      ifndef fdopen
+#        define fdopen(fd,mode) NULL /* No fdopen() */
+#      endif
 #    endif
 #  endif
 #endif
@@ -151,7 +149,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  define fdopen(fd,mode) NULL /* No fdopen() */
 #endif
 
-#if (defined(_MSC_VER) && (_MSC_VER > 600))
+#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
 #  if defined(_WIN32_WCE)
 #    define fdopen(fd,mode) NULL /* No fdopen() */
 #    ifndef _PTRDIFF_T_DEFINED
@@ -163,6 +161,19 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  endif
 #endif
 
+#if defined(__BORLANDC__) && !defined(MSDOS)
+  #pragma warn -8004
+  #pragma warn -8008
+  #pragma warn -8066
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_WIN32) && \
+    (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
+    ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+    ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#endif
+
         /* common defaults */
 
 #ifndef OS_CODE
@@ -175,40 +186,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 
          /* functions */
 
-#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
-#  ifndef HAVE_VSNPRINTF
-#    define HAVE_VSNPRINTF
-#  endif
-#endif
-#if defined(__CYGWIN__)
-#  ifndef HAVE_VSNPRINTF
-#    define HAVE_VSNPRINTF
-#  endif
-#endif
-#ifndef HAVE_VSNPRINTF
-#  ifdef MSDOS
-     /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
-        but for now we just assume it doesn't. */
-#    define NO_vsnprintf
-#  endif
-#  ifdef __TURBOC__
-#    define NO_vsnprintf
-#  endif
-#  ifdef WIN32
-     /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
-#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
-#      define vsnprintf _vsnprintf
-#    endif
-#  endif
-#  ifdef __SASC
-#    define NO_vsnprintf
-#  endif
-#endif
-#ifdef VMS
-#  define NO_vsnprintf
-#endif
-
-#if defined(pyr)
+#if defined(pyr) || defined(Z_SOLO)
 #  define NO_MEMCPY
 #endif
 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
@@ -232,16 +210,16 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #    define zmemzero(dest, len) memset(dest, 0, len)
 #  endif
 #else
-   extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
-   extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
-   extern void zmemzero OF((Bytef* dest, uInt len));
+   void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
+   int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
+   void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
 #endif
 
 /* Diagnostic functions */
 #ifdef DEBUG
 #  include <stdio.h>
-   extern int z_verbose;
-   extern void z_error    OF((char *m));
+   extern int ZLIB_INTERNAL z_verbose;
+   extern void ZLIB_INTERNAL z_error OF((char *m));
 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
@@ -257,13 +235,19 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  define Tracecv(c,x)
 #endif
 
-
-voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
-void   zcfree  OF((voidpf opaque, voidpf ptr));
+#ifndef Z_SOLO
+   voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
+                                    unsigned size));
+   void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
+#endif
 
 #define ZALLOC(strm, items, size) \
            (*((strm)->zalloc))((strm)->opaque, (items), (size))
 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
 
+/* Reverse the bytes in a 32-bit value */
+#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+                    (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+
 #endif /* ZUTIL_H */