Skip to content

Commit

Permalink
tftpd: use getopt_long() to parse options, and add usage
Browse files Browse the repository at this point in the history
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
  • Loading branch information
kerolasa committed Feb 2, 2019
1 parent 0ba1fb3 commit 9ceff21
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions tftpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <netdb.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -498,6 +499,17 @@ int tftpd_inetd(struct run_state *ctl)
return 1;
}

static void __attribute__((__noreturn__)) usage(void)
{
printf("\nUsage:\n");
printf(" %s [options] directory\n", program_invocation_short_name);
printf("\nOptions:\n");
printf(" -h, --help display this help\n");
printf(" -V, --version display version\n");
printf("\nFor more details see tftpd(8).\n");
exit(EXIT_SUCCESS);
}

int main(int ac, char **av)
{
struct run_state ctl = {
Expand All @@ -508,15 +520,28 @@ int main(int ac, char **av)
{"octet", validate_access, sendfile, recvfile, 0}
}
};
int n = 0;
int c, n = 0;
static const struct option longopts[] = {
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};

global_ctl = &ctl;

if (ac == 2 && !strcmp(av[1], "-V")) {
printf(IPUTILS_VERSION("tftpd"));
return 0;
}

while ((c = getopt_long(ac, av, "Vh", longopts, NULL)) != -1)
switch (c) {
case 'V':
printf(IPUTILS_VERSION("tftpd"));
return EXIT_SUCCESS;
case 'h':
usage();
default:
fprintf(stderr,
"Try '%s --help' for more information.\n",
program_invocation_short_name);
exit(1);
}
ac--;
av++;
while (ac-- > 0 && n < MAXARG)
Expand Down

0 comments on commit 9ceff21

Please sign in to comment.