release.sh: restore -jJAILDIR option
[minix.git] / commands / elvis / prsvunix.c
blob4af722e666daf061888b6af66dfc01192a53d5fa
1 /* prsvunix.c */
3 /* This file contains the UNIX-specific parts of the "elvprsv" program. */
5 #if OSK
6 #define ELVPRSV
7 #include "osk.c"
8 #else
9 #include <sys/stat.h>
10 #include <pwd.h>
11 #endif
13 /* This variable is used to add extra error messages for mail sent to root */
14 char *ps;
16 /* This function returns the login name of the owner of a file */
17 char *ownername(filename)
18 char *filename; /* name of a file */
20 struct stat st;
21 struct passwd *pw;
23 /* stat the file, to get its uid */
24 if (stat(filename, &st) < 0)
26 ps = "stat() failed";
27 return "root";
30 /* get the /etc/passwd entry for that user */
31 pw = getpwuid(st.st_uid);
32 if (!pw)
34 ps = "uid not found in password file";
35 return "root";
38 /* return the user's name */
39 return pw->pw_name;
43 /* This function sends a mail message to a given user, saying that a file
44 * has been preserved.
46 void mail(user, file, when)
47 char *user; /* name of user who should receive the mail */
48 char *file; /* name of original text file that was preserved */
49 char *when; /* description of why the file was preserved */
51 char cmd[80];/* buffer used for constructing a "mail" command */
52 FILE *m, *popen(); /* stream used for giving text to the "mail" program */
53 char *base; /* basename of the file */
55 /* separate the directory name from the basename. */
56 for (base = file + strlen(file); --base > file && *base != SLASH; )
59 if (*base == SLASH)
61 *base++ = '\0';
64 /* for anonymous buffers, pretend the name was "foo" */
65 if (!strcmp(base, "*"))
67 base = "foo";
70 /* open a pipe to the "mail" program */
71 #if OSK
72 sprintf(cmd, "mail \"-s=%s preserved!\" %s", base, user);
73 #else /* ANY_UNIX */
74 sprintf(cmd, "mail %s >/dev/null 2>/dev/null", user);
75 #endif
76 m = popen(cmd, "w");
77 if (!m)
79 /* Can't send mail! Hope the user figures it out. */
80 return;
83 /* Tell the user that the file was preserved */
84 fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
85 fprintf(m, "was preserved when %s.\n", when);
86 fprintf(m, "To recover this file, do the following:\n");
87 fprintf(m, "\n");
88 #if OSK
89 fprintf(m, " chd %s\n", file);
90 #else /* ANY_UNIX */
91 fprintf(m, " cd %s\n", file);
92 #endif
93 fprintf(m, " elvrec %s\n", base);
94 fprintf(m, "\n");
95 fprintf(m, "With fond wishes for a speedy recovery,\n");
96 fprintf(m, " Elvis\n");
97 if (ps)
99 fprintf(m, "\nP.S. %s\n", ps);
100 ps = (char *)0;
103 /* close the stream */
104 pclose(m);