summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/strutils.h4
-rw-r--r--lib/strutils.c40
-rw-r--r--misc-utils/Makefile.am2
-rw-r--r--misc-utils/namei.c37
4 files changed, 47 insertions, 36 deletions
diff --git a/include/strutils.h b/include/strutils.h
index e24496d3c..31cf80603 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -3,6 +3,7 @@
#include <inttypes.h>
#include <string.h>
+#include <sys/types.h>
extern int strtosize(const char *str, uintmax_t *res);
extern long strtol_or_err(const char *str, const char *errmesg);
@@ -23,4 +24,7 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
strncpy(dest, src, n-1);
dest[n-1] = 0;
}
+
+extern void strmode(mode_t mode, char *str);
+
#endif
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';
+}
diff --git a/misc-utils/Makefile.am b/misc-utils/Makefile.am
index 76b69fa4a..c88b7c858 100644
--- a/misc-utils/Makefile.am
+++ b/misc-utils/Makefile.am
@@ -20,6 +20,8 @@ CLEANFILES = chkdupexe
dist_man_MANS = cal.1 chkdupexe.1 ddate.1 logger.1 look.1 mcookie.1 \
namei.1 script.1 whereis.1 scriptreplay.1
+namei_SOURCES = namei.c $(top_srcdir)/lib/strutils.c
+
if BUILD_LIBUUID
usrbin_exec_PROGRAMS += uuidgen
dist_man_MANS += uuidgen.1
diff --git a/misc-utils/namei.c b/misc-utils/namei.c
index 0342a08cb..e75a165a2 100644
--- a/misc-utils/namei.c
+++ b/misc-utils/namei.c
@@ -38,6 +38,7 @@
#include "xalloc.h"
#include "nls.h"
#include "widechar.h"
+#include "strutils.h"
#ifndef MAXSYMLINKS
#define MAXSYMLINKS 256
@@ -362,42 +363,6 @@ follow_symlinks(struct namei *nm)
return 0;
}
-static 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';
-}
-
static int
print_namei(struct namei *nm, char *path)
{