Use one compat file

This commit is contained in:
Vasilii Rogin
2025-05-19 18:26:50 +03:00
parent 34b1a2a67f
commit b3edef7a9d
14 changed files with 234 additions and 73 deletions
+138
View File
@@ -0,0 +1,138 @@
#include "compat.h"
int strcpy_s(char* dest, size_t destsz, const char* src) {
if (!dest || !src || destsz == 0) return EINVAL;
size_t len = strlen(src);
if (len >= destsz) {
dest[0] = '\0';
return ERANGE;
}
strcpy(dest, src);
return 0;
}
int strcat_s(char *dest, size_t destsz, const char *src) {
if (!dest || !src || destsz == 0) return EINVAL;
size_t dest_len = strlen(dest);
size_t src_len = strlen(src);
if (dest_len + src_len + 1 > destsz) {
dest[0] = '\0';
return ERANGE;
}
strcat(dest, src);
return 0;
}
int rand_s(unsigned int *randomValue) {
if (!randomValue) return EINVAL;
*randomValue = (unsigned int)rand();
return 0;
}
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...) {
if (!buffer || sizeOfBuffer == 0 || !format) return EINVAL;
va_list args;
va_start(args, format);
int written = vsnprintf(buffer, sizeOfBuffer, format, args);
va_end(args);
if (written < 0 || (size_t)written >= sizeOfBuffer) {
buffer[0] = '\0'; // mimic Windows behavior
return ERANGE;
}
return 0;
}
intptr_t _findfirst(const char *pattern, struct _finddata_t *data) {
_find_handle_t *handle = calloc(1, sizeof(_find_handle_t));
if (!handle) return -1;
// Split pattern into directory path and wildcard
const char *slash = strrchr(pattern, '/');
if (slash) {
size_t len = (size_t)(slash - pattern + 1);
if (len >= sizeof(handle->path)) len = sizeof(handle->path) - 1;
strncpy(handle->path, pattern, len);
handle->path[len] = '\0';
strncpy(handle->pattern, slash + 1, sizeof(handle->pattern) - 1);
} else {
strncpy(handle->path, "./", sizeof(handle->path) - 1);
strncpy(handle->pattern, pattern, sizeof(handle->pattern) - 1);
}
handle->dir = opendir(handle->path);
if (!handle->dir) {
free(handle);
return -1;
}
struct dirent *entry;
while ((entry = readdir(handle->dir)) != NULL) {
if (fnmatch(handle->pattern, entry->d_name, 0) == 0) {
struct stat st;
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s%s", handle->path, entry->d_name);
if (stat(fullpath, &st) == 0) {
// Make the name absolute
char abspath[512];
if (realpath(fullpath, abspath)) {
strncpy(data->name, abspath, sizeof(data->name) - 1);
data->name[sizeof(data->name) - 1] = '\0';
} else {
// Fallback to relative path
strncpy(data->name, fullpath, sizeof(data->name) - 1);
data->name[sizeof(data->name) - 1] = '\0';
}
data->time_write = st.st_mtime;
data->size = st.st_size;
data->attrib = S_ISDIR(st.st_mode) ? _A_SUBDIR : _A_NORMAL;
return (intptr_t)handle;
}
}
}
closedir(handle->dir);
free(handle);
return -1;
}
int _findnext(intptr_t h, struct _finddata_t *data) {
_find_handle_t *handle = (_find_handle_t *)h;
struct dirent *entry;
while ((entry = readdir(handle->dir)) != NULL) {
if (fnmatch(handle->pattern, entry->d_name, 0) == 0) {
struct stat st;
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s%s", handle->path, entry->d_name);
if (stat(fullpath, &st) == 0) {
strncpy(data->name, entry->d_name, sizeof(data->name));
data->time_write = st.st_mtime;
data->size = st.st_size;
data->attrib = S_ISDIR(st.st_mode) ? _A_SUBDIR : _A_NORMAL;
return 0;
}
}
}
return -1;
}
int _findclose(intptr_t h) {
_find_handle_t *handle = (_find_handle_t *)h;
if (handle) {
if (handle->dir) closedir(handle->dir);
free(handle);
return 0;
}
return -1;
}
+71
View File
@@ -0,0 +1,71 @@
#ifndef _WIN32
#ifndef _COMPAT_H_DEFINED
#define _COMPAT_H_DEFINED
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#define _A_NORMAL 0x00
#define _A_SUBDIR 0x10
#define _MSC_VER;
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
int rand_s(unsigned int *randomValue);
int strcpy_s(char* dest, size_t destsz, const char* src);
int strcat_s(char *dest, size_t destsz, const char *src);
#define _stricmp strcasecmp
#define GetCurrentProcessId() ((unsigned int)getpid())
#define _mkdir(path) mkdir((path), 0777)
#define _chdir chdir
#define _getcwd getcwd
#define _stat stat
typedef __time_t time_t;
typedef u_int32_t _fsize_t;
struct _finddata_t {
unsigned attrib;
time_t time_write;
size_t size;
char name[260]; // or PATH_MAX
};
typedef struct {
DIR *dir;
char pattern[260];
char path[260];
} _find_handle_t;
intptr_t _findfirst(const char *pattern, struct _finddata_t *data);
int _findnext(intptr_t h, struct _finddata_t *data);
int _findclose(intptr_t h);
#endif
#endif
-21
View File
@@ -1,21 +0,0 @@
#ifdef _WIN32
#include <io.h>
#else
#ifndef _WINDOWS_H_DEFINED
#define _WINDOWS_H_DEFINED
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
#endif
#endif
-11
View File
@@ -1,11 +0,0 @@
#ifdef _WIN32
#else
#ifndef _COMPAT_STDLIB_H_DEFINED
#define _COMPAT_STDLIB_H_DEFINED
int rand_s(unsigned int *randomValue);
#endif
#endif
-14
View File
@@ -1,14 +0,0 @@
#ifdef _WIN32
#else
#ifndef _COMPAT_STRING_H_DEFINED
#define _COMPAT_STRING_H_DEFINED
int strcpy_s(char* dest, size_t destsz, const char* src);
int strcat_s(char *dest, size_t destsz, const char *src);
#define _stricmp strcasecmp
#endif
#endif
-12
View File
@@ -1,12 +0,0 @@
#ifdef _WIN32
#include <windows.h>
#else
#define GetCurrentProcessId() ((unsigned int)getpid())
#endif
+6 -1
View File
@@ -1,11 +1,16 @@
#define WIN32_LEAN_AND_MEAN
#define _CRT_RAND_S
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "lex.h"
#include "parse.h"
#ifdef _WIN32
#include <Windows.h>
#include <io.h>
#else
#include "compat.h"
#endif
#include "resource.h"
+2 -2
View File
@@ -35,8 +35,8 @@
* are placed here.
*/
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
static int do_if( int hash, const char * directive_name);
/* #if, #elif, #ifdef, #ifndef */
+2 -2
View File
@@ -35,8 +35,8 @@
* Some routines are used also to evaluate the value of numerical tokens.
*/
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
typedef struct optab {
char op; /* Operator */
+2 -2
View File
@@ -37,8 +37,8 @@
#if PREPROCESSED
#include "mcpp.H"
#else
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
#endif
#define ARG_ERROR (-255)
+2 -2
View File
@@ -35,8 +35,8 @@
* The post-preprocessing routines are also placed here.
*/
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
/* Function pointer to expand_macro() functions. */
char * (*expand_macro)( DEFBUF * defp, char * out, char * out_end
+2 -2
View File
@@ -70,8 +70,8 @@
* buffer.
*/
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
static void scan_id( int c);
/* Scan an identifier */
+8 -3
View File
@@ -36,10 +36,15 @@
* 1. specify the constants in "configed.H" or "noconfig.H",
* 2. append the system-dependent routines in this file.
*/
#include "system.H"
#include "internal.H"
#include "system.h"
#include "internal.h"
#ifdef _WIN32
#include <direct.h>
#else
#include "compat.h"
#endif
#include "direct.h"
#define getcwd( buf, size) _getcwd( buf, size)
#include "sys/types.h"
#include "sys/stat.h" /* For stat() */
+1 -1
View File
@@ -41,7 +41,7 @@
#define SYSTEM_H
#include "noconfig.H"
#include "noconfig.h"
/*
* PART 1 and PART 2 are defined in the above header files.