📄 dummy.c
字号:
buf = (char *) fs_get (BUFSIZE + (ssiz = 4 * ((csiz / 4) + 1)) + 1); memset (buf,'\0',ssiz); /* no slop area the first time */ while (sbuf.st_size) { /* until end of file */ read (fd,buf+ssiz,bsiz = min (sbuf.st_size,BUFSIZE)); if (search ((unsigned char *) buf,bsiz+ssiz, (unsigned char *) contents,csiz)) break; memcpy (buf,buf+BUFSIZE,ssiz); sbuf.st_size -= bsiz; /* note that we read that much */ } fs_give ((void **) &buf); /* flush buffer */ close (fd); /* finished with file */ if (!sbuf.st_size) return T;/* not found */ } /* notify main program */ mm_list (stream,delimiter,name,attributes); return T;}/* Dummy create mailbox * Accepts: mail stream * mailbox name to create * Returns: T on success, NIL on failure */long dummy_create (MAILSTREAM *stream,char *mailbox){ char *s,tmp[MAILTMPLEN]; long ret = NIL; /* validate name */ if (!(strcmp (ucase (strcpy (tmp,mailbox)),"INBOX") && (s = dummy_file (tmp,mailbox)))) { sprintf (tmp,"Can't create %s: invalid name",mailbox); mm_log (tmp,ERROR); } /* create the name */ else if ((ret = dummy_create_path (stream,tmp)) && /* done if made directory */ (s = strrchr (s,'/')) && !s[1]) return T; return ret ? set_mbx_protections (mailbox,tmp) : NIL;}/* Dummy create path * Accepts: mail stream * path name name to create * Returns: T on success, NIL on failure */long dummy_create_path (MAILSTREAM *stream,char *path){ struct stat sbuf; char c,*s,tmp[MAILTMPLEN]; int fd; long ret = NIL; char *t = strrchr (path,'/'); int wantdir = t && !t[1]; if (wantdir) *t = '\0'; /* flush trailing delimiter for directory */ if (s = strrchr (path,'/')) { /* found superior to this name? */ c = *++s; /* remember first character of inferior */ *s = '\0'; /* tie off to get just superior */ /* name doesn't exist, create it */ if ((stat (path,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && !dummy_create_path (stream,path)) return NIL; *s = c; /* restore full name */ } if (wantdir) { /* want to create directory? */ ret = !mkdir (path,(int) mail_parameters (NIL,GET_DIRPROTECTION,NIL)); *t = '/'; /* restore directory delimiter */ } /* create file */ else if ((fd = open (path,O_WRONLY|O_CREAT|O_EXCL, (int) mail_parameters(NIL,GET_MBXPROTECTION,NIL))) >= 0) ret = !close (fd); if (!ret) { /* error? */ sprintf (tmp,"Can't create mailbox node %s: %s",path,strerror (errno)); mm_log (tmp,ERROR); } return ret; /* return status */}/* Dummy delete mailbox * Accepts: mail stream * mailbox name to delete * Returns: T on success, NIL on failure */long dummy_delete (MAILSTREAM *stream,char *mailbox){ struct stat sbuf; char *s,tmp[MAILTMPLEN]; /* no trailing / (workaround BSD kernel bug) */ if ((s = strrchr (dummy_file (tmp,mailbox),'/')) && !s[1]) *s = '\0'; if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) == S_IFDIR) ? rmdir (tmp) : unlink (tmp)) { sprintf (tmp,"Can't delete mailbox %s: %s",mailbox,strerror (errno)); mm_log (tmp,ERROR); return NIL; } return T; /* return success */}/* Mail rename mailbox * Accepts: mail stream * old mailbox name * new mailbox name * Returns: T on success, NIL on failure */long dummy_rename (MAILSTREAM *stream,char *old,char *newname){ struct stat sbuf; char c,*s,tmp[MAILTMPLEN],mbx[MAILTMPLEN]; /* no trailing / allowed */ if (!(s = dummy_file (mbx,newname)) || ((s = strrchr (s,'/')) && !s[1])) { sprintf (mbx,"Can't rename %s to %s: invalid name",old,newname); mm_log (mbx,ERROR); return NIL; } if (s) { /* found superior to destination name? */ c = *++s; /* remember first character of inferior */ *s = '\0'; /* tie off to get just superior */ /* name doesn't exist, create it */ if ((stat (mbx,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && !dummy_create (stream,mbx)) return NIL; *s = c; /* restore full name */ } /* rename of non-ex INBOX creates dest */ if (!strcmp (ucase (strcpy (tmp,old)),"INBOX") && stat (dummy_file (tmp,old),&sbuf)) return dummy_create (NIL,mbx); if (rename (dummy_file (tmp,old),mbx)) { sprintf (tmp,"Can't rename mailbox %s to %s: %s",old,newname, strerror (errno)); mm_log (tmp,ERROR); return NIL; } return T; /* return success */}/* Dummy open * Accepts: stream to open * Returns: stream on success, NIL on failure */MAILSTREAM *dummy_open (MAILSTREAM *stream){ int fd; char err[MAILTMPLEN],tmp[MAILTMPLEN]; struct stat sbuf; /* OP_PROTOTYPE call */ if (!stream) return &dummyproto; err[0] = '\0'; /* no error message yet */ /* can we open the file? */ if ((fd = open (dummy_file (tmp,stream->mailbox),O_RDONLY,NIL)) < 0) { /* no, error unless INBOX */ if (strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX")) sprintf (err,"%s: %s",strerror (errno),stream->mailbox); } else { /* file had better be empty then */ fstat (fd,&sbuf); /* sniff at its size */ close (fd); if ((sbuf.st_mode & S_IFMT) != S_IFREG) sprintf (err,"Can't open %s: not a selectable mailbox",stream->mailbox); else if (sbuf.st_size) /* bogus format if non-empty */ sprintf (err,"Can't open %s (file %s): not in valid mailbox format", stream->mailbox,tmp); } if (err[0]) { /* if an error happened */ mm_log (err,stream->silent ? WARN : ERROR); return NIL; } else if (!stream->silent) { /* only if silence not requested */ mail_exists (stream,0); /* say there are 0 messages */ mail_recent (stream,0); /* and certainly no recent ones! */ stream->uid_validity = 1; } stream->inbox = T; /* note that it's an INBOX */ return stream; /* return success */}/* Dummy close * Accepts: MAIL stream * options */void dummy_close (MAILSTREAM *stream,long options){ /* return silently */}/* Dummy ping mailbox * Accepts: MAIL stream * Returns: T if stream alive, else NIL */long dummy_ping (MAILSTREAM *stream){ /* time to do another test? */ if (time (0) >= (stream->gensym + 30)) { MAILSTREAM *test = mail_open (NIL,stream->mailbox,OP_PROTOTYPE); if (!test) return NIL; /* can't get a prototype?? */ if (test->dtb == stream->dtb) { stream->gensym = time (0);/* still hasn't changed */ return T; /* try again later */ } /* looks like a new driver? */ if (!(test = mail_open (NIL,stream->mailbox,NIL))) return NIL; mail_close ((MAILSTREAM *) /* flush resources used by dummy stream */ memcpy (fs_get (sizeof (MAILSTREAM)),stream, sizeof (MAILSTREAM))); /* swap the streams */ memcpy (stream,test,sizeof (MAILSTREAM)); fs_give ((void **) &test); /* flush test now that copied */ } return T;}/* Dummy check mailbox * Accepts: MAIL stream * No-op for readonly files, since read/writer can expunge it from under us! */void dummy_check (MAILSTREAM *stream){ dummy_ping (stream); /* invoke ping */}/* Dummy expunge mailbox * Accepts: MAIL stream */void dummy_expunge (MAILSTREAM *stream){ /* return silently */}/* Dummy copy message(s) * Accepts: MAIL stream * sequence * destination mailbox * options * Returns: T if copy successful, else NIL */long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options){ if ((options & CP_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence)) fatal ("Impossible dummy_copy"); return NIL;}/* Dummy append message string * Accepts: mail stream * destination mailbox * append callback function * data for callback * Returns: T on success, NIL on failure */long dummy_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data){ struct stat sbuf; int fd = -1; int e; char tmp[MAILTMPLEN]; MAILSTREAM *ts = default_proto (T); if ((strcmp (ucase (strcpy (tmp,mailbox)),"INBOX")) && ((fd = open (dummy_file (tmp,mailbox),O_RDONLY,NIL)) < 0)) { if ((e = errno) == ENOENT) /* failed, was it no such file? */ mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL); sprintf (tmp,"%s: %s",strerror (e),mailbox); mm_log (tmp,ERROR); /* pass up error */ return NIL; /* always fails */ } if (fd >= 0) { /* found file? */ fstat (fd,&sbuf); /* get its size */ close (fd); /* toss out the fd */ if (sbuf.st_size) ts = NIL; /* non-empty file? */ } if (ts) return (*ts->dtb->append) (stream,mailbox,af,data); sprintf (tmp,"Indeterminate mailbox format: %s",mailbox); mm_log (tmp,ERROR); return NIL;}/* Dummy mail generate file string * Accepts: temporary buffer to write into * mailbox name string * Returns: local file string or NIL if failure */char *dummy_file (char *dst,char *name){ char *s = mailboxfile (dst,name); /* return our standard inbox */ return (s && !*s) ? strcpy (dst,sysinbox ()) : s;}/* Dummy canonicalize name * Accepts: buffer to write name * reference * pattern * Returns: T if success, NIL if failure */long dummy_canonicalize (char *tmp,char *ref,char *pat){ if (ref) { /* preliminary reference check */ if (*ref == '{') return NIL;/* remote reference not allowed */ else if (!*ref) ref = NIL; /* treat empty reference as no reference */ } switch (*pat) { case '#': /* namespace name */ if (mailboxfile (tmp,pat)) strcpy (tmp,pat); else return NIL; /* unknown namespace */ break; case '{': /* remote names not allowed */ return NIL; case '/': /* rooted name */ case '~': /* home directory name */ if (!ref || (*ref != '#')) {/* non-namespace reference? */ strcpy (tmp,pat); /* yes, ignore */ break; } /* fall through */ default: /* apply reference for all other names */ if (!ref) strcpy (tmp,pat); /* just copy if no namespace */ else if ((*ref != '#') || mailboxfile (tmp,ref)) { /* wants root of name? */ if (*pat == '/') strcpy (strchr (strcpy (tmp,ref),'/'),pat); /* otherwise just append */ else sprintf (tmp,"%s%s",ref,pat); } else return NIL; /* unknown namespace */ } return T;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -