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
+47
View File
@@ -0,0 +1,47 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
// CMD < FILE1 >> FILE2 2> FILE3
enum
{
EXIT_ERROR = 42,
ACCESS_FILE = 0660
};
int
main(int argc, char **argv)
{
char *cmd = argv[1];
char *file1 = argv[2];
char *file2 = argv[3];
char *file3 = argv[4];
pid_t pid = fork();
if (pid == 0) {
int fd1 = open(file1, O_RDONLY);
int fd2 = open(file2, O_WRONLY | O_APPEND | O_CREAT, ACCESS_FILE);
int fd3 = open(file3, O_WRONLY | O_TRUNC | O_CREAT, ACCESS_FILE);
if (fd1 == -1 || fd2 == -1 || fd3 == -1) {
_exit(EXIT_ERROR);
}
if (dup2(fd1, 0) == -1 || dup2(fd2, 1) == -1 || dup2(fd3, 2) == -1) {
_exit(EXIT_ERROR);
}
execlp(cmd, cmd, NULL);
_exit(EXIT_ERROR);
} else {
int status;
wait(&status);
printf("%d\n", status);
}
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int
run_command(char *cmd)
{
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
} else if (pid == 0) {
execlp(cmd, cmd, (char *) NULL);
perror("execlp");
exit(1);
} else {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
return 1;
} else {
return 0;
}
}
}
int
main(int argc, char *argv[])
{
int cmd1_success = run_command(argv[1]);
int cmd2_success = 0;
if (!cmd1_success) {
cmd2_success = run_command(argv[2]);
}
if (cmd1_success || cmd2_success) {
if (run_command(argv[3])) {
exit(0);
} else {
exit(1);
}
}
exit(1);
}
+36
View File
@@ -0,0 +1,36 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int
run(const char *cmd)
{
pid_t pid = fork();
if (pid == -1) {
exit(1);
} else if (pid == 0) {
execlp(cmd, cmd, NULL);
_exit(1);
}
int status;
waitpid(pid, &status, 0);
return WIFEXITED(status) && !WEXITSTATUS(status);
} // 1 - при успехе
int
main(int argc, char **argv)
{
if (argc != 4) {
return 1;
}
char *cmd1 = argv[1];
char *cmd2 = argv[2];
char *cmd3 = argv[3];
return !((run(cmd1) || run(cmd2)) && run(cmd3));
}
+59
View File
@@ -0,0 +1,59 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int
check(void)
{
int succes = 0;
int status;
while (wait(&status) != -1) {
if (WIFEXITED(status) && !WEXITSTATUS(status)) {
succes++;
}
}
return succes;
}
void
run(char *cmd)
{
pid_t pid = fork();
if (pid == -1) {
exit(1);
} else if (pid == 0) {
execlp(cmd, cmd, NULL);
_exit(1);
}
return;
} // 1 - при успехе
int
main(int argc, char **argv)
{
int succes = 0;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == 's') {
succes += check();
run(argv[i] + 1);
succes += check();
} else if (argv[i][0] == 'p') {
run(argv[i] + 1);
} else {
fprintf(stderr, "Error in arguments\n");
exit(1);
}
}
succes += check();
printf("%d\n", succes);
return 0;
}
+89
View File
@@ -0,0 +1,89 @@
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <linux/limits.h>
#include <time.h>
int
main(int argc, char **argv)
{
if (argc < 2) {
return 1;
}
char script_template[] = "#!/usr/bin/env python3\n"
"import os\n"
"print(";
char script_end[] = ")\n"
"os.remove(__file__)\n";
size_t script_size = strlen(script_template) + strlen(script_end) + 1;
for (int i = 1; i < argc; i++) {
script_size += strlen(argv[i]) + 1;
}
char *script_content;
if (!(script_content = malloc(script_size))) {
return 1;
}
strcpy(script_content, script_template);
for (int i = 1; i < argc; i++) {
strcat(script_content, argv[i]);
if (i < argc - 1) {
strcat(script_content, "*");
}
}
strcat(script_content, script_end);
char *tmpdir = getenv("XDG_RUNTIME_DIR");
if (!tmpdir) {
tmpdir = getenv("TMPDIR");
if (!tmpdir) {
tmpdir = "/tmp";
}
}
char script_path[PATH_MAX];
int r = snprintf(script_path, sizeof(script_path), "%s/%d", tmpdir, (int) (getpid() * time(NULL) % 1000000));
if (r < 0 || r > PATH_MAX) {
exit(1);
}
int fd = open(script_path, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd == -1) {
free(script_content);
return 1;
}
FILE *script_file = fdopen(fd, "w");
if (!script_file) {
close(fd);
unlink(script_path);
free(script_content);
return 1;
}
fprintf(script_file, "%s", script_content);
fclose(script_file);
free(script_content);
if (chmod(script_path, 0700) == -1) {
unlink(script_path);
return 1;
}
execl(script_path, script_path, (char *) NULL);
unlink(script_path);
return 1;
}