From f2b20118e0e30de649026bbf5751917638714971 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sat, 27 Apr 2024 23:54:46 +0300 Subject: [PATCH] update `C` snipplets --- code/C/AF_INET_sockets/echo_client.c | 41 ++-- code/C/AF_INET_sockets/echo_server.c | 68 +++---- code/C/{read csv => Parsing/CSV}/example.csv | 0 code/C/Parsing/CSV/read_csv.c | 31 +++ code/C/README.md | 48 ++++- code/C/SQLite/.gitignore | 1 + code/C/{ => SQLite}/Makefile | 0 code/C/SQLite/sqlite3_create.c | 52 +++++ code/C/SQLite/sqlite3_delete.c | 51 +++++ code/C/SQLite/sqlite3_insert.c | 55 +++++ code/C/SQLite/sqlite3_select.c | 50 +++++ code/C/SQLite/sqlite3_update.c | 51 +++++ code/C/{ => Strings}/buffer_len.h | 0 code/C/Strings/clear_string.c | 9 + code/C/{ => System}/disk_usage.c | 0 code/C/System/get-pid-by-process-name.c | 12 ++ code/C/{ => System}/get_ram_usage.c | 0 code/C/Transform/rgbToHex.c | 11 + .../seconds_to_minutes_and_hours.c | 0 .../ulfius-server}/.editorconfig | 0 .../ulfius-server}/.gitignore | 0 .../ulfius-server}/Makefile | 0 .../ulfius-server}/README.md | 0 .../{rest_server => Web/ulfius-server}/main.c | 0 .../ulfius-server}/qa/answer.png | Bin .../ulfius-server}/qa/question.md | 0 .../ulfius-server}/server.h | 0 .../ulfius-server}/utils.h | 0 code/C/{ => Web}/webui/.editorconfig | 0 code/C/{ => Web}/webui/call_c_from_js.c | 0 code/C/{ => Web}/webui/index.html | 0 code/C/{ => Web}/webui/minimal.c | 0 code/C/atomic.c | 36 ++-- code/C/clear_string.c | 10 - code/C/format-code | 10 - code/C/libserialport/.editorconfig | 6 - code/C/libserialport/.gitignore | 12 -- code/C/libserialport/Board/.gitignore | 1 - code/C/libserialport/Board/Board.ino | 24 --- code/C/libserialport/Board/README.md | 3 - code/C/libserialport/README.md | 18 -- code/C/libserialport/build_gcc | 20 -- code/C/libserialport/build_tcc | 20 -- code/C/libserialport/clear | 17 -- code/C/libserialport/format-code | 14 -- code/C/libserialport/listen.c | 133 ------------ code/C/read csv/read_csv.c | 31 --- code/C/rgbToHex.c | 12 -- code/C/sqlite/sqlite3_create.c | 52 ----- code/C/sqlite/sqlite3_delete.c | 51 ----- code/C/sqlite/sqlite3_insert.c | 55 ----- code/C/sqlite/sqlite3_select.c | 50 ----- code/C/sqlite/sqlite3_update.c | 51 ----- code/C/thread.c | 20 +- code/C/unixsocket/a.out | Bin 16136 -> 0 bytes code/C/unixsocket/client | Bin 16136 -> 0 bytes code/C/unixsocket/client.c | 103 +++++----- code/C/unixsocket/server | Bin 16208 -> 0 bytes code/C/unixsocket/server.c | 191 +++++++++--------- 59 files changed, 589 insertions(+), 831 deletions(-) rename code/C/{read csv => Parsing/CSV}/example.csv (100%) create mode 100644 code/C/Parsing/CSV/read_csv.c create mode 100644 code/C/SQLite/.gitignore rename code/C/{ => SQLite}/Makefile (100%) create mode 100644 code/C/SQLite/sqlite3_create.c create mode 100644 code/C/SQLite/sqlite3_delete.c create mode 100644 code/C/SQLite/sqlite3_insert.c create mode 100644 code/C/SQLite/sqlite3_select.c create mode 100644 code/C/SQLite/sqlite3_update.c rename code/C/{ => Strings}/buffer_len.h (100%) create mode 100644 code/C/Strings/clear_string.c rename code/C/{ => System}/disk_usage.c (100%) create mode 100644 code/C/System/get-pid-by-process-name.c rename code/C/{ => System}/get_ram_usage.c (100%) create mode 100644 code/C/Transform/rgbToHex.c rename code/C/{ => Transform}/seconds_to_minutes_and_hours.c (100%) rename code/C/{rest_server => Web/ulfius-server}/.editorconfig (100%) rename code/C/{rest_server => Web/ulfius-server}/.gitignore (100%) rename code/C/{rest_server => Web/ulfius-server}/Makefile (100%) rename code/C/{rest_server => Web/ulfius-server}/README.md (100%) rename code/C/{rest_server => Web/ulfius-server}/main.c (100%) rename code/C/{rest_server => Web/ulfius-server}/qa/answer.png (100%) rename code/C/{rest_server => Web/ulfius-server}/qa/question.md (100%) rename code/C/{rest_server => Web/ulfius-server}/server.h (100%) rename code/C/{rest_server => Web/ulfius-server}/utils.h (100%) rename code/C/{ => Web}/webui/.editorconfig (100%) rename code/C/{ => Web}/webui/call_c_from_js.c (100%) rename code/C/{ => Web}/webui/index.html (100%) rename code/C/{ => Web}/webui/minimal.c (100%) delete mode 100644 code/C/clear_string.c delete mode 100755 code/C/format-code delete mode 100644 code/C/libserialport/.editorconfig delete mode 100644 code/C/libserialport/.gitignore delete mode 100644 code/C/libserialport/Board/.gitignore delete mode 100644 code/C/libserialport/Board/Board.ino delete mode 100644 code/C/libserialport/Board/README.md delete mode 100644 code/C/libserialport/README.md delete mode 100755 code/C/libserialport/build_gcc delete mode 100755 code/C/libserialport/build_tcc delete mode 100755 code/C/libserialport/clear delete mode 100755 code/C/libserialport/format-code delete mode 100644 code/C/libserialport/listen.c delete mode 100644 code/C/read csv/read_csv.c delete mode 100644 code/C/rgbToHex.c delete mode 100644 code/C/sqlite/sqlite3_create.c delete mode 100644 code/C/sqlite/sqlite3_delete.c delete mode 100644 code/C/sqlite/sqlite3_insert.c delete mode 100644 code/C/sqlite/sqlite3_select.c delete mode 100644 code/C/sqlite/sqlite3_update.c delete mode 100755 code/C/unixsocket/a.out delete mode 100755 code/C/unixsocket/client delete mode 100755 code/C/unixsocket/server diff --git a/code/C/AF_INET_sockets/echo_client.c b/code/C/AF_INET_sockets/echo_client.c index 111b11f..99e471a 100644 --- a/code/C/AF_INET_sockets/echo_client.c +++ b/code/C/AF_INET_sockets/echo_client.c @@ -9,30 +9,29 @@ char message[] = "Hello there!\n"; char buf[sizeof(message)]; -int main() -{ - int sock; - struct sockaddr_in addr; +int main() { + int sock; + struct sockaddr_in addr; - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - perror("socket"); - exit(1); - } + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("socket"); + exit(1); + } - addr.sin_family = AF_INET; - addr.sin_port = htons(3425); // или любой другой порт... - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - perror("connect"); - exit(2); - } + addr.sin_family = AF_INET; + addr.sin_port = htons(3425); // или любой другой порт... + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("connect"); + exit(2); + } - send(sock, message, sizeof(message), 0); - recv(sock, buf, sizeof(message), 0); + send(sock, message, sizeof(message), 0); + recv(sock, buf, sizeof(message), 0); - printf(buf); - close(sock); + printf(buf); + close(sock); - return 0; + return 0; } diff --git a/code/C/AF_INET_sockets/echo_server.c b/code/C/AF_INET_sockets/echo_server.c index a6e4a95..560100a 100644 --- a/code/C/AF_INET_sockets/echo_server.c +++ b/code/C/AF_INET_sockets/echo_server.c @@ -5,44 +5,44 @@ #include #include -int main() -{ - int sock, listener; - struct sockaddr_in addr; - char buf[1024]; - int bytes_read; +int main() { + int sock, listener; + struct sockaddr_in addr; + char buf[1024]; + int bytes_read; - listener = socket(AF_INET, SOCK_STREAM, 0); - if (listener < 0) { - perror("socket"); - exit(1); + listener = socket(AF_INET, SOCK_STREAM, 0); + if (listener < 0) { + perror("socket"); + exit(1); + } + + addr.sin_family = AF_INET; + addr.sin_port = htons(3425); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind"); + exit(2); + } + + listen(listener, 1); + + while (1) { + sock = accept(listener, NULL, NULL); + if (sock < 0) { + perror("accept"); + exit(3); } - addr.sin_family = AF_INET; - addr.sin_port = htons(3425); - addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - perror("bind"); - exit(2); + while (1) { + bytes_read = recv(sock, buf, 1024, 0); + if (bytes_read <= 0) + break; + send(sock, buf, bytes_read, 0); } - listen(listener, 1); + close(sock); + } - while(1) { - sock = accept(listener, NULL, NULL); - if (sock < 0) { - perror("accept"); - exit(3); - } - - while(1) { - bytes_read = recv(sock, buf, 1024, 0); - if (bytes_read <= 0) break; - send(sock, buf, bytes_read, 0); - } - - close(sock); - } - - return 0; + return 0; } diff --git a/code/C/read csv/example.csv b/code/C/Parsing/CSV/example.csv similarity index 100% rename from code/C/read csv/example.csv rename to code/C/Parsing/CSV/example.csv diff --git a/code/C/Parsing/CSV/read_csv.c b/code/C/Parsing/CSV/read_csv.c new file mode 100644 index 0000000..8a3da86 --- /dev/null +++ b/code/C/Parsing/CSV/read_csv.c @@ -0,0 +1,31 @@ +#include // file handling functions +#include // atoi +#include // strtok + +int main() { + char buffer[80]; + FILE *stream = fopen("example.csv", "r"); + + while (fgets(buffer, 80, stream)) { + char *token = strtok(buffer, ","); + + // If you only need the first column of each row + // Если нам нужен только первый столбец каждой строки + if (token) { + int n = atoi(token); + printf("%s\n", n); + } + + // If you need all the values in a row + // Если нам нужны все значения подряд + while (token) { + // Just printing each integer here but handle as needed + // int n = atoi(token); + printf("%s\n", token); + + token = strtok(NULL, ","); + } + } + + return 0; +} diff --git a/code/C/README.md b/code/C/README.md index 509e2bf..50cd3fe 100644 --- a/code/C/README.md +++ b/code/C/README.md @@ -1,13 +1,43 @@ # C -## Other +## Работа со строками -- [seconds_to_minutes_and_hours.c](seconds_to_minutes_and_hours.c) - Преобразование секунд в минуты и часы -- [get_ram_usage.c](get_ram_usage.c) - Получение используемой приложением RAM -## SQLite 3 +- [clear_string.c](Strings/clear_string.c) - Очистка строки `char *` +- [`strlen()`](Strings/buffer_len.h) - Получить длину строки +- []() - -- [CREATE TABLE](sqlite/sqlite3_create.c) -- [INSERT INTO](sqlite/sqlite3_insert.c) -- [SELECT](sqlite/sqlite3_select.c) -- [UPDATE](sqlite/sqlite3_update.c) -- [DELETE](sqlite/sqlite3_delete.c) +## Преобразования + +- [rgbToHex.c](Transform/rgbToHex.c) - Преобразование RGB строки в HEX строку _(#008040)_ +- [seconds_to_minutes_and_hours.c](Transform/seconds_to_minutes_and_hours.c) - Преобразование секунд в минуты и часы + +## Парсеры + +- [read_csv.c](Parsing/CSV/read_csv.c) - Парсинг CSV файлов + +## Система + +- [get_ram_usage.c](System/get_ram_usage.c) - Получение статистики использования RAM +- [`sys/statvfs.h`](System/disk_usage.c) - Получение статиистики использования диска +- [`pidof`](System/get-pid-by-process-name.c) - Получение ID процесса по его имени + +## 🗄️ SQLite 3 + +- [CREATE TABLE](SQLite/sqlite3_create.c) — Создание таблицы +- [INSERT INTO](SQLite/sqlite3_insert.c) — Вставка +- [SELECT](SQLite/sqlite3_select.c) — Выбор +- [UPDATE](SQLite/sqlite3_update.c) — Обновление +- [DELETE](SQLite/sqlite3_delete.c) — Удаление + +## Web + +- [`webui`](Web/webui) - Пример библиотеки [webui](https://webui.me/) +- [`ulfius`](Web/ulfius-server) - Пример библиотеки ulfius + +## Другое + +- [`conio.h`](conio.h/README.md) - Описание и работа функций заголовоного файла `conio.h`. +- [unixsocket](unixsocket) - Пример работы с сокетами UNIX +- [AF_INET_sockets](AF_INET_sockets) - Пример работы с сокетами +- [atomic.c](atomic.c) - Пример `atomic` +- [thread.c](thread.c) - Пример многопоточности diff --git a/code/C/SQLite/.gitignore b/code/C/SQLite/.gitignore new file mode 100644 index 0000000..5f12808 --- /dev/null +++ b/code/C/SQLite/.gitignore @@ -0,0 +1 @@ +sqlite3_* diff --git a/code/C/Makefile b/code/C/SQLite/Makefile similarity index 100% rename from code/C/Makefile rename to code/C/SQLite/Makefile diff --git a/code/C/SQLite/sqlite3_create.c b/code/C/SQLite/sqlite3_create.c new file mode 100644 index 0000000..b95bf17 --- /dev/null +++ b/code/C/SQLite/sqlite3_create.c @@ -0,0 +1,52 @@ +#include +#include +#include + +static int callback(void *NotUsed, int argc, char **argv, char **azColName) { + int i; + for (i = 0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + + printf("\n"); + return 0; +} + +int main(int argc, char *argv[]) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + char *sql; + + /* Open database */ + rc = sqlite3_open("test.db", &db); + + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return (0); + } else { + fprintf(stdout, "Opened database successfully\n"); + } + + /* Create SQL statement */ + sql = "CREATE TABLE COMPANY(" + "ID INT PRIMARY KEY NOT NULL," + "NAME TEXT NOT NULL," + "AGE INT NOT NULL," + "ADDRESS CHAR(50)," + "SALARY REAL);"; + + /* Execute SQL statement */ + rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); + + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } else { + fprintf(stdout, "Table created successfully\n"); + } + + sqlite3_close(db); + + return 0; +} diff --git a/code/C/SQLite/sqlite3_delete.c b/code/C/SQLite/sqlite3_delete.c new file mode 100644 index 0000000..8abfcca --- /dev/null +++ b/code/C/SQLite/sqlite3_delete.c @@ -0,0 +1,51 @@ +#include +#include +#include + +static int callback(void *data, int argc, char **argv, char **azColName) { + int i; + fprintf(stderr, "%s: ", (const char *)data); + + for (i = 0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + + printf("\n"); + return 0; +} + +int main(int argc, char *argv[]) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + char *sql; + const char *data = "Callback function called"; + + /* Open database */ + rc = sqlite3_open("test.db", &db); + + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return (0); + } else { + fprintf(stderr, "Opened database successfully\n"); + } + + /* Create merged SQL statement */ + sql = "DELETE from COMPANY where ID=2; " + "SELECT * from COMPANY"; + + /* Execute SQL statement */ + rc = sqlite3_exec(db, sql, callback, (void *)data, &zErrMsg); + + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } else { + fprintf(stdout, "Operation done successfully\n"); + } + + sqlite3_close(db); + + return 0; +} diff --git a/code/C/SQLite/sqlite3_insert.c b/code/C/SQLite/sqlite3_insert.c new file mode 100644 index 0000000..d0791a2 --- /dev/null +++ b/code/C/SQLite/sqlite3_insert.c @@ -0,0 +1,55 @@ +#include +#include +#include + +static int callback(void *NotUsed, int argc, char **argv, char **azColName) { + int i; + for (i = 0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + + printf("\n"); + + return 0; +} + +int main(int argc, char *argv[]) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + char *sql; + + /* Open database */ + rc = sqlite3_open("test.db", &db); + + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return (0); + } else { + fprintf(stderr, "Opened database successfully\n"); + } + + /* Create SQL statement */ + sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " + "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); " + "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" + "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" + "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00);"; + + /* Execute SQL statement */ + rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); + + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } else { + fprintf(stdout, "Records created successfully\n"); + } + + sqlite3_close(db); + + return 0; +} diff --git a/code/C/SQLite/sqlite3_select.c b/code/C/SQLite/sqlite3_select.c new file mode 100644 index 0000000..7fa4929 --- /dev/null +++ b/code/C/SQLite/sqlite3_select.c @@ -0,0 +1,50 @@ +#include +#include +#include + +static int callback(void *data, int argc, char **argv, char **azColName) { + int i; + fprintf(stderr, "%s: ", (const char *)data); + + for (i = 0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + + printf("\n"); + return 0; +} + +int main(int argc, char *argv[]) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + char *sql; + const char *data = "Callback function called"; + + /* Open database */ + rc = sqlite3_open("test.db", &db); + + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return (0); + } else { + fprintf(stderr, "Opened database successfully\n"); + } + + /* Create SQL statement */ + sql = "SELECT * from COMPANY"; + + /* Execute SQL statement */ + rc = sqlite3_exec(db, sql, callback, (void *)data, &zErrMsg); + + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } else { + fprintf(stdout, "Operation done successfully\n"); + } + + sqlite3_close(db); + + return 0; +} diff --git a/code/C/SQLite/sqlite3_update.c b/code/C/SQLite/sqlite3_update.c new file mode 100644 index 0000000..d272f08 --- /dev/null +++ b/code/C/SQLite/sqlite3_update.c @@ -0,0 +1,51 @@ +#include +#include +#include + +static int callback(void *data, int argc, char **argv, char **azColName) { + int i; + fprintf(stderr, "%s: ", (const char *)data); + + for (i = 0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + + printf("\n"); + return 0; +} + +int main(int argc, char *argv[]) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + char *sql; + const char *data = "Callback function called"; + + /* Open database */ + rc = sqlite3_open("test.db", &db); + + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return (0); + } else { + fprintf(stderr, "Opened database successfully\n"); + } + + /* Create merged SQL statement */ + sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1; " + "SELECT * from COMPANY"; + + /* Execute SQL statement */ + rc = sqlite3_exec(db, sql, callback, (void *)data, &zErrMsg); + + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } else { + fprintf(stdout, "Operation done successfully\n"); + } + + sqlite3_close(db); + + return 0; +} diff --git a/code/C/buffer_len.h b/code/C/Strings/buffer_len.h similarity index 100% rename from code/C/buffer_len.h rename to code/C/Strings/buffer_len.h diff --git a/code/C/Strings/clear_string.c b/code/C/Strings/clear_string.c new file mode 100644 index 0000000..d75bf1a --- /dev/null +++ b/code/C/Strings/clear_string.c @@ -0,0 +1,9 @@ +int main(int argc, char const *argv[]) { + char *buffer = malloc(256 + 1); + + buffer[0] = '\0'; + strcpy(buffer, ""); + memset(buffer, '\0', sizeof(buffer)); + + return 0; +} diff --git a/code/C/disk_usage.c b/code/C/System/disk_usage.c similarity index 100% rename from code/C/disk_usage.c rename to code/C/System/disk_usage.c diff --git a/code/C/System/get-pid-by-process-name.c b/code/C/System/get-pid-by-process-name.c new file mode 100644 index 0000000..3e17a61 --- /dev/null +++ b/code/C/System/get-pid-by-process-name.c @@ -0,0 +1,12 @@ +pid_t getPIDbyName(char *ps_name) { + FILE *fp; + char *cmd = (char *)calloc(1, 200); + + sprintf(cmd, "pidof %s", ps_name); + + fp = popen(cmd, "r"); + fread(cmd, 1, 200, fp); + fclose(fp); + + return atoi(cmd); +} diff --git a/code/C/get_ram_usage.c b/code/C/System/get_ram_usage.c similarity index 100% rename from code/C/get_ram_usage.c rename to code/C/System/get_ram_usage.c diff --git a/code/C/Transform/rgbToHex.c b/code/C/Transform/rgbToHex.c new file mode 100644 index 0000000..dccd2f0 --- /dev/null +++ b/code/C/Transform/rgbToHex.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char const *argv[]) { + int red = 0; + int green = 128; + int blue = 64; + + printf("#%.2x%.2x%.2x\n", red, green, blue); // #008040 + + return 0; +} diff --git a/code/C/seconds_to_minutes_and_hours.c b/code/C/Transform/seconds_to_minutes_and_hours.c similarity index 100% rename from code/C/seconds_to_minutes_and_hours.c rename to code/C/Transform/seconds_to_minutes_and_hours.c diff --git a/code/C/rest_server/.editorconfig b/code/C/Web/ulfius-server/.editorconfig similarity index 100% rename from code/C/rest_server/.editorconfig rename to code/C/Web/ulfius-server/.editorconfig diff --git a/code/C/rest_server/.gitignore b/code/C/Web/ulfius-server/.gitignore similarity index 100% rename from code/C/rest_server/.gitignore rename to code/C/Web/ulfius-server/.gitignore diff --git a/code/C/rest_server/Makefile b/code/C/Web/ulfius-server/Makefile similarity index 100% rename from code/C/rest_server/Makefile rename to code/C/Web/ulfius-server/Makefile diff --git a/code/C/rest_server/README.md b/code/C/Web/ulfius-server/README.md similarity index 100% rename from code/C/rest_server/README.md rename to code/C/Web/ulfius-server/README.md diff --git a/code/C/rest_server/main.c b/code/C/Web/ulfius-server/main.c similarity index 100% rename from code/C/rest_server/main.c rename to code/C/Web/ulfius-server/main.c diff --git a/code/C/rest_server/qa/answer.png b/code/C/Web/ulfius-server/qa/answer.png similarity index 100% rename from code/C/rest_server/qa/answer.png rename to code/C/Web/ulfius-server/qa/answer.png diff --git a/code/C/rest_server/qa/question.md b/code/C/Web/ulfius-server/qa/question.md similarity index 100% rename from code/C/rest_server/qa/question.md rename to code/C/Web/ulfius-server/qa/question.md diff --git a/code/C/rest_server/server.h b/code/C/Web/ulfius-server/server.h similarity index 100% rename from code/C/rest_server/server.h rename to code/C/Web/ulfius-server/server.h diff --git a/code/C/rest_server/utils.h b/code/C/Web/ulfius-server/utils.h similarity index 100% rename from code/C/rest_server/utils.h rename to code/C/Web/ulfius-server/utils.h diff --git a/code/C/webui/.editorconfig b/code/C/Web/webui/.editorconfig similarity index 100% rename from code/C/webui/.editorconfig rename to code/C/Web/webui/.editorconfig diff --git a/code/C/webui/call_c_from_js.c b/code/C/Web/webui/call_c_from_js.c similarity index 100% rename from code/C/webui/call_c_from_js.c rename to code/C/Web/webui/call_c_from_js.c diff --git a/code/C/webui/index.html b/code/C/Web/webui/index.html similarity index 100% rename from code/C/webui/index.html rename to code/C/Web/webui/index.html diff --git a/code/C/webui/minimal.c b/code/C/Web/webui/minimal.c similarity index 100% rename from code/C/webui/minimal.c rename to code/C/Web/webui/minimal.c diff --git a/code/C/atomic.c b/code/C/atomic.c index 440495c..eee01dc 100644 --- a/code/C/atomic.c +++ b/code/C/atomic.c @@ -5,26 +5,24 @@ atomic_int acnt; int cnt; -int f(void* thr_data) -{ - (void)thr_data; - for(int n = 0; n < 1000; ++n) { - ++cnt; - ++acnt; - // for this example, relaxed memory order is sufficient, e.g. - // atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); - } - return 0; +int f(void *thr_data) { + (void)thr_data; + for (int n = 0; n < 1000; ++n) { + ++cnt; + ++acnt; + // for this example, relaxed memory order is sufficient, e.g. + // atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); + } + return 0; } -int main(void) -{ - thrd_t thr[10]; - for(int n = 0; n < 10; ++n) - thrd_create(&thr[n], f, NULL); - for(int n = 0; n < 10; ++n) - thrd_join(thr[n], NULL); +int main(void) { + thrd_t thr[10]; + for (int n = 0; n < 10; ++n) + thrd_create(&thr[n], f, NULL); + for (int n = 0; n < 10; ++n) + thrd_join(thr[n], NULL); - printf("The atomic counter is %u\n", acnt); - printf("The non-atomic counter is %u\n", cnt); + printf("The atomic counter is %u\n", acnt); + printf("The non-atomic counter is %u\n", cnt); } diff --git a/code/C/clear_string.c b/code/C/clear_string.c deleted file mode 100644 index 4ab0bab..0000000 --- a/code/C/clear_string.c +++ /dev/null @@ -1,10 +0,0 @@ -int main(int argc, char const *argv[]) -{ - char *buffer = malloc(256 + 1); - - buffer[0] = '\0'; - strcpy(buffer, ""); - memset(buffer, '\0', sizeof(buffer)); - - return 0; -} diff --git a/code/C/format-code b/code/C/format-code deleted file mode 100755 index 381f629..0000000 --- a/code/C/format-code +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -files=( - "file.c" -) - -for file in "${files[@]}" -do - clang-format -i --style=LLVM --sort-includes=false $file -done diff --git a/code/C/libserialport/.editorconfig b/code/C/libserialport/.editorconfig deleted file mode 100644 index 0b9487f..0000000 --- a/code/C/libserialport/.editorconfig +++ /dev/null @@ -1,6 +0,0 @@ -[{clear,build,format-code}] -indent_style = space -indent_size = 4 - -[{*.c,*.ino}] -indent_size = 2 diff --git a/code/C/libserialport/.gitignore b/code/C/libserialport/.gitignore deleted file mode 100644 index e294335..0000000 --- a/code/C/libserialport/.gitignore +++ /dev/null @@ -1,12 +0,0 @@ -# libserialport examples -port_info.c -list_ports.c -send_receive.c - -# binaries -port_info -list_ports -send_receive - -listen -abc.txt diff --git a/code/C/libserialport/Board/.gitignore b/code/C/libserialport/Board/.gitignore deleted file mode 100644 index 0b61ddb..0000000 --- a/code/C/libserialport/Board/.gitignore +++ /dev/null @@ -1 +0,0 @@ -3party/ diff --git a/code/C/libserialport/Board/Board.ino b/code/C/libserialport/Board/Board.ino deleted file mode 100644 index ec80e4e..0000000 --- a/code/C/libserialport/Board/Board.ino +++ /dev/null @@ -1,24 +0,0 @@ -/* - Author: Alexander Popov - License: Unlicense -*/ - -#include "3party/AsyncStream.h" -AsyncStream<50> serial(&Serial, '\n'); - -void setup() { - Serial.begin(9600); -} - -void loop() { - if (strcmp(serial.buf, "ping") == 0) { - Serial.println("PONG"); - } - - Serial.println("ooooo"); - // delay(1000); - Serial.println("zzzz"); - // delay(1000); - Serial.println("xxx"); - // delay(1000); -} diff --git a/code/C/libserialport/Board/README.md b/code/C/libserialport/Board/README.md deleted file mode 100644 index 980b5fe..0000000 --- a/code/C/libserialport/Board/README.md +++ /dev/null @@ -1,3 +0,0 @@ -Download `AsyncStream.h` from -https://github.com/GyverLibs/AsyncStream -and drop in to `3party` folder. diff --git a/code/C/libserialport/README.md b/code/C/libserialport/README.md deleted file mode 100644 index 7a4f9a0..0000000 --- a/code/C/libserialport/README.md +++ /dev/null @@ -1,18 +0,0 @@ -Download `libserialport` library. - -```sh -git clone git://sigrok.org/libserialport -``` - -For **build** code use `build` script or build files manually. - -Example: - -```sh -export C_INCLUDE_PATH= -export LIBRARY_PATH= - -gcc -static -Wall -O3 -o -lserialport -``` - -Arduino example project store in `Board` folder. diff --git a/code/C/libserialport/build_gcc b/code/C/libserialport/build_gcc deleted file mode 100755 index b88f52e..0000000 --- a/code/C/libserialport/build_gcc +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# -# build - script for build all files. -# -# Alexander Popov - -export C_INCLUDE_PATH=$HOME/Git/libserialport -export LIBRARY_PATH=$HOME/Git/libserialport/.libs - -for file in \ - port_info.c \ - list_ports.c \ - send_receive.c \ - listen.c \ - -do - echo -ne "[ ] Building $file...\r" - gcc -static -Wall -O3 -o ${file%.*} $file -lserialport - echo [OK -done diff --git a/code/C/libserialport/build_tcc b/code/C/libserialport/build_tcc deleted file mode 100755 index 27b7ec1..0000000 --- a/code/C/libserialport/build_tcc +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# -# build - script for build all files. -# -# Alexander Popov - -export C_INCLUDE_PATH=$HOME/Git/libserialport -export LIBRARY_PATH=$HOME/Git/libserialport/.libs - -for file in \ - port_info.c \ - list_ports.c \ - send_receive.c \ - listen.c \ - -do - echo -ne "[ ] Building $file...\r" - tcc -Wall -O3 -o ${file%.*} -I$C_INCLUDE_PATH $file $LIBRARY_PATH/libserialport.a - echo [OK -done diff --git a/code/C/libserialport/clear b/code/C/libserialport/clear deleted file mode 100755 index 49d1314..0000000 --- a/code/C/libserialport/clear +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# -# clear - script for delete all builded files. -# -# Alexander Popov - -files=( - "port_info" - "list_ports" - "send_receive" - "listen" -) - -for file in ${files[@]} -do - rm $file &> /dev/null -done diff --git a/code/C/libserialport/format-code b/code/C/libserialport/format-code deleted file mode 100755 index 2104042..0000000 --- a/code/C/libserialport/format-code +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# -# formt-code - script for beautify code by clang-format. -# -# Alexander Popov - -files=( - "listen.c" -) - -for file in "${files[@]}" -do - clang-format -i --style=LLVM --sort-includes=false $file -done diff --git a/code/C/libserialport/listen.c b/code/C/libserialport/listen.c deleted file mode 100644 index fa70ca2..0000000 --- a/code/C/libserialport/listen.c +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include -#include -#include -#include - -/* Helper function for error handling. */ -int check(enum sp_return result); -void handle_sigint(int sig); - -bool INTERRUPT = false; - -int main(int argc, char **argv) { - struct sp_port *serial_port; - char *port_name; - - const int size = 256; - char *buffer = malloc(size + 1); - - const unsigned int timeout = 1000; - int result; - - /* Get the port name from the command line. */ - if (argc != 2) { - printf("Usage: %s \n\n", argv[0]); - - struct sp_port **port_list; - enum sp_return result = sp_list_ports(&port_list); - - /* Getting the available ports. */ - if (result != SP_OK) { - puts("Getting available ports failed!"); - } else { - puts("Available ports:"); - - int i; - for (i = 0; port_list[i] != NULL; i++) { - /* Get the name of the port. */ - struct sp_port *port = port_list[i]; - char *port_name = sp_get_port_name(port); - - printf(" * %s\n", port_name); - } - - printf("\nAvailable %d ports.\n", i); - - sp_free_port_list(port_list); - } - - return -1; - } else { - port_name = argv[1]; - } - - printf("Connecting to '%s'...\n", port_name); - check(sp_get_port_by_name(port_name, &serial_port)); - check(sp_open(serial_port, SP_MODE_READ_WRITE)); - - check(sp_set_baudrate(serial_port, 9600)); - check(sp_set_bits(serial_port, 8)); - check(sp_set_parity(serial_port, SP_PARITY_NONE)); - check(sp_set_stopbits(serial_port, 1)); - check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE)); - puts("Connected."); - - signal(SIGINT, handle_sigint); - - FILE *output_file; - output_file = fopen("./abc.txt", "w"); - - /* Reading lines from serial port. */ - bool reading = true; - while (reading && !INTERRUPT) { - int pos = 0; - - /* Character-by-character reading. */ - while (pos < size) { - result = check(sp_blocking_read(serial_port, buffer + pos, 1, timeout)); - - if (result == -1) { - puts("Error reading from serial port"); - - reading = false; - break; - } else if (result == 0) { - puts("No more data"); - break; - } else { - if (buffer[pos] == '\n') { - buffer[pos] = '\0'; - break; - } - - pos++; - } - } - - puts(buffer); - fputs(buffer, output_file); - } - - fclose(output_file); - free(buffer); - - return 0; -} - -/* Helper function for error handling. */ -int check(enum sp_return result) { - char *error_message; - - switch (result) { - case SP_ERR_ARG: - puts("Error: Invalid argument."); - abort(); - case SP_ERR_FAIL: - error_message = sp_last_error_message(); - printf("Error: Failed: %s\n", error_message); - sp_free_error_message(error_message); - abort(); - case SP_ERR_SUPP: - puts("Error: Not supported."); - abort(); - case SP_ERR_MEM: - puts("Error: Couldn't allocate memory."); - abort(); - case SP_OK: - default: - return result; - } -} - -void handle_sigint(int sig) { INTERRUPT = true; } diff --git a/code/C/read csv/read_csv.c b/code/C/read csv/read_csv.c deleted file mode 100644 index 962f5a6..0000000 --- a/code/C/read csv/read_csv.c +++ /dev/null @@ -1,31 +0,0 @@ -#include // file handling functions -#include // atoi -#include // strtok - -int main() { - char buffer[80]; - FILE *stream = fopen("example.csv", "r"); - - while (fgets(buffer, 80, stream)) { - char *token = strtok(buffer, ","); - - // If you only need the first column of each row - // Если нам нужен только первый столбец каждой строки - if (token) { - int n = atoi(token); - printf("%s\n", n); - } - - // If you need all the values in a row - // Если нам нужны все значения подряд - while (token) { - // Just printing each integer here but handle as needed - // int n = atoi(token); - printf("%s\n", token); - - token = strtok(NULL, ","); - } - } - - return 0; -} diff --git a/code/C/rgbToHex.c b/code/C/rgbToHex.c deleted file mode 100644 index 9c63828..0000000 --- a/code/C/rgbToHex.c +++ /dev/null @@ -1,12 +0,0 @@ -#include - -int main(int argc, char const *argv[]) -{ - int red = 0; - int green = 128; - int blue = 64; - - printf("#%.2x%.2x%.2x\n", red, green, blue); //#008040 - - return 0; -} diff --git a/code/C/sqlite/sqlite3_create.c b/code/C/sqlite/sqlite3_create.c deleted file mode 100644 index df137a1..0000000 --- a/code/C/sqlite/sqlite3_create.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include - -static int callback(void *NotUsed, int argc, char **argv, char **azColName) { - int i; - for(i = 0; i -#include -#include - -static int callback(void *data, int argc, char **argv, char **azColName) { - int i; - fprintf(stderr, "%s: ", (const char*)data); - - for (i = 0; i -#include -#include - -static int callback(void *NotUsed, int argc, char **argv, char **azColName) { - int i; - for (i = 0; i -#include -#include - -static int callback(void *data, int argc, char **argv, char **azColName) { - int i; - fprintf(stderr, "%s: ", (const char*)data); - - for (i = 0; i -#include -#include - -static int callback(void *data, int argc, char **argv, char **azColName) { - int i; - fprintf(stderr, "%s: ", (const char*)data); - - for(i = 0; iF9m(UPevW2ALqrTXQlbR2ga}p^XfwrVggP?|>Bvfq$ZH4%w5(X7?r_$CGN&}_2{@%=c zm%XhIRqY?B(mreU&HK%JZ)V=^&d%OGf2q57Ycvv3LJQPql$MqmB+d=?r$s}lxav?# za9pL%SEm79h!W@KAb%-p8iY5n-U4v4TS1k1_{R*agv$ttlU+Uf+8L%rxN0&o*)5_f z%R%n4Q{mEH?av}(kfo1RUSd(W-;QE)sKqE8>a6869GBzF#g0U>JI;2;*-rQz4G1J$ z;z`4&(!uczatm!lmOd&Kw7Y@rf_w|>3#UGau6@lL_;32_W4m5UyHP>D+Q3S<^!HiV zQC$90lGD7C`>S^zY>vNh4zF69PVL^i~68z zYTJ%oDmt!a2orm%pN&FcrS_}uAHMX?-#_})*o*t`+t=~-d-vxaJtRKJhIEJsqasAN zA^9Pq`bSWxUx@_`e=$lU$`X{*QHaRLQj{}M&O(`w_>}7=eQ&Um%t37ATH#wu!_7Wj z(hu5*Z9Tpg4dTPEL-Uvq|2A;TugG=kcRqcu4t`-Byr&NC)WHYq;Fr|F?K=2RfmQ~nT(tC)a|*H=PGhU_v1#jt+!{>W@lY&U2H?OzP?>KJ^kAqICuA? z@}8UP-@ZAW&A9!E-Dwxy>>18xcpQ#VomWw*DDL41O6{K@r+8G2Ax5(9jIK=Kc3Ggl z%Gl%g+>2W_s`hhK<#{0Y)*J_YhSz(+4=_FwH->_XeCR5J(|SPng3yc)_uuEoeYk&J zc;1K09LU#8KHPs@%=++}>mqJOKqy5Jn)3Kp25A0v|>M-fBGmJ$vGf277AJ&#))ACuh89<#~JJ*@h!} zIF${*09;w~OB@?l#Ze=9h}z1t70{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?xCLK9Y6re7 zdafFY*S2&KP8rOW!DXH51Ky-os_}sSV&Kh`p~=aubiRppv3@X~3DM^u>ED{5Kd5nS zT65T22fw=x{vF^nuVB9q1Gj2C8{t{t`UkK;+snY4qgeF=_up&4{dv4%c7pUp@bhFk z$$N#nbFutE{jQddwtOE(G>ojCN;oN*QTBpFR@2y^`S(5NW}*B zX3&MWNfUQWz z!b#+E2@I4hqz=c4;Zzd6X5kBmnC`$h!^u;zWOjHMH-`UK`Csa^|H3U;&6!`p8wcUC z|B^mP*H0Hhpsh!BU&Qtu%q_n%>azb4cq1x)`{4h9trkV*vaSl;6R^L7`xS19BJ-gA z=YZ3mQN}O(O5ya}BjW8{ErevVCy; z46A8WD3)R``()v=PnQ0J^ZypxZ(@hC?-nlmbh4+I1??XIMlq51gz9sPpI`ER0u@bi z-bJSC!*6e~o$y&vWG;_I;olF~S09r6u)L11XK^ETBL5&@FZ(UwcoNr8aQr`Jdx^iY zSeWk76(UU9|Y^<$a;V_TyDcQxQAiQ5gH}Wq&-x9m_byjo4LX7!xMrm;Gab z?SuN%m!Fn!|G{~ReGnIWKbLFI@An}#EbzcSjz&69G9Fn!&j2Q4_uE_TMmjU#T>2GV Tx=rV^zmy(qpmPES0a5)60|StM diff --git a/code/C/unixsocket/client b/code/C/unixsocket/client deleted file mode 100755 index 1b9cb8828389f8b8c55e9e62528ebcbe8af10a0b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16136 zcmeHOeQX@X6`#8#U;>F9m(UPevW2ALqrTXQlbR2ga}p^XfwrVggP?|>Bvfq$ZH4%w5(X7?r_$CGN&}_2{@%=c zm%XhIRqY?B(mreU&HK%JZ)V=^&d%OGf2q57Ycvv3LJQPql$MqmB+d=?r$s}lxav?# za9pL%SEm79h!W@KAb%-p8iY5n-U4v4TS1k1_{R*agv$ttlU+Uf+8L%rxN0&o*)5_f z%R%n4Q{mEH?av}(kfo1RUSd(W-;QE)sKqE8>a6869GBzF#g0U>JI;2;*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JSC!*6e~o$y&vWG;_I;olF~S09r6u)L11XK^ETBL5&@FZ(UwcoNr8aQr`Jdx^iY zSeWk76(UU9|Y^<$a;V_TyDcQxQAiQ5gH}Wq&-x9m_byjo4LX7!xMrm;Gab z?SuN%m!Fn!|G{~ReGnIWKbLFI@An}#EbzcSjz&69G9Fn!&j2Q4_uE_TMmjU#T>2GV Tx=rV^zmy(qpmPES0a5)60|StM diff --git a/code/C/unixsocket/client.c b/code/C/unixsocket/client.c index 228a470..05e362b 100644 --- a/code/C/unixsocket/client.c +++ b/code/C/unixsocket/client.c @@ -12,68 +12,67 @@ #include int main(int argc, char *argv[]) { - struct sockaddr_un addr; - int i; - int ret; - int data_socket; - char buffer[BUFFER_SIZE]; + struct sockaddr_un addr; + int i; + int ret; + int data_socket; + char buffer[BUFFER_SIZE]; - /* Create local socket. */ - data_socket = socket(AF_UNIX, SOCK_STREAM, 0); - if (data_socket == -1) { - perror("socket"); - exit(EXIT_FAILURE); - } + /* Create local socket. */ + data_socket = socket(AF_UNIX, SOCK_STREAM, 0); + if (data_socket == -1) { + perror("socket"); + exit(EXIT_FAILURE); + } - /* - * For portability clear the whole structure, since some - * implementations have additional (nonstandard) fields in - * the structure. - */ - memset(&addr, 0, sizeof(struct sockaddr_un)); + /* + * For portability clear the whole structure, since some + * implementations have additional (nonstandard) fields in + * the structure. + */ + memset(&addr, 0, sizeof(struct sockaddr_un)); - /* Connect socket to socket address */ - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1); + /* Connect socket to socket address */ + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1); - ret = connect (data_socket, (const struct sockaddr *) &addr, - sizeof(struct sockaddr_un)); + ret = connect(data_socket, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)); + if (ret == -1) { + fprintf(stderr, "The server is down.\n"); + exit(EXIT_FAILURE); + } + + /* Send arguments. */ + for (i = 1; i < argc; ++i) { + ret = write(data_socket, argv[i], strlen(argv[i]) + 1); if (ret == -1) { - fprintf(stderr, "The server is down.\n"); - exit(EXIT_FAILURE); + perror("write"); + break; } + } - /* Send arguments. */ - for (i = 1; i < argc; ++i) { - ret = write(data_socket, argv[i], strlen(argv[i]) + 1); - if (ret == -1) { - perror("write"); - break; - } - } + /* Request result. */ + strcpy(buffer, "END"); + ret = write(data_socket, buffer, strlen(buffer) + 1); + if (ret == -1) { + perror("write"); + exit(EXIT_FAILURE); + } - /* Request result. */ - strcpy (buffer, "END"); - ret = write(data_socket, buffer, strlen(buffer) + 1); - if (ret == -1) { - perror("write"); - exit(EXIT_FAILURE); - } + /* Receive result. */ + ret = read(data_socket, buffer, BUFFER_SIZE); + if (ret == -1) { + perror("read"); + exit(EXIT_FAILURE); + } - /* Receive result. */ - ret = read(data_socket, buffer, BUFFER_SIZE); - if (ret == -1) { - perror("read"); - exit(EXIT_FAILURE); - } + /* Ensure buffer is 0-terminated. */ + buffer[BUFFER_SIZE - 1] = 0; - /* Ensure buffer is 0-terminated. */ - buffer[BUFFER_SIZE - 1] = 0; + printf("Result = %s\n", buffer); - printf("Result = %s\n", buffer); + /* Close socket. */ + close(data_socket); - /* Close socket. */ - close(data_socket); - - exit(EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/code/C/unixsocket/server b/code/C/unixsocket/server deleted file mode 100755 index 91ebdc11bb591f392ca5a155d1294d94f8717ec8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16208 zcmeHOeT-aH6~D9F((=*nQfVng>42>%Bs<;h$6CaN*X~E(O1~_v6jWYkcV@d|cW1_# zw`E(4t}aWuG)q z5>Ip=$109zkVmNw6xC;?f_B^3F38(hUpU$T-SMiM_$cjdVmmvk-KZd6WMCy++WS1& zAub=5g9|rMj9|EbU7)^rg}RgAIeLnj4x|#PXTgGTjCA3vFOb zt=+Iy)eNZ#U}BH*RdgV%sDJqROP+o2nTz*aeeldnn{RpWKu2W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^X!V-T1~KQ_QheqrEH(8V!G!sH#taUJs@Jaj2<4bWUtec zi}xp;-UQi-WGtL`E*Gbc!a~aAINqP?rdBiLi%v1!fN@5*SH-$B{r$z_;?y^Q@E zO=&OJem-wJgvF!D){ZUvAys`qR3pH-vW0A z?Dw+0@Te#<588i`aO^py|FSRjINR~|?0x&#JE{I{+Ovth#SI9T_yz6Rt%_5D5|h04 zx3j(Q^Bi!*4MmBc>^D<{;gY3&**6P+jq3aqynkP%O6*C+UiP8FZ(?KVzvv17I@M#3 zE3)i!g*UQ&u>TCJF)HAa#a{N=!Y5c!+7Dj;ciDao>&d=dxa{*`kC+ARe?S;wBJUaA z`o-&*yx)+Drr`Mb9-)5wDBB6wm23+p%cJlg2km*k8)bfw$FsN)JCT19u$TRqaCv_d z{b2u}Vta|din1p2b{2&N?SD-bxPGy>qefHsA|ra|p#5p8@!MykMpgKrXK4y@{{()4 z3bW~uFv)wV{QqW9Uu5Agk!_vNUfx?zH4sQ7dPbU(1%I6?Fm|MUc`uOv#|{49t=P$Z z#Qqo8FZS|(R})JKBwAvacNTl$7pTT>FYg%zwjcK_O+oC0*O0N_UiRCAjYePkDQ?8h zlgSvE^k4Ro``A9HkGA|Yi`!>)uS)ELxY+x-j6Kho3cq5*aqbv0RdJuBKl1#%lrZT# e6+N<7m|{b~xwI>an5LEN2e7aa%?lU=MD<^*!ObxM diff --git a/code/C/unixsocket/server.c b/code/C/unixsocket/server.c index f909b95..6e6aa39 100644 --- a/code/C/unixsocket/server.c +++ b/code/C/unixsocket/server.c @@ -11,113 +11,112 @@ #include int main(int argc, char *argv[]) { - struct sockaddr_un addr; - int down_flag = 0; - int ret; - int listen_socket; - int data_socket; - int result; - char buffer[BUFFER_SIZE]; + struct sockaddr_un addr; + int down_flag = 0; + int ret; + int listen_socket; + int data_socket; + int result; + char buffer[BUFFER_SIZE]; - /* - * In case the program exited inadvertently on the last run, - * remove the socket. - */ - unlink(SOCKET_NAME); + /* + * In case the program exited inadvertently on the last run, + * remove the socket. + */ + unlink(SOCKET_NAME); - /* Create local socket. */ - listen_socket = socket(AF_UNIX, SOCK_STREAM, 0); - if (listen_socket == -1) { - perror("socket"); - exit(EXIT_FAILURE); + /* Create local socket. */ + listen_socket = socket(AF_UNIX, SOCK_STREAM, 0); + if (listen_socket == -1) { + perror("socket"); + exit(EXIT_FAILURE); + } + + /* + * For portability clear the whole structure, since some + * implementations have additional (nonstandard) fields in + * the structure. + */ + memset(&addr, 0, sizeof(struct sockaddr_un)); + + /* Bind socket to socket name. */ + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1); + + ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)); + if (ret == -1) { + perror("bind"); + exit(EXIT_FAILURE); + } + + /* + * Prepare for accepting connections. The backlog size is set + * to 20. So while one request is being processed other requests + * can be waiting. + */ + ret = listen(listen_socket, 20); + if (ret == -1) { + perror("listen"); + exit(EXIT_FAILURE); + } + + /* This is the main loop for handling connections. */ + for (;;) { + /* Wait for incoming connection. */ + data_socket = accept(listen_socket, NULL, NULL); + if (data_socket == -1) { + perror("accept"); + exit(EXIT_FAILURE); } - /* - * For portability clear the whole structure, since some - * implementations have additional (nonstandard) fields in - * the structure. - */ - memset(&addr, 0, sizeof(struct sockaddr_un)); - - /* Bind socket to socket name. */ - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1); - - ret = bind(listen_socket, (const struct sockaddr *) &addr, - sizeof(struct sockaddr_un)); - if (ret == -1) { - perror("bind"); - exit(EXIT_FAILURE); - } - - /* - * Prepare for accepting connections. The backlog size is set - * to 20. So while one request is being processed other requests - * can be waiting. - */ - ret = listen(listen_socket, 20); - if (ret == -1) { - perror("listen"); - exit(EXIT_FAILURE); - } - - /* This is the main loop for handling connections. */ + result = 0; for (;;) { - /* Wait for incoming connection. */ - data_socket = accept(listen_socket, NULL, NULL); - if (data_socket == -1) { - perror("accept"); - exit(EXIT_FAILURE); - } + /* Wait for next data packet. */ + ret = read(data_socket, buffer, BUFFER_SIZE); + if (ret == -1) { + perror("read"); + exit(EXIT_FAILURE); + } - result = 0; - for(;;) { - /* Wait for next data packet. */ - ret = read(data_socket, buffer, BUFFER_SIZE); - if (ret == -1) { - perror("read"); - exit(EXIT_FAILURE); - } + /* Ensure buffer is 0-terminated. */ + buffer[BUFFER_SIZE - 1] = 0; - /* Ensure buffer is 0-terminated. */ - buffer[BUFFER_SIZE - 1] = 0; + /* Handle commands. */ + if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) { + down_flag = 1; + break; + } - /* Handle commands. */ - if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) { - down_flag = 1; - break; - } + if (!strncmp(buffer, "END", BUFFER_SIZE)) { + break; + } - if (!strncmp(buffer, "END", BUFFER_SIZE)) { - break; - } - - /* Add received summand. */ - result += atoi(buffer); - } - - /* Send result. */ - sprintf(buffer, "%d", result); - ret = write(data_socket, buffer, BUFFER_SIZE); - - if (ret == -1) { - perror("write"); - exit(EXIT_FAILURE); - } - - /* Close socket. */ - close(data_socket); - - /* Quit on DOWN command. */ - if (down_flag) { - break; - } + /* Add received summand. */ + result += atoi(buffer); } - close(listen_socket); + /* Send result. */ + sprintf(buffer, "%d", result); + ret = write(data_socket, buffer, BUFFER_SIZE); - /* Unlink the socket. */ - unlink(SOCKET_NAME); + if (ret == -1) { + perror("write"); + exit(EXIT_FAILURE); + } - exit(EXIT_SUCCESS); + /* Close socket. */ + close(data_socket); + + /* Quit on DOWN command. */ + if (down_flag) { + break; + } + } + + close(listen_socket); + + /* Unlink the socket. */ + unlink(SOCKET_NAME); + + exit(EXIT_SUCCESS); }