Wrap HACKING.md where lines exceed 78 columns.

(cherry picked from commit b10f1eba3dbfe0014c192c98fff8733e2346b658)
HACKING.md: add do-while exception, and rewrap some text around it.

(cherry picked from commit 83b579d51824b06778a6495f3b62f96d71d5e13a)
Fixing LocalExec in cf-execd-runner.c

The premature return was only doing most of the tidy-up needed, so
make it a break instead and add a flag for whether the command
actually completed; use that to condition the one Log() after the loop
that wasn't appropriate to run after this failure.  The alternate
branch to that return's if was in fact the only path out of the loop
for which the Log() was relevant, so made it set the flag for that
Log().  Made the Log() say something sensible as an alternative when
the work isn't complete.  Simplified the code to detect a non-blank
string.

Code skipping spaces at the start of a string could be simplified to
avoid the need for a bool flag to say whether we found anything else.

(cherry picked from commit 0f31fe051f0e8917938055296f497af687738e36)
Kill some stray apostrophes.

Some function names were formatted using '%s'' with two dangling
apostrophes; there should only be one before and one after !
Also: split a long line, purged dangling hspace, added braces demanded
by coding style.

(cherry picked from commit 2cd2a2d35ca96dbf6690b2688958f96ea098827c)
Leak-fixes and clean-up in FnCallPackagesMatching().

Move computation of regex outside the loop - no need to recompute the
same regex for every line of the CSV file ! - and pre-compile it so
that we can search using it more efficiently.  Localise all the
variables related to that at the same time.

Split up various long lines, notably when Log()ging failure to open
the inventory.

Move creation of some objects to where they're needed, to save the
need to tidy them away on failure in the interval.

Don't leak the allocated line when it doesn't match our expectations.

Restructure the for(;;) loop as a while with assignment in its
condition so as to move the failure-handling out of the loop and avoid
duplication of clean-up code.  Reviewed the error message on that
failure: it claimed fread() was involved, which is a lie, so re-worked
it.

(cherry picked from commit 3fc8d68041c9cae1cebb2e5438a3a040ded3e5d4)
Petty leak-fix: close socket on which we failed to listen.

About to exit with error anyway, so not crucial.

(cherry picked from commit a14023622adec2a250c1863708c62f4c295a0099)
Tidy up in SocketConnect().

PR #1952 added an ap = ap->ai_next in an if branch that duplicated one
in its else; replacing these with one after the whole branch put the
loop in the form of a for() unrolled as a while, so I rolled it up as
a for().

If an iteration of the loop managed to create a socket but failed to
connect it, we were leaking it (unless it happened to be the last time
round the loop); the "on failure close the socket" was after the loop,
where it belonged inside.  Further, the Log()ging of that tried to use
GetErrorStr() when enough other functions had been called since the
error that the error string might easily be from something unrelated
to the connection failure.  So Log() before closing the socket in the
loop and don't try to include an error string with the final "none of
it worked" Log().

Tweaked a couple of other Log() messages.

(cherry picked from commit ca9d2fb252d84788baee872707fa52ad83caf7c9)
Fix a badly-thought-out loop.

The BSD compiler warned that i >= 0 is always true for a size_t i; the
loop conditioned on that could have spun for ever.  So turn it into a
while that terminates correctly and simplify what's left.

(cherry picked from commit cad04bf29138210cebee41bf4771bdc09df3fd29)
Break out of thread main-loop on signal.

If we get a signal that we catch and use as reason to exit, we need
our threads to exit, not just the main thread.  I believe it's safe
for the threads to simply call IsPendingTermination(); it's not
super-critical that they synchronise round that, as long as they
notice after a few laps round their loops.

(cherry picked from commit de07da489e888a788c6c2b1ac1ebd05652b3d9b0)
Cleanup in FileReadFromFd().

It was possible to return without having set *truncated.
Closing the file was repeated in several places.
Our coding standard demands braces on a conditional's body, even when
it consists of just one statement.  So add those braces.

Restructuring the loop body's conditionals as an if/else-if/else
chain got rid of some continue;s.

(cherry picked from commit e86cc0d1174e0e9ef503f4098cfb874b5f42ae77)
Fix misguided strlen() in Chop() and StripTrailingNewline().

They check strlen() doesn't exceed max_length, which callers typically
pass as the buffer size; either this test is a waste of time or, when
it might fail, it'll involve reading past the end of the buffer to
discover the error - which is bad.  So use strnlen() instead, this is
what it's for.  Don't bother special-casing empty string; and re-use
the value strnlen has returned to us instead of calling again at the
start of the search for the last non-space.  Only '\0' out the first
trailing space, rather than filling the tail with '\0' over each
space.  (For "space" read isspace() in Chop() and '\n' in
StripTrailingNewline().)  Structure the two functions the same way.

(cherry picked from commit 49bfcf81735d02f39f98b1b60ffea3b56f94aeb8)
Cast is...() character class parameters to unsigned char.

These functions (isprint, isspace, etc.) take an int with "the value
of an unsigned char or EOF" according to spec.  Passing a char is thus
suspect, since char can be signed or unsigned, depending on platform.
(Many previously cast to int, which was pointless.)

(cherry picked from commit f48656d89f02aefa7fe8a7f5ff731c91170a97c8)
redmine6147: the other fix.

It's possible for the handling of a connection to close that
connection; it's typically better to leave the HandleConnection()
level of code to deal with that, but this level may as well at least
cope gracefully if something *has* done so - provided it's suitably
marked the connection as having been closed.  So have DeleteConn()
actually pay attention to whether the socket is valid before closing
it.

(cherry picked from commit 01f36718c5a25993540b4fa7b7c3b8d2df296194)
Prepend rather than appending in Item lists.

The SV lists aren't order sensitive; and appending's costs are
quadratic compared to prepend's linear costs.

(cherry picked from commit a62d9afa4b7997f4d8cd66d2828d9a282ebc8612)
Typo fix s/emtpy/empty

(cherry picked from commit 4c4b23d30e6dc94fcb6aa3224ba6176939373002)
Executable files that aren't binaries and don't start with #! lines

One needed a #! line to make it robust; chmod -x worked for the rest.

(cherry picked from commit 6ffa3d698c2feead57fbb0058d21dde900920989)