Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ clings_debug
compile_commands.json
.cache
tags
.vscode
2 changes: 1 addition & 1 deletion include/files.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef FILES_H
#define FILES_H

#include <dirent.h>
#include "osddirent.h"
#include <stdbool.h>
#include <stdio.h>

Expand Down
10 changes: 10 additions & 0 deletions include/osddirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef OSDDIRENT_H
#define OSDDIRENT_H

#ifdef _WIN32
#include <windirent.h>
#else
#include <dirent.h>
#endif

#endif
17 changes: 17 additions & 0 deletions include/windirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef WINDIRENT_H
#define WINDIRENT_H
#define DIR int

#include <string.h>
#include <stdlib.h>
#include <fileapi.h>
#include <windows.h>
struct dirent {
size_t d_ino;
int d_type;
char *d_name;

};
int alphasort(const struct dirent **a, const struct dirent **b);
int scandir(const char *path, struct dirent ***namelist, void *unused, int (*compar)(const struct dirent **, const struct dirent **));
#endif
4 changes: 2 additions & 2 deletions src/files.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "files.h"
#include <dirent.h>
// #include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -152,7 +152,7 @@ char *get_filename_no_ext(const char *filename) {
/// pointer and returns total exercise files
int load_files(FileCollection **dirs) {
struct dirent **nmlist;
int ex_dirs_ct = scandir(DIR_PATH, &nmlist, NULL, alphasort);
int ex_dirs_ct = scandir(DIR_PATH, &nmlist, NULL, alphasort); // important line

if (ex_dirs_ct < 0) {
perror("could not open directory");
Expand Down
7 changes: 5 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "files.h"
#include "runna.h"
#include "utils.h"
#include "osddirent.h"

#include <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -221,7 +221,9 @@ static void sig_handler(int _) {
(void)_;
keep_running = 0;
}

#ifdef _WIN32
#define count_dir win_count_dir
#else
int count_dir(char *dir) {
struct dirent *dp;
DIR *fd;
Expand All @@ -241,6 +243,7 @@ int count_dir(char *dir) {

return dir_count;
}
#endif

void print_usage() {
printf("Usage:\n\n"
Expand Down
4 changes: 4 additions & 0 deletions src/runna.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ int exec_compile_output(char *file_path, char *file_name_no_ext) {
closedir(dir);
} else if (ENOENT == errno) {
// need to make BIN_PATH
#ifdef _WIN32
mkdir(BIN_PATH);
#else
mkdir(BIN_PATH, 0700);
#endif
} else {
perror("probing BIN_PATH failed");
}
Expand Down
50 changes: 50 additions & 0 deletions src/windirent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "windirent.h"

const int DIR_TYPE_CODE = 4;

int alphasort(const struct dirent **a, const struct dirent **b) {
return strcoll((*a)->d_name,(*b)->d_name);
}

int scandir(
const char *path,
struct dirent ***namelist,
void *unused,
int (*compar)(const struct dirent **, const struct dirent **)
) {
size_t nmsize = sizeof(struct dirent);
int index = 0;
**namelist = malloc(nmsize);
WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(path,&FindFileData);
if (hFind == INVALID_HANDLE_VALUE) return -1;
(*namelist)[0]->d_ino = FindFileData.dwFileAttributes;
(*namelist)[0]->d_name = FindFileData.cFileName;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) (*namelist)[0]->d_type = DIR_TYPE_CODE;
else (*namelist)[0]->d_type = 0;
while(FindNextFile(hFind, &FindFileData)) {
index++;
**namelist = realloc(namelist, nmsize + sizeof(struct dirent));
(*namelist)[index]->d_ino = FindFileData.dwFileAttributes;
(*namelist)[index]->d_name = FindFileData.cFileName;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) (*namelist)[index]->d_type = DIR_TYPE_CODE;
else (*namelist)[index]->d_type = 0;
}
qsort(**namelist, index+1, sizeof(struct dirent), alphasort);
FindClose(hFind);
return 1;
}
int win_count_dir(char *dir)
{
int dir_count;
WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(dir,&FindFileData);
if (hFind == INVALID_HANDLE_VALUE) return -1;
while(FindNextFile(hFind, &FindFileData)) {
if (!strcmp(FindFileData.cFileName, ".") || !strcmp(FindFileData.cFileName, ".."))
continue; /* skip self and parent */
dir_count++;
}
FindClose(hFind);
return dir_count;
}