diff options
author | Aaron Ball <nullspoon@oper.io> | 2023-06-09 16:57:09 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2023-06-09 16:57:09 -0600 |
commit | 5ffcbc32ea71073604a2c007b5ba60ec587b812e (patch) | |
tree | 2c4a1c7b60fe3d9ee0745bdfbb212273bd425e6c | |
parent | 68322f9bf918f40d97ffbdeb12979f83597ed2f2 (diff) | |
parent | 4ee7941b4cb7c3b50e1cb054d196fac74799f9d9 (diff) | |
download | fd-enum-5ffcbc32ea71073604a2c007b5ba60ec587b812e.tar.gz fd-enum-5ffcbc32ea71073604a2c007b5ba60ec587b812e.tar.xz |
Merge branch 'implement-size-opt'
-rw-r--r-- | main.c | 57 |
1 files changed, 49 insertions, 8 deletions
@@ -30,6 +30,9 @@ struct config { char inodes; char pipes; char showstats; + char showsizefd; + char showsizelive; + char showsizedead; char truncate_dead; }; @@ -50,11 +53,26 @@ void get_help() { -s, --sockets Print list of socket file descriptors\n\ \n\ -n, --no-stats Hide file descriptor statistics\n\ + -sf, --sizefd Show total size of dead and live file descriptors\n\ + -sd, --sizedead Show total size of dead file descriptors\n\ + -sl, --sizelive Show total size of live file descriptors\n\ --truncate-dead Truncate dead file descriptors\n\ -h, --help Print this help text\n\ "); } +long fgetsize(char* path) { + FILE* fd = NULL; + long size = 0; + fd = fopen(path, "r"); + + fseek(fd, 0, SEEK_END); + size = ftell(fd); + + fclose(fd); + return size; +} + int parse_args(int argc, char* argv[], struct config* c) { // Initialize config struct c->pid = -1; @@ -65,6 +83,9 @@ int parse_args(int argc, char* argv[], struct config* c) { c->pipes = 0; c->inodes = 0; c->showstats = 1; + c->showsizefd = 0; + c->showsizedead = 0; + c->showsizelive = 0; int i = 1; while(i < argc) { @@ -82,9 +103,18 @@ int parse_args(int argc, char* argv[], struct config* c) { c->pipes = 1; } else if(strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--no-stats") == 0) { c->showstats = 0; + } else if(strcmp(argv[i], "-sfd") == 0 || strcmp(argv[i], "--sizefd") == 0) { + c->showsizefd = 1; + } else if(strcmp(argv[i], "-sd") == 0 || strcmp(argv[i], "--sizedead") == 0) { + c->showsizedead = 1; + } else if(strcmp(argv[i], "-sl") == 0 || strcmp(argv[i], "--sizelive") == 0) { + c->showsizelive = 1; } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { get_help(); exit(1); + } else if(argv[i][0] == '-') { + fprintf(stderr, "Unknown argument \"%s\"\n", argv[i]); + exit(1); } else { c->pid = strtol(argv[i], NULL, 10); } @@ -107,12 +137,14 @@ int main(int argc, char* argv[]) { char lnpath[512] = {'\0'}; int retval = 0; int len = 0; - unsigned long unknown = 0; - unsigned long dead = 0; - unsigned long live = 0; - unsigned long sockets = 0; - unsigned long inodes = 0; - unsigned long pipes = 0; + unsigned long livesize = 0; + unsigned long deadsize = 0; + unsigned long unknown = 0; + unsigned long dead = 0; + unsigned long live = 0; + unsigned long sockets = 0; + unsigned long inodes = 0; + unsigned long pipes = 0; struct stat sb; @@ -155,19 +187,22 @@ int main(int argc, char* argv[]) { printf("%s -> %s\n", fdpath, lnpath); // File desccriptors - } else if(S_ISREG(sb.st_mode) - || S_ISDIR(sb.st_mode)) { + } else if(S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)) { // Exists if(retval == 0 && fexists(lnpath)) { live++; if(c.live) printf("%s -> %s\n", fdpath, lnpath); + if(c.showsizefd || c.showsizelive) + livesize += fgetsize(fdpath); // Does not exist } else { dead++; if(c.dead) printf("%s -> %s\n", fdpath, lnpath); + if(c.showsizefd || c.showsizedead) + deadsize += fgetsize(fdpath); if(c.truncate_dead) truncate(fdpath, 0); } @@ -192,6 +227,12 @@ int main(int argc, char* argv[]) { printf("pipes: %ld\n", pipes); printf("unknown: %ld\n", unknown); } + if(c.showsizelive) + printf("livesize: %ld KB\n", livesize / 1024); + if(c.showsizedead) + printf("deadsize: %ld KB\n", deadsize / 1024); + if(c.showsizefd) + printf("fdsize: %ld KB\n", (livesize + deadsize) / 1024); return 0; } |