在 C 源代码中使用 GNU 版本的 basename() 和 dirname()

发布于 2024-11-03 10:39:01 字数 291 浏览 5 评论 0原文

如何使用 GNU C 库版本的 basename()dirname()

如果你

#include <libgen.h>

想要目录名 您已经获得了 basename() 的 POSIX 版本,而不是 GNU 版本。 (即使

#define _GNU_SOURCE

据我所知,C 中没有条件导入。gcc 是否有特定的技巧?

How do I use the GNU C Library version of basename() and dirname()?.

If you

#include <libgen.h>

for dirname
You're already getting the POSIX, not the GNU, version of basename(). (Even if you

#define _GNU_SOURCE

As far as I know there is no conditional importing in C. Is there a gcc specific trick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十级心震 2024-11-10 10:39:01

您只需自己编写它并为其指定一个与 basename 不同的名称即可。 GNU 坚持创建可以用 1-3 行编写的标准函数的替代不合格版本,这完全是愚蠢的。

char *gnu_basename(char *path)
{
    char *base = strrchr(path, '/');
    return base ? base+1 : path;
}

这样,你的程序也将更加可移植。

Just write it yourself and give it a different name than basename. This GNU insistence on creating alternate non-conforming versions of standard functions that can be written in 1-3 lines is completely batty.

char *gnu_basename(char *path)
{
    char *base = strrchr(path, '/');
    return base ? base+1 : path;
}

This way, your program will also be more portable.

剑心龙吟 2024-11-10 10:39:01

根据您应该执行的手册页,

#define _GNU_SOURCE
#include <string.h>

如果您获得 POSIX 版本,libgen.h 可能在此之前已经包含在内。您可能需要在 CPPFLAGS 中包含 -D_GNU_SOURCE 进行编译:

gcc -D_GNU_SOURCE ....

比较:POSIX 版本 与 GNU 版本在编译器资源管理器上。

According to the man page you should do

#define _GNU_SOURCE
#include <string.h>

If you get the POSIX version, libgen.h is probably already included before that point. You may want to include -D_GNU_SOURCE in the CPPFLAGS for compilation:

gcc -D_GNU_SOURCE ....

Compare: POSIX Version vs GNU Version on Compiler Explorer.

怪我太投入 2024-11-10 10:39:01

检查 libgen.h 后,我很确定我有一个无警告且无错误的解决方案:

/* my C program */
#define _GNU_SOURCE     /*  for GNU version of basename(3) */
#include <libgen.h>     /*  for dirname(3) */
#undef basename         /*  (snide comment about libgen.h removed) */
#include <string.h>     /*  for basename(3) (GNU version) and strcmp(3) */

/* rest of C program... */

使用 #undef 行,现在我的程序包含 来自 libgen.h 的 dirname(3) 和来自 string.h 的 GNU 版本的 basename(3)

gcc(版本 4.5.2)或 clang(版本 3.3)没有编译器警告/错误。

After examining libgen.h, I'm pretty sure I have a warning-free and error-free solution:

/* my C program */
#define _GNU_SOURCE     /*  for GNU version of basename(3) */
#include <libgen.h>     /*  for dirname(3) */
#undef basename         /*  (snide comment about libgen.h removed) */
#include <string.h>     /*  for basename(3) (GNU version) and strcmp(3) */

/* rest of C program... */

With the #undef line, now my program includes dirname(3) from libgen.h and the GNU version of basename(3) from string.h.

No compiler warnings/errors from either gcc (version 4.5.2) or clang (version 3.3).

长发绾君心 2024-11-10 10:39:01

确保您使用的是 GNU C 库,而不是系统(假定的)兼容 POSIX 的默认库。

这通常在 GCC 规范文件中设置。使用 -v 选项显示当前设置:

$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

Make sure you're building with the GNU C library, rather than your system's (presumed) POSIX-compatible default.

This is often set in the GCC spec file. Use the -v option to show the current settings:

$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
沦落红尘 2024-11-10 10:39:01

这是疯狂的基名和目录名有两个版本。

我们在一个大项目中工作,看起来这两个 api 已经引起了
潜在的错误。因此,我们将“basename”“dirname”标记为已弃用,以警告如果
有人使用它:

#ifdef basename
__attribute__ ((deprecated))
char *__xpg_basename(char *path);
#else
__attribute__ ((deprecated))
char *basename(const char *path);
#endif

 __attribute__ ((deprecated))
char *dirname(char *path);

我们还尝试引入一个基础的 c 基础库,例如 glib 或 libcork,
但看起来太重了。所以我们为此目的编写了一个小型库,它
实现如下:

#include <libgen.h>       // for dirname
#include <linux/limits.h> // for PATH_MAX
#include <stdio.h>        // for snprintf
#include <string.h>       // for basename
#include <stdbool.h>      // for bool

bool get_basename(const char *path, char *name, size_t name_size) {
  char path_copy[PATH_MAX] = {'\0'};
  strncpy(path_copy, path, sizeof(path_copy) - 1);
  return snprintf(name, name_size, "%s", basename(path_copy)) < name_size;
}

bool get_dirname(const char *path, char *name, size_t name_size) {
  char path_copy[PATH_MAX] = {'\0'};
  strncpy(path_copy, path, sizeof(path_copy) - 1);
  return snprintf(name, name_size, "%s", dirname(path_copy)) < name_size;
}

然后我们用 get_basename get_dirname 替换所有 basename dirname 调用。

It's crazy basename and dirname have two versions.

We worked at a big project, it looks like these two apis already caused
potentially bugs. So we marked "basename" "dirname" as deprecated for warning if
someone use it:

#ifdef basename
__attribute__ ((deprecated))
char *__xpg_basename(char *path);
#else
__attribute__ ((deprecated))
char *basename(const char *path);
#endif

 __attribute__ ((deprecated))
char *dirname(char *path);

We also try to introduce a base c foundation library such as glib or libcork,
but it looks like too heavy. So we write a tiny library for this purpose, it
implementation like this:

#include <libgen.h>       // for dirname
#include <linux/limits.h> // for PATH_MAX
#include <stdio.h>        // for snprintf
#include <string.h>       // for basename
#include <stdbool.h>      // for bool

bool get_basename(const char *path, char *name, size_t name_size) {
  char path_copy[PATH_MAX] = {'\0'};
  strncpy(path_copy, path, sizeof(path_copy) - 1);
  return snprintf(name, name_size, "%s", basename(path_copy)) < name_size;
}

bool get_dirname(const char *path, char *name, size_t name_size) {
  char path_copy[PATH_MAX] = {'\0'};
  strncpy(path_copy, path, sizeof(path_copy) - 1);
  return snprintf(name, name_size, "%s", dirname(path_copy)) < name_size;
}

Then we replace all basename dirname call with get_basename get_dirname.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文