Skip to content

Fixed a bug and made a few minor performance improvements.#152

Open
rosenrost wants to merge 4 commits intofreemint:masterfrom
rosenrost:improvements
Open

Fixed a bug and made a few minor performance improvements.#152
rosenrost wants to merge 4 commits intofreemint:masterfrom
rosenrost:improvements

Conversation

@rosenrost
Copy link
Contributor

delay.c / sleep.c:
Subtraction removed from while condition.

fclose.c:
Error handling has been added; errno is set in case of an error. For example, fclose(NULL) now correctly returns EOF instead of 0.

doprnt.c:
Bug fixed and performance improved:

  • field_width is now register int
  • Redundant flag_found removed and loop adjusted accordingly
  • ISUPPER() in the condition for do_long = LONG_VAL moved from the very end to the very beginning
  • tolower(fmt) replaced with fmt | 0x20 at a point where only 'e' and 'g' are checked
  • Bugfix: String literal manipulation replaced
    • instead of (when fmt == 'x' or fmt == 'X'):
      prefix = "x0";
      *(char*)prefix = fmt;
      ...
    • now (when fmt == 'x'):
      prefix = "x0";
      ...
    • and (when fmt == 'X'):
      prefix = "X0";
      ...
  • Use of strlen() replaced with manual counting

Error handling has been added; `errno` is set in case of an error. For example, `fclose(NULL)` now correctly returns `EOF` instead of `0`.
- `field_width` is now `register int`
- Redundant `flag_found` removed and loop adjusted accordingly
- `ISUPPER()` in the condition for `do_long = LONG_VAL` moved from the very end to the very beginning
- `tolower(fmt)` replaced with `fmt | 0x20` at a point where only 'e' and 'g' are checked
- Bugfix: String constant manipulation replaced
- Use of `strlen()` replaced with manual counting
@th-otto
Copy link
Contributor

th-otto commented Mar 13, 2026

  • fclose(NULL) should close all open file handles. Please check that again.
  • register attribute is ignored by gcc, and gives errors with newer compilers, please revert that
  • strlen() has recently been replaced by an inline version, no need to manually do that in the code. Please revert that.

@rosenrost
Copy link
Contributor Author

  • fclose(NULL) should close all open file handles. Please check that again.

No, definitely not. That's what fcloseall() does.

From the man page:

DESCRIPTION

       The fclose() function flushes the stream pointed to by stream
       (writing any buffered output data using fflush(3)) and closes the
       underlying file descriptor.

RETURN VALUE

       Upon successful completion, 0 is returned.  Otherwise, EOF is
       returned and errno is set to indicate the error.  In either case,
       any further access (including another call to fclose()) to the
       stream results in undefined behavior.

ERRORS

       EBADF  The file descriptor underlying stream is not valid.

       The fclose() function may also fail and set errno for any of the
       errors specified for the routines close(2), write(2), or
       fflush(3).

From what mintlib does:

#define	__validfp(stream)						      \
  (stream != NULL &&							      \
   ({ if (stream->__magic == _GLUEMAGIC)				      \
	stream = *((struct { int __magic; FILE **__p; } *) stream)->__p;      \
      stream->__magic == _IOMAGIC; }))

int
fclose (FILE *stream)
{
  int status;

  if (!__validfp (stream))
    {
      __set_errno (EINVAL);
      return EOF;
    }
    ...
}

- Revert field_width being register int.
- Revert replacing strlen() call by manual counting.
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.

2 participants