Replace arg parsing with getopt

This commit is contained in:
Thomas Wade 2018-11-01 10:23:59 +10:30
parent 53600774d9
commit 28c9a69005

View File

@ -10,10 +10,12 @@
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <getopt.h>
#define BUFFERT 512
#define BACKLOG 1
void usage(char* programName);
int create_server_socket(int port);
struct sockaddr_in sock_serv, sock_clt;
@ -29,12 +31,31 @@ int main(int argc, char** argv)
time_t intps;
struct tm* tmi;
/* (A) repalce the following line with suitable getopt() functionality */
if (argc != 2)
// Parse options
char argchar;
while ((argchar = getopt(argc, argv, "")) != -1)
{
perror("Usage ./a.out <num_port> <file2send>\n");
switch (argchar)
{
default:
break;
}
}
// Parse positional arguments
int port;
char* file_path;
if (argv[optind] == NULL || argv[optind + 1] == NULL)
{
usage(argv[0]);
exit(3);
}
else
{
port = atoi(argv[optind]);
file_path = argv[optind + 1];
}
int l;
int yes = 1;
@ -154,3 +175,8 @@ int main(int argc, char** argv)
return EXIT_SUCCESS;
}
void usage(char* programName)
{
fprintf(stderr, "Usage: %s [OPTIONS] <port-number> <file-to-send>\n", programName);
}