home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / prsvunix.c < prev    next >
C/C++ Source or Header  |  1994-03-26  |  3KB  |  136 lines

  1. /* prsvunix.c */
  2.  
  3. /* This file contains the UNIX-specific parts of the "elvprsv" program. */
  4.  
  5. #if OSK
  6. # define ELVPRSV
  7. # include "osk.c"
  8. #else
  9. # include <sys/stat.h>
  10. # include <pwd.h>
  11. #endif
  12. #ifndef __STDC__
  13. /* some older systems don't declare this in pwd.h, I guess. */
  14. extern struct passwd *getpwuid();
  15. #endif
  16.  
  17. /* This variable is used to add extra error messages for mail sent to root */
  18. char *ps;
  19.  
  20. /* This function returns the login name of the owner of a file */
  21. char *ownername(filename)
  22.     char    *filename;    /* name of a file */
  23. {
  24.     struct stat    st;
  25.     struct passwd    *pw;
  26.  
  27.     /* stat the file, to get its uid */
  28.     if (stat(filename, &st) < 0)
  29.     {
  30.         ps = "stat() failed";
  31.         return "root";
  32.     }
  33.  
  34.     /* get the /etc/passwd entry for that user */
  35.     pw = getpwuid(st.st_uid);
  36.     if (!pw)
  37.     {
  38.         ps = "uid not found in password file";
  39.         return "root";
  40.     }
  41.  
  42.     /* return the user's name */
  43.     return pw->pw_name;
  44. }
  45.  
  46.  
  47. /* This function sends a mail message to a given user, saying that a file
  48.  * has been preserved.
  49.  */
  50. void mail(user, file, when, tmp)
  51.     char    *user;    /* name of user who should receive the mail */
  52.     char    *file;    /* name of original text file that was preserved */
  53.     char    *when;    /* description of why the file was preserved */
  54.     char    *tmp;    /* NULL normally; else name of tmp file if user should run elvprsv -R */
  55. {
  56.     char    cmd[80];/* buffer used for constructing a "mail" command */
  57.     FILE    *m;    /* stream used for giving text to the "mail" program */
  58.     char    *base;    /* basename of the file */
  59.  
  60.     /* separate the directory name from the basename. */
  61.     for (base = file + strlen(file); --base > file && *base != SLASH; )
  62.     {
  63.     }
  64.     if (*base == SLASH)
  65.     {
  66.         *base++ = '\0';
  67.     }
  68.  
  69.     /* for anonymous buffers, pretend the name was "foo" */
  70.     if (!strcmp(base, "*"))
  71.     {
  72.         base = "foo";
  73.     }
  74.  
  75.     /* open a pipe to the "mail" program */
  76. #if OSK
  77.     sprintf(cmd, "%s \"-s=%s preserved!\" %s", MAILER, base, user);
  78. #else /* ANY_UNIX */
  79.     sprintf(cmd, "%s -s Graceland %s", MAILER, user);
  80.     switch (fork())
  81.     {
  82.       case -1: /* error */
  83.         return;
  84.  
  85.       case 0: /* child */
  86.         /* surrender any special privileges */
  87.         setuid(getuid());
  88.         break; /* continue with the rest of this function */
  89.  
  90.       default: /* parent */
  91.         wait(NULL);
  92.         return;
  93.     }
  94. #endif
  95.     m = popen(cmd, "w");
  96.     if (!m)
  97.     {
  98.         perror(cmd);
  99.         /* Can't send mail!  Hope the user figures it out. */
  100.         return;
  101.     }
  102.  
  103.     /* Tell the user that the file was preserved */
  104.     fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
  105.     fprintf(m, "was preserved when %s.\n", when);
  106.     fprintf(m, "To recover this file, do the following:\n");
  107.     fprintf(m, "\n");
  108. #if OSK
  109.     fprintf(m, "     chd %s\n", file);
  110. #else /* ANY_UNIX */
  111.     fprintf(m, "     cd %s\n", file);
  112. #endif
  113.     if (tmp)
  114.     {
  115.         fprintf(m, "     elvprsv %s\n", tmp);
  116.     }
  117.     else
  118.     {
  119.         fprintf(m, "     elvrec %s\n", base);
  120.     }
  121.     fprintf(m, "\n");
  122.     fprintf(m, "With fond wishes for a speedy recovery,\n");
  123.     fprintf(m, "                                    Elvis\n");
  124.     if (ps)
  125.     {
  126.         fprintf(m, "\nP.S. %s\n", ps);
  127.         ps = (char *)0;
  128.     }
  129.  
  130.     /* close the stream */
  131.     pclose(m);
  132. #if ANY_UNIX
  133.     exit(0);
  134. #endif
  135. }
  136.