|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T p
Length: 6592 (0x19c0)
Types: TextFile
Names: »pop_updt.c«
└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
└─⟦ac17ffda3⟧ »EurOpenD3/mail/popper-1.7b1.tar.Z«
└─⟦d6ac5563a⟧
└─⟦this⟧ »pop_updt.c«
/*
* Copyright (c) 1989 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1989 Regents of the University of California.\n\ All rights reserved.\n";
static char SccsId[] = "@(#)pop_updt.c 1.5 5/15/90";
#endif not lint
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "popper.h"
extern int errno;
static char standard_error[] =
"Error error updating primary drop. Mailbox unchanged";
/*
* updt: Apply changes to a user's POP maildrop
*/
int pop_updt (p)
POP * p;
{
FILE * md; /* Stream pointer for
the user's maildrop */
int mfd; /* File descriptor for
above */
struct stat drop_stat; /* Mail drop file
status information */
char buffer[BUFSIZ]; /* Read buffer */
MsgInfoList * mp; /* Pointer to message
info list */
register int msg_num; /* Current message
counter */
register int msg_lines; /* Current message
line counter */
register int status_written; /* Status header field
written */
register int new_msgs_arrived; /* New messages flag */
int nchar; /* Bytes read/written */
long offset; /* New mail offset */
#ifdef DEBUG
if (p->debug) {
pop_log(p,POP_DEBUG,"Performing maildrop update...");
pop_log(p,POP_DEBUG,"Checking to see if all messages were deleted");
}
#endif DEBUG
if (p->msgs_deleted == p->msg_count) {
/* Truncate before close, to avoid race condition, DO NOT UNLINK!
Another process may have opened, and not yet tried to lock */
ftruncate (fileno(p->drop),0);
fclose(p->drop) ;
return (POP_SUCCESS);
}
#ifdef DEBUG
if (p->debug)
pop_log(p,POP_DEBUG,"Opening mail drop \"%s\"",p->drop_name);
#endif DEBUG
/* Open the user's real maildrop */
if ((mfd = open(p->drop_name,O_RDWR|O_CREAT,0666)) == -1 ||
(md = fdopen(mfd,"r+")) == NULL) {
return pop_msg(p,POP_FAILURE,standard_error);
}
/* Lock the user's real mail drop */
if ( flock(mfd,LOCK_EX) == -1 ) {
fclose(md) ;
return pop_msg(p,POP_FAILURE, "flock: '%s': %s", p->temp_drop,
(errno < sys_nerr) ? sys_errlist[errno] : "");
}
/* Go to the right places */
offset = lseek(fileno(p->drop),0,2) ;
/* Append any messages that may have arrived during the session
to the temporary maildrop */
while ((nchar=read(mfd,buffer,BUFSIZ)) > 0)
if ( nchar != write(fileno(p->drop),buffer,nchar) ) {
nchar = -1;
break ;
}
if ( nchar != 0 ) {
fclose(md) ;
ftruncate(fileno(p->drop),offset) ;
fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
#ifdef DEBUG
if (p->debug)
pop_log(p,POP_DEBUG,"New messages appended to \"%s\"",p->temp_drop);
#endif DEBUG
rewind(md);
(void)ftruncate(mfd,0) ;
/* Synch stdio and the kernel for the POP drop */
rewind(p->drop);
lseek(fileno(p->drop),0,0);
/* Transfer messages not flagged for deletion from the temporary
maildrop to the new maildrop */
#ifdef DEBUG
if (p->debug)
pop_log(p,POP_DEBUG,"Creating new maildrop \"%s\" from \"%s\"",
p->drop_name,p->temp_drop);
#endif DEBUG
for (msg_num = 0;
msg_num < p->msg_count;
++msg_num) {
int doing_body;
/* Get a pointer to the message information list */
mp = &p->mlp[msg_num];
if (mp->del_flag) {
#ifdef DEBUG
if(p->debug)
pop_log(p,POP_DEBUG,
"Message %d flagged for deletion.",mp->number);
#endif DEBUG
continue;
}
fseek(p->drop,mp->offset,0);
#ifdef DEBUG
if(p->debug)
pop_log(p,POP_DEBUG,"Copying message %d.",mp->number);
#endif DEBUG
for(status_written = doing_body = 0 ;
fgets(buffer,MAXMSGLINELEN,p->drop);
fputs(buffer,md)) {
if (doing_body == 0) { /* Header */
if (strncasecmp(buffer,"Status:",7) == 0) {
if (mp->retr_flag)
fputs("Status: RO\n",md);
status_written++;
continue;
}
/* A blank line signals the end of the header. */
if (*buffer == '\n') {
doing_body = 1;
if (status_written == 0) {
if (mp->retr_flag)
fputs("Status: RO\n",md);
else
fputs("Status: U\n",md);
}
}
}
else { /* Body */
if (strncmp(buffer,"From ",5) == 0) break;
}
}
}
/* flush and check for errors now! The new mail will writen
without stdio, since we need not separate messages */
fflush(md) ;
if (ferror(md)) {
ftruncate(mfd,0) ;
fclose(md) ;
fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
/* Go to start of new mail if any */
lseek(fileno(p->drop),offset,0);
while((nchar=read(fileno(p->drop),buffer,BUFSIZ)) > 0)
if ( nchar != write(mfd,buffer,nchar) ) {
nchar = -1;
break ;
}
if ( nchar != 0 ) {
ftruncate(mfd,0) ;
fclose(md) ;
fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
/* Close the maildrop and empty temporary maildrop */
fclose(md);
ftruncate(fileno(p->drop),0);
fclose(p->drop);
return(pop_quit(p));
}