klist -s fixes

This series fixes #8022 and fixes an output bug in klist -A -s, which is so obscure that I'm not sure it deserves a ticket.

Because I started with a small refactor, I'm not sure this is suitable for backporting, but the do_ccache loop would get really ugly without the refactor.  The behavior change may not be important enough to require a backport.