|
|
1.1 root 1: /*
2: prmail [-l username] [-f fromaddress] [-c] [-r]
3:
4: Print/forward mail on this machine to another machine.
5: If no options specified, the current user's mail is printed
6: on stdout and not removed.
7:
8: Preferred usage (Has two modes):
9: Mail check-
10: A message is printed that there is mail.
11: This is intended for people who put netmail -c addr
12: in their .login files, and don't want to
13: be prompted for their password.
14: Mail check is indicated by a -c option.
15: Mail forward-
16: The mail is "mailed" to the fetcher.
17: If the -r option is given, the mail is also
18: appended to the mbox file and then removed.
19: Forward is indicated by the lack of a -c option.
20:
21: Options:
22: -l username read username's mail
23: -f fromaddress forward mail to this address
24: -r remove mail after forwarding
25: -c is a mail check, don't forward mail.
26:
27: Exit Codes:
28: 0 on normal conditions
29: 101 if can't exec mail program
30: 102 if can't get uid/name
31: 103 if can't open mail file, once it is stat'ed.
32:
33: */
34:
35: # include "defs.h"
36:
37: /*
38: mail seems to reside in one of three places:
39: 1. the user's home directory/.mail
40: 2. /usr/mail/username
41: 3. /usr/spool/mail/username
42: the conditional compilation flags for these three forms are:
43: 1. OLDMAIL
44: 2. USRMAIL
45: 3. is the default
46: */
47: # ifdef USRMAIL
48: # define MAILDIR "/usr/mail"
49: # else
50: # define MAILDIR "/usr/spool/mail"
51: # endif
52:
53: main(argc,argv)
54: char **argv;
55: {
56: long ltimeMail;
57: struct stat statbuf;
58: FILE *f;
59: char fn[BUFSIZ],buf[BUFSIZ],outbuf[BUFSIZ],*s,username[20],
60: fromaddress[BUFSIZ],toaddress[BUFSIZ],rmcmd[BUFSIZ],removemail=0,
61: fMailCheck=0, *stimeMail;
62: int i,ret,pid;
63: fromaddress[0] = 0;
64: username[0] = 0;
65: setbuf(stdout,outbuf);
66: while(argc > 1){
67: argc--, argv++;
68: if(argv[0][0] == '-'){
69: switch(argv[0][1]){
70: case 'c': fMailCheck++; break;
71: case 'f': harg(fromaddress,&argc,&argv); break;
72: case 'l': harg(username,&argc,&argv); break;
73: case 'r': removemail++; break;
74: /* it is important to ignore unknown flags for
75: compatibilty reasons */
76: }
77: }
78: }
79:
80: /* get the name of the user who's mail we're reading */
81: if(username[0] == 0){
82: s = SnCurrent();
83: if(s == NULL){
84: fprintf(stderr,"Unknown user\n");
85: exit(102);
86: }
87: strcpy(username,s);
88: }
89:
90: # ifdef OLDMAIL
91: /* handle mail directory in user's directory */
92: sprintf(fn,"%s/.mail",getenv("HOME"));
93: # else
94: sprintf(fn,"%s/%s",MAILDIR,username);
95: # endif
96: sprintf(toaddress,"%s:forward", longname(local));
97: if(fromaddress[0] != 0){
98:
99: /* don't send anything back if nothing to send */
100: if(stat(fn,&statbuf) < 0 || getsize(&statbuf) == 0L) exit(0);
101:
102: /* if a mail check, print message and exit */
103: if(fMailCheck){
104: ltimeMail = statbuf.st_mtime;
105: stimeMail = ctime(<imeMail);
106: stimeMail[strlen(stimeMail) - 6] = 0;
107: printf(
108: "\"%s\" has mail on the %s machine. \nLast updated on %s. \n",
109: username,longname(local),stimeMail);
110: printf("File %s:%s, Length %ld characters. \n",
111: longname(local),fn,getsize(&statbuf));
112: exit(0);
113: }
114:
115: /* read the mail and mail it to the account asking for it */
116: f = freopen(fn,"r",stdin);
117: if(f == NULL){
118: perror(fn);
119: exit(103);
120: }
121: while((pid = fork()) == -1);
122: if(pid == 0){
123: /* send to the person who sent this to us */
124: /* system mail must know about remote mailing */
125: mexecl(SYSMAIL1,"mail", fromaddress, 0);
126: mexecl(SYSMAIL2,"mail", fromaddress, 0);
127: mexecl(SYSMAIL3,"mail", fromaddress, 0);
128: exit(101);
129: }
130: /* parent */
131: fclose(stdin);
132: wait(&ret);
133: ret >>= 8;
134: if(removemail){
135: if(ret == 0){
136: ret = RcAppendMail(fn,username);
137: if(ret == 0){
138: sprintf(rmcmd,"rm -f %s",fn);
139: ret = system(rmcmd);
140: ret >>= 8;
141: }
142: }
143: if(ret != 0)fprintf(stderr,"Mail not removed\n");
144: }
145: exit(ret);
146: }
147:
148: /* this [archaic] section if forwarding address not specified */
149: if(stat(fn,&statbuf) < 0 || getsize(&statbuf) == 0L){
150: printf("No mail.\n");
151: exit(0);
152: }
153: f = fopen(fn,"r");
154: if(f == NULL){
155: perror(fn);
156: exit(1);
157: }
158: while((i = fread(buf,1,BUFSIZ,f)) > 0)
159: fwrite(buf,1,i,stdout);
160: fclose(f);
161: exit(0);
162: }
163: /*
164: RcAppendMail(fnFrom) returns a return code
165:
166: Copy mail from fnFrom to the end of the mbox file in the user's
167: home directory.
168: Returns 1 if error, 0 if ok.
169: Can't use getenv() because if there's no entry in utmp
170: for machines with multiple names per uid, the getenv() will
171: return the homedir of the first name/uid pair it finds.
172: */
173: RcAppendMail(fnFrom,sn)
174: char *fnFrom;
175: char *sn;
176: {
177: FILE *fdFrom, *fdTo;
178: char *shdir, fnTo[BUFSIZ], sBuf[BUFSIZ];
179: int nchar;
180:
181: # ifdef MULTNAMS
182: struct passwd *pwd;
183:
184: pwd = getpwnam(sn);
185: if(pwd == NULL)return(1);
186: shdir = pwd->pw_dir;
187: # else
188: shdir = getenv("HOME");
189: if(shdir == NULL)return(1);
190: # endif
191: sprintf(fnTo,"%s/mbox",shdir);
192: fdTo = fopen(fnTo,"a");
193: if(fdTo == NULL){
194: perror(fnTo);
195: return(1);
196: }
197:
198: fdFrom = fopen(fnFrom,"r");
199: if(fdFrom == NULL){
200: perror(fdFrom);
201: return(1);
202: }
203:
204: while((nchar = fread(sBuf,1,BUFSIZ,fdFrom)) > 0){
205: if(fwrite(sBuf,1,nchar,fdTo) != nchar){
206: perror(fnTo);
207: return(1);
208: }
209: }
210: fclose(fdFrom);
211: fclose(fdTo);
212: return(0);
213: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.