summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-02-15 13:05:19 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-02-15 13:05:19 -0400
commitde3a96ef0c626555c0ffd619593b852b725eb993 (patch)
treeb7ffb0789c5b10579f2e524c5d41ab7225ed3d1b
parent3953855df59524e84bfefe30481c48e897ea9c37 (diff)
little xargs eqivilant as a pure function
-rw-r--r--Utility/SafeCommand.hs17
1 files changed, 17 insertions, 0 deletions
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