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