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 | extern struct passwd *getpwuid();
|
---|
13 |
|
---|
14 | /* This variable is used to add extra error messages for mail sent to root */
|
---|
15 | char *ps;
|
---|
16 |
|
---|
17 | /* This function returns the login name of the owner of a file */
|
---|
18 | char *ownername(filename)
|
---|
19 | char *filename; /* name of a file */
|
---|
20 | {
|
---|
21 | struct stat st;
|
---|
22 | struct passwd *pw;
|
---|
23 |
|
---|
24 | /* stat the file, to get its uid */
|
---|
25 | if (stat(filename, &st) < 0)
|
---|
26 | {
|
---|
27 | ps = "stat() failed";
|
---|
28 | return "root";
|
---|
29 | }
|
---|
30 |
|
---|
31 | /* get the /etc/passwd entry for that user */
|
---|
32 | pw = getpwuid(st.st_uid);
|
---|
33 | if (!pw)
|
---|
34 | {
|
---|
35 | ps = "uid not found in password file";
|
---|
36 | return "root";
|
---|
37 | }
|
---|
38 |
|
---|
39 | /* return the user's name */
|
---|
40 | return pw->pw_name;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /* This function sends a mail message to a given user, saying that a file
|
---|
45 | * has been preserved.
|
---|
46 | */
|
---|
47 | void mail(user, file, when)
|
---|
48 | char *user; /* name of user who should receive the mail */
|
---|
49 | char *file; /* name of original text file that was preserved */
|
---|
50 | char *when; /* description of why the file was preserved */
|
---|
51 | {
|
---|
52 | char cmd[80];/* buffer used for constructing a "mail" command */
|
---|
53 | FILE *m, *popen(); /* stream used for giving text to the "mail" program */
|
---|
54 | char *base; /* basename of the file */
|
---|
55 |
|
---|
56 | /* separate the directory name from the basename. */
|
---|
57 | for (base = file + strlen(file); --base > file && *base != SLASH; )
|
---|
58 | {
|
---|
59 | }
|
---|
60 | if (*base == SLASH)
|
---|
61 | {
|
---|
62 | *base++ = '\0';
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* for anonymous buffers, pretend the name was "foo" */
|
---|
66 | if (!strcmp(base, "*"))
|
---|
67 | {
|
---|
68 | base = "foo";
|
---|
69 | }
|
---|
70 |
|
---|
71 | /* open a pipe to the "mail" program */
|
---|
72 | #if OSK
|
---|
73 | sprintf(cmd, "mail \"-s=%s preserved!\" %s", base, user);
|
---|
74 | #else /* ANY_UNIX */
|
---|
75 | sprintf(cmd, "mail %s >/dev/null 2>/dev/null", user);
|
---|
76 | #endif
|
---|
77 | m = popen(cmd, "w");
|
---|
78 | if (!m)
|
---|
79 | {
|
---|
80 | /* Can't send mail! Hope the user figures it out. */
|
---|
81 | return;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* Tell the user that the file was preserved */
|
---|
85 | fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
|
---|
86 | fprintf(m, "was preserved when %s.\n", when);
|
---|
87 | fprintf(m, "To recover this file, do the following:\n");
|
---|
88 | fprintf(m, "\n");
|
---|
89 | #if OSK
|
---|
90 | fprintf(m, " chd %s\n", file);
|
---|
91 | #else /* ANY_UNIX */
|
---|
92 | fprintf(m, " cd %s\n", file);
|
---|
93 | #endif
|
---|
94 | fprintf(m, " elvrec %s\n", base);
|
---|
95 | fprintf(m, "\n");
|
---|
96 | fprintf(m, "With fond wishes for a speedy recovery,\n");
|
---|
97 | fprintf(m, " Elvis\n");
|
---|
98 | if (ps)
|
---|
99 | {
|
---|
100 | fprintf(m, "\nP.S. %s\n", ps);
|
---|
101 | ps = (char *)0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* close the stream */
|
---|
105 | pclose(m);
|
---|
106 | }
|
---|