public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Cc: "Thomas Weißschuh" <linux@weissschuh.net>,
	"Shuah Khan" <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 13/15] tools/nolibc: add fopen()
Date: Sat, 26 Apr 2025 12:58:42 +0200	[thread overview]
Message-ID: <20250426105842.GF17549@1wt.eu> (raw)
In-Reply-To: <20250423-nolibc-misc-v1-13-a925bf40297b@linutronix.de>

On Wed, Apr 23, 2025 at 05:01:43PM +0200, Thomas Weißschuh wrote:
> +static __attribute__((unused))
> +FILE *fopen(const char *pathname, const char *mode)
> +{
> +	int flags, fd;
> +
> +	if (!strcmp(mode, "r"))
> +		flags = O_RDONLY;
> +	else if (!strcmp(mode, "w"))
> +		flags = O_WRONLY | O_CREAT | O_TRUNC;
> +	else if (!strcmp(mode, "a"))
> +		flags = O_WRONLY | O_CREAT | O_APPEND;
> +	else if (!strcmp(mode, "r+"))
> +		flags = O_RDWR;
> +	else if (!strcmp(mode, "w+"))
> +		flags = O_RDWR | O_CREAT | O_TRUNC;
> +	else if (!strcmp(mode, "a+"))
> +		flags = O_RDWR | O_CREAT | O_APPEND;
> +	else {
> +		SET_ERRNO(EINVAL);
> +		return NULL;
> +	}

I'm concerned by the size of the function due to the repeated strcmp()
calls (also I find strcmp()==0 more readable that !strcmp() which I tend
to read as "not string compares").

I have not tried the code below but I think it could cover it in a maybe
lighter way:

	switch (*mode) {
		case 'r": flags = O_RDONLY; break;
		case 'w': flags = O_WRONLY | O_CREAT | O_TRUNC; break;
		case 'a': flags = O_WRONLY | O_CREAT | O_APPEND; break;
        	default : SET_ERRNO(EINVAL); return NULL;
        }

	if (mode[1] == '+')
		flags = (flags & ~(O_RDONLY|O_WRONLY)) | O_RDWR;

I think it does the same but should be significantly lighter.

Willy

  reply	other threads:[~2025-04-26 10:58 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-23 15:01 [PATCH 00/15] tools/nolibc: various new functions Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 01/15] tools/nolibc: add strstr() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 02/15] tools/nolibc: add %m printf format Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 03/15] tools/nolibc: add more stat() variants Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 04/15] tools/nolibc: add mremap() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 05/15] tools/nolibc: add getrandom() Thomas Weißschuh
2025-04-26 10:31   ` Willy Tarreau
2025-04-28 10:37     ` Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 06/15] tools/nolibc: add abs() and friends Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 07/15] tools/nolibc: add support for access() and faccessat() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 08/15] tools/nolibc: add clock_getres(), clock_gettime() and clock_settime() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 09/15] tools/nolibc: add timer functions Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 10/15] tools/nolibc: add timerfd functionality Thomas Weißschuh
2025-04-26 10:33   ` Willy Tarreau
2025-04-23 15:01 ` [PATCH 11/15] tools/nolibc: add difftime() Thomas Weißschuh
2025-04-26 10:45   ` Willy Tarreau
2025-04-28 10:41     ` Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 12/15] tools/nolibc: add namespace functionality Thomas Weißschuh
2025-04-26 10:48   ` Willy Tarreau
2025-04-23 15:01 ` [PATCH 13/15] tools/nolibc: add fopen() Thomas Weißschuh
2025-04-26 10:58   ` Willy Tarreau [this message]
2025-04-23 15:01 ` [PATCH 14/15] tools/nolibc: implement fall back to sys_clock_gettime() in gettimeofday() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 15/15] tools/nolibc: implement wait() in terms of waitpid() Thomas Weißschuh
2025-04-26 11:01 ` [PATCH 00/15] tools/nolibc: various new functions Willy Tarreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250426105842.GF17549@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox