Skip to content

Transition to C99#942

Open
JohnoKing wants to merge 16 commits intoksh93:devfrom
JohnoKing:require-c99
Open

Transition to C99#942
JohnoKing wants to merge 16 commits intoksh93:devfrom
JohnoKing:require-c99

Conversation

@JohnoKing
Copy link

@JohnoKing JohnoKing commented Mar 9, 2026

This commit is a compilation of many old patches of mine. It removes obsolete code and enforces C99 as a build requirement.

Changes:

  • Remove many unused tests and useless feature tests. (This was a bit more involved for <sys/types.h>, which had annoying special casing.) This included the "check for global headers" in the iffe script.
  • Assume fchmod is present and use it instead of chmod in shcomp for very slightly better performance.
  • dotpaths_lib(): Use fstat rather than stat for the PWD for better performance.
  • Scrap pre-C99 fallbacks for iswblank(3).
  • Replaced usage of the obsolescent timeofday() macro with the more portable tvgettime() function in libast, which itself uses the modern clock_gettime internally when it's available.
  • Replaced usage of strcopy with either strcpy or stpcpy in cases where no buffer overlap occurs. There are perhaps more places where replacement is possible (ksh93v- and ksh2020 do this far more liberally), but I'm trying to be conservative with these changes to avoid accidentally introducing bugs.
  • Replaced an instance of strncopy with strlcpy in keytrap() because the buffers don't overlap.
  • Add pertinent documentation as it relates to str*copy's usage with overlapping buffers.
  • Backported a modified version of ksh93v-'s stpcpy fallback.
  • Introduced usage of C99 restrict, primarily for standards compliance in compatibility functions (though some regular functions like sfopen() will also use restrict now).
  • Assume C99 __func__ is available and drop now unnecessary C90 feature tests.
  • Added a few casts to fix some compiler warnings.
  • Remove basic iffe probes for various C99 multibyte functions (excluding those that still need tests due to platform-specific bugs). Also removed other unnecessary probes for C99 and POSIX functionality.
  • Remove unnecessary mktime.c, wc.c and wordexp.c compat files (the operating system should provide these functions).
  • Switched to using C99-style struct init throughout the codebase.
  • Removed usage of bzero, bcopy, memclear and memzero in favor of standard memset and memcpy.
  • Minor bugfix: avoid passing overlapping buffers to getaddrinfo in io.c because that violates the POSIX standard.
  • make.probe: Automatically use the latest C standard available (i.e. C23) because GCC/Clang may use something older by default whilst having support for newer standards.

I have additional changes for using C99-style for loops and bools, but those have been excluded because this patch is big enough as it is. I also encountered crashing bugs when I attempted to remove widespread usage of the struct hack, so the struct hack was only removed if it was already disabled by a preexisting __STDC_VERSION__ check.

Progresses #777 (this commit doesn't include or fix the removed _c99_in_the_wild code).

This commit is a compilation of many old patches of mine.
It removes obsolete code and enforces C99 as a build
requirement.

Changes:
- Remove many unused tests and useless feature tests.
  (This was a bit more involved for <sys/types.h>, which
  had annoying special casing.) This included the
  'check for global headers' in the iffe script.
- Assume fchmod is present and use it instead of chmod in
  shcomp for slightly better performance.
- dotpaths_lib(): Use fstat rather than stat for the PWD
  for better performance.
- Scrap pre-C99 fallbacks for iswblank(3).
- Replaced usage of the obsolescent timeofday() macro with the
  more portable tvgettime() function in libast, which itself uses
  the modern clock_gettime internally.
- Replaced usage of strcopy with either strcpy or stpcpy in
  cases where no buffer overlap occurs. There are perhaps
  more places where replacement is possible (ksh93v- and ksh2020
  do this far more liberally), but I'm trying to be conservative
  with these changes to avoid accidentally introducing bugs.
- Replaced an instance of strncopy with strlcpy in keytrap()
  because the buffers don't overlap.
  - Also replaced some instances of strncpy with strlcpy
    (vide ksh2020 commit f63ebd0 and associated issue).
- Add pertinent documentation as it relates to str*copy's
  usage with overlapping buffers.
- Backported a modified version of ksh93v-'s stpcpy fallback.
- Introduced usage of C99 restrict, primarily for standards
  compliance in compatibility functions (though some regular
  functions like sfopen() do also use restrict now).
- Assume C99 __func__ is available and drop now unneeded
  C90 feature tests.
- Added a few casts to fix some compiler warnings.
- Remove basic iffe probes for various C99 multibyte functions
  (excluding those that still need tests due to platform-specific
  bugs). Also removed other unnecessary probes for C99 and POSIX
  functionality.
- Remove unnecessary mktime.c, wc.c and wordexp.c compat files
  (the operating system should provide these functions).
- Switched to using C99-style struct init throughout the codebase.
- Removed usage of bzero, bcopy and memzero in favor of standard
  memset.

I have additional changes for using C99-style for loops and bools,
but those have been excluded because this patch is big enough as it is.
@JohnoKing JohnoKing changed the title C99 transition Transition to C99 Mar 9, 2026
JohnoKing added 15 commits March 9, 2026 00:50
This merely moves some logic out of sh_init(), though there is one minor
change in behavior:

- Initialize sh.pwdfd to -1 because it shouldn't default to stdin.
GCC and Clang may not necessarily use the latest C standard available,
so enable e.g. C23 with the help of make.probe. If the user wants to use
something else, it can be overridden with CCFLAGS.
This was probably inadvertently brought over from other unrelated
branches (i.e. thickfold and co.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant