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
+74
View File
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#include <time.h>
#include <sys/types.h>
enum
{
YEAR_OFFSET = 1900
};
int
main(void)
{
int fd[2];
pipe(fd);
time_t delta;
struct tm *dt;
pid_t pid = fork();
if (pid == 0) {
pid = fork();
if (pid == 0) {
pid = fork();
if (pid == 0) {
close(fd[0]);
delta = time(NULL);
if (write(fd[1], &delta, sizeof(delta)) == -1 || write(fd[1], &delta, sizeof(delta)) == -1 ||
write(fd[1], &delta, sizeof(delta)) == -1) {
_exit(1);
}
} else if (pid == -1) {
_exit(1);
}
close(fd[1]);
wait(NULL);
if (read(fd[0], &delta, sizeof(delta)) == -1) {
_exit(1);
}
dt = localtime(&delta);
printf("D:%02d\n", dt->tm_mday);
fflush(stdout);
_exit(0);
} else if (pid == -1) {
_exit(1);
}
close(fd[1]);
wait(NULL);
if (read(fd[0], &delta, sizeof(delta)) == -1) {
_exit(1);
}
dt = localtime(&delta);
printf("M:%02d\n", dt->tm_mon + 1);
fflush(stdout);
_exit(0);
} else if (pid == -1) {
_exit(1);
}
close(fd[1]);
wait(NULL);
if (read(fd[0], &delta, sizeof(delta)) == -1) {
_exit(1);
}
dt = localtime(&delta);
printf("Y:%04d\n", dt->tm_year + YEAR_OFFSET);
fflush(stdout);
return 0;
}
+92
View File
@@ -0,0 +1,92 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
enum
{
ACCESS_FILE = 0660
};
int
main(int argc, char **argv)
{
char *cmd1 = argv[1];
char *cmd2 = argv[2];
char *cmd3 = argv[3];
char *file1 = argv[4];
char *file2 = argv[5];
int fd1 = open(file1, O_RDONLY);
int fd2 = open(file2, O_WRONLY | O_APPEND | O_CREAT, ACCESS_FILE);
if (fd1 == -1 || fd2 == -1) {
exit(1);
}
int fd[2];
if (pipe(fd) == -1) {
return 1;
}
pid_t pid = fork();
if (pid == 0) {
close(fd[0]);
if (dup2(fd[1], 1) == -1) {
_exit(1);
}
pid = fork();
if (pid == 0) {
if (dup2(fd1, 0) == -1) {
_exit(1);
}
execlp(cmd1, cmd1, NULL);
_exit(1);
} else if (pid == -1) {
exit(1);
}
int status;
wait(&status);
if (WIFEXITED(status) && !WEXITSTATUS(status)) {
pid = fork();
if (pid == 0) {
execlp(cmd2, cmd2, NULL);
_exit(1);
} else if (pid == -1) {
exit(1);
}
wait(NULL);
}
_exit(0);
} else if (pid == -1) {
exit(1);
}
close(fd[1]);
pid = fork();
if (pid == 0) {
if (dup2(fd2, 1) == -1 || dup2(fd[0], 0)) {
_exit(1);
}
execlp(cmd3, cmd3, NULL);
_exit(1);
} else if (pid == -1) {
exit(1);
}
close(fd[0]);
while (wait(NULL) != -1)
;
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
void
ex(void)
{
// разослать SIGKILL
// а некому.
exit(1);
}
void
swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
return;
}
int
main(int argc, char **argv)
{
int fd1[2];
int fd2[2];
if (close(2) == -1 || pipe(fd1) == -1 || pipe(fd2)) {
ex();
}
for (int i = 1; i < argc; ++i) {
pid_t pid = fork();
if (pid == 0) {
if (i != 1 && i != argc - 1) {
if (dup2(fd2[1], 1) == -1 || dup2(fd1[0], 0) == -1) {
ex();
}
} else if (i == 1) {
if (dup2(fd2[1], 1) == -1) {
ex();
}
} else {
if (dup2(fd1[0], 0) == -1) {
ex();
}
}
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
execlp(argv[i], argv[i], NULL);
_exit(1);
} else if (pid == -1) {
ex();
}
swap(&fd1[0], &fd2[0]);
swap(&fd1[1], &fd2[1]);
wait(NULL);
}
return 0;
}
+73
View File
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <wait.h>
int
main(int argc, char **argv)
{
int n = (int) strtoll(argv[1], NULL, 10);
int fd1[2];
int fd2[2];
pipe(fd1);
pipe(fd2);
int tmp = 1;
write(fd2[1], &tmp, sizeof(tmp));
if (!fork()) {
int i = 0;
close(fd1[0]);
close(fd2[1]);
while (i < n) {
if (read(fd2[0], &i, sizeof(i)) == -1) {
return 0;
}
if (i >= n) {
write(fd1[1], &i, sizeof(i));
break;
}
printf("%d %d\n", 1, i);
fflush(stdout);
i++;
write(fd1[1], &i, sizeof(i));
}
return 0;
} else if (!fork()) {
int i = 0;
close(fd2[0]);
close(fd1[1]);
while (i < n) {
if (read(fd1[0], &i, sizeof(i)) == -1) {
return 0;
}
if (i >= n) {
write(fd1[1], &i, sizeof(i));
break;
}
printf("%d %d\n", 2, i);
fflush(stdout);
i++;
write(fd2[1], &i, sizeof(i));
}
return 0;
}
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
while (wait(NULL) != -1)
;
printf("Done\n");
return 0;
}