woody-woodpacker/srcs/main.c

47 lines
1008 B
C
Raw Normal View History

2024-02-14 10:37:05 +00:00
#include "../includes/woody.h"
int get_elf_file(t_elf_content *woody)
2024-02-14 08:58:04 +00:00
{
2024-02-14 10:37:05 +00:00
int fd;
off_t off;
2024-02-19 10:35:40 +00:00
fd = open(woody->file_path, O_RDONLY);
2024-02-14 10:37:05 +00:00
if (fd < 0)
{
2024-02-19 10:35:40 +00:00
ft_printf("Error: Failed to open \'%s\'\n", woody->file_path);
2024-02-14 10:37:05 +00:00
return EXIT_FAILURE;
}
off = lseek(fd, 0, SEEK_END);
if (off == -1)
{
close(fd);
2024-02-19 10:35:40 +00:00
ft_printf("Error: Failed to read file offset \'%s\'\n", woody->file_path);
2024-02-14 10:37:05 +00:00
return EXIT_FAILURE;
}
2024-02-19 10:35:40 +00:00
woody->file_size = off;
2024-03-21 14:44:29 +00:00
woody->file = mmap(NULL, woody->file_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
2024-04-11 10:20:44 +00:00
if (woody->file == MAP_FAILED)
2024-02-14 10:37:05 +00:00
{
close(fd);
2024-02-19 10:35:40 +00:00
ft_printf("Error: Failed to map file \'%s\'\n", woody->file_path);
2024-02-14 10:37:05 +00:00
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}
2024-02-14 08:58:04 +00:00
2024-02-14 10:37:05 +00:00
int main(int ac, char **av)
{
t_elf_content woody;
2024-02-14 10:37:05 +00:00
if (ac != 2)
{
return ft_put_error("Woody_woodpacker take 1 argument\n");
}
2024-02-19 10:35:40 +00:00
woody.file_path = av[1];
int ret = get_elf_file(&woody);
2024-02-14 10:37:05 +00:00
if (ret == EXIT_FAILURE)
return ret;
2024-02-21 12:54:33 +00:00
return prepare_injection(&woody);
2024-04-15 04:17:31 +00:00
}