FreeBSD/usr.sbin/ctm/ctm_rmail/ctm_rmail.c
/*
 * Put the pieces together to form a delta.
 * Returns 1 on success, and 0 on failure.
 * Note: dname, pname, and tname are room for some file names that just
 * happened to by lying around in the calling routine.  Waste not, want not!
 */
Put the pieces together to form a delta.
Returns 1 on success, and 0 on failure.
Note: dname, pname, and tname are room for some file names that just
happened to by lying around in the calling routine.  Waste not, want not!
Put the pieces together to form a delta . Returns 1 on success and 0 on failure . Note : dname pname and tname are room for some file names that just happened to by lying around in the calling routine . Waste not want not!
int
combine(char *delta, int npieces, char *dname, char *pname, char *tname)
    {
    FILE *dfp, *pfp;
    int i, n, e;
    char buf[BUFSIZ];
    int fd = -1;
    strcpy(tname, delta_dir);
    strcat(tname, "/d.XXXXXXXXXX");
    if ((fd = mkstemp(tname)) == -1 ||
	(dfp = fdopen(fd, "w")) == NULL)
	{
	if (fd != -1) {
	    close(fd);
	    err("cannot open '%s' for writing", tname);
	    }
	else
	    err("*mkstemp: '%s'", tname);
	return 0;
	}
    for (i = 1; i <= npieces; i++)
	{
	mk_piece_name(pname, delta, i, npieces);
	if ((pfp = fopen(pname, "r")) == NULL)
	    {
	    err("cannot open '%s' for reading", pname);
	    fclose(dfp);
	    unlink(tname);
	    return 0;
	    }
	while ((n = fread(buf, sizeof(char), sizeof(buf), pfp)) != 0)
	    fwrite(buf, sizeof(char), n, dfp);
	e = ferror(pfp);
	fclose(pfp);
	if (e)
	    {
	    err("error reading '%s'", pname);
	    fclose(dfp);
	    unlink(tname);
	    return 0;
	    }
	}
    fflush(dfp);
    e = ferror(dfp);
    fclose(dfp);
    if (e)
	{
	err("error writing '%s'", tname);
	unlink(tname);
	return 0;
	}
    mk_delta_name(dname, delta);
    if (rename(tname, dname) < 0)
	{
	err("*rename: '%s' to '%s'", tname, dname);
	unlink(tname);
	return 0;
	}
    chmod(dname, 0666 & ~mask);
    for (i = 1; i <= npieces; i++)
	{
	mk_piece_name(pname, delta, i, npieces);
	if (unlink(pname) < 0)
	    err("*unlink: '%s'", pname);
	}
    err("%s complete", delta);
    return 1;
    }
int combine ( char * delta , int npieces , char * dname , char * pname , char * tname ) { FILE * dfp , * pfp ; int i , n , e ; char buf [ BUFSIZ ] ; int fd = -1 ; strcpy ( tname , delta_dir ) ; strcat ( tname , " " ) ; if ( ( fd = mkstemp ( tname ) ) == -1 || ( dfp = fdopen ( fd , " " ) ) == NULL ) { if ( fd != -1 ) { close ( fd ) ; err ( " " , tname ) ; } else err ( " " , tname ) ; return 0 ; } for ( i = 1 ; i <= npieces ; i ++ ) { mk_piece_name ( pname , delta , i , npieces ) ; if ( ( pfp = fopen ( pname , " " ) ) == NULL ) { err ( " " , pname ) ; fclose ( dfp ) ; unlink ( tname ) ; return 0 ; } while ( ( n = fread ( buf , sizeof ( char ) , sizeof ( buf ) , pfp ) ) != 0 ) fwrite ( buf , sizeof ( char ) , n , dfp ) ; e = ferror ( pfp ) ; fclose ( pfp ) ; if ( e ) { err ( " " , pname ) ; fclose ( dfp ) ; unlink ( tname ) ; return 0 ; } } fflush ( dfp ) ; e = ferror ( dfp ) ; fclose ( dfp ) ; if ( e ) { err ( " " , tname ) ; unlink ( tname ) ; return 0 ; } mk_delta_name ( dname , delta ) ; if ( rename ( tname , dname ) < 0 ) { err ( " " , tname , dname ) ; unlink ( tname ) ; return 0 ; } chmod ( dname , 0666 & ~ mask ) ; for ( i = 1 ; i <= npieces ; i ++ ) { mk_piece_name ( pname , delta , i , npieces ) ; if ( unlink ( pname ) < 0 ) err ( " " , pname ) ; } err ( " " , delta ) ; return 1 ; }
Put the pieces together to form a delta.
Put the pieces together to form a delta .
/*
     * Reconstruct the delta by reading each piece in order.
     */ /*
     * Throw the pieces away.
     */