Skip to content

Commit ae7b8ab

Browse files
authored
Provide Windows replacement implementation for realpath(3) (#93)
* Provide actual replacement implementation for realpath(3) Define HAVE_REALPATH where realpath(3) exists. * Fix fgetln(3) for consistency
1 parent 499312a commit ae7b8ab

File tree

9 files changed

+57
-25
lines changed

9 files changed

+57
-25
lines changed

CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,20 @@ if (NOT CMAKE_BUILD_TYPE)
337337
endif (NOT CMAKE_BUILD_TYPE)
338338

339339

340-
# Check for missing functions: fgetln(3) is in 4.4BSD; regcomp(3) is
341-
# in POSIX.1-2001, POSIX.1-2008.
340+
# Check for missing functions: fgetln(3) is in 4.4BSD; realpath(3) is
341+
# in 4.4BSD, POSIX.1-2001; regcomp(3) is in POSIX.1-2001,
342+
# POSIX.1-2008.
342343
include(CheckSymbolExists)
343344
check_symbol_exists(fgetln "stdio.h" HAVE_FGETLN)
344345
if(HAVE_FGETLN)
345346
add_compile_definitions("HAVE_FGETLN")
346347
endif()
347348

349+
check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH)
350+
if(HAVE_REALPATH)
351+
add_compile_definitions("HAVE_REALPATH")
352+
endif()
353+
348354
check_symbol_exists(regcomp "regex.h" HAVE_REGEX)
349355
if(HAVE_REGEX)
350356
add_compile_definitions("HAVE_REGEX")
@@ -819,6 +825,10 @@ if(CBF_ENABLE_ULP)
819825
target_sources(cbf
820826
PRIVATE "${CBF__SRC}/cbf_ulp.c")
821827
endif()
828+
if(NOT HAVE_REALPATH)
829+
target_sources(cbf
830+
PRIVATE "${CBF__SRC}/realpath.c")
831+
endif()
822832

823833
set_target_properties(cbf PROPERTIES OUTPUT_NAME "cbf")
824834
set_target_properties(cbf PROPERTIES LINKER_LANGUAGE C)
@@ -1100,7 +1110,8 @@ target_link_libraries(cbf_standardize_numbers
11001110
cbf
11011111
"${libm}")
11021112
if(NOT HAVE_FGETLN)
1103-
target_sources(cbf_standardize_numbers PRIVATE "${CBF__SRC}/fgetln.c")
1113+
target_sources(cbf_standardize_numbers
1114+
PRIVATE "${CBF__SRC}/fgetln.c")
11041115
endif()
11051116

