does perror() set errno?


 
Thread Tools Search this Thread
Top Forums Programming does perror() set errno?
# 1  
Old 08-14-2009
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
# 2  
Old 08-14-2009
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  
Old 08-22-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Errno.h symbols

Hi, I need to look at a recent copy of /usr/include/errno.h from AIX 7.2 to check some symbols. In particular, I'm curious if it defines EOWNERDEAD and ENOTRECOVERABLE. Can someone who has access to 7.2 please check for me? Thanks! (1 Reply)
Discussion started by: topcat
1 Replies

2. Linux

[Errno 256] No more mirrors to try.

Dear all, CentOS 6 After executing "yum update -y" command I am facing this error. Please help me out. thanks in advance. Full error & error code is given as follow: ... (7 Replies)
Discussion started by: saqlain.bashir
7 Replies

3. Programming

need help about get errno [ENXIO] for mmap

from mmap manpage I get it's errors discription: The addresses specified by the range [off, off + len) are invalid for filedes. How could I trigger a ENXIO ? anyone can input the code? Lei (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Programming

help with C programming, perror

i am trying to use the perror function: something like perror("WRONG!!!"); but when i see the message in the terminal, it comes out like WRONG!!! : Success How can i change it to WRONG!!! : WRONG!!! well i just want to have my own custom message for the part after the colon... (1 Reply)
Discussion started by: omega666
1 Replies

5. UNIX for Dummies Questions & Answers

perror with signals

I have following problem with this code.. First time trough the main loop..... perror gives ....blocked signal:success(all other times gives illlegal seek) Should every time trought the main loop be success?? And the perror otside of main loop...didn't change mask:success That line of code... (2 Replies)
Discussion started by: joker40
2 Replies

6. Programming

A question about printing error message with perror

Dear all, I use perror in order to print an error message to the standar error. For example if a C program is called without its two necessary command line parameters then : if (argc != 3) { perror("use: ./myProgram <source file> <target file>\n"); return 1; } Now the... (2 Replies)
Discussion started by: dariyoosh
2 Replies

7. Programming

errno

Hey, Can I assume that for certain function calls, errno can never be set to a certain value. More specifically, can I assume that for if the stat function call fails, the errno can never be or "No space left on device." I am assuming that a read function cannot fail because of no space... (5 Replies)
Discussion started by: the_learner
5 Replies

8. Programming

Hi errno in sys/stat.h

How should I use errno in a c program and what info does it have . I am working with directories and files. So can any one tell me How to access errno?I am using the stat() function on \etc directory and I am alble to access only the half of the directories.I am not able to access other half and... (6 Replies)
Discussion started by: vijlak
6 Replies

9. Programming

errno pb

Hello, I need to make a lib with pthread, when I run my make file all is good. But when I run my test program, I test errno in the begining and is already set to 251. Is it normal ??? What can I modify in my Makefile to have errno set to 0 ??? Thanks $make gcc -D_REENTRANT -shared -fpic... (3 Replies)
Discussion started by: dts
3 Replies

10. Programming

Getting errno in a Multithreaded program

In Tru64 Unix, the 'errno' variable is not thread safe. Could anybody help me about how to make it thread safe or how to check 'errno' in a Multithreaded program ???? The Programming process is like this. There are some definite number of threads having their own task. There is one... (2 Replies)
Discussion started by: S.Vishwanath
2 Replies
Login or Register to Ask a Question