static char rcsid[] = "@(#)$Id: charmapcopy.c,v 1.7 2006/04/09 07:37:30 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.7 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 *****************************************************************************/

#include "defs.h"
#include "elmlib.h"
#include "s_elm.h"
#include "s_me.h"
#include "reghelper.h"
#include "charmapcopy.h"

char ** copydirs = NULL;
int     copydir_count = 0;

#include <errno.h>
#ifndef ANSI_C
extern int errno;
#endif
FILE *charmap_copy(name,pathname)
     const char *name; 
     const char *pathname;
{
    int i;
    FILE *F = NULL;
    FILE *res = NULL;
    int fd,c;


    for (i = 0; i < copydir_count; i++) {
	char * n = elm_message(FRM("%s/%s"),copydirs[i],name);
	
	F = fopen(n,"r");

	if (F) {
	    lib_transient(CATGETS(elm_msg_cat, MeSet,MeCopyingMapfile,
				  "Copying mapfile %s: %s => %s"),
			  name,n,pathname);
	    log_config((char *)pathname);
	}
	free(n);
	if (F)
	    break;
    }
    if (!F) {
	lib_transient(FRM("*** Copying mapfile %s => %s -- file %s not found?"),
		      name,pathname,name);
	return NULL;
    }

    fd = open(pathname, O_RDWR | O_CREAT | O_EXCL, 0444);
    if (fd < 0) {
	int errcode;
	
    error1:
	errcode = errno;

	lib_error(CATGETS(elm_msg_cat, MeSet, MeFileNotWriteable,
			  "File %.50s is not writeable: %s"),
		  pathname, error_description(errcode));

	fclose(F);
	return NULL;
    }


    res = fdopen(fd,"w+");
    if (!res)
	goto error1;

    while (EOF != (c = getc(F))) {
	if (putc(c,res) == EOF) {
	    int err = errno;
	    lib_error(CATGETS(elm_msg_cat, ElmSet, ElmErrorTryingToWrite,
			      "Error %s encountered trying to write to %s."),
		      error_description(err),pathname);
	    fclose(F);
	    fclose(res);
	    return NULL;
	}
    }
	   


    if (ferror(res)) {
	lib_error(CATGETS(elm_msg_cat, ElmSet, ElmErrorReading,
			  "Error reading from %.50s"),
		  name);
	fclose(F);
	fclose(res);
	return NULL;
    }

    fclose(F);

    rewind(res);
    return res;
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1