Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 620b30d

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Move win32/mingw32 realpath to simulation runtime
realpath is used for URI lookup during runtime. Belonging to [master]: - #2116 - OpenModelica/OpenModelica-testsuite#829
1 parent bd035a4 commit 620b30d

File tree

3 files changed

+197
-200
lines changed

3 files changed

+197
-200
lines changed

Compiler/runtime/systemimpl.c

Lines changed: 0 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ typedef void* iconv_t;
106106
#define S_IFLNK 0120000 /* symbolic link */
107107

108108
#include <sys/types.h>
109-
#if defined(_MSC_VER)
110-
#include <win32_dirent.h>
111-
#if !defined(PATH_MAX)
112-
#define PATH_MAX MAX_PATH
113-
#endif
114-
#else
115-
#include <dirent.h>
116-
#endif
117-
118-
char *realpath(const char *path, char resolved_path[PATH_MAX]);
119109

120110
#else
121111
/* includes/defines specific for LINUX/OS X */
@@ -2452,184 +2442,6 @@ const char* SystemImpl__gettext(const char *msgid)
24522442
}
24532443

24542444

2455-
#if defined(__MINGW32__) || defined(_MSC_VER)
2456-
2457-
#ifdef __MINGW32__
2458-
2459-
char *realpath(const char *path, char resolved_path[PATH_MAX])
2460-
{
2461-
if (!_fullpath(resolved_path, path, PATH_MAX))
2462-
{
2463-
const char *c_tokens[0]={};
2464-
const char* fmt = "System.realpath failed on %s with errno: %d";
2465-
char* msg = (char*)omc_alloc_interface.malloc_atomic(strlen(path) + strlen(fmt) + 10);
2466-
sprintf(msg, fmt, path, errno);
2467-
c_add_message(NULL,6000,
2468-
ErrorType_scripting,
2469-
ErrorLevel_warning,
2470-
msg,
2471-
c_tokens,
2472-
0);
2473-
resolved_path = (char*)path;
2474-
}
2475-
return resolved_path;
2476-
}
2477-
2478-
#else
2479-
2480-
/*
2481-
realpath() Win32 implementation, supports non standard glibc extension
2482-
This file has no copyright assigned and is placed in the Public Domain.
2483-
Written by Nach M. S. September 8, 2005
2484-
*/
2485-
2486-
#include <windows.h>
2487-
#include <stdlib.h>
2488-
#include <limits.h>
2489-
#include <errno.h>
2490-
#include <sys/stat.h>
2491-
2492-
char *realpath(const char *path, char resolved_path[PATH_MAX])
2493-
{
2494-
char *return_path = 0;
2495-
2496-
if (path) //Else EINVAL
2497-
{
2498-
if (resolved_path)
2499-
{
2500-
return_path = resolved_path;
2501-
}
2502-
else
2503-
{
2504-
//Non standard extension that glibc uses
2505-
return_path = (char*)malloc(PATH_MAX);
2506-
}
2507-
2508-
if (return_path) //Else EINVAL
2509-
{
2510-
//This is a Win32 API function similar to what realpath() is supposed to do
2511-
size_t size = GetFullPathNameA(path, PATH_MAX, return_path, 0);
2512-
2513-
//GetFullPathNameA() returns a size larger than buffer if buffer is too small
2514-
if (size > PATH_MAX)
2515-
{
2516-
if (return_path != resolved_path) //Malloc'd buffer - Unstandard extension retry
2517-
{
2518-
size_t new_size;
2519-
2520-
free(return_path);
2521-
return_path = (char*)malloc(size);
2522-
2523-
if (return_path)
2524-
{
2525-
new_size = GetFullPathNameA(path, size, return_path, 0); //Try again
2526-
2527-
if (new_size > size) //If it's still too large, we have a problem, don't try again
2528-
{
2529-
free(return_path);
2530-
return_path = 0;
2531-
errno = ENAMETOOLONG;
2532-
}
2533-
else
2534-
{
2535-
size = new_size;
2536-
}
2537-
}
2538-
else
2539-
{
2540-
//I wasn't sure what to return here, but the standard does say to return EINVAL
2541-
//if resolved_path is null, and in this case we couldn't malloc large enough buffer
2542-
errno = EINVAL;
2543-
}
2544-
}
2545-
else //resolved_path buffer isn't big enough
2546-
{
2547-
return_path = 0;
2548-
errno = ENAMETOOLONG;
2549-
}
2550-
}
2551-
2552-
//GetFullPathNameA() returns 0 if some path resolve problem occured
2553-
if (!size)
2554-
{
2555-
if (return_path != resolved_path) //Malloc'd buffer
2556-
{
2557-
free(return_path);
2558-
}
2559-
2560-
return_path = 0;
2561-
2562-
//Convert MS errors into standard errors
2563-
switch (GetLastError())
2564-
{
2565-
case ERROR_FILE_NOT_FOUND:
2566-
errno = ENOENT;
2567-
break;
2568-
2569-
case ERROR_PATH_NOT_FOUND: case ERROR_INVALID_DRIVE:
2570-
errno = ENOTDIR;
2571-
break;
2572-
2573-
case ERROR_ACCESS_DENIED:
2574-
errno = EACCES;
2575-
break;
2576-
2577-
default: //Unknown Error
2578-
errno = EIO;
2579-
break;
2580-
}
2581-
}
2582-
2583-
//If we get to here with a valid return_path, we're still doing good
2584-
if (return_path)
2585-
{
2586-
struct stat stat_buffer;
2587-
2588-
//Make sure path exists, stat() returns 0 on success
2589-
if (stat(return_path, &stat_buffer))
2590-
{
2591-
if (return_path != resolved_path)
2592-
{
2593-
free(return_path);
2594-
}
2595-
2596-
return_path = 0;
2597-
//stat() will set the correct errno for us
2598-
}
2599-
//else we succeeded!
2600-
}
2601-
}
2602-
else
2603-
{
2604-
errno = EINVAL;
2605-
}
2606-
}
2607-
else
2608-
{
2609-
errno = EINVAL;
2610-
}
2611-
2612-
if (return_path == NULL)
2613-
{
2614-
const char* fmt = "System.realpath failed on %s with errno: %d";
2615-
char* msg = (char*)malloc(strlen(path) + strlen(fmt) + 10);
2616-
sprintf(msg, fmt, path, errno);
2617-
c_add_message(NULL,6000,
2618-
ErrorType_scripting,
2619-
ErrorLevel_warning,
2620-
msg,
2621-
NULL,
2622-
0);
2623-
resolved_path = (char*)path;
2624-
return_path = (char*)path;
2625-
}
2626-
2627-
return return_path;
2628-
}
2629-
#endif /* mingw */
2630-
2631-
#endif /* mingw and msvc */
2632-
26332445
int System_getTerminalWidth(void)
26342446
{
26352447
#if defined(__MINGW32__) || defined(_MSC_VER)

SimulationRuntime/c/util/omc_msvc.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
#endif
3434

3535
#include "omc_msvc.h"
36+
#include "omc_error.h"
3637
#include <stdarg.h>
3738
#include <stdio.h>
3839
#include <stdlib.h>
@@ -344,6 +345,169 @@ int omc_dladdr(void *addr, Dl_info *info)
344345

345346
#endif
346347

348+
#if defined(__MINGW32__) || defined(_MSC_VER)
349+
350+
#ifdef __MINGW32__
351+
352+
char *realpath(const char *path, char resolved_path[PATH_MAX])
353+
{
354+
if (!_fullpath(resolved_path, path, PATH_MAX))
355+
{
356+
FILE_INFO info = omc_dummyFileInfo;
357+
omc_assert_warning(info, "System.realpath failed on %s with errno: %d", path, errno);
358+
resolved_path = (char*)path;
359+
}
360+
return resolved_path;
361+
}
362+
363+
#else
364+
365+
/*
366+
realpath() Win32 implementation, supports non standard glibc extension
367+
This file has no copyright assigned and is placed in the Public Domain.
368+
Written by Nach M. S. September 8, 2005
369+
*/
370+
371+
#include <windows.h>
372+
#include <stdlib.h>
373+
#include <limits.h>
374+
#include <errno.h>
375+
#include <sys/stat.h>
376+
377+
char *realpath(const char *path, char resolved_path[PATH_MAX])
378+
{
379+
char *return_path = 0;
380+
381+
if (path) //Else EINVAL
382+
{
383+
if (resolved_path)
384+
{
385+
return_path = resolved_path;
386+
}
387+
else
388+
{
389+
//Non standard extension that glibc uses
390+
return_path = (char*)malloc(PATH_MAX);
391+
}
392+
393+
if (return_path) //Else EINVAL
394+
{
395+
//This is a Win32 API function similar to what realpath() is supposed to do
396+
size_t size = GetFullPathNameA(path, PATH_MAX, return_path, 0);
397+
398+
//GetFullPathNameA() returns a size larger than buffer if buffer is too small
399+
if (size > PATH_MAX)
400+
{
401+
if (return_path != resolved_path) //Malloc'd buffer - Unstandard extension retry
402+
{
403+
size_t new_size;
404+
405+
free(return_path);
406+
return_path = (char*)malloc(size);
407+
408+
if (return_path)
409+
{
410+
new_size = GetFullPathNameA(path, size, return_path, 0); //Try again
411+
412+
if (new_size > size) //If it's still too large, we have a problem, don't try again
413+
{
414+
free(return_path);
415+
return_path = 0;
416+
errno = ENAMETOOLONG;
417+
}
418+
else
419+
{
420+
size = new_size;
421+
}
422+
}
423+
else
424+
{
425+
//I wasn't sure what to return here, but the standard does say to return EINVAL
426+
//if resolved_path is null, and in this case we couldn't malloc large enough buffer
427+
errno = EINVAL;
428+
}
429+
}
430+
else //resolved_path buffer isn't big enough
431+
{
432+
return_path = 0;
433+
errno = ENAMETOOLONG;
434+
}
435+
}
436+
437+
//GetFullPathNameA() returns 0 if some path resolve problem occured
438+
if (!size)
439+
{
440+
if (return_path != resolved_path) //Malloc'd buffer
441+
{
442+
free(return_path);
443+
}
444+
445+
return_path = 0;
446+
447+
//Convert MS errors into standard errors
448+
switch (GetLastError())
449+
{
450+
case ERROR_FILE_NOT_FOUND:
451+
errno = ENOENT;
452+
break;
453+
454+
case ERROR_PATH_NOT_FOUND: case ERROR_INVALID_DRIVE:
455+
errno = ENOTDIR;
456+
break;
457+
458+
case ERROR_ACCESS_DENIED:
459+
errno = EACCES;
460+
break;
461+
462+
default: //Unknown Error
463+
errno = EIO;
464+
break;
465+
}
466+
}
467+
468+
//If we get to here with a valid return_path, we're still doing good
469+
if (return_path)
470+
{
471+
struct stat stat_buffer;
472+
473+
//Make sure path exists, stat() returns 0 on success
474+
if (stat(return_path, &stat_buffer))
475+
{
476+
if (return_path != resolved_path)
477+
{
478+
free(return_path);
479+
}
480+
481+
return_path = 0;
482+
//stat() will set the correct errno for us
483+
}
484+
//else we succeeded!
485+
}
486+
}
487+
else
488+
{
489+
errno = EINVAL;
490+
}
491+
}
492+
else
493+
{
494+
errno = EINVAL;
495+
}
496+
497+
if (return_path == NULL)
498+
{
499+
FILE_INFO info = omc_dummyFileInfo;
500+
omc_assert_warning(info, "System.realpath failed on %s with errno: %d", path, errno);
501+
resolved_path = (char*)path;
502+
return_path = (char*)path;
503+
}
504+
505+
return return_path;
506+
}
507+
#endif /* mingw */
508+
509+
#endif /* mingw and msvc */
510+
347511
#ifdef __cplusplus
348512
}
349513
#endif

0 commit comments

Comments
 (0)