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
+50
View File
@@ -0,0 +1,50 @@
#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 <dirent.h>
#include <linux/limits.h>
struct Files
{
char *pathname;
__ino_t st_ino;
__dev_t st_dev;
};
int
main(int argc, char **argv)
{
struct stat st;
if (lstat(".", &st) == -1) {
printf("stat error\n");
return 1;
}
DIR *dir = opendir("..");
struct dirent *de;
while ((de = readdir(dir))) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
continue;
}
struct stat sttmp;
char buf[PATH_MAX];
snprintf(buf, PATH_MAX, "%s/%s", "..", de->d_name);
if (lstat(buf, &sttmp) == -1) {
printf("lstat error\n");
return 1;
}
printf("%s\n", de->d_name);
if (sttmp.st_dev == st.st_dev && sttmp.st_ino == st.st_ino) {
printf("%s\n", de->d_name);
}
}
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include <time.h>
#include <stdio.h>
void
f(int *y, int *m, int *d, int offset)
{ // offset in seconds
struct tm t = {0};
// struct tm* tptr = calloc(1, sizeof(struct tm));
t.tm_isdst = -1;
t.tm_year = *y - 1900;
t.tm_mon = *m - 1;
t.tm_mday = *d;
time_t time = mktime(&t);
time += offset;
localtime_r(&time, &t);
*y = t.tm_year + 1900;
*m = t.tm_mon + 1;
*d = t.tm_mday;
return;
}
int
main(void)
{
int y = 2021;
int m = 1;
int d = 1;
f(&y, &m, &d, 65 * 60 * 60 * 24);
printf("%d-%d-%d\n", y, m, d);
return 0;
}
+45
View File
@@ -0,0 +1,45 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
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;
}
int
main(int argc, char **argv)
{
long long year, day, dw, res = 0;
str_to_ll(argv[1], &year);
str_to_ll(argv[2], &day);
str_to_ll(argv[3], &dw);
struct tm t = {0};
t.tm_isdst = -1;
t.tm_mday = 1;
t.tm_year = year - 1900;
while (t.tm_year == year - 1900) {
mktime(&t);
if (t.tm_mday == day && dw == t.tm_wday) {
res++;
}
t.tm_mday++;
}
printf("%d\n", (int) res);
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
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;
}
int
main(int argc, char **argv)
{
long long year, mth, res = 0;
str_to_ll(argv[1], &year);
str_to_ll(argv[2], &mth);
struct tm t = {0};
t.tm_isdst = -1;
t.tm_mday = 1;
t.tm_mon = mth - 1;
t.tm_year = year - 1900;
while (t.tm_mon == mth - 1) {
if (t.tm_wday > 0 && t.tm_wday < 5) {
res += 8;
} else if (t.tm_wday == 5) {
res += 6;
}
t.tm_mday++;
mktime(&t);
}
printf("%d\n", (int) res);
return 0;
}