Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-14-2009
ebd ebd is offline
Registered User
 

Join Date: Aug 2009
Posts: 1
Question does perror() set errno?

here the program gives a odd result:

Code:
#include <stdio.h>
int main(){
    perror("first");
    perror("next");
    return 0;
}

result:

first: Success
next: Illegal seek

why? any resonable explanation? i found no information about this in man pages.
thanks in advance
Sponsored Links
  #2 (permalink)  
Old 08-14-2009
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 6,172
perror uses errno.

all it does is the equivalent of sprintf your phrase along with the result of strerror(errno).
You are experiencing undefined behavior - meaning do not call perror twice like that.

You idea may or may not work correctly - undefined means the run time library writer decides what if anything some aspect of C coding will do. In your case it probably trashed errno after the first call.

Write your own perror() if you don't want to use it conventionally.
  #3 (permalink)  
Old 08-22-2009
Registered User
 

Join Date: Aug 2009
Location: Munich (Germany)
Posts: 94
What Jim means is that errno is undefined after a successful library call. You should only call perror() immediately after a failing call.

As a matter of fact, the glibc implementation of perror() calls some other functions such as dup(), fdopen() that modify errno, even if perror() itself always succeed.

I would personally expect that clean library code doesn't expose their mess to outside. But we cannot complain, as POSIX/SUS allows such behavior.


Cheers,
Loïc
--
My Blog: Loïc OnStage

“UNIX is simple. It just takes a genius to understand its simplicity.” – Dennis Ritchie
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
errno the_learner Programming 5 10-02-2008 05:39 PM
Hi errno in sys/stat.h vijlak Programming 6 11-11-2006 09:13 PM
errno pb dts Programming 3 08-06-2004 10:51 AM
login error after sys-unconfig, errno = 13 roing UNIX for Dummies Questions & Answers 14 02-08-2004 11:25 AM
Getting errno in a Multithreaded program S.Vishwanath Programming 2 03-25-2002 09:58 AM



All times are GMT -4. The time now is 10:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0