[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

9033.0. "ftp signal" by TKOV60::OGINO () Tue Mar 04 1997 23:36

In the attached programs, ftp is used to copy files from remote site via tel. 
line. A program called alm_watch.c sends a user defined signal at 7:30 to 
cftp_get() in alm_copy.c to end the program. In cftp_get(), it is set to get 
next packet if a packet comes and its timer is set to wait 30 min. for the 
next packet. The program alm_copy.c does not stop until there is no packet 
even though alm_watch.c sends 7:30 signal to stop. But some how, ftp, not the
alm_copy.c program, gets this 7:30 signal from alm_watch.c and stops copying. 
Is there anyway to avoid ftp to get this signal?

Thanks in advance.

Yoji

alm_watch.c ---------------------------

#include "alm_all.h"


static char alm_start_copy_time[ALM_TIME_SIZE];
static char alm_end_copy_time[ALM_TIME_SIZE];
int child_pid;
int c_child_pid;
static int dbx_flag = 0;
static int copy_end_flag = 0;
static char today[ALM_DATE_SIZE];

void main(int argc, char argv)
{
if (argc >= 2) dbx_flag = 1;
    alm_f_log_date(today, ALM_DATE_FMT);
    alm_w_init();
    alm_w_start_copy();
}

void alm_w_init()
{
    char *env;

    signal(SIGINT,  alm_w_sig_int);
    signal(SIGCHLD, alm_w_sig_chld);

    if ((env = alm_f_getenv("ALM_START_COPY_TIME")) != NULL){
 	strcpy(alm_start_copy_time, env);
    }
    if ((env = alm_f_getenv("ALM_END_COPY_TIME")) != NULL){
 	strcpy(alm_end_copy_time, env);
    }
    if ((env = alm_f_getenv("ALM_WATCH_LOG_FILE")) != NULL){
        fp_watch_log = fopen(env, ALM_FOPEN_AD);
    }
}

int alm_w_start_copy()
{
    child_pid = alm_w_fork_copy();
    alm_w_wait();
}

int alm_w_fork_copy()
{
    int     c_pid;

    switch(c_pid = fork()){
        case -1:
            alm_f_error(fp_watch_log, "Error: Can't Creat New Process", ALM_ERROR);
            break;
        case  0:
            alm_c_main(dbx_flag);
            alm_f_error(fp_watch_log, "Information: Exit copy process", ALM_ERROR);
            break;
        default:
            return(c_pid);
    }
}

void alm_w_wait()
{
    char *env;
    int sleep_time, status;
    int copy_start_flag = 0;
    char now_time[ALM_TIME_SIZE];
    char now_day[ALM_DATE_SIZE];
    memset(now_time, 0, ALM_TIME_SIZE);
    if ((env = alm_f_getenv("ALM_SLEEP_TIME")) != NULL){
 	sleep_time = atoi(env);
    } else sleep_time = 60;

    for(FOREVER){
        sleep(sleep_time);
        alm_f_log_date(now_time, ALM_WAIT_FULL_FMT);
        alm_f_log_date(now_day, ALM_DATE_FMT);
        if (dbx_flag == 0){
            if (strcmp(alm_start_copy_time, now_time) < 0){
                if (copy_start_flag == 0){
                    kill(child_pid, SIGUSR1);
                    alm_f_error(fp_watch_log,
        		    "Information: Copy start time arrival\n", ALM_WARNING);
                    copy_start_flag = 1;
                }
            }
        } else copy_start_flag = 1;
        if (strncmp(today, now_day, ALM_DATE_SIZE) != 0){
            if ((copy_start_flag != 0) &&
				(strcmp(alm_end_copy_time, now_time) <= 0)){
		if (copy_end_flag == 0) {
		   copy_end_flag = 1;
                   if (kill(child_pid, SIGUSR2) != 0) {
                      perror ("Kill failed SIGUSR2"); 
                      exit(1);
                   }
                   alm_f_error(fp_watch_log,
		       "Information: Copy end time arrival\n", ALM_WARNING);
                }
            }
        }
    }
}

void alm_w_sig_int()
{
    int status;

    signal(SIGCHLD, SIG_IGN);
    kill(child_pid, SIGKILL);
    wait(&status);
    alm_f_error(fp_watch_log,
		"*****ALM watch process exit(get sigkill)*****\n", ALM_ERROR);
}

void alm_w_sig_chld()
{
    int status;

    wait(&status);
    alm_f_error(fp_watch_log,
	"Information: ALM copy process exit(get sigchld).\n", ALM_WARNING);
    if (copy_end_flag == 0){
        alm_f_error(fp_watch_log,
	    "Warning: ALM copy process abnormal end.\n", ALM_WARNING);
	kill(c_child_pid,SIGKILL);
    } else {
        alm_f_error(fp_watch_log,
	 "Information: ALM watch process exit(get sigchld).\n", ALM_ERROR);
    }
}



alm_copy.c ---------------------------

#include "alm_all.h"

struct copy_site	site[ALM_HOST_NUM];
struct copy_site*	site_p[ALM_HOST_NUM];
char			site_env[ALM_HOST_NUM];
int dist_flag[ALM_HOST_NUM];
int ftp_disc;
int con_host_name;
short int alm_ftp_timeout;
static int dbx_flag = 0;
static int copy_end_flag = 0;
char make_file[ALM_NAME_SIZE];
static char g_sock[] = "/tmp/alm_ipc";
static unsigned int g_time = 1;
static int netsoc;
static int sockfd;
int file_init_flag;
int init_flag[ALM_HOST_NUM];

void alm_c_main(int dbx)
{
    char err_buf[ALM_BUF_SIZE];
    int wait_time;
    char *env;
    dbx_flag = dbx;
    alm_c_init();

    if (dbx_flag == 0){
        alm_c_first_set_tbl_all();
        sprintf(err_buf, "ALM distribute file table routine start.");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        if ((env = alm_f_getenv("ALM_DIST_INTERVAL_TIME")) != NULL){
            wait_time = atoi(env);
        } else wait_time = 120;
        for(FOREVER){
            alm_c_dist_tbl();
            sprintf(err_buf,
	      "Information: Waiting for table distribute. TIME:[%d]", wait_time);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            sleep(wait_time);
        }
    } else {
        sprintf(err_buf, "ALM get file(table) routine start.");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        alm_c_sig_start_copy();
    }
}

void alm_c_sig_start_copy()
{
    char err_buf[ALM_BUF_SIZE];

    if (alm_f_getenv("ALM_LOAD_COMMAND") != "") 
        alm_c_send_client(NULL, " ");

    for (FOREVER){
        if (copy_end_flag == 0){
            alm_c_get_tbl();
        } else {
            kill(c_child_pid,SIGKILL);
            sprintf(err_buf, "Exec shell process normal end.");
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            sprintf(err_buf, "ALM Copy process normal end.");
            alm_f_error(fp_copy_log, err_buf, ALM_ERROR);
        }
    }
}

void alm_c_init()
{
    int i;
    char *env;
    char err_buf[ALM_BUF_SIZE];

    signal(SIGUSR1,  alm_c_sig_start_copy); // Start copy signal 
    signal(SIGUSR2,  alm_c_sig_end_copy); // Stop copy signal
    signal(SIGCHLD, alm_c_sig_chld);

    alm_c_set_copy_site();
    if (alm_f_open_log_file() != ALM_F_EXIST){
        sprintf(err_buf,
		"Information: Reset site-env file. [file don't exist]");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        for (i = 0; i < ALM_HOST_NUM; i++) site_env[i] = 'N';
        fputs(site_env, fp_site_file);
    } else {
        sprintf(err_buf,
		"Information: Site-env file exist. Restart or debug mode.");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        if (fgets(site_env, ALM_HOST_NUM + 1, fp_site_file) == NULL){
            sprintf(err_buf,
		"Information: Reset site-env file. [file exist]");
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            for (i = 0; i < ALM_HOST_NUM; i++) site_env[i] = 'N';
            fputs(site_env, fp_site_file);
        }
    }

    file_init_flag = atoi(alm_f_getenv("ALM_FILE_INIT_FLAG"));
    for (i = 0; i < ALM_HOST_NUM; i++){
        dist_flag[i] = ALM_SUCCESS; 
        if(file_init_flag == ALM_INIT){
            init_flag[i] = ALM_INIT;
        }
        else{
            init_flag[i] = ALM_NOT_INIT;
        }
    }

    alm_c_set_tbl_all();

    if ((env = alm_f_getenv("ALM_FTP_TIMEOUT")) != NULL){
        alm_ftp_timeout = (short int)atoi(env);
    } else alm_ftp_timeout = 300;

    if (alm_c_set_socket() == ALM_ERROR){
        sprintf(err_buf,
		"Information: alm_c_set_socket: Can,t create socket.");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    }
}

int alm_c_open_cftp(void)
{
    int  status;
    char err_buf[ALM_BUF_SIZE];

    if((status = cftp_open(&ftp_disc)) != FTP__NORMAL) {
        sprintf(err_buf, "Error: cftp_open::%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        return(ALM_ERROR);
    }
    return(ALM_SUCCESS);
}

int alm_c_close_cftp(void)
{
    int  status;
    char err_buf[ALM_BUF_SIZE];

    if((status = cftp_close(ftp_disc)) != FTP__NORMAL) {
        sprintf(err_buf, "Error: cftp_close::%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        return(ALM_ERROR);
    }
    return(ALM_SUCCESS);
}

void alm_c_set_tbl_all(void)
{
    char err_buf[ALM_BUF_SIZE];
    int i,status;

    for (i = 0; i < ALM_HOST_NUM; i++){
        if(dist_flag[i] == ALM_ERROR) continue;
        status = alm_f_check_file(site_p[i]->put_file,site_p[i]->putfp);
        if(status == ALM_ERROR){
            dist_flag[i] = ALM_ERROR;
            continue;
        }
        if((site_p[i]->p_root = alm_c_set_tbl(site_p[i]->putfp)) == NULL){
            sprintf(err_buf,
		"Warning: Host_id(%d) table file not exist FILE:[%s] LINE:[%d]"
							, i, DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            dist_flag[i] = ALM_ERROR;
        }
    }
}

struct data_tree* alm_c_set_tbl(FILE *fd)
{
    struct data_tree *p, *get_p;
    char buf[ALM_BUF_SIZE];
    int i;

    p = NULL;

    if (fd == NULL) return((struct data_tree*)NULL);

    for (i = 0; fgets(buf, ALM_BUF_SIZE, fd) != NULL; i++){
        p = alm_f_set_data_tree(p, buf);
        if (i == 0) get_p = p;
    }
    return(get_p);
}

void alm_c_set_get_tbl_fp()
{
    char *env;
    char remote_file[ALM_NAME_SIZE];
    int  i;
    
    for (i = 0; i < ALM_HOST_NUM; i++){
        if (site_p[i]->getfp != NULL){
            fclose(site_p[i]->getfp);
        }
    }

    if ((env = alm_f_getenv("ALM_IBM_OS_GET_FILE")) != NULL){
        site_p[ALM_IBM_OS]->getfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_TK_GET_FILE")) != NULL){
        site_p[ALM_IBM_TK]->getfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_UNI_TK_GET_FILE")) != NULL){
        site_p[ALM_UNI_TK]->getfp = fopen(env, ALM_FOPEN_R);
    }
}

void alm_c_set_copy_site()
{
    char *env;
    int  i;

    for (i = 0; i < ALM_HOST_NUM; i++){
        site_p[i] = &site[i];
    }

    site_p[ALM_IBM_OS]->hostname   = alm_f_getenv("ALM_IBM_OS_HOST");
    site_p[ALM_IBM_OS]->put_file   = alm_f_getenv("ALM_IBM_OS_PUT_FILE");
    site_p[ALM_IBM_OS]->get_file   = alm_f_getenv("ALM_IBM_OS_GET_FILE");
    site_p[ALM_IBM_OS]->old_file   = alm_f_getenv("ALM_IBM_OS_OLD_FILE");
    site_p[ALM_IBM_OS]->new_file   = alm_f_getenv("ALM_IBM_OS_NEW_FILE");
    site_p[ALM_IBM_OS]->base_file  = alm_f_getenv("ALM_IBM_OS_BASE_FILE");
    site_p[ALM_IBM_OS]->local_dir  = alm_f_getenv("ALM_IBM_OS_LOCAL_DIR");
    site_p[ALM_IBM_OS]->remote_dir = alm_f_getenv("ALM_IBM_OS_REMOTE_DIR");
    site_p[ALM_IBM_OS]->getfp      = NULL;
    if ((env = alm_f_getenv("ALM_IBM_OS_PUT_FILE")) != NULL){
        site_p[ALM_IBM_OS]->putfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_OS_OLD_FILE")) != NULL){
        site_p[ALM_IBM_OS]->oldfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_OS_NEW_FILE")) != NULL){
        site_p[ALM_IBM_OS]->newfp = fopen(env, ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_IBM_OS_BASE_FILE")) != NULL){
        site_p[ALM_IBM_OS]->basefp = fopen(env,ALM_FOPEN_R);
    }

    site_p[ALM_IBM_TK]->hostname   = alm_f_getenv("ALM_IBM_TK_HOST");
    site_p[ALM_IBM_TK]->put_file   = alm_f_getenv("ALM_IBM_TK_PUT_FILE");
    site_p[ALM_IBM_TK]->get_file   = alm_f_getenv("ALM_IBM_TK_GET_FILE");
    site_p[ALM_IBM_TK]->old_file   = alm_f_getenv("ALM_IBM_TK_OLD_FILE");
    site_p[ALM_IBM_TK]->new_file   = alm_f_getenv("ALM_IBM_TK_NEW_FILE");
    site_p[ALM_IBM_TK]->base_file  = alm_f_getenv("ALM_IBM_TK_BASE_FILE");
    site_p[ALM_IBM_TK]->local_dir  = alm_f_getenv("ALM_IBM_TK_LOCAL_DIR");
    site_p[ALM_IBM_TK]->remote_dir = alm_f_getenv("ALM_IBM_TK_REMOTE_DIR");
    site_p[ALM_IBM_TK]->getfp      = NULL;
    if ((env = alm_f_getenv("ALM_IBM_TK_PUT_FILE")) != NULL){
        site_p[ALM_IBM_TK]->putfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_TK_NEW_FILE")) != NULL){
        site_p[ALM_IBM_TK]->newfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_TK_OLD_FILE")) != NULL){
        site_p[ALM_IBM_TK]->oldfp = fopen(env, ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_IBM_TK_BASE_FILE")) != NULL){
        site_p[ALM_IBM_TK]->basefp = fopen(env, ALM_FOPEN_R);
    }

    site_p[ALM_UNI_TK]->hostname   = alm_f_getenv("ALM_UNI_TK_HOST");
    site_p[ALM_UNI_TK]->put_file   = alm_f_getenv("ALM_UNI_TK_PUT_FILE");
    site_p[ALM_UNI_TK]->get_file   = alm_f_getenv("ALM_UNI_TK_GET_FILE");
    site_p[ALM_UNI_TK]->old_file   = alm_f_getenv("ALM_UNI_TK_OLD_FILE");
    site_p[ALM_UNI_TK]->new_file   = alm_f_getenv("ALM_UNI_TK_NEW_FILE");
    site_p[ALM_UNI_TK]->base_file  = alm_f_getenv("ALM_UNI_TK_BASE_FILE");
    site_p[ALM_UNI_TK]->local_dir  = alm_f_getenv("ALM_UNI_TK_LOCAL_DIR");
    site_p[ALM_UNI_TK]->remote_dir = alm_f_getenv("ALM_UNI_TK_REMOTE_DIR");
    site_p[ALM_UNI_TK]->getfp      = NULL;
    if ((env = alm_f_getenv("ALM_UNI_TK_PUT_FILE")) != NULL){
        site_p[ALM_UNI_TK]->putfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_UNI_TK_NEW_FILE")) != NULL){
        site_p[ALM_UNI_TK]->newfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_UNI_TK_OLD_FILE")) != NULL){
        site_p[ALM_UNI_TK]->oldfp = fopen(env, ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_UNI_TK_BASE_FILE")) != NULL){
        site_p[ALM_UNI_TK]->basefp = fopen(env, ALM_FOPEN_R);
    }
}


int alm_c_connect_host(int i, int tbl_or_data)
{
    char pass[ALM_NAME_SIZE];
    char user[ALM_NAME_SIZE];
    char err_buf[ALM_BUF_SIZE];
    int status, wait_time, copy_type = 0;
    char *env;

    if (site_p[i] ==  (struct copy_site*)NULL) return(ALM_NOTFOUND);

    if (site_p[i]->hostname == (char*)NULL){
        sprintf(err_buf,
	   "Warning: Host name(%d) don't set ENV. FILE:[%s] LINE:[%d]",
			                   			i, DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        return(ALM_NOTFOUND);
    }

    if ((env = alm_f_getenv("ALM_WAIT_CHANGE_TIME")) != NULL){
        wait_time = atoi(env);
    } else wait_time = 180;

    if ((i == ALM_IBM_TK) || (i == ALM_IBM_OS)){
        if (tbl_or_data == 1) { // Data copy
            sprintf(err_buf,
	      "Information: Waiting change ftp site. TIME:[%d]", wait_time);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            sleep(wait_time);
        }
    }

    if (alm_c_open_cftp() == ALM_ERROR){
        exit(1);
    }

    copy_type = FTP_M_TYPE_BINARY;

    if (tbl_or_data == 1) copy_type = FTP_M_TYPE_ASCII;

    if ((status = cftp_autologin(site_p[i]->hostname, user, pass))!= FTP__NORMAL){
        sprintf(err_buf, "Error: cftp_autologin::%s:FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        return(ALM_ERROR);
    }
    sprintf(err_buf,
     "Information: Now login. Host:[%s], User:[%s]", site_p[i]->hostname, user);
    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);

    if ((status = cftp_login(ftp_disc, site_p[i]->hostname, user, pass,
						0, copy_type)) != FTP__NORMAL){
        sprintf(err_buf, "Error: cftp_login::%s:FILE:[%s] LINE:[%d]",
					cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING); return(ALM_ERROR);
    }

    if ((status = cftp_set_timeout(ftp_disc, alm_ftp_timeout)) != FTP__NORMAL){
        sprintf(err_buf, "Error: cftp_set_timeout::%s:FILE:[%s] LINE:[%d]",
					cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING); return(ALM_ERROR);
    }
    
    return(ALM_SUCCESS);
}

void alm_c_dist_tbl()
{
    char err_buf[ALM_BUF_SIZE];
    char rpl_buf[ALM_BUF_SIZE];
    char remote_file[ALM_BUF_SIZE];
    char *dist_file_name, dist_dir;
    int i, status, size, stat;
    int cc;

    dist_file_name = alm_f_getenv("ALM_DIST_FILE");

    for (i = 0; i < ALM_HOST_NUM; i++){

        if(dist_flag[i] == ALM_ERROR) continue;

        if (alm_c_check_site(i) == 1){ /* Table file already copy */
            if(init_flag[i] == ALM_INIT){
                sprintf(err_buf, 
		    "Information: File management table initialize. File:[%s]"
							,site_p[i]->put_file);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                rewind(site_p[i]->basefp);
                rewind(site_p[i]->putfp);
                while((cc = fgetc(site_p[i]->basefp)) != EOF){
                    fputc(cc,site_p[i]->putfp);
                }
                fflush(site_p[i]->putfp);
                if((alm_f_check_file(site_p[i]->put_file,site_p[i]->putfp)) 
							== ALM_ERROR){
                    dist_flag[i] = ALM_ERROR;
                    continue;
                }
                alm_c_set_tbl_all();
                init_flag[i] = ALM_NOT_INIT;
            }
            continue; 
        }

        status = alm_c_connect_host(i, 1); // 1 is table copy
        if (status == ALM_ERROR){
            sprintf(err_buf, 
		"Error: alm_c_connect_host:Status:%d:FILE:[%s] LINE:[%d]",
						status, DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            alm_c_close_cftp();
            continue;
        } else if (status == ALM_NOTFOUND){
            continue;
        }

        if (strlen(site_p[i]->remote_dir) != 0){
            strcpy(remote_file, site_p[i]->remote_dir);
            strcat(remote_file, dist_file_name);
        } else continue;

	if ((stat = alm_c_first_actions(i)) != ALM_SUCCESS) continue;

	if ((status = cftp_put_file(ftp_disc, site_p[i]->new_file,
				remote_file, &size)) != FTP__NORMAL){
            if (status == FTP__REMERROR){
                stat = cftp_get_err_reply(ftp_disc, rpl_buf);
                sprintf(err_buf, "Error: cftp_put_file::%s FILE:[%s] LINE:[%d]",
			                   rpl_buf,  DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            }
            sprintf(err_buf, "Error: cftp_put_file::%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            continue;
        } else {
            sprintf(err_buf, "Information: Table distribute success. Host:[%s]",
			site_p[i]->hostname);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            if(file_init_flag == ALM_INIT){
                sprintf(err_buf, "Information: File management table initialize. File:[%s]",site_p[i]->put_file);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                rewind(site_p[i]->basefp);
                rewind(site_p[i]->putfp);
                while((cc = fgetc(site_p[i]->basefp)) != EOF){
                    fputc(cc,site_p[i]->putfp);
                }
                fflush(site_p[i]->putfp);
                if((alm_f_check_file(site_p[i]->put_file,site_p[i]->putfp)) 
							== ALM_ERROR){
                    dist_flag[i] = ALM_ERROR;
                    continue;
                }
                alm_c_set_tbl_all();
                init_flag[i] = ALM_NOT_INIT;
            }
            else{
                sprintf(err_buf,"Information: File management table not initialize. File:[%s]",site_p[i]->put_file);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            }
            alm_c_set_site_env(i);
            fclose(site_p[i]->basefp);
	}
        alm_c_close_cftp();
    }
}

void alm_c_set_site_env(int site_number)
{

    char temp_buf[ALM_HOST_NUM];
    char err_buf[ALM_BUF_SIZE];

    if (site_env[site_number] == 'N'){
        if (fgets(temp_buf, ALM_HOST_NUM + 1, fp_site_file) == NULL){
	    site_env[site_number] = 'Y';
            strncpy(temp_buf, site_env, ALM_HOST_NUM);
            rewind(fp_site_file);
	    fputs(temp_buf, fp_site_file);
	    fflush(fp_site_file);
        sprintf(err_buf,
		"Information: Add site-env file. [site_env is 'N']");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        } else {
	    site_env[site_number] = 'Y';
            rewind(fp_site_file);
	    fputs(site_env, fp_site_file);
	    fflush(fp_site_file);
        sprintf(err_buf,
		"Information: Add site-env file. [site_env is 'Y']");
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
	}
    }
}

int alm_c_check_site(int site_number)
{
    char err_buf[ALM_BUF_SIZE];

    if (dbx_flag != 0){
        if (strchr(site_env, 'Y') == NULL){
            sprintf(err_buf,
		"Error: All ftp site is not available.FILE:[%s] LINE:[%d]",
								DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_ERROR);
        }
    }

    if (site_env[site_number] == 'Y'){
	return(1);
    } else {
	return(0);
    }
}

char *alm_c_compare_tbl(struct copy_site* s)
{
    int    cmp;
    char err_buf[ALM_BUF_SIZE];
    struct data_tree *get, *put;

    alm_c_set_get_tbl_fp(); // Set get_file pointer
    if((s->g_root = alm_c_set_tbl(s->getfp)) == NULL){
        sprintf(err_buf,
                  "Warning: %s Table file not exist FILE:[%s] LINE:[%d]",
						s->get_file, DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    }

    get = s->g_root;
    put = s->p_root;

    if ((get != NULL) && (put != NULL)){
        memset(make_file,NULL,ALM_NAME_SIZE);
        alm_c_name_match_p(get, put);
        return(make_file);
    }
    return("");
}

void alm_c_name_match_p(struct data_tree *getp, struct data_tree *putp)
{
    if (putp != NULL){
        alm_c_name_match_p(getp, putp->left);
        alm_c_name_match_g(getp, putp);
        alm_c_name_match_p(getp, putp->right);
    } 
}

void alm_c_name_match_g(struct data_tree *getp, struct data_tree *putp)
{
    if (getp != NULL){
        alm_c_name_match_g(getp->left, putp);
        if (strcmp(getp->cdata->name, putp->cdata->name) == 0){
            if (strcmp(getp->cdata->m_flag, putp->cdata->m_flag) != 0){
                if(strcmp(getp->cdata->m_flag,"Y") == 0){
                    strcpy(make_file, putp->cdata->name);
                }
	    }
            else{
                if(strcmp(getp->cdata->m_flag,"Y") == 0){
                    if(strcmp(getp->cdata->m_time,putp->cdata->m_time) != 0){
                        strcpy(make_file,putp->cdata->name);
                    }
                }
            }
        }
        alm_c_name_match_g(getp->right, putp);
    }
}

int alm_c_get_data(struct copy_site *s, char *file, int site_number)
{
    char err_buf[ALM_BUF_SIZE];
    char rpl_buf[ALM_BUF_SIZE];
    char local_file[ALM_NAME_SIZE];
    char remote_file[ALM_NAME_SIZE];
    char *get_file_name;
    int i, status, size, stat;
    char compress[] = ".Z";
/* gzip 
    char compress[3];
    char comp[] = ".Z";
    char gzip[] = ".gz";

    if ((site_number == ALM_IBM_OS) || (site_number == ALM_IBM_TK)){
        strncpy(compress, gzip, sizeof(gzip));
    } else {
        strncpy(compress, comp, sizeof(comp));
    }

*/
    sleep(1);
    strcpy(local_file, s->local_dir);
    strcat(local_file, file);
    strcpy(remote_file, s->remote_dir);
    strcat(remote_file, file);
    strcat(local_file, compress);
    strcat(remote_file, compress);

    sprintf(err_buf, "Information: File copy start\n Remote:[%s] Local:[%s]",
			                   remote_file, local_file);
    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    if ((status = cftp_get_file(ftp_disc, local_file,
				remote_file, &size)) != FTP__NORMAL){
        if (status == FTP__REMERROR){
            stat = cftp_get_err_reply(ftp_disc, rpl_buf);
            sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   rpl_buf,  DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }
        sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        return(ALM_ERROR);
    }
    sprintf(err_buf,
	"Information: File copy success\n Remote:[%s] Local:[%s] Size:[%d]Byte",
			                   remote_file, local_file, size);
    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    return(ALM_SUCCESS);
}

void alm_c_get_tbl()
{
    char err_buf[ALM_BUF_SIZE];
    char rpl_buf[ALM_BUF_SIZE];
    char file[ALM_NAME_SIZE];
    char remote_file[ALM_NAME_SIZE];
    char *get_file_name, *env;
    int i, status, size, stat;
    int wait_time;

    get_file_name = alm_f_getenv("ALM_DIST_FILE");

    for (i = 0; i < ALM_HOST_NUM; i++){
        if (copy_end_flag != 0) break;

        if (i == ALM_IBM_OS){
            if ((env = alm_f_getenv("ALM_COPY_INTERVAL_TIME")) != NULL){
                wait_time = atoi(env);
            } else wait_time = 120;
            sprintf(err_buf,
	      "Information: Copy interval. TIME:[%d]", wait_time);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            sleep(wait_time);
        }

        if (alm_c_check_site(i) == 0) continue; /* ftp site don't connect */

        if (strlen(site_p[i]->remote_dir) != 0){
            strcpy(remote_file, site_p[i]->remote_dir);
            strcat(remote_file, get_file_name);
        } 
        else {
            continue;
        }

        status = alm_c_connect_host(i, 1); // 1 is table copy
	if (status == ALM_ERROR){
            sprintf(err_buf,
		"Error: alm_c_connect_host:Status:%d:FILE:[%s] LINE:[%d]",
						status, DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
	    alm_c_close_cftp();
            continue;
        } else if (status == ALM_NOTFOUND){
            continue;
        }

	if ((status = cftp_get_file(ftp_disc, site_p[i]->get_file,
				remote_file, &size)) != FTP__NORMAL){
            if (status == FTP__REMERROR){
                stat = cftp_get_err_reply(ftp_disc, rpl_buf);
                sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   rpl_buf,  DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            }
            sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            alm_c_close_cftp();
            continue;
        }
        if (size <= 0) {
            sprintf(err_buf,
               "Information: File management tbl is empty:[%s]", remote_file);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            alm_c_close_cftp();
            continue;
        }
        alm_c_set_get_tbl_fp();
        if((alm_f_check_data(site_p[i]->getfp,remote_file)) == ALM_ERROR){
            alm_c_close_cftp();
            continue;
        }
        sprintf(err_buf, "Information: Get file. [%s]", site_p[i]->get_file);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);

        for(FOREVER){
            strcpy(file, alm_c_compare_tbl(site_p[i]));
            if (strlen(file) != 0){
                alm_c_close_cftp();
                status = alm_c_connect_host(i, 0); // 0 is data copy
	        if (status == ALM_ERROR){
                    sprintf(err_buf,	
		      "Error: alm_c_connect_host:Status:%d:FILE:[%s] LINE:[%d]",
						status, DEBUG_INFO);
                    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
		    break;
                }
                if (alm_c_get_data(site_p[i], file, i) != ALM_ERROR){
                    status = alm_c_tbl_update(site_p[i], file, 0);
                    if (status == ALM_SUCCESS) {
    		        alm_c_send_client(site_p[i], file);
                    } else {
                        alm_c_close_cftp();
                        break;
                    }
                } else {
                    status = alm_c_tbl_update(site_p[i], file, 1);
                    alm_c_close_cftp();
		    break;
                }
            } else {
                sprintf(err_buf,
		"Information: Data don't update. host:[%d] FILE:[%s] LINE:[%d]",
			                   			i, DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                alm_c_close_cftp();
                break;
            }
        }
    }
}

int alm_c_tbl_update(struct copy_site *s, char* file, int flag)
{
    struct data_tree *getp, *putp;
    char buf[ALM_BUF_SIZE];
    char new_buf[ALM_BUF_SIZE];
    char err_buf[ALM_BUF_SIZE];
    fpos_t filepos;

    putp = alm_f_search_tree(s->p_root, file);
    if(putp == NULL){
        return(ALM_ERROR);
    }
    getp = alm_f_search_tree(s->g_root, file);
    if(getp == NULL){
        return(ALM_ERROR);
    }
    memset(new_buf,NULL,ALM_BUF_SIZE);

    rewind(s->putfp);
        if (fgetpos(s->putfp, &filepos) != 0){
            sprintf(err_buf,
	    	    "Error: File don't get position. [%s] FILE:[%s] LINE:[%d]",
			                   s->put_file,  DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            sprintf (err_buf, 
		    "Error: HOST:[%s] PUTFILE:[%s]", s->hostname, s->put_file);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            return (ALM_ERROR);
        }
        while((fgets(buf, sizeof(buf), s->putfp)) != NULL){
            if (strcmp(putp->data_buf, buf) == 0){
                fflush(s->putfp);
                if (fsetpos(s->putfp, &filepos) != 0){
                    sprintf(err_buf,
		    "Error: File don't set position. [%s] FILE:[%s] LINE:[%d]",
			                   s->put_file,  DEBUG_INFO);
                    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                    sprintf (err_buf, 
		        "Error: HOST:[%s] PUTFILE:[%s]", s->hostname, s->put_file);
                    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                    return (ALM_ERROR);
                }
	        alm_c_make_new_buf(new_buf, getp->data_buf, flag);
                
                if(fputs(new_buf, s->putfp) == EOF){
                    sprintf(err_buf,
			"Error: String don't set file %s. FILE:[%s] LINE:[%d]",
			                   s->put_file,  DEBUG_INFO);
                    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                    return (ALM_ERROR);
                } else {
                    putp->data_buf = strdup(new_buf);
                    free(putp->cdata);
                    putp->cdata = alm_f_set_copy_struct(putp->cdata, new_buf);
                    fflush(s->putfp); break;
                }
            }
            if (fgetpos(s->putfp, &filepos) != 0){
                sprintf(err_buf,
	    	    "Error: File don't get position. [%s] FILE:[%s] LINE:[%d]",
			                   s->put_file,  DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                sprintf (err_buf, 
		    "Error: HOST:[%s] PUTFILE:[%s]", s->hostname, s->put_file);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                return (ALM_ERROR);
            }
        }
    return (ALM_SUCCESS);
}

void alm_c_make_new_buf(char* new, char* old, int flag)
{
    int num, i, head_tail, tail_head;
    char date[ALM_TIME_SIZE];

    strcpy(new, old);

    num = COPY_NAME_SIZE + COPY_YN_SIZE + COPY_TIME_SIZE + (COPY_DEL*3);
    if (flag == 0) new[num] = 'Y';
    else new[num] = 'E';

    head_tail = num + COPY_YN_SIZE + COPY_DEL;
    tail_head = head_tail + COPY_TIME_SIZE;

    alm_f_log_date(date, ALM_TIME_FMT);

    for (i = 0; i < COPY_TIME_SIZE; i++){
        new[head_tail+i] = date[i];
    }
}

void alm_c_sig_end_copy()
{
    alm_f_error(fp_copy_log, "Information: Get copy end signal from parent process", ALM_WARNING);
    copy_end_flag++;
    return;
}

void alm_c_sig_chld()
{
    int status;
    wait((int *)&status);
}

int alm_c_set_socket(void)
{
    int status;
    char buff[ALM_IPC_BUFSIZ];

    (void)signal(SIGCLD,(void (*)())alm_c_sig_chld);

    c_child_pid = fork();
    switch(c_child_pid){
      case (-1) :
        perror("fork()");
        return (ALM_ERROR);
      case 0 :
        alm_c_exec_shell();
        alm_f_error(fp_copy_log, "Information: Stop child process [alm_c_exec_shell].", ALM_ERROR);
      default :
        sockfd = alm_ipc_parent(g_sock);
        if (sockfd < 0) {
            fprintf(stderr,"alm_ipc_parent() Error.[%s]\n",strerror(errno));
            alm_f_error(fp_copy_log, "Information: alm_ipc_parent() error", ALM_WARNING);
            return (ALM_ERROR);
        }
        break;
    }
    if ((netsoc = accept(sockfd,0,0)) < 0) {
        perror("accept()");
        alm_f_error(fp_copy_log, "Information: accept() error", ALM_WARNING);
        return (ALM_ERROR);
    }
}

int alm_c_send_client(struct copy_site *s, char *file)
{
    int offset;
    int  status;
    char buff[ALM_IPC_BUFSIZ];
    char err_buf[ALM_BUF_SIZE];

    (void)memset(buff,0,sizeof(buff));

    offset = 0;
    if (s == NULL){
        (void)memcpy(&buff[offset]," " ,1);
        offset ++;
        (void)memcpy(&buff[offset]," " ,1);
        offset ++;
    } else {
        (void)memcpy(&buff[offset],file,strlen(file)+1);
        offset += strlen(file)+1;
        (void)memcpy(&buff[offset],s->local_dir,strlen(s->local_dir)+1);
        offset += strlen(s->local_dir)+1;
    }
    (void)memcpy(&buff[offset]," " ,2);
    offset += 2;
    status = alm_ipc_write(netsoc,offset,(char *)&buff[0]);
    if (status != 0) {
        alm_ipc_close(sockfd);
        alm_ipc_close(netsoc);
        fprintf(stderr,"alm_ipc_write() Error.[%s]\n",strerror(errno));
        return(ALM_ERROR);
    }
    sprintf(err_buf, "Information: Write socket, file:[%s]", file);
    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    return(ALM_SUCCESS);
}

void alm_c_exec_shell(void)
{
    int status;
    int c_pid, daily;
    int c_sock;
    char *argument[5], *env, *command;
    char local_file[ALM_NAME_SIZE];
    char compress[] = ".Z";
    char param[ALM_IPC_BUFLEN];
    char path[ALM_IPC_BUFLEN];
    char file[ALM_IPC_BUFLEN];
    char buff[ALM_IPC_BUFSIZ];
    char err_buf[ALM_BUF_SIZE];

    (void)signal(SIGCLD, alm_c_sig_chld);

    c_sock = alm_ipc_child(g_sock,g_time);
    if (c_sock < 0) {
        alm_ipc_close(c_sock);
        fprintf(stderr,"alm_ipc_child() Error.[%s]\n",strerror(errno));
        alm_f_error(fp_copy_log, "Information: alm_ipc_child() Error.", ALM_WARNING);
    }
    for (;;) {
        status = alm_ipc_read(c_sock,file,path,param);
        if (status != 0) {
            if (errno == EINTR) {
                sprintf(err_buf, "Information: alm_ipc_read get EINTR, continue", file);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
		continue;
	    }
            alm_ipc_close(c_sock);
            sprintf(err_buf,"alm_ipc_read() Error.[%s]\n",strerror(errno));
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }
        sprintf(err_buf, "Information: Read socket, file:[%s]", file);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);

        env = alm_f_getenv("ALM_D_OR_W");

        if (strncmp(file, " ", 1) != 0){
            command = alm_f_getenv("ALM_ARRIVAL_COMMAND");
            strcpy(local_file, file);
            strcat(local_file, compress);
            argument[0] = "raw_arrived.sh";
            argument[1] = local_file;
            argument[2] = env;
            argument[3] = path;
            argument[4] = NULL;
            fprintf(stderr, "command = %s com_name = %s file = [%s], D_OR_W = %s, dir = %s\n", command, argument[0], argument[1], argument[2], argument[3]);
            sprintf(err_buf, "Information: command = %s com_name = %s file = [%s], D_OR_W = %s, dir = %s", command, argument[0], argument[1], argument[2], argument[3]);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        } else {
            command = alm_f_getenv("ALM_LOAD_COMMAND");
            argument[0] = "start_load.sh";
            argument[1] = env;
            argument[2] = NULL;
            fprintf(stderr, "command = %s, com_name = %s D_OR_W = %s\n", command, argument[0], argument[1]);
            sprintf(err_buf, "Information: command = %s, com_name = %s D_OR_W = %s", command, argument[0], argument[1]);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }
        if (command == NULL) continue;

        switch(c_pid = fork()){
          case -1:
            alm_f_error(fp_copy_log, "Information: Can't Creat New Process(shell)", ALM_WARNING);
            break;
          case  0:
	    alm_ipc_close(c_sock);
            sprintf(err_buf, "Information: Shell start, file:[%s]", file);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            execvp(command, argument);
            sprintf(err_buf, "Information: Shell End, file:[%s]", file);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            alm_f_error(fp_copy_log, "Information: Complete to exec shell.", ALM_ERROR);
          default:
            break;
        }
    }
}

int alm_c_first_actions(int site_num)
{
    int status;
    int update_num = 0;
    char file[ALM_NAME_SIZE];
    char tempfile[ALM_BUF_SIZE];
    char err_buf[ALM_BUF_SIZE];
    int    cc;

    if ((status = alm_c_first_get_tbl(site_num)) != ALM_SUCCESS) return(status);
    if(file_init_flag != ALM_INIT){
        rewind(site_p[site_num]->getfp);
        rewind(site_p[site_num]->newfp);
        while((cc = fgetc(site_p[site_num]->getfp)) != EOF){
            fputc(cc,site_p[site_num]->newfp);
        }
        fflush(site_p[site_num]->newfp);
        status = alm_f_check_file(site_p[site_num]->new_file
						,site_p[site_num]->newfp);
        if(status == ALM_ERROR){
            dist_flag[site_num] = status;
            return(status);
        }
        if((site_p[site_num]->n_root = alm_c_set_tbl(site_p[site_num]->newfp)) 
								== NULL){
            sprintf(err_buf, 
	    	"Warning: Host_id(%d) table file not exist FILE:[%s] LINE:[%d]"
							,site_num,DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }
    }

    for(FOREVER){
        strcpy(file,alm_c_first_compare(site_num));
        if(strlen(file) != 0){
            if((status = (alm_c_first_update(site_num,file))) != ALM_SUCCESS){
                return(status);
            }
            update_num++;
        }
        else{
            if(update_num == 0){
                sprintf(err_buf, "Information: Data don't update. host:[%d] FILE:[%s] LINE:[%d]",site_num,DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            }
            else{
                sprintf(err_buf,"Information: %d Data update. host:[%d] FILE:[%s] LINE:[%d]",update_num,site_num,DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            }
            return(ALM_SUCCESS);
        }
    }
}

int alm_c_first_get_tbl(int i)
{
    char err_buf[ALM_BUF_SIZE];
    char rpl_buf[ALM_BUF_SIZE];
    char file[ALM_NAME_SIZE];
    char remote_file[ALM_NAME_SIZE];
    char *get_file_name, *env;
    int status, size, stat;
    int wait_time;

    get_file_name = alm_f_getenv("ALM_DIST_FILE");

    if (i == ALM_IBM_OS){
        if ((env = alm_f_getenv("ALM_COPY_INTERVAL_TIME")) != NULL){
            wait_time = atoi(env);
        } else wait_time = 120;
        sprintf(err_buf,
	      "Information: Copy interval. TIME:[%d]", wait_time);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        sleep(wait_time);
    }


    if (strlen(site_p[i]->remote_dir) != 0){
        strcpy(remote_file, site_p[i]->remote_dir);
        strcat(remote_file, get_file_name);
    } else return(ALM_NOTFOUND);

    if ((status = cftp_get_file(ftp_disc, site_p[i]->get_file,
				remote_file, &size)) != FTP__NORMAL){
        if (status == FTP__REMERROR){
            stat = cftp_get_err_reply(ftp_disc, rpl_buf);
            sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   rpl_buf,  DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }
        sprintf(err_buf, "Error: cftp_get_file:%s FILE:[%s] LINE:[%d]",
			                   cftp_strerror(status),  DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        alm_c_close_cftp();
        return(ALM_NOTFOUND);
    }
    if (size <= 0) {
        sprintf(err_buf,
           "Information: File management tbl is empty:[%s]", remote_file);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        alm_c_close_cftp();
        return(ALM_ERROR);
    }
    alm_c_set_get_tbl_fp();
    if((alm_f_check_data(site_p[i]->getfp,remote_file)) == ALM_ERROR){
        alm_c_close_cftp();
        return(ALM_ERROR);
    }
    sprintf(err_buf, "Information: Get file. [%s]", site_p[i]->get_file);
    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    return(ALM_SUCCESS);
}

char* alm_c_first_compare(int i)
{
    char err_buf[ALM_BUF_SIZE];
    struct data_tree *get,*old;

    alm_c_set_get_tbl_fp();

    if((site_p[i]->g_root = alm_c_set_tbl(site_p[i]->getfp)) == NULL){
        sprintf(err_buf,"Warning: %s Table file not exist FILE:[%s] LINE:[%d]",site_p[i]->get_file, DEBUG_INFO);
        alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
    }

    get = site_p[i]->g_root;
    old = site_p[i]->o_root;

    if((get != NULL) && (old != NULL)){
        memset(make_file,NULL,ALM_NAME_SIZE);
        alm_c_name_match_p(get,old);
        return(make_file);
    }
    return("");
}

int alm_c_first_update(int i, char *file)
{
    struct data_tree *getp,*newp,*oldp;
    char   err_buf[ALM_BUF_SIZE];
    char   buf[ALM_BUF_SIZE];
    char   temp_buf[ALM_BUF_SIZE];
    fpos_t filepos;
    
    getp = alm_f_search_tree(site_p[i]->g_root, file);
    if(getp == NULL){
        return(ALM_ERROR);
    }
    newp = alm_f_search_tree(site_p[i]->n_root, file);
    if(newp == NULL){
        return(ALM_ERROR);
    }
    oldp = alm_f_search_tree(site_p[i]->o_root, file);
    if(oldp == NULL){
        return(ALM_ERROR);
    }

    rewind(site_p[i]->newfp);
        if(fgetpos(site_p[i]->newfp, &filepos) != 0){
            sprintf(err_buf,"Error: File don't get position. [%s] FILE:[%s] LINE:[%d]",site_p[i]->new_file, DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            return(ALM_ERROR);
        }
        while((fgets(buf,sizeof(buf),site_p[i]->newfp)) != NULL){
            if(strcmp(newp->data_buf,buf) == 0){
                fflush(site_p[i]->newfp);
                if(fsetpos(site_p[i]->newfp,&filepos) != 0){
                    sprintf(err_buf,"Error: File don't set position. [%s] FILE:[%s] LINE:[%d]",site_p[i]->new_file, DEBUG_INFO);
                    alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                    return(ALM_ERROR);
                }
                strcpy(temp_buf,getp->data_buf);
                if(fputs(temp_buf,site_p[i]->newfp) == EOF){
                    sprintf(err_buf,"Error: String don't set file %s. FILE:[%s] LINE:[%d]",site_p[i]->new_file,DEBUG_INFO);
                    alm_f_error(fp_copy_log,err_buf,ALM_WARNING);
                    return(ALM_ERROR);
                }
                else{
                    fflush(site_p[i]->newfp);
                    oldp->data_buf = strdup(temp_buf);
                    free(oldp->cdata);
                    oldp->cdata 
                        = alm_f_set_copy_struct(oldp->cdata,oldp->data_buf);
                    break;
                }
            }
            if(fgetpos(site_p[i]->newfp, &filepos) != 0){
                sprintf(err_buf,"Error: File don't get position. [%s] FILE:[%s] LINE:[%d]",site_p[i]->new_file, DEBUG_INFO);
                alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
                return(ALM_ERROR);
            }
        }
    return(ALM_SUCCESS);
}

void alm_c_first_set_tbl_all()
{
    char err_buf[ALM_BUF_SIZE];
    int i,status;

    for(i = 0; i < ALM_HOST_NUM; i++){
        status = alm_f_check_file(site_p[i]->old_file,site_p[i]->oldfp);
        if(status == ALM_ERROR){
            dist_flag[i] = status;
        }
        if((site_p[i]->o_root = alm_c_set_tbl(site_p[i]->oldfp)) == NULL){
            sprintf(err_buf,"Warning: Host_id(%d) table file not exist FILE:[%s] LINE:[%d]",i,DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
            dist_flag[i] = ALM_ERROR;
        }

        status = alm_f_check_file(site_p[i]->new_file,site_p[i]->newfp);
        if(status == ALM_ERROR){
            dist_flag[i] = status;
        }
        if((site_p[i]->n_root = alm_c_set_tbl(site_p[i]->newfp)) == NULL){
            sprintf(err_buf,"Warning: Host_id(%d) table file not exist FILE:[%s] LINE:[%d]",i,DEBUG_INFO);
            alm_f_error(fp_copy_log, err_buf, ALM_WARNING);
        }

        status = alm_f_check_file(site_p[i]->base_file,site_p[i]->basefp);
        if(status == ALM_ERROR){
            dist_flag[i] = status;
        }
    }
}

void alm_c_first_init()
{
    char *env;
    int i;


    site_p[ALM_IBM_OS]->new_file = alm_f_getenv("ALM_IBM_OS_NEW_FILE");
    site_p[ALM_IBM_OS]->old_file = alm_f_getenv("ALM_IBM_OS_OLD_FILE");
    if((env = alm_f_getenv("ALM_IBM_OS_NEW_FILE")) != NULL){
        site_p[ALM_IBM_OS]->newfp = fopen(env,ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_IBM_OS_OLD_FILE")) != NULL){
        site_p[ALM_IBM_OS]->oldfp = fopen(env,ALM_FOPEN_R);
    }

    site_p[ALM_IBM_TK]->new_file = alm_f_getenv("ALM_IBM_TK_NEW_FILE");
    site_p[ALM_IBM_TK]->old_file = alm_f_getenv("ALM_IBM_TK_OLD_FILE");
    if((env = alm_f_getenv("ALM_IBM_TK_NEW_FILE")) != NULL){
        site_p[ALM_IBM_TK]->newfp = fopen(env,ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_IBM_TK_OLD_FILE")) != NULL){
        site_p[ALM_IBM_TK]->oldfp = fopen(env,ALM_FOPEN_R);
    }

    site_p[ALM_UNI_TK]->new_file = alm_f_getenv("ALM_UNI_TK_NEW_FILE");
    site_p[ALM_UNI_TK]->old_file = alm_f_getenv("ALM_UNI_TK_OLD_FILE");
    if((env = alm_f_getenv("ALM_UNI_TK_NEW_FILE")) != NULL){
        site_p[ALM_UNI_TK]->newfp = fopen(env,ALM_FOPEN_R);
    }
    if((env = alm_f_getenv("ALM_UNI_TK_OLD_FILE")) != NULL){
        site_p[ALM_UNI_TK]->oldfp = fopen(env,ALM_FOPEN_R);
    }


    alm_c_first_set_tbl_all();
}

void alm_c_set_put_file_fp()
{
    char *env;
    int  i;

    for(i = 0; i < ALM_HOST_NUM; i++){
        if(site_p[i]->putfp != NULL){
            fclose(site_p[i]->putfp);
        }
    }

    if ((env = alm_f_getenv("ALM_IBM_OS_PUT_FILE")) != NULL){
        site_p[ALM_IBM_OS]->putfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_IBM_TK_PUT_FILE")) != NULL){
        site_p[ALM_IBM_TK]->putfp = fopen(env, ALM_FOPEN_R);
    }
    if ((env = alm_f_getenv("ALM_UNI_TK_PUT_FILE")) != NULL){
        site_p[ALM_UNI_TK]->putfp = fopen(env, ALM_FOPEN_R);
    }
}
T.RTitleUserPersonal
Name
DateLines
9033.1UNIX Ver.3.2d on 8400TKOV60::OGINOWed Mar 05 1997 00:387
These programs are run on Digital UNIX Ver3.2d on AlphaServer8400.
Remote systems are two RS6000s and a UNISYS host. They are accessed via tel. 
line(modem). 

Thanks,

Yoji