LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use errno (https://www.linuxquestions.org/questions/programming-9/how-to-use-errno-234667/)

sibtay 09-24-2004 05:40 AM

How to use errno
 
can any one plz tell me how can you use errno.h functions to display errors (especially the reason of segmentation faults)

barisdemiray 09-24-2004 07:33 AM

Re: How to use errno
 
Quote:

Originally posted by sibtay
can any one plz tell me how can you use errno.h functions to display errors (especially the reason of segmentation faults)
AFAIK errno is application specific and if an application segfaults we cannot get errno. But you can use strerror for other errors, as in:

Code:

[baris@rhinox]$ cat perr.c
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
        FILE *fp;

        if ( (fp = fopen("/etc/shadow", "r")) )
                printf("REPORT: File opened successfully\n");
        else
                printf("ERROR: %s\n", strerror(errno));

        return 0;
}

[baris@rhinox]$ gcc -Wall -o perr perr.c
[baris@rhinox]$ ./perr
ERROR: Permission denied
[baris@rhinox]$

Hope this helps.

cracauer 09-24-2004 07:37 AM

On a segfault the errno mechanism doesn't apply.

You use the errno variable and functions after failed systemcalls and after failure of selected library calls. You ned to consult the documentation for individual calls to figure out whether they will use errno to tell you about errors.

jim mcnamara 09-24-2004 09:33 AM

Segfaults are signals from the kernel memory management sub-system.

Nothing more.

In order to determine where the problem is you have examine the core file using a debugger like gdb.

Hko 09-24-2004 09:38 AM

AFIA the only way to examine why a program segfaulted, is using the debugger (gdb).

Either make sure core dumps are enabled (see man bash, search for ulimit) and load the core dump into gdb. Or run the program inside gdb and try to make it segfault. Somtimes a program does not segfault in gdb, while it would when run outside gdb, so the core dump is probably the best option.


All times are GMT -5. The time now is 08:09 PM.