Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/osx/x86/src/test/test_component.c
Views: 11784
/*1* test_component: Read in a component and execute it2*/34#include <stdio.h>5#include <stdlib.h>6#include <fcntl.h>7#include <unistd.h>8#include <pthread.h>910int read_and_exec_file(char* file)11{12char* buf = malloc(10000);13int f, n;1415if ((f = open(file, O_RDONLY, 0)) < 0) {16perror("open");17exit(EXIT_FAILURE);18}1920if ((n = read(f, buf, 100000)) < 0) {21perror("read");22exit(EXIT_FAILURE);23}2425//printf("==> Read %d bytes, executing component...\n", n);2627((void(*)(void))buf)();2829printf("==> Done.\n");3031return 0;32}3334int create_read_and_exec_thread(char* file)35{36int err;37pthread_t pthread;38void* return_value;3940if ((err = pthread_create(&pthread, NULL,41read_and_exec_file, (void*)file)) != 0) {42fprintf(stderr, "pthread_create: %s\n", strerror(err));43return -1;44}4546if ((err = pthread_join(pthread, &return_value)) != 0) {47fprintf(stderr, "pthread_join: %s\n", strerror(err));48return -1;49}5051return 0;52}5354int main(int argc, char* argv[])55{56int c;57int threaded = 0;5859while ((c = getopt(argc, argv, "tp:")) != EOF) {60switch (c) {61case 't':62threaded = 1;63break;64default:65fprintf(stderr, "usage: %s [ -t ] payload_bin\n", argv[0]);66exit(EXIT_FAILURE);67}68}697071if (threaded)72create_read_and_exec_thread(argv[optind]);73else74read_and_exec_file(argv[optind]);75}767778