From 7cbf60faeb8b81f404619d7105ea753833eb403c Mon Sep 17 00:00:00 2001 From: damage Date: Sun, 25 Aug 2024 20:39:13 +0200 Subject: [PATCH] formated output welcome to malloc hell :) --- README.md | 12 ++++----- shperm.c | 79 ++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 70 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8eab9b7..b4dcf53 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ shows permissions of path, given by argument, and all directories above ## example ``` # shperm . -Realpath: /home/damage/git/shperm -/home/damage/git/shperm: damage damage rwx r-x r-x -/home/damage/git: damage damage rwx r-x r-x -/home/damage: damage damage rwx --- --- -/home: root root rwx r-x r-x -/: root root rwx r-x r-x +rwx r-x r-x damage damage /home/damage/git/shperm/shperm +rwx r-x r-x damage damage /home/damage/git/shperm +rwx r-x r-x damage damage /home/damage/git +rwx --- --- damage damage /home/damage +rwx r-x r-x root root /home +rwx r-x r-x root root / ``` \ No newline at end of file diff --git a/shperm.c b/shperm.c index 769b497..043b8df 100644 --- a/shperm.c +++ b/shperm.c @@ -13,46 +13,96 @@ #define PERMISSION_GROUP 1 #define PERMISSION_OTHERS 0 -void printPermission(char* path, int permissions, int uid, int gid) { - // print current path - printf("%s: ", path); +typedef struct { + char* username; + char* groupname; + char permissionStr[3][4]; + char* path; +} line; + +line* lines; +int linesNo = 0; +int maxUsername = 0; +int maxGroupname = 0; + +void addLine(char* path, int permissions, int uid, int gid) { + linesNo++; + + // add one line to the lines array + if (linesNo == 1) { + lines = malloc(sizeof(*lines)); + } else { + if ((lines = realloc(lines, linesNo * sizeof(*lines))) == NULL) { + fprintf(stderr, "Error allocating memory\n"); + exit(4); + } + } + + // calculate line (index) we are at, which is 0-based (thus -1) + int idx = linesNo - 1; // print user struct passwd* user = getpwuid(uid); - printf("%s ", user->pw_name); + lines[idx].username = malloc(strlen(user->pw_name) + 1); + strcpy(lines[idx].username, user->pw_name); + if (maxUsername < strlen(lines[idx].username)) { + maxUsername = strlen(lines[idx].username); + } // print group struct group* group = getgrgid(gid); - printf("%s ", group->gr_name); + lines[idx].groupname = malloc(strlen(group->gr_name) + 1); + strcpy(lines[idx].groupname, group->gr_name); + if (maxGroupname < strlen(lines[idx].groupname)) { + maxGroupname = strlen(lines[idx].groupname); + } // print permissions - char permissionStr[3][4] = {"---\0", "---\0", "---\0"}; + strcpy(lines[idx].permissionStr[0], "---"); + strcpy(lines[idx].permissionStr[1], "---"); + strcpy(lines[idx].permissionStr[2], "---"); for (int i = 0; i < 9; i++) { // clear out all permissions except the i'th bit int tmpPermission = permissions & (1 << i); + int permissionUGO = i / 3; + int permissionRWX = (8 - i) % 3; // read if (tmpPermission & S_IRUSR || tmpPermission & S_IRGRP || tmpPermission & S_IROTH) { - permissionStr[i / 3][(8 - i) % 3] = 'r'; + lines[idx].permissionStr[permissionUGO][permissionRWX] = 'r'; } // write if (tmpPermission & S_IWUSR || tmpPermission & S_IWGRP || tmpPermission & S_IWOTH) { - permissionStr[i / 3][(8 - i) % 3] = 'w'; + lines[idx].permissionStr[permissionUGO][permissionRWX] = 'w'; } // execute if (tmpPermission & S_IXUSR || tmpPermission & S_IXGRP || tmpPermission & S_IXOTH) { - permissionStr[i / 3][(8 - i) % 3] = 'x'; + lines[idx].permissionStr[permissionUGO][permissionRWX] = 'x'; } } - printf("%s %s %s\n", permissionStr[PERMISSION_USER], permissionStr[PERMISSION_GROUP], permissionStr[PERMISSION_OTHERS]); + + // print current path + lines[idx].path = malloc(strlen(path) + 1); + strcpy(lines[idx].path, path); +} + +void dumpLines() { + for (int i = 0; i < linesNo; i++) { + printf("%s ", lines[i].permissionStr[PERMISSION_USER]); + printf("%s ", lines[i].permissionStr[PERMISSION_GROUP]); + printf("%s ", lines[i].permissionStr[PERMISSION_OTHERS]); + printf("%-*s ", maxUsername, lines[i].username); + printf("%-*s ", maxGroupname, lines[i].groupname); + printf("%s\n", lines[i].path); + } } int main(int argc, char* argv[]) { @@ -65,24 +115,23 @@ int main(int argc, char* argv[]) { path = malloc(PATH_MAX); if (realpath(argv[1], path)) { - printf("Realpath: %s\n", path); - char* base; do { struct stat sb; base = basename(path); if (stat(path, &sb) == 0) { - printPermission(path, sb.st_mode, sb.st_uid, sb.st_gid); + addLine(path, sb.st_mode, sb.st_uid, sb.st_gid); } else { - fprintf(stderr, "Error (%d): %s\n", errno, strerror(errno)); + fprintf(stderr, "Error (%d): %s: %s\n", errno, strerror(errno), path); exit(3); } path = dirname(path); } while (strcmp(base, "/")); + dumpLines(); } else { - fprintf(stderr, "Error (%d): %s\n", errno, strerror(errno)); + fprintf(stderr, "Error (%d): %s: %s\n", errno, strerror(errno), path); exit(2); }