aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/slibc-human.h21
-rw-r--r--src/slibc-human/humanmode.c75
2 files changed, 94 insertions, 2 deletions
diff --git a/include/slibc-human.h b/include/slibc-human.h
index ee55568..4c98902 100644
--- a/include/slibc-human.h
+++ b/include/slibc-human.h
@@ -33,7 +33,10 @@
-enum machinemode_mode
+/**
+ * Representation settings for file permissions.
+ */
+enum humanmode_mode
{
/**
* Return in the format where 0750
@@ -182,7 +185,21 @@ enum unescape_mode
-char* humanmode(char* restrict buffer, mode_t perm, enum machinemode_mode mode);
+/**
+ * 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, mode_t, enum humanmode_mode);
int machinemode(mode_t* restrict mode, mode_t* restrict mask, const char* restrict str);
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;
+}
+