summaryrefslogtreecommitdiff
path: root/config.c
blob: 20c0bd14a6f9dd6d1111f200634d13311449e455 (plain)
    1 /**
    2  * Fd-enum is a simple tool to list open file descriptors of a pid by type
    3  * Copyright (C) 2023  Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "config.h"
   19 
   20 void get_help() {
   21   puts("Usage: \n\
   22   -d, --dead       Print list of dead file descriptors (deleted but still open)\n\
   23   -i, --inodes     Print list of inode file descriptors\n\
   24   -l, --live       Print list of live file descriptors (open and existing)\n\
   25   -p, --pipes      Print list of pipe file descriptors\n\
   26   -s, --sockets    Print list of socket file descriptors\n\
   27 \n\
   28   -n, --no-stats   Hide file descriptor statistics\n\
   29   -sf, --sizefd    Show total size of dead and live file descriptors\n\
   30   -sd, --sizedead  Show total size of dead file descriptors\n\
   31   -sl, --sizelive  Show total size of live file descriptors\n\
   32   --truncate-dead  Truncate dead file descriptors\n\
   33   -h, --help       Print this help text\n\
   34 ");
   35 }
   36 
   37 int config_from_argv(struct config* c, int argc, char* argv[]) {
   38   int i = 1;
   39   while(i < argc) {
   40     if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dead") == 0) {
   41       c->dead = 1;
   42     } else if(strcmp(argv[i], "--truncate-dead") == 0) {
   43       c->truncate_dead = 1;
   44     } else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--live") == 0) {
   45       c->live = 1;
   46     } else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--sockets") == 0) {
   47       c->sockets = 1;
   48     } else if(strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--inodes") == 0) {
   49       c->inodes = 1;
   50     } else if(strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pipes") == 0) {
   51       c->pipes = 1;
   52     } else if(strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--no-stats") == 0) {
   53       c->showstats = 0;
   54     } else if(strcmp(argv[i], "-sfd") == 0 || strcmp(argv[i], "--sizefd") == 0) {
   55       c->showsizefd = 1;
   56     } else if(strcmp(argv[i], "-sd") == 0 || strcmp(argv[i], "--sizedead") == 0) {
   57       c->showsizedead = 1;
   58     } else if(strcmp(argv[i], "-sl") == 0 || strcmp(argv[i], "--sizelive") == 0) {
   59       c->showsizelive = 1;
   60     } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
   61       get_help();
   62       return 1;
   63     } else if(argv[i][0] == '-') {
   64       fprintf(stderr, "Unknown argument \"%s\"\n", argv[i]);
   65       return 1;
   66     } else {
   67       c->pid = strtol(argv[i], NULL, 10);
   68     }
   69 
   70     i++;
   71   }
   72 
   73   if(c->pid == -1) {
   74     fprintf(stderr, "Must specify valid pid\n");
   75     return 2;
   76   }
   77   return 0;
   78 }

Generated by cgit