formated output
welcome to malloc hell :)
This commit is contained in:
parent
72311d8b48
commit
7cbf60faeb
12
README.md
12
README.md
@ -5,10 +5,10 @@ shows permissions of path, given by argument, and all directories above
|
|||||||
## example
|
## example
|
||||||
```
|
```
|
||||||
# shperm .
|
# shperm .
|
||||||
Realpath: /home/damage/git/shperm
|
rwx r-x r-x damage damage /home/damage/git/shperm/shperm
|
||||||
/home/damage/git/shperm: damage damage rwx r-x r-x
|
rwx r-x r-x damage damage /home/damage/git/shperm
|
||||||
/home/damage/git: damage damage rwx r-x r-x
|
rwx r-x r-x damage damage /home/damage/git
|
||||||
/home/damage: damage damage rwx --- ---
|
rwx --- --- damage damage /home/damage
|
||||||
/home: root root rwx r-x r-x
|
rwx r-x r-x root root /home
|
||||||
/: root root rwx r-x r-x
|
rwx r-x r-x root root /
|
||||||
```
|
```
|
79
shperm.c
79
shperm.c
@ -13,46 +13,96 @@
|
|||||||
#define PERMISSION_GROUP 1
|
#define PERMISSION_GROUP 1
|
||||||
#define PERMISSION_OTHERS 0
|
#define PERMISSION_OTHERS 0
|
||||||
|
|
||||||
void printPermission(char* path, int permissions, int uid, int gid) {
|
typedef struct {
|
||||||
// print current path
|
char* username;
|
||||||
printf("%s: ", path);
|
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
|
// print user
|
||||||
struct passwd* user = getpwuid(uid);
|
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
|
// print group
|
||||||
struct group* group = getgrgid(gid);
|
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
|
// 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++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
// clear out all permissions except the i'th bit
|
// clear out all permissions except the i'th bit
|
||||||
int tmpPermission = permissions & (1 << i);
|
int tmpPermission = permissions & (1 << i);
|
||||||
|
int permissionUGO = i / 3;
|
||||||
|
int permissionRWX = (8 - i) % 3;
|
||||||
|
|
||||||
// read
|
// read
|
||||||
if (tmpPermission & S_IRUSR
|
if (tmpPermission & S_IRUSR
|
||||||
|| tmpPermission & S_IRGRP
|
|| tmpPermission & S_IRGRP
|
||||||
|| tmpPermission & S_IROTH) {
|
|| tmpPermission & S_IROTH) {
|
||||||
permissionStr[i / 3][(8 - i) % 3] = 'r';
|
lines[idx].permissionStr[permissionUGO][permissionRWX] = 'r';
|
||||||
}
|
}
|
||||||
|
|
||||||
// write
|
// write
|
||||||
if (tmpPermission & S_IWUSR
|
if (tmpPermission & S_IWUSR
|
||||||
|| tmpPermission & S_IWGRP
|
|| tmpPermission & S_IWGRP
|
||||||
|| tmpPermission & S_IWOTH) {
|
|| tmpPermission & S_IWOTH) {
|
||||||
permissionStr[i / 3][(8 - i) % 3] = 'w';
|
lines[idx].permissionStr[permissionUGO][permissionRWX] = 'w';
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute
|
// execute
|
||||||
if (tmpPermission & S_IXUSR
|
if (tmpPermission & S_IXUSR
|
||||||
|| tmpPermission & S_IXGRP
|
|| tmpPermission & S_IXGRP
|
||||||
|| tmpPermission & S_IXOTH) {
|
|| 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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@ -65,24 +115,23 @@ int main(int argc, char* argv[]) {
|
|||||||
path = malloc(PATH_MAX);
|
path = malloc(PATH_MAX);
|
||||||
|
|
||||||
if (realpath(argv[1], path)) {
|
if (realpath(argv[1], path)) {
|
||||||
printf("Realpath: %s\n", path);
|
|
||||||
|
|
||||||
char* base;
|
char* base;
|
||||||
do {
|
do {
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
||||||
base = basename(path);
|
base = basename(path);
|
||||||
if (stat(path, &sb) == 0) {
|
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 {
|
} else {
|
||||||
fprintf(stderr, "Error (%d): %s\n", errno, strerror(errno));
|
fprintf(stderr, "Error (%d): %s: %s\n", errno, strerror(errno), path);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
path = dirname(path);
|
path = dirname(path);
|
||||||
} while (strcmp(base, "/"));
|
} while (strcmp(base, "/"));
|
||||||
|
dumpLines();
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Error (%d): %s\n", errno, strerror(errno));
|
fprintf(stderr, "Error (%d): %s: %s\n", errno, strerror(errno), path);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user