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
+31
View File
@@ -0,0 +1,31 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
enum
{
KIBIBYTE = 1024
};
int
main(int argc, char *argv[])
{
long long int sum = 0;
for (int i = 1; i < argc; ++i) {
struct stat st;
if (!(lstat(argv[i], &st) < 0 || st.st_nlink != 1 || !S_ISREG(st.st_mode) || st.st_size % KIBIBYTE != 0)) {
sum += st.st_size;
}
}
printf("%lld\n", sum);
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
enum
{
MAX_MODE = 0777,
};
int
main(int argc, char *argv[])
{
const char s[] = "rwxrwxrwx";
for (int i = 1; i < argc; ++i) {
char *p;
errno = 0;
int n = strtol(argv[i], &p, 8);
if (errno || *p || p == argv[i] || n > MAX_MODE || n < 0) {
return 1;
}
for (int j = 0; j < sizeof(s) - 1; ++j) {
putchar((n & (1 << (sizeof(s) - 2 - j))) ? s[j] : '-');
}
printf("\n");
}
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
#include <stdlib.h>
enum
{
RWX_PERMISSIONS = 0777,
};
int
parse_rwx_permissions(const char *str)
{
if (str == NULL) {
return -1;
}
const char expected[] = "rwxrwxrwx";
unsigned res = 0;
int i = 0;
while (str[i]) {
res <<= 1;
if (str[i] == expected[i] && expected[i]) {
res |= 1;
} else if (str[i] != '-' || !expected[i]) {
return -1;
}
++i;
}
if (res > RWX_PERMISSIONS || res < 0 || expected[i]) {
return -1;
}
return res;
}
+92
View File
@@ -0,0 +1,92 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <string.h>
#include <linux/limits.h>
struct File
{
char *pathname;
__ino_t st_ino;
__dev_t st_dev;
};
int
cmp_structs(struct File elem1, struct stat elem2)
{
return elem1.st_dev == elem2.st_dev && elem1.st_ino == elem2.st_ino;
}
int
cmp_mas(const void *a, const void *b)
{
struct File elem1 = *(struct File *) a;
struct File elem2 = *(struct File *) b;
return strcmp(elem1.pathname, elem2.pathname);
}
int
main(int argc, char **argv)
{
struct File *mas = calloc(10, sizeof(struct File));
if (mas == NULL) {
fprintf(stderr, "Error! Can't calloc mas\n");
return 1;
}
int n = 0, capacity = 10;
for (int i = 1; i < argc; ++i) {
struct stat st;
if (stat(argv[i], &st) < 0) {
continue;
}
int flag = 0;
for (int j = 0; j < n; ++j) {
if (cmp_structs(mas[j], st)) { // 1 if equal
flag = 1;
if (strcmp(mas[j].pathname, argv[i]) < 0) {
mas[j].pathname = argv[i];
}
break;
}
}
if (!flag) {
if (n == capacity) {
capacity *= 2;
while (!realloc(mas, capacity * sizeof(mas[0])) && capacity > n) {
capacity--;
}
if (capacity == n) {
fprintf(stderr, "Error! Can't realloc mas\n");
return 1;
}
}
mas[n++] = (struct File){argv[i], st.st_ino, st.st_dev};
}
}
qsort(mas, n, sizeof(mas[0]), cmp_mas);
for (int i = 0; i < n; ++i) {
printf("%s\n", mas[i].pathname);
}
free(mas);
return 0;
}
+99
View File
@@ -0,0 +1,99 @@
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <stdio.h>
char *
relativize_path(const char *path1, const char *path2)
{
char *spath1 = calloc(strlen(path1) + 2, sizeof(char));
strcpy(spath1, path1);
spath1[strlen(path1)] = '\0';
if (spath1[strlen(path1) - 1] != '/') {
strcat(spath1, "/");
}
char *res;
if ((res = calloc(PATH_MAX, sizeof(char))) == NULL) {
exit(1);
}
int i = 0;
int ptr = 0;
while (spath1[i] == path2[i] && spath1[i]) {
if (spath1[i] == '/') {
ptr = i;
}
i++;
}
int deep1 = 0;
while (spath1[i]) {
if (spath1[i] == '/') {
deep1++;
}
i++;
}
if (!deep1 && path2[strlen(path2) - 1] == '/') {
snprintf(res, PATH_MAX, ".");
return res;
}
int deep2 = 0;
i = ptr + 1;
while (path2[i]) {
if (path2[i] == '/') {
deep2++;
}
i++;
}
for (int j = 0; j < deep1; j++) {
res[j * 3] = '.';
res[j * 3 + 1] = '.';
res[j * 3 + 2] = '/';
}
if (deep2 == 0 && deep1) {
if (res[strlen(res) - 1] == '/') {
res[strlen(res) - 1] = '\0';
}
return res;
}
snprintf(res + deep1 * 3, PATH_MAX - deep1 * 3, "%s", path2 + ptr + 1);
if (res[strlen(res) - 1] == '/') {
res[strlen(res) - 1] = '\0';
}
return res;
}
int
main()
{
const char *path1 = "/home/krosh/cmc/contest5";
const char *path2 = "/home/krosh/cmc/contest5/subdir/file.txt";
char *relative_path = relativize_path(path1, path2);
printf("Relative path: %s\n", relative_path);
const char *path3 = "/a/b/c/d";
const char *path4 = "/a/e/f";
printf("Relative path: %s\n", relativize_path(path3, path4));
const char *path5 = "/a/f/g/h/j";
const char *path6 = "/a/f/g/h";
printf("Relative path: %s\n", relativize_path(path5, path6));
const char *path7 = "/a";
const char *path8 = "/a";
printf("Relative path: %s\n", relativize_path(path7, path8));
return 0;
}
+99
View File
@@ -0,0 +1,99 @@
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <stdio.h>
char *
relativize_path(const char *path1, const char *path2)
{
char *spath1 = calloc(strlen(path1) + 2, sizeof(char));
strcpy(spath1, path1);
spath1[strlen(path1)] = '\0';
if (spath1[strlen(path1) - 1] != '/') {
strcat(spath1, "/");
}
char *res;
if ((res = calloc(PATH_MAX, sizeof(char))) == NULL) {
exit(1);
}
int i = 0;
int ptr = 0;
while (spath1[i] == path2[i] && spath1[i]) {
if (spath1[i] == '/') {
ptr = i;
}
i++;
}
int deep1 = 0;
while (spath1[i]) {
if (spath1[i] == '/') {
deep1++;
}
i++;
}
if (!deep1 && path2[strlen(path2) - 1] == '/') {
snprintf(res, PATH_MAX, ".");
return res;
}
int deep2 = 0;
i = ptr + 1;
while (path2[i]) {
if (path2[i] == '/') {
deep2++;
}
i++;
}
for (int j = 0; j < deep1; j++) {
res[j * 3] = '.';
res[j * 3 + 1] = '.';
res[j * 3 + 2] = '/';
}
if (deep2 == 0 && deep1) {
if (res[strlen(res) - 1] == '/') {
res[strlen(res) - 1] = '\0';
}
return res;
}
snprintf(res + deep1 * 3, PATH_MAX - deep1 * 3, "%s", path2 + ptr + 1);
if (res[strlen(res) - 1] == '/') {
res[strlen(res) - 1] = '\0';
}
return res;
}
int
main()
{
const char *path1 = "/home/krosh/cmc/contest5";
const char *path2 = "/home/krosh/cmc/contest5/subdir/file.txt";
char *relative_path = relativize_path(path1, path2);
printf("Relative path: %s\n", relative_path);
const char *path3 = "/a/b/c/d";
const char *path4 = "/a/e/f";
printf("Relative path: %s\n", relativize_path(path3, path4));
const char *path5 = "/a/f/g/h/j";
const char *path6 = "/a/f/g/h";
printf("Relative path: %s\n", relativize_path(path5, path6));
const char *path7 = "/a";
const char *path8 = "/a";
printf("Relative path: %s\n", relativize_path(path7, path8));
return 0;
}