diff --git a/Makefile b/Makefile index 1be3cb6..db2f4d0 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,29 @@ -CC = gcc -CFLAGS += -std=c99 -Wall -Werror -pedantic -g -INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log -INCLUDES := $(patsubst %,-I %,$(INCDIR)) -LDFLAGS = -L . -OPTFLAGS = #-O3 -LIBS = -lpthread -SOCKET = ./cs_sock +CC = gcc +CFLAGS += -std=c99 -Wall -Werror -pedantic -g +INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log ./lib/api +INCLUDES := $(patsubst %,-I %,$(INCDIR)) +LDFLAGS = -L . +OPTFLAGS = #-O3 +LIBS = -lpthread +SOCKET = ./cs_sock -BUILD_DIR = ./build -SRC_DIR = ./src -LIB_DIR = ./lib -OBJ_DIR = ./obj +BUILD_DIR = ./build +SRC_DIR = ./src +LIB_DIR = ./lib +OBJ_DIR = ./obj -OBJ_SRC_SERVER := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(SRC_DIR)/server*.c)))) -OBJ_SRC_CLIENT := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(SRC_DIR)/client*.c)))) -OBJ_LIBS := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(LIB_DIR)/*/*.c)))) +OBJ_SRC_SERVER := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(SRC_DIR)/server*.c)))) +OBJ_SRC_CLIENT := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(SRC_DIR)/client*.c)))) +OBJ_LIBS_SERVER := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(filter-out $(wildcard $(LIB_DIR)/api/*.c),$(wildcard $(LIB_DIR)/*/*.c))))) +OBJ_LIBS_CLIENT := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(filter-out $(wildcard $(LIB_DIR)/threadpool/*.c) $(wildcard $(LIB_DIR)/log/*.c),$(wildcard $(LIB_DIR)/*/*.c))))) -OBJ_SERVER := $(OBJ_SRC_SERVER) $(OBJ_LIBS) -OBJ_CLIENT := $(OBJ_SRC_CLIENT) $(OBJ_LIBS) +OBJ_SERVER := $(OBJ_SRC_SERVER) $(OBJ_LIBS_SERVER) +OBJ_CLIENT := $(OBJ_SRC_CLIENT) $(OBJ_LIBS_CLIENT) -VPATH := $(SRC_DIR) $(wildcard $(LIB_DIR)/*) +VPATH := $(SRC_DIR) $(wildcard $(LIB_DIR)/*) -.DEFAULT_GOAL := all +.DEFAULT_GOAL := all .PHONY: all clean cleanall @@ -70,7 +71,11 @@ $(BUILD_DIR)/server: $(OBJ_SERVER) # -CLIENT-RULES----------------------------------------------------------------- -$(OBJ_DIR)/client.o: client.c +$(OBJ_DIR)/api.o: api.c api.h + @$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + @echo $@ + +$(OBJ_DIR)/client.o: client.c api.h @$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @echo $@ @@ -87,4 +92,4 @@ cleanall : clean @rm -rf $(OBJ_DIR)/* @rm -f $(SOCKET) -# print-% : ; @echo $* = $($*) +print-% : ; @echo $* = $($*) diff --git a/lib/api/api.c b/lib/api/api.c new file mode 100644 index 0000000..8c2ddb8 --- /dev/null +++ b/lib/api/api.c @@ -0,0 +1,67 @@ +#include + + + +// ----------------------------------------------------------------------------- +/* funzioni ausiliarie */ + + + + + + + + + +// ----------------------------------------------------------------------------- +int openConnection(const char* sockname, int msec, const struct timespec abstime){ + return 1; +} + +int closeConnection(const char* sockname){ + return 1; +} + +int openFile(const char* pathname, int flags){ + return 1; +} + +int readFile(const char* pathname, void** buf, size_t* size){ + return 1; +} + +int readNFiles(int N, const char* dirname){ + return 1; +} + +int writeFile(const char* pathname, const char* dirname){ + return 1; +} + +int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname){ + return 1; +} + +int lockFile(const char* pathname){ + return 1; +} + +int unlockFile(const char* pathname){ + return 1; +} + +int closeFile(const char *pathname){ + return 1; +} + +int removeFile(const char* pathname){ + return 1; +} + +int setDirectory(char* Dir, int rw){ + return 1; +} + +void printInfo(int p){ + return; +} diff --git a/lib/api/api.h b/lib/api/api.h new file mode 100644 index 0000000..5266956 --- /dev/null +++ b/lib/api/api.h @@ -0,0 +1,40 @@ +#pragma once +#ifndef _API_CLIENT +#define _API_CLIENT + +#include + +// struttura dati per la lista dei file aperti +typedef struct openFile_s { + char *filename; // nome del file + struct openFile_s *next; // puntatore al prossimo elemento nella lista +} openFile_t; + + +int openConnection(const char* sockname, int msec, const struct timespec abstime); + +int closeConnection(const char* sockname); + +int openFile(const char* pathname, int flags); + +int readFile(const char* pathname, void** buf, size_t* size); + +int readNFiles(int N, const char* dirname); + +int writeFile(const char* pathname, const char* dirname); + +int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname); + +int lockFile(const char* pathname); + +int unlockFile(const char* pathname); + +int closeFile(const char *pathname); + +int removeFile(const char* pathname); + +int setDirectory(char* Dir, int rw); + +void printInfo(int p); + +#endif /* _API_CLIENT */ diff --git a/src/client.c b/src/client.c index 33c14ce..0f7f49c 100644 --- a/src/client.c +++ b/src/client.c @@ -1,3 +1,190 @@ -int main() { +#include +#include +#include +#include +#include +#include + +#include + +#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 \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; }