file.c: add some endif labels where appropriate

"Appropriate" for me being ifdefs which:

1) span enough lines to be hard-to-follow (I need big fonts to see)

2) nested within other ifdefs, or within large functions
   (I have a short attention span)

rb_file_expand_path_internal is the prime example of a large
function I struggle to understand due to the amount of
platform-specific ifdefs.  Hopefully this helps others with poor
vision and/or short attention spans.
This commit is contained in:
Eric Wong 2023-01-30 22:50:37 +00:00 committed by Eric Wong
parent 3a7367ccc3
commit 0285aa87f1

118
file.c
View File

@ -129,7 +129,7 @@ int flock(int, int);
# endif # endif
#else #else
# define STAT(p, s) stat((p), (s)) # define STAT(p, s) stat((p), (s))
#endif #endif /* _WIN32 */
#if defined _WIN32 || defined __APPLE__ #if defined _WIN32 || defined __APPLE__
# define USE_OSPATH 1 # define USE_OSPATH 1
@ -264,7 +264,7 @@ rb_str_encode_ospath(VALUE path)
rb_encoding *utf8 = rb_utf8_encoding(); rb_encoding *utf8 = rb_utf8_encoding();
path = rb_str_conv_enc(path, enc, utf8); path = rb_str_conv_enc(path, enc, utf8);
} }
#endif #endif /* USE_OSPATH */
return path; return path;
} }
@ -308,7 +308,7 @@ rb_CFString_class_initialize_before_fork(void)
CFRelease(m); CFRelease(m);
CFRelease(s); CFRelease(s);
} }
# endif # endif /* HAVE_WORKING_FORK */
static VALUE static VALUE
rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len) rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
@ -409,9 +409,9 @@ ignored_char_p(const char *p, const char *e, rb_encoding *enc)
} }
return 0; return 0;
} }
#else #else /* !__APPLE__ */
# define NORMALIZE_UTF8PATH 0 # define NORMALIZE_UTF8PATH 0
#endif #endif /* __APPLE__ */
#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n) #define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
@ -964,7 +964,7 @@ typedef struct stat statx_data;
# define stat_birthtime stat_ctime # define stat_birthtime stat_ctime
#else #else
# undef HAVE_STAT_BIRTHTIME # undef HAVE_STAT_BIRTHTIME
#endif #endif /* defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) */
/* /*
* call-seq: * call-seq:
@ -1189,8 +1189,8 @@ statx(int dirfd, const char *pathname, int flags,
{ {
return (int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf); return (int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
} }
# endif # endif /* __linux__ */
# endif # endif /* HAVE_STATX */
typedef struct no_gvl_statx_data { typedef struct no_gvl_statx_data {
struct statx *stx; struct statx *stx;
@ -1289,7 +1289,8 @@ typedef struct statx statx_data;
# define rb_statx(file, st, mask) rb_stat(file, st) # define rb_statx(file, st, mask) rb_stat(file, st)
#else #else
# define statx_has_birthtime(st) 0 # define statx_has_birthtime(st) 0
#endif #endif /* !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
defined(HAVE_STRUCT_STATX_STX_BTIME) */
static int static int
rb_stat(VALUE file, struct stat *st) rb_stat(VALUE file, struct stat *st)
@ -1478,7 +1479,7 @@ rb_group_member(GETGROUPS_T gid)
ALLOCV_END(v); ALLOCV_END(v);
return rv; return rv;
#endif #endif /* defined(_WIN32) || !defined(HAVE_GETGROUPS) */
} }
#ifndef S_IXUGO #ifndef S_IXUGO
@ -1529,9 +1530,9 @@ eaccess(const char *path, int mode)
return -1; return -1;
#else #else
return access(path, mode); return access(path, mode);
#endif #endif /* USE_GETEUID */
} }
#endif #endif /* HAVE_EACCESS */
struct access_arg { struct access_arg {
const char *path; const char *path;
@ -2836,7 +2837,7 @@ utime_failed(struct apply_arg *aa)
} }
rb_syserr_fail_path(e, path); rb_syserr_fail_path(e, path);
} }
#endif #endif /* UTIME_EINVAL */
#if defined(HAVE_UTIMES) #if defined(HAVE_UTIMES)
@ -2860,8 +2861,8 @@ RBIMPL_WARNING_POP()
# define utimensat rb_utimensat() # define utimensat rb_utimensat()
# else /* __API_AVAILABLE macro does nothing on gcc */ # else /* __API_AVAILABLE macro does nothing on gcc */
__attribute__((weak)) int utimensat(int, const char *, const struct timespec [2], int); __attribute__((weak)) int utimensat(int, const char *, const struct timespec [2], int);
# endif # endif /* defined(__has_attribute) && __has_attribute(availability) */
# endif # endif /* __APPLE__ && < MAC_OS_X_VERSION_13_0 */
static int static int
utime_internal(const char *path, void *arg) utime_internal(const char *path, void *arg)
@ -2874,15 +2875,15 @@ utime_internal(const char *path, void *arg)
# if defined(__APPLE__) # if defined(__APPLE__)
const int try_utimensat = utimensat != NULL; const int try_utimensat = utimensat != NULL;
const int try_utimensat_follow = utimensat != NULL; const int try_utimensat_follow = utimensat != NULL;
# else # else /* !__APPLE__ */
# define TRY_UTIMENSAT 1 # define TRY_UTIMENSAT 1
static int try_utimensat = 1; static int try_utimensat = 1;
# ifdef AT_SYMLINK_NOFOLLOW # ifdef AT_SYMLINK_NOFOLLOW
static int try_utimensat_follow = 1; static int try_utimensat_follow = 1;
# else # else
const int try_utimensat_follow = 0; const int try_utimensat_follow = 0;
# endif # endif
# endif # endif /* __APPLE__ */
int flags = 0; int flags = 0;
if (v->follow ? try_utimensat_follow : try_utimensat) { if (v->follow ? try_utimensat_follow : try_utimensat) {
@ -2897,15 +2898,15 @@ utime_internal(const char *path, void *arg)
if (result < 0 && errno == ENOSYS) { if (result < 0 && errno == ENOSYS) {
# ifdef AT_SYMLINK_NOFOLLOW # ifdef AT_SYMLINK_NOFOLLOW
try_utimensat_follow = 0; try_utimensat_follow = 0;
# endif # endif /* AT_SYMLINK_NOFOLLOW */
if (!v->follow) if (!v->follow)
try_utimensat = 0; try_utimensat = 0;
} }
else else
# endif # endif /* TRY_UTIMESAT */
return result; return result;
} }
#endif #endif /* defined(HAVE_UTIMENSAT) */
if (tsp) { if (tsp) {
tvbuf[0].tv_sec = tsp[0].tv_sec; tvbuf[0].tv_sec = tsp[0].tv_sec;
@ -2920,7 +2921,7 @@ utime_internal(const char *path, void *arg)
return utimes(path, tvp); return utimes(path, tvp);
} }
#else #else /* !defined(HAVE_UTIMES) */
#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H #if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
struct utimbuf { struct utimbuf {
@ -2942,8 +2943,7 @@ utime_internal(const char *path, void *arg)
} }
return utime(path, utp); return utime(path, utp);
} }
#endif /* !defined(HAVE_UTIMES) */
#endif
static VALUE static VALUE
utime_internal_i(int argc, VALUE *argv, int follow) utime_internal_i(int argc, VALUE *argv, int follow)
@ -3314,12 +3314,13 @@ static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'};
#endif #endif
#ifndef USE_NTFS #ifndef USE_NTFS
#if defined _WIN32 # if defined _WIN32
#define USE_NTFS 1 # define USE_NTFS 1
#else # else
#define USE_NTFS 0 # define USE_NTFS 0
#endif # endif
#endif #endif
#ifndef USE_NTFS_ADS #ifndef USE_NTFS_ADS
# if USE_NTFS # if USE_NTFS
# define USE_NTFS_ADS 1 # define USE_NTFS_ADS 1
@ -3333,6 +3334,7 @@ static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'};
#else #else
#define istrailinggarbage(x) 0 #define istrailinggarbage(x) 0
#endif #endif
#if USE_NTFS_ADS #if USE_NTFS_ADS
# define isADS(x) ((x) == ':') # define isADS(x) ((x) == ':')
#else #else
@ -3400,8 +3402,8 @@ not_same_drive(VALUE path, int drive)
return has_unc(p); return has_unc(p);
} }
} }
#endif #endif /* _WIN32 */
#endif #endif /* DOSISH_DRIVE_LETTER */
static inline char * static inline char *
skiproot(const char *path, const char *end, rb_encoding *enc) skiproot(const char *path, const char *end, rb_encoding *enc)
@ -3445,7 +3447,7 @@ rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
if (has_drive_letter(path)) if (has_drive_letter(path))
return (char *)(path + 2); return (char *)(path + 2);
#endif #endif
#endif #endif /* defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) */
return (char *)path; return (char *)path;
} }
@ -3540,7 +3542,7 @@ ntfs_tail(const char *path, const char *end, rb_encoding *enc)
} }
return (char *)path; return (char *)path;
} }
#endif #endif /* USE_NTFS */
#define BUFCHECK(cond) do {\ #define BUFCHECK(cond) do {\
bdiff = p - buf;\ bdiff = p - buf;\
@ -3645,7 +3647,7 @@ rb_home_dir_of(VALUE user, VALUE result)
return result; return result;
} }
#ifndef _WIN32 #ifndef _WIN32 /* this encompasses rb_file_expand_path_internal */
VALUE VALUE
rb_default_home_dir(VALUE result) rb_default_home_dir(VALUE result)
{ {
@ -3675,7 +3677,7 @@ rb_default_home_dir(VALUE result)
if (NIL_P(login_name)) { if (NIL_P(login_name)) {
rb_raise(rb_eArgError, "couldn't find login name -- expanding `~'"); rb_raise(rb_eArgError, "couldn't find login name -- expanding `~'");
} }
# endif # endif /* !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID) */
VALUE pw_dir = rb_getpwdirnam_for_login(login_name); VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
if (NIL_P(pw_dir)) { if (NIL_P(pw_dir)) {
@ -3690,7 +3692,7 @@ rb_default_home_dir(VALUE result)
rb_str_resize(pw_dir, 0); rb_str_resize(pw_dir, 0);
return result; return result;
} }
#endif #endif /* defined HAVE_PWD_H */
if (!dir) { if (!dir) {
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'"); rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
} }
@ -3820,7 +3822,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
s += 2; s += 2;
} }
} }
#endif #endif /* DOSISH_DRIVE_LETTER */
else if (!rb_is_absolute_path(s)) { else if (!rb_is_absolute_path(s)) {
if (!NIL_P(dname)) { if (!NIL_P(dname)) {
rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result); rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result);
@ -3840,7 +3842,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
p = skipprefix(buf, p, enc); p = skipprefix(buf, p, enc);
} }
else else
#endif #endif /* defined DOSISH || defined __CYGWIN__ */
p = chompdirsep(skiproot(buf, p, enc), p, enc); p = chompdirsep(skiproot(buf, p, enc), p, enc);
} }
else { else {
@ -3893,7 +3895,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
else { else {
do ++s; while (istrailinggarbage(*s)); do ++s; while (istrailinggarbage(*s));
} }
#endif #endif /* USE_NTFS */
break; break;
case '/': case '/':
#if defined DOSISH || defined __CYGWIN__ #if defined DOSISH || defined __CYGWIN__
@ -3918,7 +3920,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
} }
} }
} }
#endif #endif /* USE_NTFS */
break; break;
case '/': case '/':
#if defined DOSISH || defined __CYGWIN__ #if defined DOSISH || defined __CYGWIN__
@ -3943,7 +3945,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
break; break;
} }
} }
#endif #endif /* __APPLE__ */
Inc(s, fend, enc); Inc(s, fend, enc);
break; break;
} }
@ -3967,8 +3969,8 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
s -= prime_len; /* alternative */ s -= prime_len; /* alternative */
} }
} }
# endif # endif /* USE_NTFS_ADS */
#endif #endif /* USE_NTFS */
BUFCOPY(b, s-b); BUFCOPY(b, s-b);
rb_str_set_len(result, p-buf); rb_str_set_len(result, p-buf);
} }
@ -3989,7 +3991,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE; const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
#else #else
char w32buf[MAXPATHLEN]; char w32buf[MAXPATHLEN];
#endif #endif /* HAVE_CYGWIN_CONV_PATH */
const char *path; const char *path;
ssize_t bufsize; ssize_t bufsize;
int lnk_added = 0, is_symlink = 0; int lnk_added = 0, is_symlink = 0;
@ -4013,12 +4015,12 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
b = w32buf; b = w32buf;
} }
} }
#else #else /* !HAVE_CYGWIN_CONV_PATH */
bufsize = MAXPATHLEN; bufsize = MAXPATHLEN;
if (cygwin_conv_to_win32_path(path, w32buf) == 0) { if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
b = w32buf; b = w32buf;
} }
#endif #endif /* !HAVE_CYGWIN_CONV_PATH */
if (is_symlink && b == w32buf) { if (is_symlink && b == w32buf) {
*p = '\\'; *p = '\\';
strlcat(w32buf, p, bufsize); strlcat(w32buf, p, bufsize);
@ -4030,7 +4032,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
lnk_added = 0; lnk_added = 0;
} }
*p = '/'; *p = '/';
#endif #endif /* __CYGWIN__ */
rb_str_set_len(result, p - buf + strlen(p)); rb_str_set_len(result, p - buf + strlen(p));
encidx = ENCODING_GET(result); encidx = ENCODING_GET(result);
tmp = result; tmp = result;
@ -4078,14 +4080,14 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
} }
#endif #endif
} }
#endif #endif /* USE_NTFS */
rb_str_set_len(result, p - buf); rb_str_set_len(result, p - buf);
rb_enc_check(fname, result); rb_enc_check(fname, result);
ENC_CODERANGE_CLEAR(result); ENC_CODERANGE_CLEAR(result);
return result; return result;
} }
#endif /* _WIN32 */ #endif /* !_WIN32 (this ifdef started above rb_default_home_dir) */
#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1) #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
@ -4325,7 +4327,7 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE f
rb_hash_aset(loopcheck, testpath, rb_str_dup_frozen(*resolvedp)); rb_hash_aset(loopcheck, testpath, rb_str_dup_frozen(*resolvedp));
} }
else else
#endif #endif /* HAVE_READLINK */
{ {
VALUE s = rb_str_dup_frozen(testpath); VALUE s = rb_str_dup_frozen(testpath);
rb_hash_aset(loopcheck, s, s); rb_hash_aset(loopcheck, s, s);
@ -4494,7 +4496,7 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, rb_encoding *origenc, enum
} }
rb_sys_fail_path(unresolved_path); rb_sys_fail_path(unresolved_path);
} }
# endif # endif /* !defined(__LINUX__) && !defined(__APPLE__) */
if (origenc && origenc != rb_enc_get(resolved)) { if (origenc && origenc != rb_enc_get(resolved)) {
if (!rb_enc_str_asciionly_p(resolved)) { if (!rb_enc_str_asciionly_p(resolved)) {
@ -4512,7 +4514,7 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, rb_encoding *origenc, enum
RB_GC_GUARD(unresolved_path); RB_GC_GUARD(unresolved_path);
return resolved; return resolved;
#else #else /* !HAVE_REALPATH */
if (mode == RB_REALPATH_CHECK) { if (mode == RB_REALPATH_CHECK) {
VALUE arg[3]; VALUE arg[3];
arg[0] = basedir; arg[0] = basedir;
@ -4649,13 +4651,13 @@ ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encodin
p++; p++;
f = 0; f = 0;
} }
#endif #endif /* DOSISH_DRIVE_LETTER */
#ifdef DOSISH_UNC #ifdef DOSISH_UNC
else { else {
p = "/"; p = "/";
} }
#endif #endif /* DOSISH_UNC */
#endif #endif /* defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC */
} }
else { else {
if (!(p = strrdirsep(name, end, enc))) { if (!(p = strrdirsep(name, end, enc))) {
@ -4897,7 +4899,7 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
continue; continue;
#else #else
e = p; /* get the last dot of the last component */ e = p; /* get the last dot of the last component */
#endif #endif /* USE_NTFS */
} }
#if USE_NTFS #if USE_NTFS
else if (isADS(*p)) { else if (isADS(*p)) {