libc.obj: use bitfield flags in file instead of int variables && rewrite freopen, fopen #691

Merged
Egor00f merged 5 commits from Egor00f/kolibrios:`libc.obj`--use-bitfield-`flags`-in-`FILE`-instead-of-`int`-variables into main 2026-09-22 12:39:51 +00:00
Owner

use bitfield flags flags in file instead of int variables mode, error, eof.

And rewrite weird logic opening/close files

Changes

fread, ungetc

just check flag read instead of stream->mode != _FILEMODE_W && stream->mode != _FILEMODE_A

fopen

  • added more checks and fix memory leak on error
  • use fopen_r instead of freopen

freopen

  • close stream
  • implemented support of NULL filename
  • move logic of open file to fopen_r

fclose

  • fix memory leak by call fclose_r
    reason: stream name creating by strdup.

New functions

fopen_r

just open file in out.
same with old freopen, but optimized for bitfield.

fclose_r

free stream name. May be later will be added something more

use bitfield `flags` flags in file instead of `int` variables `mode`, `error`, `eof`. And rewrite weird logic opening/close files # Changes ## [`fread`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-c1ee50cbe35b42b6194ed787e4f0273aaa857aaf), [`ungetc`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-cf33d23b7f2df4f6fe1f87babdb6df8e249dd595) just check flag `read` instead of `stream->mode != _FILEMODE_W && stream->mode != _FILEMODE_A` ## [`fopen`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-0b8822108a0c9c5d24e6fe7c5613935962f768fa) + added more checks and fix memory leak on error + use [`fopen_r`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-01b999679dbec08cf1f97f9c1f5409cdde815b43) instead of `freopen` ## [`freopen`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-7936b834056ff344a18b4ecb5e6a5960645eb9a0) + close stream + implemented support of `NULL` filename + move logic of open file to [`fopen_r`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-01b999679dbec08cf1f97f9c1f5409cdde815b43) ## [`fclose`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-8de54c5c73de542d4205b4542cd3557c31218fcb) + fix memory leak by call [`fclose_r`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-3f1a8cf681e2950215189346a85d16849b914558) [reason](https://git.kolibrios.org/KolibriOS/kolibrios/src/commit/e0e8a3536415a10fb8db8001693c86cab018d181/programs/develop/ktcc/libc.obj/source/stdio/freopen.c#L40): stream name creating by `strdup`. # New functions ## [`fopen_r`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-01b999679dbec08cf1f97f9c1f5409cdde815b43) just open file in `out`. same with old `freopen`, but optimized for bitfield. ## [`fclose_r`](https://git.kolibrios.org/KolibriOS/kolibrios/pulls/691/files#diff-3f1a8cf681e2950215189346a85d16849b914558) free stream name. May be later will be added something more
Egor00f added the
Priority
Low
4
Category
Libraries
Lang/C
labels 2026-09-11 08:05:55 +00:00
CODEOWNERS rules requested review from system 2026-09-11 08:05:56 +00:00
Egor00f requested review from apps 2026-09-11 08:05:57 +00:00
Egor00f force-pushed `libc.obj`--use-bitfield-`flags`-in-`FILE`-instead-of-`int`-variables from d640e4cd76 to 89768bb3b5 2026-09-15 13:34:48 +00:00 Compare
dunkaist approved these changes 2026-09-22 10:02:02 +00:00
Egor00f added 5 commits 2026-09-22 10:06:34 +00:00
```c
int no_file = _ksys_file_get_info(_name, &info);
if (!no_file && info.attributes & IS_FOLDER) {
    return _set_errno(out, EISDIR);
}
```
looks a strange... maybe it mean `!no_file || info.attributes & IS_FOLDER`, but it still wrong
name creating by `strdup`
fixed memory leak in `freopen`(now it if `fopen_r`) when can't alloc mem for `FILE`
fix codestyle
Test PR / Build (en_US) (pull_request) Successful in 2m37s
Test PR / Build (es_ES) (pull_request) Successful in 2m42s
Test PR / Build (ru_RU) (pull_request) Successful in 2m45s
5ee4b4b025
Egor00f force-pushed `libc.obj`--use-bitfield-`flags`-in-`FILE`-instead-of-`int`-variables from 89768bb3b5 to 5ee4b4b025 2026-09-22 10:06:34 +00:00 Compare
CODEOWNERS rules requested review from system 2026-09-22 10:06:35 +00:00
Leency approved these changes 2026-09-22 10:29:21 +00:00
Egor00f merged commit c121db7a88 into main 2026-09-22 12:39:51 +00:00
Egor00f deleted branch `libc.obj`--use-bitfield-`flags`-in-`FILE`-instead-of-`int`-variables 2026-09-22 12:39:52 +00:00
Sign in to join this conversation.
No Reviewers
3 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: KolibriOS/kolibrios#691