LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2004, 05:40 AM   #1
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
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)
 
Old 09-24-2004, 07:33 AM   #2
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
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.
 
Old 09-24-2004, 07:37 AM   #3
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
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.
 
Old 09-24-2004, 09:33 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 09-24-2004, 09:38 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
under HP-UX, why errno is always 0 in a thread? iclinux Programming 8 10-21-2014 04:58 AM
MySQL, errno 13 Ephracis Linux - Software 1 01-02-2005 07:11 PM
errno cynthia Linux - Networking 3 09-24-2004 01:58 PM
errno.h, but it IS declared...... privateprimate Linux - Software 3 09-08-2004 07:37 PM
apache died HELP (errno:13) rob_roman23 Linux - General 4 09-03-2003 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration