📄 unix.c
字号:
/* * Program: UNIX mail routines * * Author: Mark Crispin * Networks and Distributed Computing * Computing & Communications * University of Washington * Administration Building, AG-44 * Seattle, WA 98195 * Internet: MRC@CAC.Washington.EDU * * Date: 20 December 1989 * Last Edited: 1 September 2000 * * Copyright 2000 by the University of Washington * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, provided * that the above copyright notice appears in all copies and that both the * above copyright notice and this permission notice appear in supporting * documentation, and that the name of the University of Washington not be * used in advertising or publicity pertaining to distribution of the software * without specific, written prior permission. This software is made * available "as is", and * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * *//* DEDICATION * * This file is dedicated to my dog, Unix, also known as Yun-chan and * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after * a two-month bout with cirrhosis of the liver. * * He was a dear friend, and I miss him terribly. * * Lift a leg, Yunie. Luv ya forever!!!! */#include <stdio.h>#include <ctype.h>#include <errno.h>extern int errno; /* just in case */#include <signal.h>#include "mail.h"#include "osdep.h"#include <time.h>#include <sys/stat.h>#include "unix.h"#include "pseudo.h"#include "fdstring.h"#include "misc.h"#include "dummy.h"/* UNIX mail routines *//* Driver dispatch used by MAIL */DRIVER unixdriver = { "unix", /* driver name */ DR_LOCAL|DR_MAIL, /* driver flags */ (DRIVER *) NIL, /* next driver */ unix_valid, /* mailbox is valid for us */ unix_parameters, /* manipulate parameters */ unix_scan, /* scan mailboxes */ unix_list, /* list mailboxes */ unix_lsub, /* list subscribed mailboxes */ NIL, /* subscribe to mailbox */ NIL, /* unsubscribe from mailbox */ unix_create, /* create mailbox */ unix_delete, /* delete mailbox */ unix_rename, /* rename mailbox */ NIL, /* status of mailbox */ unix_open, /* open mailbox */ unix_close, /* close mailbox */ NIL, /* fetch message "fast" attributes */ NIL, /* fetch message flags */ NIL, /* fetch overview */ NIL, /* fetch message envelopes */ unix_header, /* fetch message header */ unix_text, /* fetch message text */ NIL, /* fetch partial message text */ NIL, /* unique identifier */ NIL, /* message number */ NIL, /* modify flags */ unix_flagmsg, /* per-message modify flags */ NIL, /* search for message based on criteria */ NIL, /* sort messages */ NIL, /* thread messages */ unix_ping, /* ping mailbox to see if still alive */ unix_check, /* check for new messages */ unix_expunge, /* expunge deleted messages */ unix_copy, /* copy messages to another mailbox */ unix_append, /* append string message to mailbox */ NIL /* garbage collect stream */}; /* prototype stream */MAILSTREAM unixproto = {&unixdriver}; /* driver parameters */static long unix_fromwidget = T;/* UNIX mail validate mailbox * Accepts: mailbox name * Returns: our driver if name is valid, NIL otherwise */DRIVER *unix_valid (char *name){ int fd; DRIVER *ret = NIL; char *t,file[MAILTMPLEN]; struct stat sbuf; time_t tp[2]; errno = EINVAL; /* assume invalid argument */ /* must be non-empty file */ if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) { if (!sbuf.st_size)errno = 0;/* empty file */ else if ((fd = open (file,O_RDONLY,NIL)) >= 0) { /* OK if mailbox format good */ if (unix_isvalid_fd (fd)) ret = &unixdriver; else errno = -1; /* invalid format */ close (fd); /* close the file */ tp[0] = sbuf.st_atime; /* preserve atime and mtime */ tp[1] = sbuf.st_mtime; utime (file,tp); /* set the times */ } } /* in case INBOX but not unix format */ else if ((errno == ENOENT) && ((name[0] == 'I') || (name[0] == 'i')) && ((name[1] == 'N') || (name[1] == 'n')) && ((name[2] == 'B') || (name[2] == 'b')) && ((name[3] == 'O') || (name[3] == 'o')) && ((name[4] == 'X') || (name[4] == 'x')) && !name[5]) errno = -1; return ret; /* return what we should */}/* UNIX mail test for valid mailbox * Accepts: file descriptor * scratch buffer * Returns: T if valid, NIL otherwise */long unix_isvalid_fd (int fd){ int zn; int ret = NIL; char tmp[MAILTMPLEN],*s,*t,c = '\n'; memset (tmp,'\0',MAILTMPLEN); if (read (fd,tmp,MAILTMPLEN-1) >= 0) { for (s = tmp; (*s == '\r') || (*s == '\n') || (*s == ' ') || (*s == '\t');) c = *s++; if (c == '\n') VALID (s,t,ret,zn); } return ret; /* return what we should */}/* UNIX manipulate driver parameters * Accepts: function code * function-dependent value * Returns: function-dependent return value */void *unix_parameters (long function,void *value){ void *ret = NIL; switch ((int) function) { case SET_FROMWIDGET: unix_fromwidget = (long) value; case GET_FROMWIDGET: ret = (void *) unix_fromwidget; break; } return ret;}/* UNIX mail scan mailboxes * Accepts: mail stream * reference * pattern to search * string to scan */void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents){ if (stream) dummy_scan (NIL,ref,pat,contents);}/* UNIX mail list mailboxes * Accepts: mail stream * reference * pattern to search */void unix_list (MAILSTREAM *stream,char *ref,char *pat){ if (stream) dummy_list (NIL,ref,pat);}/* UNIX mail list subscribed mailboxes * Accepts: mail stream * reference * pattern to search */void unix_lsub (MAILSTREAM *stream,char *ref,char *pat){ if (stream) dummy_lsub (NIL,ref,pat);}/* UNIX mail create mailbox * Accepts: MAIL stream * mailbox name to create * Returns: T on success, NIL on failure */long unix_create (MAILSTREAM *stream,char *mailbox){ char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN]; long ret = NIL; int i,fd; time_t ti = time (0); if (!(s = dummy_file (mbx,mailbox))) { sprintf (tmp,"Can't create %.80s: invalid name",mailbox); mm_log (tmp,ERROR); } /* create underlying file */ else if (dummy_create_path (stream,s)) { /* done if made directory */ if ((s = strrchr (s,'/')) && !s[1]) return T; if ((fd = open (mbx,O_WRONLY, (int) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) { sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno)); mm_log (tmp,ERROR); unlink (mbx); /* delete the file */ } /* in case a whiner with no life */ else if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T; else { /* initialize header */ memset (tmp,'\0',MAILTMPLEN); sprintf (tmp,"From %s %sDate: ",pseudo_from,ctime (&ti)); rfc822_fixed_date (s = tmp + strlen (tmp)); /* write the pseudo-header */ sprintf (s += strlen (s), "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000", pseudo_name,pseudo_from,mylocalhost (),pseudo_subject, (unsigned long) ti); for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i)) sprintf (s += strlen (s)," %s",default_user_flag (i)); sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg); if ((write (fd,tmp,strlen (tmp)) < 0) || close (fd)) { sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx, strerror (errno)); mm_log (tmp,ERROR); unlink (mbx); /* delete the file */ } else ret = T; /* success */ } close (fd); /* close file, set proper protections */ } return ret ? set_mbx_protections (mailbox,mbx) : NIL;}/* UNIX mail delete mailbox * Accepts: MAIL stream * mailbox name to delete * Returns: T on success, NIL on failure */long unix_delete (MAILSTREAM *stream,char *mailbox){ return unix_rename (stream,mailbox,NIL);}/* UNIX mail rename mailbox * Accepts: MAIL stream * old mailbox name * new mailbox name (or NIL for delete) * Returns: T on success, NIL on failure */long unix_rename (MAILSTREAM *stream,char *old,char *newname){ long ret = NIL; char c,*s = NIL; char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN]; DOTLOCK lockx; int fd,ld; long i; struct stat sbuf; mm_critical (stream); /* get the c-client lock */ if (newname && !((s = dummy_file (tmp,newname)) && *s)) sprintf (tmp,"Can't rename mailbox %.80s to %.80s: invalid name", old,newname); /* lock out other c-clients */ else if ((ld = lockname (lock,dummy_file (file,old),LOCK_EX|LOCK_NB,&i)) < 0) sprintf (tmp,"Mailbox %.80s is in use by another process",old); else { if ((fd = unix_lock (file,O_RDWR,S_IREAD|S_IWRITE,&lockx,LOCK_EX)) < 0) sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno)); else { if (newname) { /* want rename? */ /* found superior to destination name? */ if (s = strrchr (s,'/')) { c = *++s; /* remember first character of inferior */ *s = '\0'; /* tie off to get just superior */ /* name doesn't exist, create it */ if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && !dummy_create (stream,newname)) { unix_unlock (fd,NIL,&lockx); unix_unlock (ld,NIL,NIL); unlink (lock); mm_nocritical (stream); return ret; /* return success or failure */ } *s = c; /* restore full name */ } if (rename (file,tmp)) sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname, strerror (errno)); else ret = T; /* set success */ } else if (unlink (file)) sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno)); else ret = T; /* set success */ unix_unlock (fd,NIL,&lockx); } unix_unlock (ld,NIL,NIL); /* flush the lock */ unlink (lock); } mm_nocritical (stream); /* no longer critical */ if (!ret) mm_log (tmp,ERROR); /* log error */ return ret; /* return success or failure */}/* UNIX mail open * Accepts: Stream to open * Returns: Stream on success, NIL on failure */MAILSTREAM *unix_open (MAILSTREAM *stream){ long i; int fd; char tmp[MAILTMPLEN]; DOTLOCK lock; long retry; /* return prototype for OP_PROTOTYPE call */ if (!stream) return user_flags (&unixproto); retry = stream->silent ? 1 : KODRETRY; if (stream->local) fatal ("unix recycle stream"); stream->local = memset (fs_get (sizeof (UNIXLOCAL)),0,sizeof (UNIXLOCAL)); /* note if an INBOX or not */ stream->inbox = !strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX"); /* canonicalize the stream mailbox name */ dummy_file (tmp,stream->mailbox); /* flush old name */ fs_give ((void **) &stream->mailbox); /* save canonical name */ stream->mailbox = cpystr (tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -