gpt4 book ai didi

c - Linux 中的 setprogname

转载 作者:可可西里 更新时间:2023-11-01 11:44:13 32 4
gpt4 key购买 nike

在 Unix 中,我知道有函数 setprognamegetprogname。两者分别用于设置和获取程序名称。它们位于库 stdilib.c 中。我想知道 Linux 是否也内置了这些功能,因为我无法让它们在 Linux 机器(Ubuntu 10.04)上运行。这些功能可用吗?代码如下,以防我在网上找到的手册页没有说明全部情况并且我没有添加我需要的内容。谢谢!

#define _XOPEN_SOURCE 500

#include<sys/stat.h>
#include<sys/types.h>

#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

int main (int argc, char *argv[])
{
setprogname(argv[0]);

最佳答案

来自*BSD man page关于 {set,get}progname :

The getprogname() and setprogname() functions manipulate the name of the current program. They are used by error-reporting routines to produce consistent output.

如果你想用errorerror_at_line自定义错误报告的程序名,你可以声明并设置error_print_progname变量为described in the glibc manual :

As mentioned above the error and error_at_line functions can be customized by defining a variable named error_print_progname.

— Variable: void (*) error_print_progname (void) If the error_print_progname variable is defined to a non-zero value the function pointed to is called by error or error_at_line. It is expected to print the program name or do something similarly useful.

The function is expected to be print to the stderr stream and must be able to handle whatever orientation the stream has.

The variable is global and shared by all threads.

编辑:我只是查看了关于 {set,get}prognameerror_print_progname 的 gnulib 手册:

This variable is missing on all non-glibc platforms: MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 11 2011-11, Cygwin, mingw, MSVC 9, Interix 3.5, BeOS.

关于c - Linux 中的 setprogname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439358/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com