Added chdir and parameter

This commit is contained in:
Christopher Ramey 2015-02-04 14:54:41 +00:00 committed by cdramey
parent 8c1009cea3
commit d5bbdd6c44

View File

@ -19,7 +19,7 @@ static char *exec_name;
static void print_help() static void print_help()
{ {
fprintf(stdout, "usage: %s\n", exec_name); fprintf(stdout, "usage: %s [-p port] [-b addr] directory\n", exec_name);
fprintf(stdout, " -p, --port <port> Port to listen on (default: 10800)\n"); fprintf(stdout, " -p, --port <port> Port to listen on (default: 10800)\n");
fprintf(stdout, " -b, --bind <address> Address to bind to (default: 0.0.0.0)\n"); fprintf(stdout, " -b, --bind <address> Address to bind to (default: 0.0.0.0)\n");
fprintf(stdout, " -h, --help Print this message and exit\n"); fprintf(stdout, " -h, --help Print this message and exit\n");
@ -52,7 +52,7 @@ int main(int argc, char *argv[])
exec_name = basename(argv[0]); exec_name = basename(argv[0]);
for(int ch; (ch = getopt_long(argc, argv, "b:p:vh", longopts, NULL)) != -1;){ for(int ch; (ch = getopt_long(argc, argv, "b:p:vh0", longopts, NULL)) != -1;){
switch(ch){ switch(ch){
case 'p': case 'p':
port = (int)strtol(optarg, NULL, 10); port = (int)strtol(optarg, NULL, 10);
@ -82,6 +82,20 @@ int main(int argc, char *argv[])
} }
} }
// Accept final argument, the directory
if(argc == (optind+1)){
// Fail if we can't chdir to that directory
if(chdir(argv[argc-1]) == -1){
fprintf(stderr, "%s: cannot change directory - %s\n",
exec_name, strerror(errno)
);
goto shutdown_error;
}
} else {
print_help();
goto shutdown_error;
}
// Establish the socket // Establish the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1){ if(sockfd == -1){