Imported Upstream version 1.6
[debian/gzip] / lib / quotearg.c
index 1ea583d9c2a8ac5c2924a0bf49341aab3e34f9b4..40114d7fbd40afc655b6a54656fe399b7debc719 100644 (file)
@@ -1,6 +1,6 @@
 /* quotearg.c - quote arguments for output
 
-   Copyright (C) 1998-2002, 2004-2012 Free Software Foundation, Inc.
+   Copyright (C) 1998-2002, 2004-2013 Free Software Foundation, Inc.
 
    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
@@ -348,7 +348,12 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
 
       if (backslash_escapes
           && quote_string_len
-          && i + quote_string_len <= argsize
+          && (i + quote_string_len
+              <= (argsize == SIZE_MAX && 1 < quote_string_len
+                  /* Use strlen only if we must: when argsize is SIZE_MAX,
+                     and when the quote string is more than 1 byte long.
+                     If we do call strlen, save the result.  */
+                  ? (argsize = strlen (arg)) : argsize))
           && memcmp (arg + i, quote_string, quote_string_len) == 0)
         {
           if (elide_outer_quotes)
@@ -929,7 +934,7 @@ quotearg_custom_mem (char const *left_quote, char const *right_quote,
 }
 
 
-/* The quoting option used by quote_n and quote.  */
+/* The quoting option used by the functions of quote.h.  */
 struct quoting_options quote_quoting_options =
   {
     locale_quoting_style,
@@ -939,13 +944,25 @@ struct quoting_options quote_quoting_options =
   };
 
 char const *
-quote_n (int n, char const *name)
+quote_n_mem (int n, char const *arg, size_t argsize)
 {
-  return quotearg_n_options (n, name, SIZE_MAX, &quote_quoting_options);
+  return quotearg_n_options (n, arg, argsize, &quote_quoting_options);
 }
 
 char const *
-quote (char const *name)
+quote_mem (char const *arg, size_t argsize)
 {
-  return quote_n (0, name);
+  return quote_n_mem (0, arg, argsize);
+}
+
+char const *
+quote_n (int n, char const *arg)
+{
+  return quote_n_mem (n, arg, SIZE_MAX);
+}
+
+char const *
+quote (char const *arg)
+{
+  return quote_n (0, arg);
 }