init commit

This commit is contained in:
2025-04-13 21:48:15 +03:00
commit 23255e9121
192 changed files with 12200 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
#include <stdio.h> // Standard I/O functions
#include <stdlib.h> // Standard library functions: memory allocation, process control, conversions, etc.
#include <unistd.h> // POSIX API: read, write, close, etc.
#include <sys/types.h> // Data types used in system calls
#include <sys/stat.h> // Data returned by the functions fstat(), lstat(), and stat()
#include <limits.h> // Sizes of basic types
#include <dirent.h> // Directory entry format
#include <string.h> // String handling functions
enum
{
ARGS_NUM = 2,
};
int
main(int argc, char *argv[])
{
if (argc != ARGS_NUM) {
fprintf(stderr, "Wrong number of arguments\n");
return 1;
}
int n = 0;
DIR *ddir = opendir(argv[1]);
struct dirent *de;
while ((de = readdir(ddir))) {
char *name = calloc(PATH_MAX, sizeof(char));
if (!name) {
return 1;
}
int ret = snprintf(name, PATH_MAX, "%s/%s", argv[1], de->d_name);
if (ret < 0 || ret >= PATH_MAX) {
free(name);
return 1;
}
struct stat st;
if (stat(name, &st) < 0) {
continue;
}
char ext[] = ".exe";
if (S_ISREG(st.st_mode) && !access(name, X_OK) && strlen(name) >= 4 &&
!strcmp(name + strlen(name) - (sizeof(ext) - 1), ext)) {
++n;
}
}
printf("%d\n", n);
closedir(ddir);
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
long long
stlen(char *buf)
{
long long i = 0;
for (; buf[i] != '\0'; ++i) {
}
return i;
}
long long
shift(char *buf, long long st, long long sh, long long len)
{
long long i = st;
if (i + sh > len) {
return len;
}
for (; i + sh <= len; ++i) {
buf[i] = buf[i + sh];
}
return len - sh;
}
void
normalize_path(char *buf)
{
long long len = stlen(buf);
for (int i = 1; buf[i]; ++i) {
if (buf[i] == '.' && buf[i - 1] == '/') {
if (buf[i + 1] == '.' && (buf[i + 2] == '/' buf[i + 2] == '\0')) {
if (i - 2 >= 0) {
int j;
for (j = i - 2; buf[j] != '/'; --j) {
}
++j;
if (buf[i + 2] == '/') {
len = shift(buf, j, i + 3 - j, len);
i = j - 1;
} else if (!buf[i + 2]) {
len = shift(buf, j, i + 2 - j, len);
i = j - 1;
}
} else {
if (buf[i + 2] == '/') {
len = shift(buf, i, 3, len);
--i;
} else if (!buf[i + 2]) {
len = shift(buf, i, 2, len);
--i;
}
}
} else if (buf[i + 1] == '/' buf[i + 1] == '\0') {
if (buf[i + 1] == '/') {
len = shift(buf, i, 2, len);
--i;
} else if (!buf[i + 1]) {
len = shift(buf, i, 1, len);
--i;
}
}
}
}
len = stlen(buf);
if (len != 1) {
if (buf[len - 1] == '/') {
buf[len - 1] = '\0';
}
}
}
+110
View File
@@ -0,0 +1,110 @@
#include <stdio.h> // Standard I/O functions
#include <stdlib.h> // Standard library functions: memory allocation, process control, conversions, etc.
#include <fcntl.h> // File control options
#include <unistd.h> // POSIX API: read, write, close, etc.
#include <sys/file.h> // File control options
#include <sys/types.h> // Data types used in system calls
#include <sys/stat.h> // Data returned by the functions fstat(), lstat(), and stat()
#include <linux/limits.h> // Sizes of basic types
#include <limits.h> // Sizes of basic types
#include <errno.h> // Error reporting macros
#include <stdint.h> // Exact-width integer types
#include <math.h> // Mathematical functions
#include <dirent.h> // Directory entry format
#include <string.h> // String handling functions
#include <sys/stat.h> // Data returned by the functions fstat(), lstat(), and stat()
// переписать на strcmp.
void
normalize_path(char *buf)
{
unsigned long long pt_real = 0, pt_res = 0;
unsigned long long len = 0; // проверить правильное вычисление длины
while (buf[len++] != '\0')
;
len--;
while (pt_real < len) {
printf("pt: %lld -- %d %d %d\n", pt_real, pt_real - 2 == len, buf[pt_real] == '.',
buf[pt_real + 1] == '.'); // может вызвать выход за массив
if ((len - pt_real >= 3 && buf[pt_real] == '.' && buf[pt_real + 1] == '.' && buf[pt_real + 2] == '/') ||
(pt_real - 2 == len && buf[pt_real] == '.' && buf[pt_real + 1] == '.')) {
printf("meow\n");
if (pt_real > 0 && buf[pt_real - 1] == '/') {
pt_real += 3;
if (pt_res == 1) {
continue;
}
if (pt_res == 0) { // impossible ?
pt_res = 1;
continue;
}
pt_res -= 2;
while (pt_res > 0 && buf[pt_res] != '/') {
pt_res--;
}
pt_res++;
continue;
}
} else if ((len - pt_real >= 2 && buf[pt_real] == '.' && buf[pt_real + 1] == '/') ||
(len == pt_real - 1 && buf[pt_real] == '.')) {
if (pt_real > 0 && buf[pt_real - 1] == '/') {
// printf("paw\n");
pt_real += 2;
continue;
}
}
buf[pt_res] = buf[pt_real];
pt_real++;
pt_res++;
}
// if (pt_real > 1 && buf[pt_real - 1] == '.') {
// if (pt_real > 2 && buf[pt_real - 2] == '.' && buf[pt_real - 3] == '/') {
// if (pt_res > 1) {
// pt_res -= 2;
// while (pt_res > 0 && buf[pt_res - 1] != '/') {
// pt_res--;
// }
// }
// } else if (pt_real > 1 && buf[pt_real - 2] == '/') {
// if (pt_res > 0) {
// pt_res--;
// }
// }
// }
buf[pt_res] = '\0';
if (pt_res > 1 && buf[pt_res - 1] == '/') {
buf[pt_res - 1] = '\0';
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
return EXIT_FAILURE;
}
char path[PATH_MAX];
strncpy(path, argv[1], PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
normalize_path(path);
printf("Normalized path: %s\n", path);
return EXIT_SUCCESS;
}
+114
View File
@@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/limits.h>
#include <limits.h>
#include <errno.h>
#include <stdint.h>
#include <math.h>
#include <dirent.h>
#include <string.h>
size_t
len(const char *buf)
{
size_t i = 0;
while (buf[i] != '\0') {
++i;
}
return i;
}
size_t
shift_left(char *str, size_t st, size_t shift_amount, size_t length)
{
size_t i = st;
if (i + shift_amount > length) {
return length;
}
while (i + shift_amount < length) {
str[i] = str[i + shift_amount];
++i;
}
return length - shift_amount;
}
void
normalize_path(char *path)
{
size_t length = len(path);
size_t i = 1;
while (path[i]) {
if (path[i - 1] == '/' && path[i] == '.') {
if (path[i + 1] == '.' && (path[i + 2] == '/' || !path[i + 2])) {
if (i < 1) {
if (path[i + 2] == '/') {
length = shift_left(path, i, 3, length);
--i;
} else if (!path[i + 2]) {
length = shift_left(path, i, 2, length);
--i;
}
} else {
size_t j = i - 2;
while (j > 0 && path[j] != '/') {
--j;
}
++j;
if (path[i + 2] == '/') {
length = shift_left(path, j, i + 3 - j, length);
i = j - 1;
} else if (!path[i + 2]) {
length = shift_left(path, j, i + 2 - j, length);
i = j - 1;
}
}
} else if (path[i + 1] == '/' || !path[i + 1]) {
if (path[i + 1] == '/') {
length = shift_left(path, i, 2, length);
--i;
} else if (!path[i + 1]) {
length = shift_left(path, i, 1, length);
--i;
}
}
}
++i;
}
if (len(path) != 1 && path[len(path) - 1] == '/') {
path[len(path) - 1] = '\0';
}
return;
}
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
return EXIT_FAILURE;
}
char path[PATH_MAX];
strncpy(path, argv[1], PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
normalize_path(path);
printf("Normalized path: %s\n", path);
return EXIT_SUCCESS;
}
+36
View File
@@ -0,0 +1,36 @@
#include <stdlib.h>
struct s1
{
char f1;
long long f2;
char f3;
};
struct s2
{
char f1;
char f3;
long long f2;
};
size_t
compactify(void *ptr, size_t size)
{
if (size == 0) {
return 0;
}
size_t count = size / sizeof(struct s1);
struct s1 *src = (struct s1 *) ptr;
struct s2 *dst = (struct s2 *) ptr;
for (size_t i = 0; i < count; ++i) {
struct s1 tmp = src[i];
dst[i].f1 = tmp.f1;
dst[i].f3 = tmp.f3;
dst[i].f2 = tmp.f2;
}
return count * sizeof(struct s2);
}
+119
View File
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <linux/limits.h>
#include <errno.h>
enum
{
ARGS_NUM = 3,
MAX_DIRS = 1000,
};
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
void
list_files(char *path_from, const int start, size_t max_size, int depth)
{
if (depth > 4) {
return;
}
DIR *dir = opendir(path_from);
if (dir == NULL) {
return;
}
char **dirs = (char **) calloc(MAX_DIRS, sizeof(char *));
if (dirs == NULL) {
exit(1);
}
int cnt = 0;
struct dirent *dd;
while ((dd = readdir(dir))) {
if (!(strcmp(dd->d_name, ".") && strcmp(dd->d_name, ".."))) {
continue;
}
char name[PATH_MAX];
int ret = snprintf(name, sizeof(name), "%s/%s", path_from, dd->d_name);
if (ret < 0 || ret >= PATH_MAX) {
exit(1);
}
struct stat st;
if (lstat(name, &st) < 0) {
continue;
}
if (S_ISDIR(st.st_mode)) {
dirs[cnt] = (char *) calloc(PATH_MAX, sizeof(char));
memcpy(dirs[cnt], name, strlen(name));
cnt++;
} else if (st.st_size <= max_size && !access(name, R_OK) && S_ISREG(st.st_mode)) {
ret = snprintf(name, sizeof(name), "%s/%s", path_from + start + 1, dd->d_name);
if (ret < 0 || ret >= PATH_MAX) {
exit(1);
}
printf("%s\n", name + (name[0] == '/'));
}
}
for (int i = 0; i < cnt; i++) {
list_files(dirs[i], start, max_size, depth + 1);
free(dirs[i]);
}
closedir(dir);
free(dirs);
return;
}
int
main(int argc, char *argv[])
{
if (argc != ARGS_NUM) {
return 1;
}
int len_arg = strlen(argv[1]);
char *dir_path = calloc(PATH_MAX, sizeof(char));
memcpy(dir_path, argv[1], len_arg - (argv[1][len_arg - 1] == '/'));
int len_path = strlen(dir_path);
if (!len_path) {
dir_path[0] = '/';
dir_path[1] = '\0';
}
long long max_size;
str_to_ll(argv[2], &max_size);
list_files(dir_path, len_path, max_size, 1);
free(dir_path);
return EXIT_SUCCESS;
}