Added first functionality to client
This commit is contained in:
189
src/client.c
189
src/client.c
@ -1,3 +1,190 @@
|
||||
int main() {
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <api.h>
|
||||
|
||||
#define UNIX_PATH_MAX 256
|
||||
#define MAXARGLENGTH 256
|
||||
|
||||
static char globalSocket[UNIX_PATH_MAX] = "";
|
||||
|
||||
|
||||
|
||||
// struttura della lista dei comandi
|
||||
typedef struct cmd_s {
|
||||
char name; // nome del comando
|
||||
char *arg; // (eventuale) argomento del comando
|
||||
struct cmd_s *next; // puntatore al prossimo comando nella lista
|
||||
} cmd_t;
|
||||
|
||||
// struttura dati per il comando -w
|
||||
typedef struct cmdw_s {
|
||||
char *dir;
|
||||
int print;
|
||||
int tot;
|
||||
int curn;
|
||||
} cmdw_t;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// helper functions
|
||||
|
||||
// libera la memoria della lista dei comandi
|
||||
void destroyCommandList(cmd_t *l);
|
||||
// aggiunge un comando alla lista
|
||||
int addCommand(cmd_t **l, char cmd, char *arg);
|
||||
|
||||
// per chiudere la connessione prima dell'uscita
|
||||
void cleanup() {
|
||||
if (strncmp(globalSocket, "", 1) != 0) {
|
||||
closeConnection(globalSocket);
|
||||
}
|
||||
}
|
||||
|
||||
// -h
|
||||
static void usage(const char *argv0) {
|
||||
// TODO change this
|
||||
fprintf(stdout, "use: %s <config file location>\n", argv0);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// MAIN
|
||||
int main(int argc, char* argv[]) {
|
||||
atexit(cleanup);
|
||||
|
||||
if(argc <= 1) { // no arguments => -h
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct sigaction siga;
|
||||
// ignoro il segnale SIGPIPE
|
||||
memset(&siga, 0, sizeof(siga));
|
||||
siga.sa_handler = SIG_IGN;
|
||||
if (sigaction(SIGPIPE, &siga, NULL) == -1) {
|
||||
perror("sigaction.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// lista dei comandi
|
||||
cmd_t *cmds = NULL;
|
||||
cmds = calloc(1, sizeof(cmd_t));
|
||||
// struttra per -w
|
||||
cmdw_t *cmdw = NULL;
|
||||
|
||||
int opt;
|
||||
char args[MAXARGLENGTH];
|
||||
|
||||
char f = 0;
|
||||
|
||||
// add args to list
|
||||
while ((opt = getopt(argc, argv, ":hpf:t:w:W:D:r:R:d:l:u:c:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h': // help message
|
||||
usage(argv[0]);
|
||||
goto _cleanup;
|
||||
case 'f': // socket name
|
||||
if(!f) {
|
||||
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
|
||||
fprintf(stderr, "Il comando -f necessita di un argomento.\n");
|
||||
goto _cleanup;
|
||||
}
|
||||
memset(args, 0, MAXARGLENGTH);
|
||||
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
|
||||
addCommand(&cmds, opt, optarg);
|
||||
++f;
|
||||
}
|
||||
break;
|
||||
case 'p': // print to stdout
|
||||
printInfo(1);
|
||||
break;
|
||||
case 'w': // send files from folder (n is specified after)
|
||||
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
|
||||
fprintf(stderr, "Il comando -w necessita di un argomento.\n");
|
||||
goto _cleanup;
|
||||
}
|
||||
if(!cmdw) {
|
||||
cmdw = calloc(1, sizeof(cmdw_t));
|
||||
if(!cmdw) {
|
||||
perror("calloc");
|
||||
goto _cleanup;
|
||||
}
|
||||
}
|
||||
memset(args, 0, MAXARGLENGTH);
|
||||
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
|
||||
addCommand(&cmds, opt, args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
_cleanup:
|
||||
if(cmds) {
|
||||
destroyCommandList(cmds);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
void destroyCommandList(cmd_t *l) {
|
||||
if (!l) {
|
||||
errno = EINVAL;
|
||||
return;
|
||||
}
|
||||
|
||||
cmd_t *tmp;
|
||||
// scorro tutta la lista e libero la memoria
|
||||
while (l) {
|
||||
tmp = l;
|
||||
|
||||
free(l->arg);
|
||||
l = l->next;
|
||||
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
int addCommand(cmd_t **l, char cmd, char *arg) {
|
||||
if(!l) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cmd_t *new = calloc(1, sizeof(cmd_t));
|
||||
if(!new) {
|
||||
perror("calloc");
|
||||
return -1;
|
||||
}
|
||||
new->name = cmd;
|
||||
|
||||
if (arg) {
|
||||
new->arg = malloc(MAXARGLENGTH);
|
||||
if (new->arg == NULL) {
|
||||
perror("malloc arg");
|
||||
free(new);
|
||||
return -1;
|
||||
}
|
||||
strncpy(new->arg, arg, strnlen(arg, MAXARGLENGTH-1)+1);
|
||||
}
|
||||
new->next = NULL;
|
||||
cmd_t *tail = *l;
|
||||
|
||||
// se lista vuota aggiungo in cima, altrimenti scorro la lista
|
||||
if (*l == NULL) {
|
||||
*l = new;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (tail->next) {
|
||||
tail = tail->next;
|
||||
}
|
||||
|
||||
tail->next = new;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user