0

Is there an alternative to the err() family of functions found in <err.h> that displays the program name, a semicolon, a space, and the error message... without exiting the program?

Pseudocode

I want

print "<program name>: <error message>"

and not

print "<program name>: <error message>"
exit program
return <status>

EDIT: I cannot use argv[0] (as the program name) to write out the message myself, as I am writing a library

crypticcu
  • 23
  • 4
  • 1
    The alternative is to write it yourself. It is not very difficult. – Eugene Sh. Jan 06 '21 at 21:55
  • The problem is getting the name of the program itself. I cannot use argv[0], as I am working on a C library. – crypticcu Jan 06 '21 at 21:59
  • You could look at the `err` source code for insights. It is opensource. Or you can add an initialization function for your library which will be passed `argv[0]` as parameter. – Eugene Sh. Jan 06 '21 at 22:04
  • On the asssumption that you know which program generates the error, you might find `__FILE__`, `__LINE__`, and `__FUNCTION__` (if supported) more useful, which can be passed by the calling function which sees an offence. – Weather Vane Jan 06 '21 at 22:22
  • @WeatherVane I cannot assume this, as others who compile my code might name the program differently. I do not know a way to derive the invocation name before compile time, but err() somehow manages to. I have a feeling this is compiler-dependent because is not part of the standard library... – crypticcu Jan 06 '21 at 22:29
  • Sorry, I still don't understand. Is it you who wants the executable name, or the person who is using your library, and already knows the name of their own program? Or is your function internal to the library, and not user-called? One thing you can do, is to have a library initialisation call, where the user supplies the executable name (and anything else). – Weather Vane Jan 06 '21 at 22:32
  • It is the function internal to the library that requires the executable name (what the final program is compiled as). – crypticcu Jan 06 '21 at 22:42
  • *but err() somehow manages to* If `err()` can do it, [you can do it](https://stackoverflow.com/questions/4031672/without-access-to-argv0-how-do-i-get-the-program-name). – Andrew Henle Jan 07 '21 at 10:34

3 Answers3

0

See man error.

error is the same as err but will return if status == 0, exit otherwise.

CONFORMING TO

  • These functions and variables are GNU extensions, and should not be used in programs intended to be portable.
crypticcu
  • 23
  • 4
Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
  • It states ": Operation not permitted" after invocation of error() before exiting. While I have not gotten this approach to work, I have found out about `program_invocation_name` through the same man page you me, though. By using `extern` I was able to successfully print the error message with the program name! – crypticcu Jan 06 '21 at 22:12
  • Wow, i've tested it right now and this it what i got: _Interrupted system call_. I have to admit, i never used `error` before, i need to do some research on that. – Erdal Küçük Jan 06 '21 at 22:20
0

program_invocation_name works just as well for getting the name of the program, as Erdal Küçük mentioned. From this, we can print the full error message, program name included.

The variable is part of glibc and can be retrieved using:

extern char *program_invocation_name;

Because it is not a part of the C standard library, programs using this variable should not be expected to be portable.

crypticcu
  • 23
  • 4
0

The warn/warnx functions from err.h will do the same thing as err, but will return.

For example:

warnx("message here");
puts("I'm still running!");

Output:

a.out: message here
I'm still running!