summaryrefslogtreecommitdiffstats
path: root/mount/sundries.c
diff options
context:
space:
mode:
authorKarel Zak2007-05-04 09:14:04 +0200
committerKarel Zak2007-05-04 09:14:04 +0200
commit6573c985a4077fa7d50ccb993bae177526fde8ec (patch)
tree530d9d7c06834472fd1f1672002c62e8f4afa719 /mount/sundries.c
parentmount: use growable string for options (diff)
downloadkernel-qcow2-util-linux-6573c985a4077fa7d50ccb993bae177526fde8ec.tar.gz
kernel-qcow2-util-linux-6573c985a4077fa7d50ccb993bae177526fde8ec.tar.xz
kernel-qcow2-util-linux-6573c985a4077fa7d50ccb993bae177526fde8ec.zip
mount: use realloc for xstrconcat functions
Everywhere in code is s = xstrconcat3(s, t, u); that's nonsense use malloc(new_s); free(s); return new_s; Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'mount/sundries.c')
-rw-r--r--mount/sundries.c86
1 files changed, 44 insertions, 42 deletions
diff --git a/mount/sundries.c b/mount/sundries.c
index 1be9099ba..e6208e435 100644
--- a/mount/sundries.c
+++ b/mount/sundries.c
@@ -15,6 +15,7 @@
#include "fstab.h"
#include "sundries.h"
#include "realpath.h"
+#include "xmalloc.h"
#include "nfsmount.h"
#include "nls.h"
@@ -32,50 +33,53 @@ xstrndup (const char *s, int n) {
return t;
}
+/* reallocates its first arg - typical use: s = xstrconcat3(s,t,u); */
char *
-xstrconcat2 (const char *s, const char *t) {
- char *res;
-
- if (!s) s = "";
- if (!t) t = "";
- res = xmalloc(strlen(s) + strlen(t) + 1);
- strcpy(res, s);
- strcat(res, t);
- return res;
-}
+xstrconcat3 (char *s, const char *t, const char *u) {
+ size_t len = 0;
-/* frees its first arg - typical use: s = xstrconcat3(s,t,u); */
-char *
-xstrconcat3 (const char *s, const char *t, const char *u) {
- char *res;
-
- if (!s) s = "";
- if (!t) t = "";
- if (!u) u = "";
- res = xmalloc(strlen(s) + strlen(t) + strlen(u) + 1);
- strcpy(res, s);
- strcat(res, t);
- strcat(res, u);
- free((void *) s);
- return res;
+ len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) + (u ? strlen(u) : 0);
+
+ if (!len)
+ return NULL;
+ if (!s) {
+ s = xmalloc(len + 1);
+ *s = '\0';
+ }
+ else
+ s = xrealloc(s, len + 1);
+ if (t)
+ strcat(s, t);
+ if (u)
+ strcat(s, u);
+ return s;
}
/* frees its first arg - typical use: s = xstrconcat4(s,t,u,v); */
char *
-xstrconcat4 (const char *s, const char *t, const char *u, const char *v) {
- char *res;
-
- if (!s) s = "";
- if (!t) t = "";
- if (!u) u = "";
- if (!v) v = "";
- res = xmalloc(strlen(s) + strlen(t) + strlen(u) + strlen(v) + 1);
- strcpy(res, s);
- strcat(res, t);
- strcat(res, u);
- strcat(res, v);
- free((void *) s);
- return res;
+xstrconcat4 (char *s, const char *t, const char *u, const char *v) {
+ size_t len = 0;
+
+ len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) +
+ (u ? strlen(u) : 0) + (v ? strlen(v) : 0);
+
+ if (!len)
+ return NULL;
+ if (!s) {
+ s = xmalloc(len + 1);
+ *s = '\0';
+ }
+ else
+ s = xrealloc(s, len + 1);
+ if (t)
+ strcat(s, t);
+ if (u)
+ strcat(s, u);
+ if (v)
+ strcat(s, v);
+ return s;
+
+
}
/* Call this with SIG_BLOCK to block and SIG_UNBLOCK to unblock. */
@@ -96,15 +100,13 @@ block_signals (int how) {
void
error (const char *fmt, ...) {
va_list args;
- char *fmt2;
if (mount_quiet)
return;
- fmt2 = xstrconcat2 (fmt, "\n");
va_start (args, fmt);
- vfprintf (stderr, fmt2, args);
+ vfprintf (stderr, fmt, args);
va_end (args);
- free (fmt2);
+ fputc('\n', stderr);
}
/* True if fstypes match. Null *TYPES means match anything,