From de3a96ef0c626555c0ffd619593b852b725eb993 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 15 Feb 2013 13:05:19 -0400 Subject: little xargs eqivilant as a pure function --- Utility/SafeCommand.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'Utility') diff --git a/Utility/SafeCommand.hs b/Utility/SafeCommand.hs index 026456327..e6075e888 100644 --- a/Utility/SafeCommand.hs +++ b/Utility/SafeCommand.hs @@ -85,3 +85,20 @@ prop_idempotent_shellEscape :: String -> Bool prop_idempotent_shellEscape s = [s] == (shellUnEscape . shellEscape) s prop_idempotent_shellEscape_multiword :: [String] -> Bool prop_idempotent_shellEscape_multiword s = s == (shellUnEscape . unwords . map shellEscape) s + +{- Segements a list of filenames into groups that are all below the manximum + - command-line length limit. Does not preserve order. -} +segmentXargs :: [FilePath] -> [[FilePath]] +segmentXargs l = go l [] 0 [] + where + go [] c _ r = c:r + go (f:fs) c accumlen r + | len < maxlen && newlen > maxlen = go (f:fs) [] 0 (c:r) + | otherwise = go fs (f:c) newlen r + where + len = length f + newlen = accumlen + len + + {- 10k of filenames per command, well under Linux's 20k limit; + - allows room for other parameters etc. -} + maxlen = 10240 -- cgit v1.2.3