init
This commit is contained in:
commit
29da8777e6
9 changed files with 582 additions and 0 deletions
173
trq/trq.c
Normal file
173
trq/trq.c
Normal file
|
@ -0,0 +1,173 @@
|
|||
/*
|
||||
* trq.c
|
||||
*
|
||||
* Created on: 17 авг. 2022 г.
|
||||
* Author: Alexander Zhirov
|
||||
* Mail: alexander@zhirov.website
|
||||
* Telegram: alexanderzhirov
|
||||
*/
|
||||
|
||||
#include "trq.h"
|
||||
|
||||
#include <syslog.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <regex.h>
|
||||
|
||||
void ts_free(ts_args *args)
|
||||
{
|
||||
if (args)
|
||||
{
|
||||
if (args->command)
|
||||
free(args->command);
|
||||
if (args->file_src)
|
||||
free(args->file_src);
|
||||
if (args->file_dst)
|
||||
free(args->file_dst);
|
||||
if (args->path_src)
|
||||
free(args->path_src);
|
||||
if (args->command)
|
||||
free(args->path_dst);
|
||||
free(args);
|
||||
}
|
||||
}
|
||||
|
||||
static char* ts_concat_path(const char *file)
|
||||
{
|
||||
if (!file)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ Invalid value file (ts_concat_path)\n");
|
||||
exit(3);
|
||||
}
|
||||
const char *path_mac = "mac.cfg/";
|
||||
size_t len1 = strlen(path_mac);
|
||||
size_t len2 = strlen(file);
|
||||
char *result = (char*) malloc(sizeof(char) * (len1 + len2 + 1));
|
||||
if (!result)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ Disk full or allocation exceeded (ts_concat_path)\n");
|
||||
exit(3);
|
||||
}
|
||||
strncpy(result, path_mac, (len1 + 1));
|
||||
strncpy(result + len1, file, (len2 + 1));
|
||||
return result;
|
||||
}
|
||||
|
||||
static char* ts_copy_string(const char *string)
|
||||
{
|
||||
if (!string)
|
||||
return NULL;
|
||||
int len = strlen(string);
|
||||
char *tmp = (char*) calloc(1, sizeof(char) * (len + 1));
|
||||
if (!tmp)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ Disk full or allocation exceeded (ts_copy_string)\n");
|
||||
exit(3);
|
||||
}
|
||||
strncpy(tmp, string, (len + 1));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
ts_args* ts_get_arguments(const char *filename, const char *path)
|
||||
{
|
||||
ts_args *args = (ts_args*) calloc(1, sizeof(ts_args));
|
||||
if (!args)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ Disk full or allocation exceeded (ts_get_arguments)\n");
|
||||
exit(3);
|
||||
}
|
||||
char separator[2] = ";\0";
|
||||
char *part = NULL;
|
||||
int len = strlen(filename);
|
||||
int next = 1; /* reading the next iteration */
|
||||
char *tmp_filename = (char*) calloc(1, sizeof(char) * (len + 1));
|
||||
if (!tmp_filename)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ Disk full or allocation exceeded (ts_get_arguments)\n");
|
||||
exit(3);
|
||||
}
|
||||
strncpy(tmp_filename, filename, (len + 1));
|
||||
part = strtok(tmp_filename, separator);
|
||||
while (part)
|
||||
{
|
||||
switch (args->size)
|
||||
{
|
||||
case 0:
|
||||
regex_t regex;
|
||||
regcomp(®ex, "^01-[0-9a-f]{2}(-[0-9a-f]{2}){5}$", REG_EXTENDED);
|
||||
int reti = regexec(®ex, part, 0, NULL, 0);
|
||||
if (reti)
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ The MAC address was entered incorrectly (ts_get_arguments)\n");
|
||||
exit(100);
|
||||
}
|
||||
args->file_dst = ts_copy_string(part);
|
||||
args->path_dst = ts_concat_path(part);
|
||||
break;
|
||||
case 1:
|
||||
if (!strncmp("cr", part, 3)) {}
|
||||
else if (!strncmp("rm", part, 3))
|
||||
next = 0;
|
||||
else
|
||||
{
|
||||
syslog(LOG_NOTICE, "TRQ The command was specified incorrectly (ts_get_arguments)\n");
|
||||
exit(100);
|
||||
}
|
||||
args->command = ts_copy_string(part);
|
||||
break;
|
||||
case 2:
|
||||
args->file_src = ts_copy_string(part);
|
||||
args->path_src = ts_concat_path(part);
|
||||
break;
|
||||
default:
|
||||
syslog(LOG_NOTICE, "TRQ Invalid request format (ts_get_arguments)\n");
|
||||
exit(100);
|
||||
}
|
||||
++args->size;
|
||||
if (next)
|
||||
part = strtok(NULL, separator);
|
||||
else
|
||||
part = NULL;
|
||||
}
|
||||
free(tmp_filename);
|
||||
if (!args->size)
|
||||
{
|
||||
free(args);
|
||||
args = NULL;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
void ts_syslog(const ts_args *args)
|
||||
{
|
||||
if (args)
|
||||
{
|
||||
if (args->size > 2)
|
||||
syslog(LOG_NOTICE, "TRQ Create symlink %s on %s\n", args->file_dst, args->file_src);
|
||||
else
|
||||
syslog(LOG_NOTICE, "TRQ Remove symlink %s\n", args->file_dst);
|
||||
}
|
||||
}
|
||||
|
||||
void ts_symlink(const ts_args *args)
|
||||
{
|
||||
if (args)
|
||||
{
|
||||
if (args->size > 2)
|
||||
{
|
||||
if (access(args->path_src, F_OK) == 0 && !symlink(args->file_src, args->path_dst))
|
||||
syslog(LOG_NOTICE, "TRQ A symbolic link %s has been created\n", args->file_dst);
|
||||
else
|
||||
syslog(LOG_NOTICE, "TRQ Failed to create symbolic link %s. %s\n", args->file_dst, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (access(args->path_dst, F_OK) == 0 && !unlink(args->path_dst))
|
||||
syslog(LOG_NOTICE, "TRQ A symbolic link %s has been removed\n", args->file_dst);
|
||||
else
|
||||
syslog(LOG_NOTICE, "TRQ Failed to remove symbolic link %s. %s\n", args->file_dst, strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue