static gint iend[13];
static void fill_iend_array(void);
static void fill_cr_array_all(const gchar *contest_line, gint qnum);
static void make_contest_line(gchar *contest_line, gint qnum);
static void read_ini_file(void);
static void read_contest_file(void);
static void read_ded_file(void);
static void read_locus_file(void);
static void read_country_file(void);
static void read_call_library_file(void);
static void read_help_file(void);
static void write_empty_file(void);
static gint write_backup_file(const gchar *filnam);
static void write_ini_file(void);
static void write_call_record(gint send_record);
static void write_ded_file(gint qnum, const gchar *cmd, gint send_record);
static void rewrite_contest_file(void);
static void write_call_library_file(void);
static void read_radio_file(void);
static void write_radio_file(const gchar *filnam, gint iupd);

static void fill_iend_array(void)
{
  iend[0] = sizeof(cr_array[0].qso)-2;
  iend[1] = iend[0]+1;
  iend[2] = iend[1]+1;
  iend[3] = iend[2]+sizeof(cr_array[0].on_off)-1;
  iend[4] = iend[3]+sizeof(cr_array[0].date)-1;
  iend[5] = iend[4]+sizeof(cr_array[0].time)-1;
  iend[6] = iend[5]+sizeof(cr_array[0].call)-1;
  iend[7] = iend[6]+sizeof(cr_array[0].sent)-1;
  iend[8] = iend[7]+sizeof(cr_array[0].rec_num)-1;
  iend[9] = iend[8]+sizeof(cr_array[0].rec_county)-1;
  iend[10] = iend[9]+sizeof(cr_array[0].points)-1;
  iend[11] = iend[10]+sizeof(cr_array[0].tx)-1;
  iend[12] = iend[11]+sizeof(cr_array[0].prefix)-1;
}

static void fill_cr_array_all(const gchar *contest_line, gint qnum)
{
  gchar mbn[2], field_hold[20];

  concat(cr_array[qnum].qso, contest_line, 0, iend[0], COPY);
  concat(mbn, contest_line, iend[0]+1, iend[1], COPY);
  cr_array[qnum].iband = atoi(mbn);
  concat(mbn, contest_line, iend[1]+1, iend[2], COPY);
  cr_array[qnum].mode = atoi(mbn);
  concat(cr_array[qnum].on_off, contest_line,
         iend[2]+1, iend[3], COPY);
  concat(cr_array[qnum].date, contest_line,
         iend[3]+1, iend[4], COPY);
  concat(cr_array[qnum].time, contest_line,
         iend[4]+1, iend[5], COPY);
  concat(field_hold, contest_line, iend[5]+1, iend[6], COPY);
  right_blank_unpad (field_hold);
  strcpy(cr_array[qnum].call, field_hold);
  concat(field_hold, contest_line, iend[6]+1, iend[7], COPY);
  right_blank_unpad (field_hold);
  strcpy(cr_array[qnum].sent, field_hold);
  concat(field_hold, contest_line, iend[7]+1, iend[8], COPY);
  right_blank_unpad (field_hold);
  strcpy(cr_array[qnum].rec_num, field_hold);
  concat(field_hold, contest_line, iend[8]+1, iend[9], COPY);
  right_blank_unpad (field_hold);
  strcpy(cr_array[qnum].rec_county, field_hold);
  concat(cr_array[qnum].points, contest_line,
         iend[9]+1, iend[10], COPY);
  concat(cr_array[qnum].tx, contest_line,
         iend[10]+1, iend[11], COPY);
  concat(field_hold, contest_line, iend[11]+1, iend[12], COPY);
  right_blank_unpad (field_hold);
  strcpy(cr_array[qnum].prefix, field_hold);
}

static void make_contest_line(gchar *contest_line, gint qnum)
{
  gchar mbn[2], field_hold[20];

  strcpy(contest_line, cr_array[qnum].qso);
  int_to_char (cr_array[qnum].iband, mbn);
  strcat(contest_line, mbn);
  int_to_char (cr_array[qnum].mode, mbn);
  strcat(contest_line, mbn);
  strcat(contest_line, cr_array[qnum].on_off);
  strcat(contest_line, cr_array[qnum].date);
  strcat(contest_line, cr_array[qnum].time);
  strcpy(field_hold, cr_array[qnum].call);
  right_blank_pad(field_hold, sizeof(cr_array[qnum].call));
  strcat(contest_line, field_hold);
  strcpy(field_hold, cr_array[qnum].sent);
  right_blank_pad(field_hold, sizeof(cr_array[qnum].sent));
  strcat(contest_line, field_hold);
  strcpy(field_hold, cr_array[qnum].rec_num);
  right_blank_pad(field_hold, sizeof(cr_array[qnum].rec_num));
  strcat(contest_line, field_hold);
  strcpy(field_hold, cr_array[qnum].rec_county);
  right_blank_pad(field_hold, sizeof(cr_array[qnum].rec_county));
  strcat(contest_line, field_hold);
  strcat(contest_line, cr_array[qnum].points);
  strcat(contest_line, cr_array[qnum].tx);
  strcpy(field_hold, cr_array[qnum].prefix);
  right_blank_pad(field_hold, sizeof(cr_array[qnum].prefix));
  strcat(contest_line, field_hold);
  add_eol_chars (contest_line);
}

static void read_ini_file(void)
{
  FILE *fp;
  gchar filnam[13], msg[49], contest_ini_line[76];
  gint i, icolon, lcil, socil;
  guint answer;
  gint i1=0;

  strcpy(filnam, contest_file_base);
  strcat(filnam, ".INI");
  fp = fopen(filnam, "r");
  if(fp==NULL)
   {
    strcpy(msg, filnam);
    strcat(msg, " does not exist.  Use default? (Y/N)");
    play_bells (DUPE_SOUND);
    answer = boxed_message (msg, 0, LT_GRAY);
    if(toupper_ui(answer)=='Y') fp = fopen("DEFAULT.INI","r");
    if(fp==NULL) return;
   }
  socil = sizeof(contest_ini_line);
  for(i=0; i<MAX_INIT_ITEMS; i++)
   {
    fgets(contest_ini_line, socil, fp);
    if(feof(fp)) break;
    icolon = kb_index (contest_ini_line, ":");
    if(icolon>-1)
     {
      lcil = strlen(contest_ini_line);
      if(contest_ini_line[lcil-1]=='\n') contest_ini_line[lcil-1] = '\0';
      if(contest_ini_line[lcil-2]=='\r') contest_ini_line[lcil-2] = '\0';
      lcil = strlen(contest_ini_line);
      if(lcil>icolon+1)
       concat(init_screen_choices[i], contest_ini_line,
              icolon+1, lcil-1, COPY);
     }
   }
  if(!kb_index (contest_ini_line, set_screen_items[0]))
   {
    if(lcil>icolon+1)
     concat(set_screen_choices[0], contest_ini_line,
            icolon+1, lcil-1, COPY);
    strcpy(init_screen_choices[MAX_INIT_ITEMS-2], port_bracket[0]);
    strcpy(init_screen_choices[MAX_INIT_ITEMS-1], radio_bracket[0]);
    i1 = 1;
   }
  for(i=i1; i<MAX_SET_ITEMS; i++)
   {
    fgets(contest_ini_line, socil, fp);
    if(feof(fp)) break;
    icolon = kb_index (contest_ini_line, ":");
    if(icolon>-1)
     {
      lcil = strlen(contest_ini_line);
      if(contest_ini_line[lcil-1]=='\n') contest_ini_line[lcil-1] = '\0';
      if(contest_ini_line[lcil-2]=='\r') contest_ini_line[lcil-2] = '\0';
      lcil = strlen(contest_ini_line);
      if(lcil>icolon+1)
       concat(set_screen_choices[i], contest_ini_line,
              icolon+1, lcil-1, COPY);
     }
   }
  i1 = 0;
  if(!kb_index (contest_ini_line, "Keyer Speed:"))
   {
    if(lcil>icolon+1)
     concat(keyer_screen_choices[0], contest_ini_line,
            icolon+1, lcil-1, COPY);
    strcpy(set_screen_choices[MAX_SET_ITEMS-2], yn_bracket[1]);
    strcpy(set_screen_choices[MAX_SET_ITEMS-1], cat_bracket[0]);
    i1 = 1;
   }
  if(!strcmp(set_screen_choices[1], "<NEVER>"))
   strcpy(set_screen_choices[1], dupe_bracket[0]);
  for(i=i1; i<MAX_KEYER_ITEMS; i++)
   {
    fgets(contest_ini_line, socil, fp);
    if(feof(fp)) break;
    icolon = kb_index (contest_ini_line, ":");
    if(icolon>-1)
     {
      lcil = strlen(contest_ini_line);
      if(contest_ini_line[lcil-1]=='\n') contest_ini_line[lcil-1] = '\0';
      if(contest_ini_line[lcil-2]=='\r') contest_ini_line[lcil-2] = '\0';
      lcil = strlen(contest_ini_line);
      if(lcil>icolon+1)
       concat(keyer_screen_choices[i], contest_ini_line,
              icolon+1, lcil-1, COPY);
     }
   }
  fclose(fp);
}

static void read_contest_file(void)
{
  FILE *fp;
  gint i, socl;
  gchar filnam[13], msg[49], contest_line[76];
  gchar ssc_hold[9];

  strcpy(filnam, contest_file_name);
  fp = fopen(filnam, "r");
  if(fp==NULL)
   {
    strcpy(msg, "Error reading ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  icall_count = 0;
  socl = sizeof(contest_line);
  for( ; ; )
   {
    fgets(contest_line, socl, fp);
    if(feof(fp)) break;
    icall_count++;
    fill_cr_array_all (contest_line, icall_count);
   }
  fclose(fp);
  iscroll_log = icall_count;
  for(i=0; i<icall_count; i++)
   {
    if(check_for_dupe (i+1, i, FALSE, main_drawing_area))
     strcpy(cr_array[i+1].dupe, "*");
    insert_tree (i+1, i);
   }
  for(i=1; i<=icall_count; i++)
   check_for_mult (i, RESTART);
  for(i=1; i<=icall_count; i++)
   increment_totals (i);
  strcpy(cr_array[icall_count+1].on_off, on_off_char[1]);
  if(icall_count)
   {
    iband = cr_array[icall_count].iband;
    mode = cr_array[icall_count].mode;
    cr_array[icall_count+1].iband = cr_array[icall_count].iband;
    cr_array[icall_count+1].mode = cr_array[icall_count].mode;
    strcpy(cr_array[icall_count+1].on_off, on_off_char[2]);
    switch (isel_contest)
     {
      case FD :
      case SS :
      case WAA :
       strcpy(cr_array[icall_count+1].rec_num, "");
       break;
      default :
       strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
     }
    if(isel_contest==TEN || isel_contest==NYQP)
     {
      if(mode==CW && atoi(set_screen_choices[0])<111)
       {
        strcpy(ssc_hold, set_screen_choices[0]);
        concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
        strcat(set_screen_choices[0], "9");
        concat(set_screen_choices[0], ssc_hold, 2, strlen(ssc_hold), !COPY);
        write_ini_file ();
       }
      else if(mode==PH && atoi(set_screen_choices[0])>59)
       {
        strcpy(ssc_hold, set_screen_choices[0]);
        concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
        concat(set_screen_choices[0], ssc_hold, 3, strlen(ssc_hold), !COPY);
        write_ini_file ();
       }
     }
    strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
   }
}

static void read_ded_file(void)
{
  FILE *fp;
  gint socl;
  gchar filnam[13], contest_line[76];

  ded_num = 0;
  strcpy(filnam, contest_file_base);
  strcat(filnam, ".DED");
  fp = fopen(filnam, "r");
  if(fp==NULL)
   return;
  socl = sizeof(contest_line);
  for( ; ; )
   {
    fgets(contest_line, socl, fp);
    if(feof(fp)) break;
    ded_num++;
    strcpy(ded_array[ded_num], contest_line);
   }
  fclose(fp);
}

static void read_locus_file(void)
{
  FILE *fp;
  gint i, j, icn, soli;
  gchar filnam[13], msg[49], clat[5], clong[6];
  gchar line_in[80];
  gint n=0, moichido=1;
  gint col_num[3][8] = {
              {0, 14, 16, 19, 21, 24, 26, 30}, /* COUNTY.CQP */
              {0, 20, 22, 24, 26, 29, 31, 35}, /* SECTION.LST */
              {0, 15, 17, 18, 20, 23, 25, 29}}; /* STATE.LST */

  for(i=0; i<MAX_LOCI; i++)
   {
    loci.locus_name[i][0] = '\0';
    loci.locus_abv[i][0] = '\0';
    loci.locus_lat[i] = 0.;
    loci.locus_long[i] = 0.;
   }
  for(i=0; i<12; i++)
   {
    for(j=0; j<MAX_LOCI; j++)
     loci.locus_worked[i][j] = 0;
   }
  switch (isel_contest)
   {
    case NYQP :
     moichido = 2;
     icn = 1;
     strcpy(filnam, "COUNTY.NY");
     break;
    case FD :
    case SS :
    case A160 :
     icn = 1;
     strcpy(filnam, "SECTION.LST");
     break;
    case TEN :
     moichido = 2;
     icn = 2;
     strcpy(filnam, "STATE.LST");
     break;
    case WAA :
     icn = 2;
     strcpy(filnam, "STATE.LST");
     break;
    default :
     loci.number_of_loci = -1;
     return;
   }
  while(moichido)
   {
    fp = fopen(filnam, "r");
    if(fp==NULL)
     {
      strcpy(msg, "Error reading ");
      strcat(msg, filnam);
      rw_error_box (main_drawing_area, msg);
      return;
     }
    soli = sizeof(line_in);
    for( ; ; )
     {
      fgets(line_in, soli, fp);
      if(feof(fp) || strlen(line_in)<col_num[icn][7])
       break;
      n++;
      concat(loci.locus_name[n], line_in,
             col_num[icn][0], col_num[icn][1], COPY);
      concat(loci.locus_abv[n], line_in,
             col_num[icn][2], col_num[icn][3], COPY);
      concat(clat, line_in, col_num[icn][4], col_num[icn][5], COPY);
      loci.locus_lat[n] = atof(clat);
      concat(clong, line_in, col_num[icn][6], col_num[icn][7], COPY);
      loci.locus_long[n] = atof(clong);
     }
    fclose(fp);
    moichido--;
    if(moichido)
     {
      if(isel_contest==TEN)
       {
        icn = 1;
        strcpy(filnam, "XESTATE.LST");
       }
      else if(isel_contest==NYQP)
       {
        loci.number_of_countyloci = n;
        icn = 2;
        strcpy(filnam, "NYPSTATE.LST");
       }
     }
   }
  strcpy(loci.locus_name[0], "Unknownisville");
  concat(loci.locus_abv[0], "UNKNOWN", 0,
         col_num[icn][3]-col_num[icn][2], COPY);
  loci.locus_lat[0] = -89.;
  if(isel_contest==FD || isel_contest==NYQP)
   {
    n++;
    strcpy(loci.locus_name[n], "Dxistan");
    strcpy(loci.locus_abv[n], "DX");
    loci.locus_lat[n] = -89.;
   }
  for(i=0; i<=n; i++)
   {
    right_blank_unpad (loci.locus_name[i]);
    right_blank_unpad (loci.locus_abv[i]);
   }
  loci.number_of_loci = n;
  loci.number_of_usloci = n;
  for(i=0; i<=n; i++)
   if(!strcmp(loci.locus_abv[i], "SD"))
    {
     loci.number_of_usloci = i;
     break;
    }
  if(isel_contest==NYQP)
   if(strcmp(init_screen_choices[6], "NY"))
    {
     loci.number_of_loci = loci.number_of_countyloci;
     loci.number_of_usloci = loci.number_of_countyloci;
    }
}

static void read_country_file(void)
{
  FILE *fp;
  gint i, j, k, lcil, comma, soli;
  gchar filnam[13], msg[49], clat[5], clong[6], cabv[5];
  gchar line_in[80], country_del[8], contest_nc[NUMBER_OF_CONTESTS][208];
  gint n=0, notc=-1, nsyn=0, idel=-1, cstart=9;

  strcpy(filnam, "COUNTRY.LST");
  fp = fopen(filnam, "r");
  if(fp==NULL)
   {
    strcpy(msg, "Error reading ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  for(i=0; i<NUMBER_OF_CONTESTS; i++)
   contest_nc[i][0] = '\0';
  for(i=0; i<MAX_COUNTRIES; i++)
   {
    countries.country_name[i][0] = '\0';
    countries.country_abv[i][0] = '\0';
    countries.country_syn[i][0] = '\0';
    countries.continent_abv[i][0] = '\0';
    countries.zones_in_country[i][0] = '\0';
    countries.country_lat[i] = 0.;
    countries.country_long[i] = 0.;
   }
  for(i=0; i<6; i++)
   {
    for(j=0; j<MAX_COUNTRIES; j++)
     countries.icountry_worked[i][j] = 0;
    for(j=0; j<MAX_ZONES; j++)
     countries.izone_worked[i][j] = 0;
   }
  soli = sizeof(line_in);
  for( ; ; )
   {
    fgets(line_in, soli, fp);
    if(feof(fp)) break;
    lcil = strlen(line_in);
    if(line_in[lcil-1]=='\n') line_in[lcil-1] = '\0';
    if(line_in[lcil-2]=='\r') line_in[lcil-2] = '\0';
    lcil = strlen(line_in);
    if(line_in[0]=='-')
     {
      notc++;
      concat(contest_nc[notc], line_in, 2, lcil-1, COPY);
     }
    else if(line_in[0]==' ')
     concat(contest_nc[notc], line_in, 11, lcil-1, !COPY);
    else if(line_in[0]=='=')
     {
      if(nsyn)
       {
        concat(countries.country_syn[n], ",", 0, 0, !COPY);
        concat(countries.country_syn[n], line_in, 2, lcil-1, !COPY);
       }
      else
       {
        nsyn = 1;
        concat(countries.country_syn[n], line_in, 2, lcil-1, COPY);
       }
     }
    else
     {
      n++;
      nsyn = 0;
      concat(countries.country_name[n], line_in, 0, 30, COPY);
      concat(countries.country_abv[n], line_in, 32, 38, COPY);
      concat(countries.zones_in_country[n], line_in, 56, lcil-1, COPY);
      concat(clat, line_in, 40, 43, COPY);
      countries.country_lat[n] = atof(clat);
      if(line_in[44]=='S')
       countries.country_lat[n] *= -1;
      concat(clong, line_in, 46, 50, COPY);
      countries.country_long[n] = atof(clong);
      if(line_in[51]=='E')
       countries.country_long[n] *= -1;
      concat(countries.continent_abv[n], line_in, 53, 54, COPY);
     }
   }
  fclose(fp);
  strcpy(countries.country_name[0], "Unknownistan");
  strcpy(countries.country_abv[0], "UNKNOWN");
  strcpy(countries.country_syn[0], "UNKNOWN");
  strcpy(countries.continent_abv[0], "AN");
  strcpy(countries.zones_in_country[0], "00");
  countries.country_lat[0] = -89.;
  for(i=0; i<=n; i++)
   {
    right_blank_unpad (countries.country_name[i]);
    right_blank_unpad (countries.country_abv[i]);
    right_blank_unpad (countries.country_syn[i]);
    right_blank_unpad (countries.continent_abv[i]);
    right_blank_unpad (countries.zones_in_country[i]);
   }
  for(i=0; i<=notc; i++)
   {
    strcpy(cabv, contest_abvs[isel_contest]);
    if(!kb_index (contest_nc[i], cabv))
     {
      idel = i;
      lcil = strlen(contest_nc[i]);
      if(contest_nc[i][lcil-1]!=',')
       {
        contest_nc[i][lcil] = ',';
        contest_nc[i][lcil+1] = '\0';
       }
      break;
     }
    }
  comma = cstart-1;
  for(i=cstart; i<strlen(contest_nc[idel]); i++)
   {
    if(contest_nc[idel][i]==',')
     {
      concat(country_del, contest_nc[idel], comma+1, i-1, COPY);
      for(j=1; j<=n; j++)
       if(!strcmp(country_del, countries.country_abv[j]))
        {
         while(!strcmp(country_del, countries.country_abv[j]))
          {
           for(k=j; k<=n; k++)
            {
             strcpy(countries.country_name[k],
                    countries.country_name[k+1]);
             strcpy(countries.country_abv[k],
                    countries.country_abv[k+1]);
             strcpy(countries.country_syn[k],
                    countries.country_syn[k+1]);
             strcpy(countries.continent_abv[k],
                    countries.continent_abv[k+1]);
             strcpy(countries.zones_in_country[k],
                    countries.zones_in_country[k+1]);
             countries.country_lat[k] = countries.country_lat[k+1];
             countries.country_long[k] = countries.country_long[k+1];
            }
           n--;
          }
         break;
        }
      comma = i;
     }
   }
  for(i=0; i<=n; i++)
   if(!countries.country_syn[i][0])
    {
     lcil = kb_index (countries.country_abv[i], "-");
     if(lcil<0)
      lcil = kb_index (countries.country_abv[i], "(");
     if(lcil<0)
      lcil = strlen(countries.country_abv[i]);
     concat(countries.country_syn[i], countries.country_abv[i],
            0, lcil-1, COPY);
    }
  for(i=0; i<=n; i++)
   {
    lcil = strlen(countries.country_syn[i]);
    if(countries.country_syn[i][lcil-1]!=',')
     {
      countries.country_syn[i][lcil] = ',';
      countries.country_syn[i][lcil+1] = '\0';
     }
   }
  countries.number_of_countries = n;
}

static void read_call_library_file(void)
{
  FILE *fp;
  gint i, lcil, soli;
  gchar msg[49];
  gchar line_in[65];
  gint n=-1;
  gint col_num[18] = {0, 12, 14, 17, 19, 25, 27, 28, 30, 31,
                      33, 34, 36, 38, 40, 58, 60, 61};

  switch (isel_contest)
   {
    case FD :
     strcpy(clib.filnam, "CALL_FD.LST");
     break;
    case SS :
     strcpy(clib.filnam, "CALL_SS.LST");
     break;
    default :
     strcpy(clib.filnam, "CALL_LIB.LST");
   }
  fp = fopen(clib.filnam, "r");
  if(fp==NULL)
   {
    strcpy(msg, "Error reading ");
    strcat(msg, clib.filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  soli = sizeof(line_in);
  for( ; ; )
   {
    fgets(line_in, soli, fp);
    if(feof(fp)) break;
    lcil = strlen(line_in);
    if(line_in[lcil-1]=='\n') line_in[lcil-1] = '\0';
    if(line_in[lcil-2]=='\r') line_in[lcil-2] = '\0';
    right_blank_pad (line_in, soli);
    lcil = strlen(line_in);
    n++;
    concat(clib.call[n], line_in,
           col_num[0], col_num[1], COPY);
    concat(clib.year[n], line_in,
           col_num[2], col_num[3], COPY);
    concat(clib.country[n], line_in,
           col_num[4], col_num[5], COPY);
    concat(clib.cqzone[n], line_in,
           col_num[6], col_num[7], COPY);
    concat(clib.iaruzone[n], line_in,
           col_num[8], col_num[9], COPY);
    concat(clib.state[n], line_in,
           col_num[10], col_num[11], COPY);
    concat(clib.section[n], line_in,
           col_num[12], col_num[13], COPY);
    concat(clib.county[n], line_in,
           col_num[14], col_num[15], COPY);
    concat(clib.check[n], line_in,
           col_num[16], col_num[17], COPY);
   }
  fclose(fp);
  for(i=0; i<=n; i++)
   {
    right_blank_unpad (clib.call[i]);
    right_blank_unpad (clib.country[i]);
    right_blank_unpad (clib.section[i]);
    right_blank_unpad (clib.county[i]);
   }
  clib.number_of_calls = n+1;
}

static void read_help_file(void)
{
  FILE *fp;
  gint soli;
  gchar filnam[13], msg[49];
  gchar line_in[80];
  gint n=-1;

  switch (isel_contest)
   {
    case FD :
     strcpy(filnam, "HELPFD.LST");
     break;
    default :
     strcpy(filnam, "HELP.LST");
   }
  fp = fopen(filnam, "r");
  if(fp==NULL)
   {
    strcpy(msg, "Error reading ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  soli = sizeof(line_in);
  for( ; ; )
   {
    fgets(line_in, soli, fp);
    if(feof(fp)) break;
    remove_eol_chars (line_in);
    n++;
    strcpy(helpbv.help_lines[n], line_in);
   }
  fclose(fp);
  ipage_screen[1][IDT_HELP] = n/12;
  helpbv.nhl = n+1;
}

static void write_empty_file(void)
{
  FILE *fp;
  gchar filnam[13], msg[49];

  strcpy(filnam, contest_file_name);
  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
//  fflush(fp);
//  fsync(fileno(fp));
  fclose(fp);
}

static gint write_backup_file(const gchar *filnam)
{
  gint icopy, idot;
  gchar filnam_hold[13], msg[49];

  idot = kb_index (filnam, ".");
  if(idot>-1)
   concat(filnam_hold, filnam, 0, idot-1, COPY);
  else
   strcpy(filnam_hold, filnam);
  strcpy(msg, "cp ");
  strcat(msg, filnam);
  strcat(msg, " ");
  strcat(msg, filnam_hold);
  strcat(msg, ".BAK");
  icopy = system(msg);
  return icopy;
}

static void write_ini_file(void)
{
  FILE *fp;
  gchar filnam[13], msg[49], contest_ini_line[76];
  gint i;

  strcpy(filnam, contest_file_base);
  strcat(filnam, ".INI");
  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  for(i=0; i<MAX_INIT_ITEMS; i++)
   {
    strcpy(contest_ini_line, init_screen_items[i]);
    left_blank_unpad(contest_ini_line);
    strcat(contest_ini_line, ":");
    strcat(contest_ini_line, init_screen_choices[i]);
    add_eol_chars (contest_ini_line);
    fputs(contest_ini_line, fp);
   }
  for(i=0; i<MAX_SET_ITEMS; i++)
   {
    strcpy(contest_ini_line, set_screen_items[i]);
    left_blank_unpad(contest_ini_line);
    strcat(contest_ini_line, ":");
    strcat(contest_ini_line, set_screen_choices[i]);
    add_eol_chars (contest_ini_line);
    fputs(contest_ini_line, fp);
   }
  for(i=0; i<MAX_KEYER_ITEMS; i++)
   {
    strcpy(contest_ini_line, keyer_screen_items[i]);
    left_blank_unpad(contest_ini_line);
    strcat(contest_ini_line, ":");
    strcat(contest_ini_line, keyer_screen_choices[i]);
    add_eol_chars (contest_ini_line);
    fputs(contest_ini_line, fp);
   }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_call_record(gint send_record)
{
  FILE *fp;
  gchar filnam[13], msg[49], contest_line[76];

  strcpy(filnam, contest_file_name);
  fp = fopen(filnam, "a");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  make_contest_line (contest_line, icall_count);
  fputs(contest_line, fp);
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
  if(sockfd>-1 && send_record)
   send_to_server (contest_line, "W");
}

static void write_ded_file(gint qnum, const gchar *cmd, gint send_record)
{
  FILE *fp;
  gchar filnam[13], msg[49], contest_line[76];

  strcpy(filnam, contest_file_base);
  strcat(filnam, ".DED");
  fp = fopen(filnam, "a");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  strcpy(contest_line, cmd);
  add_eol_chars (contest_line);
  fputs(contest_line, fp);
  make_contest_line (contest_line, qnum);
  fputs(contest_line, fp);
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
  if(sockfd>-1 && send_record)
   send_to_server (contest_line, cmd);
}

static void rewrite_contest_file(void)
{
  FILE *fp;
  gchar filnam[13], msg[49], contest_line[76];
  gint i;

  strcpy(filnam, contest_file_name);
  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  for(i=1; i<=icall_count; i++)
   {
    make_contest_line (contest_line, i);
    fputs(contest_line, fp);
   }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_call_library_file(void)
{
  FILE *fp;
  gchar msg[49], library_line[65];
  gint i;

  fp = fopen(clib.filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, clib.filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  for(i=0; i<clib.number_of_calls; i++)
   {
    strcpy(clib.call[9999], clib.call[i]);
    right_blank_pad (clib.call[9999], sizeof(clib.call[9999]));
    strcpy(clib.year[9999], clib.year[i]);
    right_blank_pad (clib.year[9999], sizeof(clib.year[9999]));
    strcpy(clib.country[9999], clib.country[i]);
    right_blank_pad (clib.country[9999], sizeof(clib.country[9999]));
    strcpy(clib.cqzone[9999], clib.cqzone[i]);
    right_blank_pad (clib.cqzone[9999], sizeof(clib.cqzone[9999]));
    strcpy(clib.iaruzone[9999], clib.iaruzone[i]);
    right_blank_pad (clib.iaruzone[9999], sizeof(clib.iaruzone[9999]));
    strcpy(clib.state[9999], clib.state[i]);
    right_blank_pad (clib.state[9999], sizeof(clib.state[9999]));
    strcpy(clib.section[9999], clib.section[i]);
    right_blank_pad (clib.section[9999], sizeof(clib.section[9999]));
    strcpy(clib.county[9999], clib.county[i]);
    right_blank_pad (clib.county[9999], sizeof(clib.county[9999]));
    strcpy(clib.check[9999], clib.check[i]);
    right_blank_pad (clib.check[9999], sizeof(clib.check[9999]));
    strcpy(library_line, clib.call[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.year[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.country[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.cqzone[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.iaruzone[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.state[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.section[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.county[9999]);
    strcat(library_line, " ");
    strcat(library_line, clib.check[9999]);
    right_blank_unpad(library_line);
    add_eol_chars (library_line);
    fputs(library_line, fp);
   }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void read_radio_file(void)
{
  FILE *fp;
  gint i, soli;
  gchar filnam[13], line_in[22];

  get_cat_filename (filnam);
  if(!strcmp(init_screen_choices[19], radio_bracket[2]))
   {
    fp = fopen(filnam, "r");
    if(fp==NULL)
     {
      write_radio_file (filnam, 0);
      rcs[1] = rcs[0];
      return;
     }
    soli = sizeof(line_in);
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].co_onoff[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].co_value[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].na[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].sh[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].is[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].rf[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].gt[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].ra[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    for(i=0; i<12; i++)
     {
      fgets(line_in, soli, fp);
      if(feof(fp)) break;
      concat(rcs[1].pa[i], line_in, kb_index (line_in, "=")+1,
             strlen(line_in)-2, COPY);
     }
    fclose(fp);
   }
}

static void write_radio_file(const gchar *filnam, gint iupd)
{
  FILE *fp;
  gint i;
  gchar line_out[22], ich[3];

  fp = fopen(filnam, "w");
  if(fp==NULL)
   return;
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "co_onoff[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].co_onoff[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "co_value[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].co_value[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "na[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].na[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "sh[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].sh[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "is[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].is[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "rf[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].rf[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "gt[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].gt[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "ra[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].ra[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  for(i=0; i<12; i++)
   {
    int_to_char (i, ich);
    strcpy(line_out, "pa[");
    strcat(line_out, ich);
    strcat(line_out, "]=");
    strcat(line_out, rcs[iupd].pa[i]);
    strcat(line_out, "\n");
    fputs(line_out, fp);
   }
  fclose(fp);
}
