summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak2010-11-24 16:41:20 +0100
committerKarel Zak2010-11-24 17:08:32 +0100
commitce877f2d161b80119fd6bf6ccd2c7013252156d7 (patch)
tree8da155620529ca2103509a257841ecc2691a22e4 /lib
parentlib: [xalloc] add xstrdup() (diff)
downloadkernel-qcow2-util-linux-ce877f2d161b80119fd6bf6ccd2c7013252156d7.tar.gz
kernel-qcow2-util-linux-ce877f2d161b80119fd6bf6ccd2c7013252156d7.tar.xz
kernel-qcow2-util-linux-ce877f2d161b80119fd6bf6ccd2c7013252156d7.zip
lib: [strutils] move strmode() from namei.c to strutils.c
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/strutils.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index f394800de..dcae9f2d3 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -8,6 +8,7 @@
#include <ctype.h>
#include <errno.h>
#include <err.h>
+#include <sys/stat.h>
static int do_scale_by_power (uintmax_t *x, int base, int power)
{
@@ -184,3 +185,42 @@ err:
errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
return 0;
}
+
+/*
+ * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must
+ * be 10 bytes.
+ */
+void strmode(mode_t mode, char *str)
+{
+ if (S_ISDIR(mode))
+ str[0] = 'd';
+ else if (S_ISLNK(mode))
+ str[0] = 'l';
+ else if (S_ISCHR(mode))
+ str[0] = 'c';
+ else if (S_ISBLK(mode))
+ str[0] = 'b';
+ else if (S_ISSOCK(mode))
+ str[0] = 's';
+ else if (S_ISFIFO(mode))
+ str[0] = 'p';
+ else if (S_ISREG(mode))
+ str[0] = '-';
+
+ str[1] = mode & S_IRUSR ? 'r' : '-';
+ str[2] = mode & S_IWUSR ? 'w' : '-';
+ str[3] = (mode & S_ISUID
+ ? (mode & S_IXUSR ? 's' : 'S')
+ : (mode & S_IXUSR ? 'x' : '-'));
+ str[4] = mode & S_IRGRP ? 'r' : '-';
+ str[5] = mode & S_IWGRP ? 'w' : '-';
+ str[6] = (mode & S_ISGID
+ ? (mode & S_IXGRP ? 's' : 'S')
+ : (mode & S_IXGRP ? 'x' : '-'));
+ str[7] = mode & S_IROTH ? 'r' : '-';
+ str[8] = mode & S_IWOTH ? 'w' : '-';
+ str[9] = (mode & S_ISVTX
+ ? (mode & S_IXOTH ? 't' : 'T')
+ : (mode & S_IXOTH ? 'x' : '-'));
+ str[10] = '\0';
+}