Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <errno.h>
- #ifndef PATH_MAX
- #define PATH_MAX (4096)
- #endif
- const char pref_string[] = \
- "#!/usr/bin/env python3\n" \
- "import os\n" \
- "print(1";
- int
- main(int argc, char **argv)
- {
- if (argc < 2) {
- return EXIT_SUCCESS;
- }
- const char *dir = getenv("XDG_RUNTIME_DIR");
- if (!dir) {
- dir = getenv("TMPDIR");
- }
- if (!dir) {
- dir = "/tmp/";
- }
- char file_path[PATH_MAX];
- int file_num = 0, fd = -1;
- do {
- unsigned expected = snprintf(file_path, sizeof(file_path), "%s/tmp_%d.py", dir, file_num++);
- if (expected > sizeof(file_path)) {
- return EXIT_FAILURE;
- }
- errno = 0;
- fd = open(file_path, O_CREAT | O_WRONLY | O_EXCL, 0700);
- } while(errno == EEXIST);
- if (errno != 0) {
- return EXIT_FAILURE;
- }
- FILE *file = fdopen(fd, "w");
- fprintf(file, pref_string);
- for (int i = 1; i < argc; ++i) {
- fprintf(file, "*%s\n", argv[i]);
- }
- fprintf(file, ")\nos.remove(\"%s\")", file_path);
- fclose(file);
- close(fd);
- execlp(file_path, file_path, NULL);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement