Skip to contentSkip to conversation

checkxalloc: nudge regex, fix newfound instances

PATCH Applied Dave Reisner 3 participants 11 messages Mar 18, 2012 – Mar 20, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 checkxalloc: nudge regex, fix newfound instances

Using the -w flag with grep actually fought against us here, and hid some instances where xalloc functions weren't used. Discard it in favor of an explicit word boundary as a prefix to the function name, and extend our requirements on the trailing side of the pattern.

This also fixes the few new instances that were overlooked because of the regex's deficiency.

---
 fdisk/sfdisk.c       |    2 +-
 sys-utils/swapon.c   |    4 ++--
 tools/checkxalloc.sh |    2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
fdisk/sfdisk.c +1 -1
index 6267652..e766b04 100644
@@ -304,7 +304,7 @@ restore_sectors(char *dev) {
 	error(_("partition restore file has wrong size - not restoring\n"));
 	goto err;
     }
-    if (!(ss0 = (char *)malloc(statbuf.st_size))) {
+    if (!(ss0 = xmalloc(statbuf.st_size))) {
 	error(_("out of memory?\n"));
 	goto err;
     }
sys-utils/swapon.c +2 -2
index f17fad6..fe16169 100644
@@ -197,7 +197,7 @@ read_proc_swaps(void) {
 			       *p = '\0';
 		}
 
-		q = realloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
+		q = xrealloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
 		if (q == NULL)
 			break;
 		swapFiles = q;
@@ -640,7 +640,7 @@ swapon_all(void) {
 		if (!streq(fstab->mnt_type, MNTTYPE_SWAP))
 			continue;
 
-		opts = strdup(fstab->mnt_opts);
+		opts = xstrdup(fstab->mnt_opts);
 
 		for (opt = strtok(opts, ","); opt != NULL;
 		     opt = strtok(NULL, ",")) {
tools/checkxalloc.sh +1 -1
index 578340e..6634c82 100755
@@ -10,7 +10,7 @@ cd "$(git rev-parse --show-toplevel)" || {
 }
 
 git grep -zl '#include "xalloc.h"' |
-  xargs -0 grep -nwE '[^x](([cm]|re)alloc|strdup)\('
+  xargs -0 grep -nE '\b(([cm]|re)alloc|strdup)[[:space:]]*\([^)]'
 
 result=$?
 
Signature · 1 line
1.7.9.4
#1 lore raw

PATCH 2/5 tunelp: remove old, now unneeded header

malloc and friends are provided by stdlib.h.

---
 sys-utils/tunelp.c |    1 -
 1 file changed, 1 deletion(-)
sys-utils/tunelp.c +0 -1
index 700fe65..98b00f6 100644
@@ -52,7 +52,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#include <malloc.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
Signature · 1 line
1.7.9.4
maintainer Applied
#2 lore raw

Re: [PATCH 2/5] tunelp: remove old, now unneeded header

Quoted 2 lines · Dave Reisner
 sys-utils/tunelp.c |    1 -
 1 file changed, 1 deletion(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw

PATCH 3/5 include/ttyutils.h: add include guards

In reply to Dave Reisner
---
 include/ttyutils.h |    4 ++++
 1 file changed, 4 insertions(+)
include/ttyutils.h +4 -0
index 949b72a..f638aa0 100644
@@ -1,3 +1,5 @@
+#ifndef UTIL_LINUX_TTYUTILS_H
+#define UTIL_LINUX_TTYUTILS_H
 
 #include <termios.h>
 
@@ -71,3 +73,5 @@ static inline void reset_virtual_console(struct termios *tp, int flags)
 	tp->c_cc[VLNEXT]   = CLNEXT;
 	tp->c_cc[VEOL2]    = _POSIX_VDISABLE;
 }
+
+#endif /* UTIL_LINUX_TTYUTILS_H */
Signature · 1 line
1.7.9.4
maintainer Applied
#4 lore raw

Re: [PATCH 3/5] include/ttyutils.h: add include guards

Quoted 2 lines · Dave Reisner
 include/ttyutils.h |    4 ++++
 1 file changed, 4 insertions(+)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw

PATCH 4/5 findmnt: add FS size, avail, used, and use% columns

In reply to Dave Reisner

Provide display of filesystem attributes from statvfs(3). These are all displayed in human readable format.

---
 misc-utils/findmnt.8 |    4 ++++
 misc-utils/findmnt.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
misc-utils/findmnt.8 +4 -0
index b6d80f0..c1734ca 100644
@@ -103,6 +103,10 @@ Define output columns.  Currently supported are
 .BR OLD-OPTIONS,
 .BR VFS-OPTIONS ,
 .BR FS-OPTIONS ,
+.BR SIZE ,
+.BR AVAIL ,
+.BR USED ,
+.BR USE% ,
 .BR LABEL
 and
 .BR UUID .
misc-utils/findmnt.c +53 -1

92 lines not on this page · raw message

Signature · 1 line
1.7.9.4
maintainer Applied
#6 lore raw

Re: [PATCH 4/5] findmnt: add FS size, avail, used, and use% columns

Quoted 2 lines · Dave Reisner
 misc-utils/findmnt.8 |    4 ++++
 misc-utils/findmnt.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++++-

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#7 lore raw

PATCH 5/5 findmnt: add -D, --df option to imitate df(1)

In reply to Dave Reisner
---
 misc-utils/findmnt.8 |    3 +++
 misc-utils/findmnt.c |   20 ++++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
misc-utils/findmnt.8 +3 -0
index c1734ca..8ad4432 100644
@@ -49,6 +49,9 @@ Search in
 The output is in the tree-like format.  This is the default.
 .IP "\fB\-c, \-\-canonicalize\fP"
 Canonicalize all printed paths.
+.IP "\fB\-D, \-\-df\fP"
+Imitate the output of df(1). This option is equivalent to
+"-o SOURCE,FSTYPE,SIZE,USED,AVAIL,USE%,TARGET".
 .IP "\fB\-d, \-\-direction \fIword\fP"
 The search direction -
 .IR forward
misc-utils/findmnt.c +18 -2
index 547be66..84092d2 100644
@@ -890,6 +890,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	fprintf(out, _(
 	" -a, --ascii            use ASCII chars for tree formatting\n"
 	" -c, --canonicalize     canonicalize printed paths\n"
+	" -D, --df               imitate the output of df(1)\n"
 	" -d, --direction <word> direction of search, 'forward' or 'backward'\n"
 	" -e, --evaluate         convert tags (LABEL/UUID) to device names\n"
 	" -F, --tab-file <path>  alternative file for --fstab, --mtab or --kernel options\n"
@@ -936,7 +937,7 @@ int main(int argc, char *argv[])
 	struct libmnt_table *tb = NULL;
 	char **tabfiles = NULL;
 	int direction = MNT_ITER_FORWARD;
-	int i, c, rc = -1, timeout = -1;
+	int i, c, rc = -1, timeout = -1, df_output = 0;
 	int ntabfiles = 0, tabtype = 0;
 
 	/* table.h */
@@ -946,6 +947,7 @@ int main(int argc, char *argv[])
 	    { "ascii",        0, 0, 'a' },
 	    { "canonicalize", 0, 0, 'c' },
 	    { "direction",    1, 0, 'd' },
+	    { "df",           0, 0, 'D' },
 	    { "evaluate",     0, 0, 'e' },
 	    { "first-only",   0, 0, 'f' },
 	    { "fstab",        0, 0, 's' },
@@ -982,7 +984,7 @@ int main(int argc, char *argv[])
 	tt_flags |= TT_FL_TREE;
 
 	while ((c = getopt_long(argc, argv,
-				"acd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:",
+				"acDd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:",
 				longopts, NULL)) != -1) {
 		switch(c) {
 		case 'a':
@@ -991,6 +993,10 @@ int main(int argc, char *argv[])
 		case 'c':
 			flags |= FL_CANONICALIZE;
 			break;
+		case 'D':
+			tt_flags &= ~TT_FL_TREE;
+			df_output = 1;
+			break;
 		case 'd':
 			if (!strcmp(optarg, "forward"))
 				direction = MNT_ITER_FORWARD;
@@ -1100,6 +1106,16 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (df_output) {
+		columns[ncolumns++] = COL_SOURCE;
+		columns[ncolumns++] = COL_FSTYPE;
+		columns[ncolumns++] = COL_SIZE;
+		columns[ncolumns++] = COL_USED;
+		columns[ncolumns++] = COL_AVAIL;
+		columns[ncolumns++] = COL_USEPERC;
+		columns[ncolumns++] = COL_TARGET;
+	}
+
 	/* default columns */
 	if (!ncolumns) {
 		if (flags & FL_POLL)
Signature · 1 line
1.7.9.4
maintainer Applied
#8 lore raw

Re: [PATCH 5/5] findmnt: add -D, --df option to imitate df(1)

Quoted 3 lines · Dave Reisner
 misc-utils/findmnt.8 |    3 +++
 misc-utils/findmnt.c |   20 ++++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

Applied, thanks.

I have added built-in filter to filter out pseudo filesystems for the df output and new --all option to disable this filter.

Karel

PS. yes, we have to fix (=de-duplicate) our and df(1) output, this is
still on TODO list.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#9 lore raw
In reply to Dave Reisner
Quoted 20 lines · Dave Reisner

Using the -w flag with grep actually fought against us here, and hid some instances where xalloc functions weren't used. Discard it in favor of an explicit word boundary as a prefix to the function name, and extend our requirements on the trailing side of the pattern.

This also fixes the few new instances that were overlooked because of the regex's deficiency.

---
 fdisk/sfdisk.c       |    2 +-
 sys-utils/swapon.c   |    4 ++--
 tools/checkxalloc.sh |    2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
fdisk/sfdisk.c +0 -0
index 6267652..e766b04 100644

fdisk/sfdisk.c restore_sectors(char *dev) { · line 310

Quoted 8 lines
fdisk/sfdisk.c +1 -1
@@ -304,7 +304,7 @@ restore_sectors(char *dev) {
 	error(_("partition restore file has wrong size - not restoring\n"));
 	goto err;
     }
-    if (!(ss0 = (char *)malloc(statbuf.st_size))) {
+    if (!(ss0 = xmalloc(statbuf.st_size))) {
 	error(_("out of memory?\n"));
 	goto err;
     }

xmalloc and friends abort the program when allocation fail.

sys-utils/swapon.c read_proc_swaps(void) { · line 202

Quoted 12 lines
sys-utils/swapon.c +1 -1
index f17fad6..fe16169 100644
@@ -197,7 +197,7 @@ read_proc_swaps(void) {
 			       *p = '\0';
 		}
 
-		q = realloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
+		q = xrealloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
 		if (q == NULL)
 			break;

same here. This would actually break functionality.

Quoted 23 lines
sys-utils/swapon.c +1 -1
 		swapFiles = q;
@@ -640,7 +640,7 @@ swapon_all(void) {
 		if (!streq(fstab->mnt_type, MNTTYPE_SWAP))
 			continue;
 
-		opts = strdup(fstab->mnt_opts);
+		opts = xstrdup(fstab->mnt_opts);
 
 		for (opt = strtok(opts, ","); opt != NULL;
 		     opt = strtok(NULL, ",")) {
tools/checkxalloc.sh +1 -1
index 578340e..6634c82 100755
@@ -10,7 +10,7 @@ cd "$(git rev-parse --show-toplevel)" || {
 }
 
 git grep -zl '#include "xalloc.h"' |
-  xargs -0 grep -nwE '[^x](([cm]|re)alloc|strdup)\('
+  xargs -0 grep -nE '\b(([cm]|re)alloc|strdup)[[:space:]]*\([^)]'
 
 result=$?
 
maintainer
#10 lore raw
In reply to Dave Reisner
Quoted 4 lines · Dave Reisner
 fdisk/sfdisk.c       |    2 +-
 sys-utils/swapon.c   |    4 ++--
 tools/checkxalloc.sh |    2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

Applied with some minor changes, thanks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com