From d74ba6de54f8ee1c41effbe7522fc670bff4224d Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 19 Jul 2022 13:13:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D1=8B=20=D1=81=20=D0=B2=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=D0=BC=20=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 44 ++++++++++++++++++++++++++++++++++++++++++++ address.c | 5 +++-- concat.c | 4 ++-- db.c | 33 ++++++++++++++------------------- gui.c | 33 +++++++++++++++------------------ node_settings.c | 14 ++++++++------ parameter.h | 2 +- rxrandr_broker.c | 6 ++++-- 8 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..73179f5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 2.8) +project(xfree) +#include_directories(/home/alexander/repositories/iup/include) +include_directories(/root/iup/include) +set(SOURCE_EXE + address.c + arguments.c + concat.c + db.c + gui.c + images.c + monitor.c + node_settings.c + rxrandr_broker.c + rxrandr.c + settings.c + xfreerdp.c + xrdp.c +) +add_executable(xfree ${SOURCE_EXE}) +#target_link_libraries(xfree iup X11 Xrandr m /home/alexander/programming/c/iup/sqlite3.a) +target_link_libraries(xfree + gtk-3 + gdk-3 + pangocairo-1.0 + pango-1.0 + atk-1.0 + cairo-gobject + cairo + gdk_pixbuf-2.0 + gio-2.0 + gobject-2.0 + glib-2.0 + X11 + Xrandr + m + + dl + pthread + + /root/iup/lib/Linux515/libiup.a + /root/SQLite/sqlite3.a +) +#target_link_libraries(xfree iup X11 Xrandr m sqlite3 -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lX11 -lXrandr -lm) diff --git a/address.c b/address.c index 9c603f2..69c70d0 100644 --- a/address.c +++ b/address.c @@ -21,8 +21,9 @@ char *getHostIP(char *dnsName) if (he) { char *ip = inet_ntoa(*(struct in_addr*)he->h_addr); - result = (char *)malloc(sizeof(char) * strlen(ip)); - strcpy(result, ip); + size_t size = strlen(ip) + 1; + result = (char *)malloc(sizeof(char) * size); + strncpy(result, ip, size); } return result; } diff --git a/concat.c b/concat.c index 8ee7b56..d75a07a 100644 --- a/concat.c +++ b/concat.c @@ -26,11 +26,11 @@ char* concat(char *s1, char *s2) if (s1) { - strcpy(result, s1); + strncpy(result, s1, (len1 + 1)); } if (s2) { - strcpy(result + len1, s2); + strncpy(result + len1, s2, (len2 + 1)); } return result; diff --git a/db.c b/db.c index 5bcf388..d6d1aa1 100644 --- a/db.c +++ b/db.c @@ -6,6 +6,7 @@ */ #include "sqlite3.h" +//#include #include #include @@ -15,7 +16,7 @@ char* getPathDB(char *path) { static char *current = NULL; - if (path) + if (path && !current) { current = path; } @@ -53,8 +54,9 @@ static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName) { if (!getParameter(atoi(argv[0]))) { - addParameterKey(atoi(argv[0]), argv[1], atoi(argv[2]), atoi(argv[3]), argv[7] ? getParameter(atoi(argv[7])) : NULL, - argv[8] ? getParameter(atoi(argv[8])) : NULL); + addParameterKey(atoi(argv[0]), argv[1], atoi(argv[2]), atoi(argv[3]), + (argv[7] ? getParameter(atoi(argv[7])) : NULL), + (argv[8] ? getParameter(atoi(argv[8])) : NULL)); } if (argv[4] && argv[5]) @@ -97,7 +99,6 @@ bool dbLoadData() static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName) { - fprintf(stderr, "dbCreateHostsList 1\n"); Hosts *hosts = *(Hosts**) answer; Host *host = (Host*) malloc(sizeof(Host)); host->data = (char**) malloc(sizeof(char*) * argc); @@ -105,12 +106,11 @@ static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColNa for (int i = 0; i < argc; i++) { - host->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i])); - strcpy(host->data[i], argv[i]); + size_t size = strlen(argv[i]) + 1; + host->data[i] = (char*) malloc(sizeof(char) * size); + strncpy(host->data[i], argv[i], size); } - fprintf(stderr, "dbCreateHostsList 2\n"); - Host **tmp = hosts->host; hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size); for (size_t i = 0; i < hosts->size - 1; ++i) @@ -123,14 +123,11 @@ static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColNa } hosts->host[hosts->size - 1] = host; - fprintf(stderr, "dbCreateHostsList 3\n"); - return 0; } Hosts* dbGetHostsList() { - fprintf(stderr, "dbGetHostsList 1\n"); sqlite3 *db = dbGetBase(getPathDB(NULL)); if (!db) { @@ -139,7 +136,6 @@ Hosts* dbGetHostsList() char *err_msg = 0; char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`"; - fprintf(stderr, "dbGetHostsList 2\n"); Hosts *hosts = (Hosts*) malloc(sizeof(Hosts)); hosts->size = 0; hosts->host = NULL; @@ -151,9 +147,7 @@ Hosts* dbGetHostsList() sqlite3_close(db); exit(-1); } - fprintf(stderr, "dbGetHostsList 3\n"); sqlite3_close(db); - fprintf(stderr, "dbGetHostsList 4\n"); return hosts; } @@ -399,8 +393,9 @@ static int dbCreateMonitorsList(void *answer, int argc, char **argv, char **azCo for (int i = 0; i < argc; i++) { - monitor->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i])); - strcpy(monitor->data[i], argv[i]); + size_t size = strlen(argv[i]) + 1; + monitor->data[i] = (char*) malloc(sizeof(char) * size); + strncpy(monitor->data[i], argv[i], size); } Monitor **tmp = monitors->monitor; @@ -423,7 +418,7 @@ Monitors* dbGetMonitorsList() sqlite3 *db = dbGetBase(getPathDB(NULL)); if (!db) { - return false; + exit(-1); } char *err_msg = 0; @@ -436,10 +431,10 @@ Monitors* dbGetMonitorsList() if (sqlite3_exec(db, sql, dbCreateMonitorsList, &monitors, &err_msg) != SQLITE_OK) { - fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg); + fprintf(stderr, "Получение списка мониторов. Ошибка выполнения запроса: %s\n", err_msg); sqlite3_free(err_msg); sqlite3_close(db); - exit(-1); + exit(-2); } sqlite3_close(db); diff --git a/gui.c b/gui.c index 9f34cec..dfa8591 100644 --- a/gui.c +++ b/gui.c @@ -58,25 +58,24 @@ static int guiConnect(Ihandle *self) static void createHostsList(Ihandle *iupList) { - fprintf(stderr, "ERROR 1\n"); Hosts *hosts = dbGetHostsList(); - fprintf(stderr, "ERROR 2\n"); char *indexItem = NULL; char *serverName = NULL; for (size_t i = 0; i < hosts->size; ++i) { Host *host = hosts->host[i]; - indexItem = (char *)malloc(sizeof(char) * strlen(host->data[0])); - strcpy(indexItem, host->data[0]); - serverName = (char *)malloc(sizeof(char) * strlen(host->data[1])); - strcpy(serverName, host->data[1]); + size_t size = strlen(host->data[0]) + 1; + indexItem = (char *)malloc(sizeof(char) * size); + strncpy(indexItem, host->data[0], size); + size = strlen(host->data[1]) + 1; + serverName = (char *)malloc(sizeof(char) * size); + strncpy(serverName, host->data[1], size); IupSetAttribute(iupList, indexItem, serverName); if (atoi(host->data[2])) IupSetAttribute(iupList, "VALUE", serverName); } - fprintf(stderr, "ERROR 3\n"); dbFreeHosts(hosts); } @@ -107,15 +106,13 @@ Ihandle* guiStart() inputServer = IupList(NULL); IupSetAttribute(inputServer, "NAME", "SERVER"); IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)"); - fprintf(stderr, "guiStart 1\n"); createHostsList(inputServer); - fprintf(stderr, "guiStart 2\n"); IupSetAttribute(inputServer, "SIZE", "100"); IupSetAttribute(inputServer, "EDITBOX", "YES"); IupSetAttribute(inputServer, "DROPDOWN", "YES"); hBoxServer = IupHbox(labelServer, inputServer, NULL); IupSetAttribute(hBoxServer, "MARGIN", "10x10"); - fprintf(stderr, "guiStart 3\n"); + labelUser = IupLabel("Пользователь:"); IupSetAttribute(labelUser, "SIZE", "80x16"); IupSetAttribute(labelUser, "ALIGNMENT", "ARIGHT:ACENTER"); @@ -127,7 +124,7 @@ Ihandle* guiStart() IupSetAttribute(inputUser, "SIZE", "100"); hBoxUser = IupHbox(labelUser, inputUser, NULL); IupSetAttribute(hBoxUser, "MARGIN", "10x0"); - fprintf(stderr, "guiStart 4\n"); + labelPassword = IupLabel("Пароль:"); IupSetAttribute(labelPassword, "SIZE", "80x16"); IupSetAttribute(labelPassword, "ALIGNMENT", "ARIGHT:ACENTER"); @@ -140,38 +137,38 @@ Ihandle* guiStart() IupSetAttribute(inputPassword, "PASSWORD", "YES"); hBoxPassword = IupHbox(labelPassword, inputPassword, NULL); IupSetAttribute(hBoxPassword, "MARGIN", "10x10"); - fprintf(stderr, "guiStart 5\n"); + btnConnect = IupButton("Подключение", NULL); IupSetHandle("CONNECT", btnConnect); IupSetAttribute(btnConnect, "NAME", "CONNECT"); IupSetAttribute(btnConnect, "TIP", "Выполнить подключение"); IupSetAttribute(btnConnect, "BGCOLOR", "0 179 0"); IupSetAttribute(btnConnect, "FGCOLOR", "255 255 255"); - fprintf(stderr, "guiStart 6\n"); + btnClose = IupButton("Закрыть", NULL); IupSetHandle("CLOSE", btnClose); IupSetAttribute(btnClose, "NAME", "CLOSE"); IupSetAttribute(btnClose, "TIP", "Закрыть FreeRDP"); IupSetAttribute(btnClose, "BGCOLOR", "204 0 0"); IupSetAttribute(btnClose, "FGCOLOR", "255 255 255"); - fprintf(stderr, "guiStart 7\n"); + btnSettings = IupButton("Настройки", NULL); IupSetAttribute(btnSettings, "NAME", "SETTINGS"); IupSetAttribute(btnSettings, "TIP", "Перейти в настройки"); IupSetAttribute(btnSettings, "BGCOLOR", "0 136 204"); IupSetAttribute(btnSettings, "FGCOLOR", "255 255 255"); - fprintf(stderr, "guiStart 8\n"); + hBoxButtons = IupHbox(IupFill(), btnSettings, btnConnect, btnClose, IupFill(), NULL); IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER"); IupSetAttribute(hBoxButtons, "GAP", "10"); IupSetAttribute(hBoxButtons, "MARGIN", "10x10"); - fprintf(stderr, "guiStart 9\n"); + vBoxMain = IupVbox(labelImage, hBoxServer, hBoxUser, hBoxPassword, hBoxButtons, NULL); IupSetCallback(btnConnect, "ACTION", (Icallback) guiConnect); IupSetCallback(btnClose, "ACTION", (Icallback) guiExit); IupSetCallback(btnSettings, "ACTION", (Icallback) settingsMainWindow); - fprintf(stderr, "guiStart 10\n"); + dlg = IupDialog(vBoxMain); IupSetAttribute(dlg, "IMAGE", "BG_HEAD"); IupSetAttribute(dlg, "ICON", "icon"); @@ -180,6 +177,6 @@ Ihandle* guiStart() IupSetAttribute(dlg, "PARENTDIALOG", "MAIN_WINDOW"); IupSetAttribute(dlg, "DEFAULTENTER", "CONNECT"); IupSetAttribute(dlg, "DEFAULTESC", "CLOSE"); - fprintf(stderr, "guiStart 11\n"); + return dlg; } diff --git a/node_settings.c b/node_settings.c index aa87236..3a74035 100644 --- a/node_settings.c +++ b/node_settings.c @@ -24,8 +24,9 @@ static NodeValue* newNodeValue(Value name, char *current, bool set) node->change = false; node->next = NULL; - node->current = (char*) malloc(sizeof(char) * strlen(current)); - strcpy(node->current, current); + size_t size = strlen(current) + 1; + node->current = (char*) malloc(sizeof(char) * size); + strncpy(node->current, current, size); return node; } @@ -63,8 +64,9 @@ static NodeParameter* newNodeParameter(Parameter name, char *key, bool set, bool node->conflict = conflict; node->next = NULL; - node->key = (char*) malloc(sizeof(char) * strlen(key)); - strcpy(node->key, key); + size_t size = strlen(key) + 1; + node->key = (char*) malloc(sizeof(char) * size); + strncpy(node->key, key, size); return node; } @@ -304,7 +306,7 @@ void setParameterValue(Parameter pName, Value vName, char *current) return; } - size_t length = strlen(current); + size_t length = strlen(current) + 1; if (!length) { @@ -338,7 +340,7 @@ void setParameterValue(Parameter pName, Value vName, char *current) { free(nodeValue->current); nodeValue->current = (char*) malloc(sizeof(char) * length); - strcpy(nodeValue->current, current); + strncpy(nodeValue->current, current, length); } else { diff --git a/parameter.h b/parameter.h index cbcd5c1..b78f3dc 100644 --- a/parameter.h +++ b/parameter.h @@ -22,8 +22,8 @@ typedef enum PARAMETER_SOUND, PARAMETER_COMPRESSION, PARAMETER_FULLSCREEN, - PARAMETER_MONITORS, PARAMETER_MULTIMONITOR, + PARAMETER_MONITORS, PARAMETER_AUTHENTICATION, PARAMETER_SECURITY, PARAMETER_BITSPERPIXEL, diff --git a/rxrandr_broker.c b/rxrandr_broker.c index 137cc24..1f80566 100644 --- a/rxrandr_broker.c +++ b/rxrandr_broker.c @@ -19,11 +19,13 @@ x_info *getXInfo() for (int i = 0; i < monitors->count; ++i) { - monitors->monitor[i].ptrName = (char *)malloc(sizeof(char) * strlen(monitors->monitor[i].name)); + size_t size = strlen(monitors->monitor[i].name) + 1; + + monitors->monitor[i].ptrName = (char *)malloc(sizeof(char) * size); monitors->monitor[i].ptrIndexItem = (char *)malloc(sizeof(char) * 3); monitors->monitor[i].ptrIndexMonitor = (char *)malloc(sizeof(char) * 3); - strcpy(monitors->monitor[i].ptrName, monitors->monitor[i].name); + strncpy(monitors->monitor[i].ptrName, monitors->monitor[i].name, size); sprintf(monitors->monitor[i].ptrIndexItem, "%d", i + 1); sprintf(monitors->monitor[i].ptrIndexMonitor, "%d", i); }