Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<libgen.h> is required for basename(3) #4689

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

obache
Copy link
Contributor

@obache obache commented Oct 17, 2023

From missing a header, basename() may be recognized as return type int.

@elliefm
Copy link
Contributor

elliefm commented Oct 19, 2023

My man page for basename(3) says:

There are two different versions of basename() - the POSIX version described
above, and the GNU version, which one gets after

           #define _GNU_SOURCE         /* See feature_test_macros(7) */
           #include <string.h>

The GNU version never modifies its argument, and returns the empty string when
path has a trailing slash, and in particular also when it is "/". There is no
GNU version of dirname().

With glibc, one gets the POSIX version of basename() when <libgen.h> is in‐
cluded, and the GNU version otherwise.

On Linux, my config.h has #define _GNU_SOURCE 1, and relocate_by_id.c includes both config.h and string.h, so it will currently be using the GNU version of the function. Including libgen.h will switch Linux to using the POSIX version of the function.

Looking at some other sources where we use progname = basename(argv[0]):

  • imap/relocate_by_id.c
  • imap/jmap_expire.c
  • imap/reconstruct.c
  • imap/cyrdump.c
  • imap/cyr_expire.c

reconstruct, cyrdump, and cyr_expire all include libgen.h, and get the POSIX behaviour. relocate_by_id and jmap_expire do not, and get the GNU behaviour. Based on the older tools including libgen.h, I think we intend to have the POSIX behaviour here, and the libgen detail was just missed in the newer tools. So libgen should be added to both relocate_by_id and jmap_expire.

@ksmurchison @rsto What do you think?

@elliefm elliefm requested review from rsto and ksmurchison October 19, 2023 23:30
@obache
Copy link
Contributor Author

obache commented Oct 20, 2023

On BSD, gegprogname(3) is useful to get progname.
Or you can use setprogname(argv[0]) and getprogname() if such functions exists.

@ksmurchison
Copy link
Contributor

@elliefm yes, I would expect that we want the POSIX variant. I had no intention of pulling in any GNU-specific variant.

@elliefm elliefm merged commit 48b9b4f into cyrusimap:master Oct 24, 2023
elliefm added a commit to elliefm/cyrus-imapd that referenced this pull request Oct 24, 2023
@obache obache deleted the fix-missing-headers-for-basename branch October 25, 2023 05:24
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.

3 participants