Replace arg parsing with getopt
This commit is contained in:
parent
53600774d9
commit
28c9a69005
32
server1.c
32
server1.c
@ -10,10 +10,12 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
#define BUFFERT 512
|
#define BUFFERT 512
|
||||||
#define BACKLOG 1
|
#define BACKLOG 1
|
||||||
|
|
||||||
|
void usage(char* programName);
|
||||||
int create_server_socket(int port);
|
int create_server_socket(int port);
|
||||||
struct sockaddr_in sock_serv, sock_clt;
|
struct sockaddr_in sock_serv, sock_clt;
|
||||||
|
|
||||||
@ -29,12 +31,31 @@ int main(int argc, char** argv)
|
|||||||
time_t intps;
|
time_t intps;
|
||||||
struct tm* tmi;
|
struct tm* tmi;
|
||||||
|
|
||||||
/* (A) repalce the following line with suitable getopt() functionality */
|
// Parse options
|
||||||
if (argc != 2)
|
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);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
port = atoi(argv[optind]);
|
||||||
|
file_path = argv[optind + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int l;
|
int l;
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
@ -154,3 +175,8 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void usage(char* programName)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [OPTIONS] <port-number> <file-to-send>\n", programName);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user