11061117
add_executable(cbf_tail

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ endif
736736
CC = gcc
737737
C++ = g++
738738
ifneq ($(CBFDEBUG),)
739-
CFLAGS = -g -O0 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing -DCBFDEBUG=1 $(HDF5CFLAGS)
739+
CFLAGS = -g -O0 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing -DCBFDEBUG=1 $(HDF5CFLAGS)
740740
else
741-
CFLAGS = -g -O3 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
741+
CFLAGS = -g -O3 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
742742
endif
743743
LDFLAGS =
744744
F90C = gfortran

Makefile_LINUX

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ endif
735735
#########################################################
736736
CC = gcc
737737
C++ = g++
738-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
738+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
739739
LDFLAGS =
740740
F90C = gfortran
741741
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz

Makefile_OSX

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ endif
736736
#########################################################
737737
CC = gcc
738738
C++ = g++
739-
CFLAGS = -g -O2 -Wall -std=c99 -pedantic $(HDF5CFLAGS)
739+
CFLAGS = -g -O2 -Wall -std=c99 -pedantic -DHAVE_REALPATH $(HDF5CFLAGS)
740740
LDFLAGS =
741741
F90C = gfortran
742742
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz

examples/cbf_standardize_numbers.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#include <string.h>
1616

1717

18-
char * fgetln(FILE *, size_t *);
18+
#ifndef HAVE_FGETLN
19+
# define fgetln _cbf_fgetln
20+
char *
21+
_cbf_fgetln(FILE *stream, size_t *len);
22+
#endif
1923

2024
void usage ( void ) {
2125

m4/Makefile.m4

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ endif
743743
#########################################################
744744
CC = gcc
745745
C++ = g++
746-
CFLAGS = -g -O2 -Wall -std=c99 -pedantic $(HDF5CFLAGS)
746+
CFLAGS = -g -O2 -Wall -std=c99 -pedantic -DHAVE_REALPATH $(HDF5CFLAGS)
747747
LDFLAGS =
748748
F90C = gfortran
749749
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -769,7 +769,7 @@ cbf_system,`OSX_gcc42',`
769769
#########################################################
770770
CC = gcc
771771
C++ = g++
772-
CFLAGS = -g -O2 -Wall -std=c99 -pedantic $(HDF5CFLAGS)
772+
CFLAGS = -g -O2 -Wall -std=c99 -pedantic -DHAVE_REALPATH $(HDF5CFLAGS)
773773
LDFLAGS =
774774
F90C = gfortran
775775
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -795,7 +795,7 @@ cbf_system,`OSX_gcc42_DMALLOC',`
795795
#########################################################
796796
CC = gcc
797797
C++ = g++
798-
CFLAGS = -g -O2 -Wall -std=c99 -pedantic -DDMALLOC -DDMALLOC_FUNC_CHECK -I$(HOME)/include $(HDF5CFLAGS)
798+
CFLAGS = -g -O2 -Wall -std=c99 -pedantic -DDMALLOC -DDMALLOC_FUNC_CHECK -DHAVE_REALPATH -I$(HOME)/include $(HDF5CFLAGS)
799799
LDFLAGS =
800800
F90C = gfortran
801801
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -822,7 +822,7 @@ cbf_system,`LINUX_64',`
822822
#########################################################
823823
CC = gcc -m64
824824
C++ = g++ -m64
825-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
825+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
826826
LDFLAGS =
827827
F90C = gfortran -m64
828828
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -850,7 +850,7 @@ cbf_system,`LINUX_gcc42',`
850850
#########################################################
851851
CC = gcc
852852
C++ = g++
853-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
853+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
854854
LDFLAGS =
855855
F90C = gfortran
856856
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -877,7 +877,7 @@ cbf_system,`LINUX',`
877877
#########################################################
878878
CC = gcc
879879
C++ = g++
880-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
880+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
881881
LDFLAGS =
882882
F90C = gfortran
883883
#F90FLAGS = -g -fno-range-check -fallow-invalid-boz
@@ -905,7 +905,7 @@ cbf_system,`LINUX_gcc42_DMALLOC', `
905905
#########################################################
906906
CC = gcc
907907
C++ = g++
908-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing \
908+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing \
909909
-DDMALLOC -DDMALLOC_FUNC_CHECK $(HDF5CFLAGS) -I$(HOME)/include
910910
LDFLAGS =
911911
F90C = gfortran
@@ -932,7 +932,7 @@ cbf_system,`LINUX_DMALLOC',`
932932
#########################################################
933933
CC = gcc
934934
C++ = g++
935-
CFLAGS = -g -O2 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing \
935+
CFLAGS = -g -O2 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing \
936936
-DDMALLOC -DDMALLOC_FUNC_CHECK $(HDF5CFLAGS) -I$(HOME)/include
937937
LDFLAGS =
938938
F90C = gfortran
@@ -1105,9 +1105,9 @@ RANLIB = ',
11051105
CC = gcc
11061106
C++ = g++
11071107
ifneq ($(CBFDEBUG),)
1108-
CFLAGS = -g -O0 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing -DCBFDEBUG=1 $(HDF5CFLAGS)
1108+
CFLAGS = -g -O0 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing -DCBFDEBUG=1 $(HDF5CFLAGS)
11091109
else
1110-
CFLAGS = -g -O3 -Wall -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
1110+
CFLAGS = -g -O3 -Wall -DHAVE_REALPATH -D_USE_XOPEN_EXTENDED -fno-strict-aliasing $(HDF5CFLAGS)
11111111
endif
11121112
LDFLAGS =
11131113
F90C = gfortran

src/cbf_hdf5.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ extern "C" {
283283
#include <assert.h>
284284
#include <errno.h>
285285

286+
#ifndef HAVE_REALPATH
287+
#define realpath _cbf_realpath
288+
char *
289+
_cbf_realpath(const char *path, char *resolved_path);
290+
#endif
291+
286292
static int cbf_find_array_data_h5type
287293
(hid_t * const type,
288294
unsigned int bits,

src/fgetln.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
#include <stdlib.h>
3232
#include <string.h>
3333

34-
#ifndef HAVE_fgetln
3534
char *
36-
fgetln(FILE *stream, size_t *len)
35+
_cbf_fgetln(FILE *stream, size_t *len)
3736
{
3837
static char *buf = NULL;
3938
static size_t buflen = 0;
@@ -63,4 +62,3 @@ fgetln(FILE *stream, size_t *len)
6362
*len = strlen(buf);
6463
return buf;
6564
}
66-
#endif /* !defined(HAVE_fgetln) */

src/realpath.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
#include <stdlib.h>
2-
char *realpath(const char *path, char *resolved_path)
1+
#ifdef _WIN32
2+
# include <windows.h>
3+
#endif
4+
5+
char *
6+
_cbf_realpath(const char *path, char *resolved_path)
37
{
4-
return 0;
5-
}
8+
#ifdef _WIN32
9+
if (path == NULL) {
10+
errno = EINVAL;
11+
return (NULL);
12+
}
613

14+
return (_fullpath(resolved_path, path, MAX_PATH));
15+
#else
16+
errno = ENOSYS;
17+
return (NULL);
18+
#endif
19+
}

0 commit comments

Comments
 (0)