Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added execution of directory #32

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/cjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extern unsigned int lib_tinycc_include_varargs_h_len;
// from file.c
extern long file_size(const char *filename);
extern char* file_load(const char *filename);
extern char* dir_load(const char *path);
extern bool write_to_file(char *path, char *filename, char *buf, unsigned int len);
extern bool rm_recursive(char *path);
#ifdef LIBC_MINGW32
Expand Down Expand Up @@ -859,6 +860,7 @@ int main(int argc, char **argv) {
static bool version = false;
char tmptemplate[] = "/tmp/CJIT-exec.XXXXXX";
char *tmpdir = NULL;
char *code = NULL;
int res = 1;

static const struct cflag options[] = {
Expand Down Expand Up @@ -924,7 +926,26 @@ int main(int argc, char **argv) {
goto endgame;
}
_err("Source to execute: %s",code_path);
char *code = file_load(code_path);


#ifndef LIBC_MINGW32 // POSIX only
struct stat st;
if (stat(code_path, &st) == -1) {
_err("File not found: %s",code_path);
goto endgame;
}
if (S_ISDIR(st.st_mode)) {
_err("%s: it is a directory path. Recursively adding all sources.", code_path);
tcc_add_include_path(TCC, code_path);
code = dir_load(code_path);
} else {
code = file_load(code_path);
}
#else
/* FIXME: Add Windows support for directory */
code = file_load(code_path);
#endif

char *err_msg = NULL;
if(!code) {
_err("File not found: %s",code_path);
Expand Down
78 changes: 77 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
#include <rpc.h>
#pragma comment(lib, "rpcrt4.lib")
#pragma comment(lib, "shlwapi.lib")
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#endif

extern void _err(const char *fmt, ...);

// Function to get the length of a file in bytes
Expand Down Expand Up @@ -68,6 +71,8 @@
return NULL;
}

_err("Loading source file %s",filename);

fread(contents, 1, length, file);
contents[length] = '\0'; // Null-terminate the string
fclose(file);
Expand Down Expand Up @@ -115,6 +120,77 @@
return true;
}

#ifndef LIBC_MINGW32

static char *full_content = NULL;


static int file_load_ftw(const char *pathname,
const struct stat *sbuf,
int type, struct FTW *ftwb) {
FILE *fd;
char *content = NULL;
if (type == FTW_F) {
size_t pathlen = strlen(pathname);
if (pathname[pathlen-1] == 'c' &&
pathname[pathlen-2] == '.') {
content = file_load(pathname);
if (content == NULL) {
_err("Error: file_load %s",pathname);
return -1;
}
if (full_content == NULL) {
full_content = content;
} else {
full_content = realloc(full_content, strlen(full_content) + strlen(content) + 1);
if (full_content == NULL) {
_err("Error: realloc full_content");
return -1;
}
strcat(full_content, content);

Check warning on line 150 in src/file.c

View workflow job for this annotation

GitHub Actions / 🚨 C lint

[cpplint] reported by reviewdog 🐶 Almost always, snprintf is better than strcat [runtime/printf] [4] Raw Output: src/file.c:150: Almost always, snprintf is better than strcat [runtime/printf] [4]
}
}
}
return 0;
}

/* dir_load: nftw version */
char *dir_load(const char *path)
{
struct stat sb;
FILE *fd;
char *content = NULL;

if (stat(path, &sb) != 0) {
_err("Error: %s",path);
_err("%s",strerror(errno));
return NULL;
}
if (!S_ISDIR(sb.st_mode)) {
_err("Error: %s is not a directory",path);
return NULL;
}
if (nftw(path, file_load_ftw, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) < 0) {
_err("Error: nftw path %s",path);
_err("%s",strerror(errno));
return NULL;
}
return full_content;
}


#else
char *dir_load(const char *path)
{
/* FIXME */
_err("Error: dir_load not implemented on Windows");
return NULL;
}


#endif


#ifdef LIBC_MINGW32
///////////////
// WINDOWS SHIT
Expand Down
7 changes: 7 additions & 0 deletions test/multifile/a/b/c/myfunc3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

int myfunc3(void)
{
printf("hello from myfunc3\n");
return 0;
}
13 changes: 13 additions & 0 deletions test/multifile/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include "myfunc.h"



int main(void)
{
myfunc();
myfunc2();
myfunc3();
return 0;

}
5 changes: 5 additions & 0 deletions test/multifile/myfunc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int myfunc(void)
{
printf("hello from myfunc\n");
return 0;
}
3 changes: 3 additions & 0 deletions test/multifile/myfunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int myfunc(void);
int myfunc2(void);
int myfunc3(void);
6 changes: 6 additions & 0 deletions test/multifile/myfunc2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>
int myfunc2(void)
{
printf("hello from myfunc2\n");
return 0;
}