home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / msgd / Common / answerback.c next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  2.0 KB  |  75 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /fsys4/usr/source/users/msg/Common/RCS/answerback.c,v 1.2 90/12/29 21:59:53 jmsellen Exp $";
  3. #endif
  4.  
  5. #if 0
  6. /*
  7.  * $Log:    answerback.c,v $
  8.  * Revision 1.2  90/12/29  21:59:53  jmsellen
  9.  * Use ANSBACK define from Include/msg.h
  10.  * 
  11.  * Revision 1.1  87/08/06  19:06:19  sahayman
  12.  * Initial revision
  13.  * 
  14.  */
  15. #endif
  16.  
  17. /* answerback.c
  18.    Return a user's answerback message to the person who sent him a
  19.    message.
  20. */
  21.  
  22. #include "msg.h"
  23. #include <pwd.h>
  24.  
  25. #define MYBUFSIZ    (BUFSIZ/2)    /* BUFSIZ is max errmsg size */
  26.  
  27.  
  28. /* We may be sending to multiple instances of the same user, so it makes
  29.    since to remember the last user we looked up and his/her UID to avoid
  30.    searching the passwd file again. */
  31.  
  32. answerback( errcode, user, line )
  33. int errcode;
  34. char *user;
  35. char *line;
  36. {
  37.     static char lastuser[25];    /* too hard to use sizeof() */
  38.     static int lastuid = -1;
  39.     struct stat sbuf;
  40.     FILE *fp;
  41.     char answerbuf[sizeof(ANSBACK)+25];  /* lots of extra room */
  42.     /* find name of answerback file */
  43.     (void) sprintf( answerbuf, "%s%s", ANSBACK, line );
  44.     /* Now make sure that if the answerback file exists, it belongs to
  45.        the user we're sending to */
  46.     if ( stat( answerbuf, &sbuf ) == -1 )
  47.     return;        /* no answerback exists */
  48.     if ( strcmp( user, lastuser ) != 0 ) {
  49.     /* we don't know this guy, so look him up */
  50.     struct passwd *pw = getpwnam( user );
  51.     endpwent();
  52.     if ( pw == (struct passwd *)NULL )
  53.         /* I know this guy exists, but ignore it */
  54.         return;
  55.     (void) strcpy( lastuser, user );
  56.     lastuid = pw->pw_uid;
  57.     }
  58.     if ( sbuf.st_uid != lastuid )
  59.     return;        /* not his file */
  60.     /* we don't care if we can't read his message - it's his problem */
  61.     if ( (fp=fopen(answerbuf,"r")) != FPNULL ) {
  62.     char buf[MYBUFSIZ+1];
  63.     int len;
  64.     if ( (len=fread( buf, 1, MYBUFSIZ, fp )) > 0 ) {
  65.         if ( buf[len-1] =='\n' ) len--;
  66.         buf[len] = '\0'; /* make it terminated */
  67.         if ( errcode == ANS_WARN )
  68.         errmsg( errcode, user, line, buf );
  69.         else
  70.         errmsg( errcode, user, hostname, line, buf );
  71.     }
  72.     (void) fclose( fp );
  73.     }
  74. }
  75.