aboutsummaryrefslogtreecommitdiff
path: root/lib/e2p/ls.c
diff options
context:
space:
mode:
authorAndreas Dilger <adilger@dilger.ca>2020-01-14 14:42:18 -0700
committerTheodore Ts'o <tytso@mit.edu>2020-01-24 23:06:58 -0500
commitbc56227376223c02b16703691fd0de6929a1036b (patch)
tree152c8394b763419a418e0c68fc1bab20e0367d7f /lib/e2p/ls.c
parent6b430d60caf6c4080bd32783659a819425657b69 (diff)
downloade2fsprogs-bc56227376223c02b16703691fd0de6929a1036b.tar.gz
mmp: abstract out repeated 'sizeof(buf), buf' usage
The printf("%.*s") format requires both the buffer size and buffer pointer to be specified for each use. Since this is repeatedly given as "(int)sizeof(buf), (char *)buf" for mmp_nodename and mmp_bdevname fields, with typecasts to avoid compiler warnings. Add a helper macro EXT2_LEN_STR() to avoid repeated boilerplate code. This can also be used for other superblock buffer fields that may not have NUL-terminated strings (e.g. s_volume_name, s_last_mounted, s_{first,last}_error_func, s_mount_opts) to simplify code and avoid the need for temporary buffers for NUL-termination. Annotate the superblock string fields that may not be NUL-terminated. Signed-off-by: Andreas Dilger <adilger@dilger.ca> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Diffstat (limited to 'lib/e2p/ls.c')
-rw-r--r--lib/e2p/ls.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/lib/e2p/ls.c b/lib/e2p/ls.c
index 5a446178..15210999 100644
--- a/lib/e2p/ls.c
+++ b/lib/e2p/ls.c
@@ -224,7 +224,7 @@ static const char *quota_type2prefix(enum quota_type qtype)
void list_super2(struct ext2_super_block * sb, FILE *f)
{
int inode_blocks_per_group;
- char buf[80], *str;
+ char *str;
time_t tm;
enum quota_type qtype;
@@ -232,18 +232,16 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
EXT2_INODE_SIZE(sb)) +
EXT2_BLOCK_SIZE(sb) - 1) /
EXT2_BLOCK_SIZE(sb));
- if (sb->s_volume_name[0]) {
- memset(buf, 0, sizeof(buf));
- strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
- } else
- strcpy(buf, "<none>");
- fprintf(f, "Filesystem volume name: %s\n", buf);
- if (sb->s_last_mounted[0]) {
- memset(buf, 0, sizeof(buf));
- strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
- } else
- strcpy(buf, "<not available>");
- fprintf(f, "Last mounted on: %s\n", buf);
+ if (sb->s_volume_name[0])
+ fprintf(f, "Filesystem volume name: %.*s\n",
+ EXT2_LEN_STR(sb->s_volume_name));
+ else
+ fprintf(f, "Filesystem volume name: <none>\n");
+ if (sb->s_last_mounted[0])
+ fprintf(f, "Last mounted on: %.*s\n",
+ EXT2_LEN_STR(sb->s_last_mounted));
+ else
+ fprintf(f, "Last mounted on: <not available>\n");
fprintf(f, "Filesystem UUID: %s\n", e2p_uuid2str(sb->s_uuid));
fprintf(f, "Filesystem magic number: 0x%04X\n", sb->s_magic);
fprintf(f, "Filesystem revision #: %d", sb->s_rev_level);
@@ -259,7 +257,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
print_super_flags(sb, f);
print_mntopts(sb, f);
if (sb->s_mount_opts[0])
- fprintf(f, "Mount options: %s\n", sb->s_mount_opts);
+ fprintf(f, "Mount options: %.*s\n",
+ EXT2_LEN_STR(sb->s_mount_opts));
fprintf(f, "Filesystem state: ");
print_fs_state (f, sb->s_state);
fprintf(f, "\n");
@@ -419,10 +418,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
if (sb->s_first_error_time) {
tm = sb->s_first_error_time;
fprintf(f, "First error time: %s", ctime(&tm));
- memset(buf, 0, sizeof(buf));
- strncpy(buf, (char *)sb->s_first_error_func,
- sizeof(sb->s_first_error_func));
- fprintf(f, "First error function: %s\n", buf);
+ fprintf(f, "First error function: %.*s\n",
+ EXT2_LEN_STR(sb->s_first_error_func));
fprintf(f, "First error line #: %u\n",
sb->s_first_error_line);
fprintf(f, "First error inode #: %u\n",
@@ -433,10 +430,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
if (sb->s_last_error_time) {
tm = sb->s_last_error_time;
fprintf(f, "Last error time: %s", ctime(&tm));
- memset(buf, 0, sizeof(buf));
- strncpy(buf, (char *)sb->s_last_error_func,
- sizeof(sb->s_last_error_func));
- fprintf(f, "Last error function: %s\n", buf);
+ fprintf(f, "Last error function: %.*s\n",
+ EXT2_LEN_STR(sb->s_last_error_func));
fprintf(f, "Last error line #: %u\n",
sb->s_last_error_line);
fprintf(f, "Last error inode #: %u\n",