ccplusplus.com
Learn C, C++ Concepts
Saturday, October 8, 2011
reentrant functions in c
/************************************************************************************** * File : call-reentrant-function.c * Author : Saurabh Gupta * Desc : reentrant functions in c example * Source : http://saurabhgupta0527.blogspot.com/2011/08/posix-signal-tutorial.html * Created : PM 01:29 08 October 2011 * Note : Due to security exposure I am not showing the output as it need to * change my security settings of the linux box. You can run and play * locally on your linux box **************************************************************************************/ #include
/* some systems still require this */ #include
#include
#include
#include
/* for convenience */ #include
/* for convenience */ #include
/* for offsetof */ #include
/* for convenience */ #include
/* for convenience */ #include
/* for SIG_ERR */ #include
#include
#define MAXLINE 4096 /* max line length */ void err_sys(const char *, ...); static void my_alarm(int); static void err_doit(int, int, const char *, va_list); static void my_alarm(int signo) { struct passwd *rootptr; printf("in signal handler\n"); if ((rootptr = getpwnam("root")) == NULL) { err_sys("getpwnam(root) error"); } else { printf ("root pwd not null\n"); } alarm(1); } /* * Print a message and return to caller. * Caller specifies "errnoflag". */ static void err_doit(int errnoflag, int error, const char *fmt, va_list ap) { char buf[MAXLINE]; vsnprintf(buf, MAXLINE, fmt, ap); if (errnoflag) snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s", strerror(error)); strcat(buf, "\n"); fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(NULL); /* flushes all stdio output streams */ } void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(1, errno, fmt, ap); va_end(ap); exit(1); } int main(void) { struct passwd *ptr; //signal(SIGALRM, my_alarm); //alarm(1); for ( ; ; ) { if ((ptr = getpwnam("sgupta")) == NULL) { err_sys("getpwnam error"); } if (strcmp(ptr->pw_name, "sgupta") != 0) { printf("return value corrupted!, pw_name = %s\n", ptr->pw_name); } } }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment