0

How can I add an error to the perror stack?

here is an example of what I want

#include <stdio.h>
#include <stdlib.h>

int Div (int a, int b, int * c) {
   if (b == 0) {
       // add to perror: "cannot divide by zero!"
       return 0;
   }
   *c = a / b;
   return 1;
}

int main () {
   int n;
   if (!Div(2, 0, &n)) {
      perror("could not divide");
   }
   return 1;
}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74

2 Answers2

2

There is no standard (or nonstandard, on the systems I'm aware of) way to add new errno values; you can assign to errno to use existing values, but this is not a good idea for anything not part of the standard library.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
2

The only way is to change the C standard library, and you don't want to do that.

If you change libc and use the modified one, you can add your own errno numbers. But then your program will only work correctly on systems with your modified "standard" library.

ugoren
  • 16,023
  • 3
  • 35
  • 65