permconv

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit a0a625d2e653984ddbe60d28a74e357fe818f0a0
Author: Friedel Schön <[email protected]>
Date:   Sat,  7 Sep 2024 18:06:28 +0200

first commit

Diffstat:
Apermconv | 0
Apermconv.c | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/permconv b/permconv Binary files differ. diff --git a/permconv.c b/permconv.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void from_oct(int permission, char *result) { + sprintf(result, "%c%c%c%c%c%c%c%c%c", (permission & 0400) ? 'r' : '-', + (permission & 0200) ? 'w' : '-', (permission & 0100) ? 'x' : '-', + (permission & 0040) ? 'r' : '-', (permission & 0020) ? 'w' : '-', + (permission & 0010) ? 'x' : '-', (permission & 0004) ? 'r' : '-', + (permission & 0002) ? 'w' : '-', (permission & 0001) ? 'x' : '-'); +} + +int to_oct(const char *permission) { + int result = 0; + for (int i = 0; i < 9; i++) { + if (permission[i] != '-') { + result |= 1 << (8 - i); + } + } + return result; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s <permission>\n", argv[0]); + return EXIT_FAILURE; + } + + char *permission = argv[1]; + int numericPermission; + + // Check if input is numeric or symbolic + if (permission[0] >= '0' && permission[0] <= '7') { + // Numeric input + numericPermission = strtol(permission, NULL, 8); + from_oct(numericPermission, permission); + printf("%s\n", permission); + } else { + // Symbolic input + numericPermission = to_oct(permission); + printf("%o\n", numericPermission); + } + + return EXIT_SUCCESS; +}