LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-26-2012, 12:57 PM   #1
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Rep: Reputation: Disabled
strftime


This is might be really silly, I;ve been doing some trial and error testing and I am kinda clueless here..
I perform a stat() on a file that is not present. As a result errno is set to 2.
After that i call a function to perform logging and suddenly in the leg i see the errno set to 21 (Is a directory). By using printfs at various points in the code, I finally tracked down to strftime causing the change in errno. I dont know much about this function but does this have any potential of setting the errno to 21?

Code:
   if (clock_gettime(CLOCK_REALTIME, &time) == -1) {                   \
        sprintf(timestamp, "%s", <something>);           \
    //errno is 2
    } else {                                                            \
       //errno is 2
        ms_part = (time.tv_nsec/1000000L) % 1000L;                      \
           //errno is 2                                                             \
        (void)strftime(timestamp,                                       \
                       sizeof("Mon dd HH:MM:SS.msec : "),                \
                       "%b %d %H:%M:%S",                                \
                       localtime(&time.tv_sec));                        \
        //errno is 21!!        
    }

Last edited by akshay_satish; 11-26-2012 at 12:59 PM.
 
Old 11-26-2012, 01:27 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
man errno:

Quote:
Its [errno] value is significant only when the return value of the
call indicated an error (i.e., -1 from most system calls; -1 or NULL from most
library functions); a function that succeeds is allowed to change errno.
Quote:
A common mistake is to do
Code:
           if (somecall() == -1) {
               printf("somecall() failed\n");
               if (errno == ...) { ... }
           }
where errno no longer needs to have the value it had upon return from
somecall() (i.e., it may have been changed by the printf(3)). If the value of
errno should be preserved across a library call, it must be saved:
Code:
           if (somecall() == -1) {
               int errsv = errno;
               printf("somecall() failed\n");
               if (errsv == ...) { ... }
           }

Last edited by millgates; 11-26-2012 at 01:32 PM.
 
1 members found this post helpful.
  


Reply



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
strftime(buffer,50,"%d/%m/%Y %H:%M:%S",loctime); alaios Programming 3 09-09-2005 06:26 AM
strftime - 61 seconds?!?!?!? esnagel Linux - General 2 09-12-2002 07:06 PM

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

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

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