aboutsummaryrefslogtreecommitdiffstats
path: root/src/slibc-human
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-11-15 22:25:03 +0100
committerMattias Andrée <maandree@operamail.com>2015-11-15 22:25:03 +0100
commitd670dea78e1cad0d3b01db31f0c2a1a7d6960194 (patch)
tree6931714261f4d5b1eb4273fb1065f23fdc6b8a6f /src/slibc-human
parentm doc mallocz (diff)
downloadslibc-d670dea78e1cad0d3b01db31f0c2a1a7d6960194.tar.gz
slibc-d670dea78e1cad0d3b01db31f0c2a1a7d6960194.tar.bz2
slibc-d670dea78e1cad0d3b01db31f0c2a1a7d6960194.tar.xz
implement humanmode
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/slibc-human')
-rw-r--r--src/slibc-human/humanmode.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/slibc-human/humanmode.c b/src/slibc-human/humanmode.c
new file mode 100644
index 0000000..57b1534
--- /dev/null
+++ b/src/slibc-human/humanmode.c
@@ -0,0 +1,75 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <slib-human.h>
+#include <stdlib.h>
+#include <errno.h>
+
+
+
+/**
+ * Convert file permission from machine representation to human representation.
+ *
+ * @param buffer Sufficiently large buffer for the output, or `NULL`.
+ * 18 characters is always sufficient, regardless of `mode`.
+ * @param perm Machine representation of the permissions, will be masked with 07777.
+ * @param mode Representation style, 0 for default.
+ * @return Human representation of the file permissions, `NULL` on error.
+ * On success, the caller is responsible for deallocating the
+ * returned pointer, if and only if `buffer` is `NULL`.
+ *
+ * @throws EINVAL If `mode` is invalid.
+ * @throws ENOMEM The process cannot allocate more memory.
+ */
+char* humanmode(char* restrict buffer, mode_t perm, enum humanmode_mode mode)
+{
+ mode_t perm_ = perm | (mode != MACHINEMODE_MASK ? 07777 : 0);
+ int name = mode & MACHINEMODE_MASK;
+ char* w;
+
+ if (mode & ~3)
+ return errno = EINVAL, NULL;
+ if (!mode)
+ mode = MACHINEMODE_STAT;
+
+ if (buffer == NULL)
+ buffer = malloc((mode == MACHINEMODE_STAT ? 10 : 18) * sizeof(char));
+ if (buffer == NULL)
+ return NULL;
+ w = buffer;
+
+ if (name) *w++ = 'u', *w++ = '=';
+ if (perm_ & S_IRUSR) *w++ = (perm & S_IRUSR) ? 'r' : '-';
+ if (perm_ & S_IWUSR) *w++ = (perm & S_IWUSR) ? 'w' : '-';
+ if (perm & S_ISUID) *w++ = (perm & S_IXUSR) ? 's' : 'S';
+ else if (perm_ & S_IXUSR) *w++ = (perm & S_IXUSR) ? 'x' : '-';
+
+ if (name) *w++ = ',', *w++ = 'g', *w++ = '=';
+ if (perm_ & S_IRGRP) *w++ = (perm & S_IRGRP) ? 'r' : '-';
+ if (perm_ & S_IWGRP) *w++ = (perm & S_IWGRP) ? 'w' : '-';
+ if (perm & S_ISGID) *w++ = (perm & S_IXGRP) ? 's' : 'S';
+ else if (perm_ & S_IXGRP) *w++ = (perm & S_IXGRP) ? 'x' : '-';
+
+ if (name) *w++ = ',', *w++ = 'o', *w++ = '=';
+ if (perm_ & S_IROTH) *w++ = (perm & S_IROTH) ? 'r' : '-';
+ if (perm_ & S_IWOTH) *w++ = (perm & S_IWOTH) ? 'w' : '-';
+ if (perm & S_ISVTX) *w++ = (perm & S_IXOTH) ? 't' : 'T';
+ else if (perm_ & S_IXOTH) *w++ = (perm & S_IXOTH) ? 'x' : '-';
+
+ return *w = 0, buffer;
+}
+