static void write_email_files(gchar email_file_names[5][13]);
static void write_summary_file(const gchar *filnam, gint margin);
static void write_dupe_file(const gchar *filnam, gint margin);
static void write_log_file(const gchar *filnam, gint margin);
static void write_multiplier_file(const gchar *filnam, gint margin);
static void write_cabrillo_file(const gchar *filnam);

static void write_email_files(gchar email_file_names[5][13])
{
  gchar email_file_base[9], dot_ext[5];
  gint i, lci, lcil;
  gint margin=0;

  switch (isel_contest)
   {
    case DX :
    case FD :
    case SS :
    case A160 :
    case TEN :
    case WAA :
     strcpy(dot_ext, ".TXT");
     break;
    case WPX :
    case NYQP :
    case WW :
     strcpy(dot_ext, ".CBR");
     break;
   }
  lcil = strlen(init_screen_choices[0]);
  lci = lcil<9 ? lcil : 8;
  concat(email_file_base, init_screen_choices[0], 0, lci-1, COPY);
  lcil = kb_index (email_file_base, "/");
  if(lcil>-1)
   email_file_base[lcil] = '-';
  for(i=0; i<5; i++)
   strcpy(email_file_names[i], email_file_base);
  strcat(email_file_names[0], ".SUM");
  strcat(email_file_names[1], ".DUP");
  strcat(email_file_names[2], ".LOG");
  strcat(email_file_names[3], ".MLT");
  strcat(email_file_names[4], dot_ext);
  write_summary_file (email_file_names[0], margin);
  write_dupe_file (email_file_names[1], margin);
  write_log_file (email_file_names[2], margin);
  if(isel_contest==WPX)
   write_multiplier_file (email_file_names[3], margin);
  write_cabrillo_file (email_file_names[4]);
}

static void write_summary_file(const gchar *filnam, gint margin)
{
  FILE *fp;
  gint i, j, k, pm;
  gchar blank80[81], under_line[66], msg[49], line_out[84], score_line[12];
  gchar station[14], mode_line[6], power_line[5], sum_line[7], mult_line[2];
  gchar locus_line[4], ops_line[45];
  gint iqsos[2] = {0, 0}, idupes[2] = {0, 0}, ipoints[2] = {0, 0};
  gint mults[2] = {0, 0}, izones[2] = {0, 0}, nlw[2] = {0, 0};
  gchar skip_line[3] = {'\r', '\n', '\0'};
  const gchar *stpr = "State/Province: ";
  const gchar *category_choices[8] = {"Single Operator",
               "Single Operator, Assisted",
               "Single Operator, Tribander/S-E",
               "Multi-Operator, Single-Transmitter",
               "Multi-Operator, Two Transmitters",
               "Multi-Operator, Two Transmitters",
               "Multi-Operator, Multi-Transmitter",
               "School-Club"};
  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  memset(blank80, ' ', sizeof(blank80)-1);
  blank80[80] = '\0';
  for(i=0; i<65; i++)
   under_line[i] = '_';
  under_line[65] = '\0';
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    {
     iqsos[j] += score_mb.qsos[j][i];
     idupes[j] += score_mb.dupes[j][i];
     ipoints[j] += score_mb.points[j][i];
     mults[j] += score_mb.mults[j][i];
     izones[j] += score_mb.zones[j][i];
    }
  if(!iqsos[CW])
   strcpy(mode_line, "Phone");
  else if(!iqsos[PH])
   strcpy(mode_line, "CW   ");
  else
   strcpy(mode_line, "Mixed");
  if(!strcmp(init_screen_choices[12], power_bracket[0]))
   strcpy(power_line, "QRP ");
  else if(!strcmp(init_screen_choices[12], power_bracket[1]))
   strcpy(power_line, "Low ");
  else
   strcpy(power_line, "High");
  concat(line_out, blank80, 0, margin+20, COPY);
  concat(line_out, full_year, 0, 1, !COPY);
  concat(line_out, cr_array[1].date, 4, 5, !COPY);
  strcat(line_out, " ");
  strcat(line_out, select_contest_items[isel_contest]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Call: ");
  strcpy(station, init_screen_choices[0]);
  right_blank_pad (station, sizeof(station));
  strcat(line_out, station);
  concat(line_out, blank80, 0, 5, !COPY);
  switch (isel_contest)
   {
    case DX :
     strcat(line_out, stpr);
     concat(line_out, set_screen_choices[0], 3-mode, 4-mode, !COPY);
     concat(line_out, blank80, 0, 8, !COPY);
     strcat(line_out, "Section: ___");
     break;
    case WPX :
     strcat(line_out, stpr);
     strcat(line_out, init_screen_choices[6]);
     break;
    case FD :
     strcat(line_out, stpr);
     strcat(line_out, init_screen_choices[6]);
     concat(line_out, blank80, 0, 8, !COPY);
     strcat(line_out, "Section: ");
     concat(line_out, set_screen_choices[0], 2,
            strlen(set_screen_choices[0])-1, !COPY);
     break;
    case NYQP :
     strcat(line_out, "State/Province/County: ");
     concat(line_out, set_screen_choices[0], 3-mode,
            strlen(set_screen_choices[0])-1, !COPY);
     break;
    case WW :
     strcat(line_out, stpr);
     strcat(line_out, init_screen_choices[6]);
     concat(line_out, blank80, 0, 12, !COPY);
     strcat(line_out, "Zone: ");
     concat(line_out, set_screen_choices[0], 3-mode, 4-mode, !COPY);
     break;
    case SS :
     strcat(line_out, stpr);
     strcat(line_out, init_screen_choices[6]);
     concat(line_out, blank80, 0, 8, !COPY);
     strcat(line_out, "Section: ");
     concat(line_out, set_screen_choices[0], 3,
            strlen(set_screen_choices[0])-1, !COPY);
     break;
    case A160 :
     strcat(line_out, stpr);
     strcat(line_out, init_screen_choices[6]);
     concat(line_out, blank80, 0, 8, !COPY);
     strcat(line_out, "Section: ");
     concat(line_out, set_screen_choices[0], 3-mode,
            strlen(set_screen_choices[0])-1, !COPY);
     break;
    case TEN :
     strcat(line_out, stpr);
     concat(line_out, set_screen_choices[0], 3-mode, 4-mode, !COPY);
     concat(line_out, blank80, 0, 8, !COPY);
     strcat(line_out, "Section: ___");
     break;
    case WAA :
     strcat(line_out, "State/Province/Country: __");
     break;
   }
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Category: ");
  for(i=0; i<8; i++)
   if(!strcmp(init_screen_choices[11], category_bracket[i]))
    {
     strcat(line_out, category_choices[i]);
     if(i>2)
      {
       strcpy(ops_line, "Operators: ");
       concat(ops_line, under_line, 0, 32, !COPY);
      }
     else if(strcmp(init_screen_choices[0], init_screen_choices[1]))
      {
       strcpy(ops_line, "Operator: ");
       strcat(ops_line, init_screen_choices[1]);
      }
     else
      strcpy(ops_line, "");
     break;
    }
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  if(strlen(ops_line))
   {
    concat(line_out, blank80, 0, margin, COPY);
    strcat(line_out, ops_line);
    add_eol_chars (line_out);
    fputs(line_out, fp);
    fputs(skip_line, fp);
   }
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Mode: ");
  strcat(line_out, mode_line);
  concat(line_out, blank80, 0, 9, !COPY);
  strcat(line_out, "Power: ");
  strcat(line_out, power_line);
  concat(line_out, blank80, 0, 9, !COPY);
  strcat(line_out, "Hours of Operation: __");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  fputs(skip_line, fp);
  switch (isel_contest)
   {
    case DX :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net        QSO        Country        Claimed");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               QSOs      Points       Mults          Score");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 3, !COPY);
       int_to_comma (score_mb.qsos[mode][i]-score_mb.dupes[mode][i],
                     sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 5, !COPY);
       int_to_comma (score_mb.points[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.mults[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (iqsos[mode]-idupes[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (mults[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode]*mults[mode], score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case FD :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net CW      Net PH");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "                QSOs        QSOs");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.qsos[CW][i]-score_mb.dupes[CW][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 5, !COPY);
       int_to_comma (score_mb.qsos[PH][i]-score_mb.dupes[PH][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW]+iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Points");
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (ipoints[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[CW]+ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Power Multiplier");
     concat(line_out, blank80, 0, 21, !COPY);
     if(!strcmp(init_screen_choices[12], power_bracket[0]))
      pm = 5;
     else if(!strcmp(init_screen_choices[12], power_bracket[1]))
      pm = 2;
     else
      pm = 1;
     int_to_comma (pm, sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Score");
     concat(line_out, blank80, 0, 28, !COPY);
     int_to_comma (pm*(ipoints[CW]+ipoints[PH]), sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Bonus Points");
     concat(line_out, blank80, 0, 24, !COPY);
     concat(line_out, under_line, 0, 6, !COPY);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Claimed Score");
     concat(line_out, blank80, 0, 23, !COPY);
     concat(line_out, under_line, 0, 6, !COPY);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case WPX :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net        QSO        Prefix         Claimed");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               QSOs      Points       Mults          Score");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 3, !COPY);
       int_to_comma (score_mb.qsos[mode][i]-score_mb.dupes[mode][i],
                     sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 5, !COPY);
       int_to_comma (score_mb.points[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.mults[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (iqsos[mode]-idupes[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (mults[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode]*mults[mode], score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case NYQP :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net CW      Net PH");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "                QSOs        QSOs");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.qsos[CW][i]-score_mb.dupes[CW][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 5, !COPY);
       int_to_comma (score_mb.qsos[PH][i]-score_mb.dupes[PH][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW]+iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Points");
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (ipoints[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[CW]+ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<12; i++)
      for(j=1; j<loci.number_of_loci; j++)
       if(loci.locus_worked[i][j])
        {
         if(j>loci.number_of_countyloci)
          nlw[1]++;
         else
          nlw[0]++;
        }
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "County Multiplier");
     concat(line_out, blank80, 0, 20, !COPY);
     int_to_comma (nlw[0], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     if(!strcmp(init_screen_choices[6], "NY"))
      {
       concat(line_out, blank80, 0, margin, COPY);
       strcat(line_out, "State/Province Multiplier");
       concat(line_out, blank80, 0, 12, !COPY);
       int_to_comma (nlw[1], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
       concat(line_out, blank80, 0, margin, COPY);
       strcat(line_out, "Total Multiplier");
       concat(line_out, blank80, 0, 21, !COPY);
       int_to_comma (nlw[0]+nlw[1], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Claimed Score");
     concat(line_out, blank80, 0, 24, !COPY);
     int_to_comma ((mults[CW]+mults[PH])*(ipoints[CW]+ipoints[PH]),
                   sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case WW :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "              Net       QSO       Country     Zone       Claimed");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "              QSOs     Points      Mults      Mults       Score");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 2, !COPY);
       int_to_comma (score_mb.qsos[mode][i]-score_mb.dupes[mode][i],
                     sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.points[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 3, !COPY);
       int_to_comma (score_mb.mults[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.zones[mode][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 2, !COPY);
     int_to_comma (iqsos[mode]-idupes[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (ipoints[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (mults[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (izones[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 2, !COPY);
     int_to_comma (ipoints[mode]*(mults[mode]+izones[mode]), score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case SS :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net        QSO        Section        Claimed");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               QSOs      Points       Mults          Score");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     concat(line_out, blank80, 0, 12, !COPY);
     int_to_comma (iqsos[mode]-idupes[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (mults[mode], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[mode]*mults[mode], score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "                     ARRL Sections Check-Off List");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               (Sections worked marked with an asterisk)");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(j=1; j<11; j++)
      {
       concat(line_out, blank80, 0, margin, COPY);
       concat(line_out, blank80, 0, 9, !COPY);
       for(i=j; i<=loci.number_of_loci; i=i+10)
        {
         strcat(line_out, "  ");
         strcpy(mult_line, " ");
         for(k=0; k<6; k++)
          if(loci.locus_worked[k][i])
           {
            strcpy(mult_line, "*");
            break;
           }
         strcpy(locus_line, loci.locus_abv[i]);
         right_blank_pad (locus_line, sizeof(locus_line));
         strcat(line_out, mult_line);
         strcat(line_out, locus_line);
        }
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     break;
    case A160 :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSOs");
     concat(line_out, blank80, 0, 15, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Points");
     concat(line_out, blank80, 0, 9, !COPY);
     int_to_comma (ipoints[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "States/Prov");
     for(i=0; i<2; i++)
      for(j=1; j<=loci.number_of_loci; j++)
       if(loci.locus_worked[i][j])
        nlw[i]++;
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (nlw[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Countries");
     nlw[0] = 0;
     nlw[1] = 0;
     for(i=0; i<2; i++)
      for(j=1; j<=countries.number_of_countries; j++)
       if(countries.icountry_worked[i][j])
        nlw[i]++;
     concat(line_out, blank80, 0, 10, !COPY);
     int_to_comma (nlw[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Total Mults");
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (mults[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Claimed Score");
     concat(line_out, blank80, 0, 1, !COPY);
     int_to_comma ((ipoints[CW]+ipoints[PH])*(mults[CW]+mults[PH]),
                   score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case TEN :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "                        CW          Phone           All");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSOs");
     concat(line_out, blank80, 0, 15, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 7, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW]+iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Points");
     concat(line_out, blank80, 0, 9, !COPY);
     int_to_comma (ipoints[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 7, !COPY);
     int_to_comma (ipoints[CW]+ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "States/Prov");
     for(i=0; i<2; i++)
      for(j=1; j<=loci.number_of_loci; j++)
       if(loci.locus_worked[i][j])
        nlw[i]++;
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (nlw[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (nlw[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 7, !COPY);
     int_to_comma (nlw[CW]+nlw[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Countries");
     nlw[0] = 0;
     nlw[1] = 0;
     for(i=0; i<2; i++)
      for(j=1; j<=countries.number_of_countries; j++)
       if(countries.icountry_worked[i][j])
        nlw[i]++;
     concat(line_out, blank80, 0, 10, !COPY);
     int_to_comma (nlw[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (nlw[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 7, !COPY);
     int_to_comma (nlw[CW]+nlw[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Total Mults");
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (mults[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 8, !COPY);
     int_to_comma (mults[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 7, !COPY);
     int_to_comma (mults[CW]+mults[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Claimed Score");
     concat(line_out, blank80, 0, 30, !COPY);
     int_to_comma ((ipoints[CW]+ipoints[PH])*(mults[CW]+mults[PH]),
                   score_line);
     left_blank_pad (score_line, sizeof(score_line));
     strcat(line_out, score_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
    case WAA :
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "               Net CW      Net PH");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out,
      "                QSOs        QSOs");
     add_eol_chars (line_out);
     fputs(line_out, fp);
     fputs(skip_line, fp);
     for(i=0; i<6; i++)
      {
       concat(line_out, blank80, 0, margin+2, COPY);
       strcat(line_out, bandf[i]);
       strcat(line_out, " MHz");
       concat(line_out, blank80, 0, 4, !COPY);
       int_to_comma (score_mb.qsos[CW][i]-score_mb.dupes[CW][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       concat(line_out, blank80, 0, 5, !COPY);
       int_to_comma (score_mb.qsos[PH][i]-score_mb.dupes[PH][i], sum_line);
       left_blank_pad (sum_line, sizeof(sum_line));
       strcat(line_out, sum_line);
       add_eol_chars (line_out);
       fputs(line_out, fp);
      }
     fputs(skip_line, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "All Bands");
     concat(line_out, blank80, 0, 4, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (iqsos[CW]-idupes[CW]+iqsos[PH]-idupes[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "QSO Points");
     concat(line_out, blank80, 0, 3, !COPY);
     int_to_comma (ipoints[CW], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     concat(line_out, blank80, 0, 5, !COPY);
     int_to_comma (ipoints[CW]+ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     concat(line_out, blank80, 0, margin, COPY);
     strcat(line_out, "Claimed Score");
     concat(line_out, blank80, 0, 24, !COPY);
     int_to_comma (ipoints[CW]+ipoints[PH], sum_line);
     left_blank_pad (sum_line, sizeof(sum_line));
     strcat(line_out, sum_line);
     add_eol_chars (line_out);
     fputs(line_out, fp);
     break;
   }
  fputs(skip_line, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out,
   "Club Participation?  ( ) Yes  ( ) No   If yes, print name below.");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Name of Club: ");
  concat(line_out, under_line, 0, 49, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Rigs: ");
  concat(line_out, under_line, 0, 57, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  concat(line_out, under_line, 0, 63, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Antennas: ");
  concat(line_out, under_line, 0, 53, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  concat(line_out, under_line, 0, 63, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Remarks: ");
  concat(line_out, under_line, 0, 54, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  concat(line_out, under_line, 0, 63, !COPY);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out,
   "This is to certify that in this contest I have operated my");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out,
   "transmitter within the limitations of my license and have");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out,
   "observed fully the rules and regulations of the contest.");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, "Date ");
  concat(line_out, under_line, 0, 9, !COPY);
  strcat(line_out, " Signature ");
  concat(line_out, under_line, 0, 22, !COPY);
  strcat(line_out, ", ");
  strcat(line_out, init_screen_choices[1]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(skip_line, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, init_screen_choices[2]);
  strcat(line_out, ", ");
  strcat(line_out, init_screen_choices[1]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, init_screen_choices[3]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  if(strlen(init_screen_choices[4]))
   {
    concat(line_out, blank80, 0, margin, COPY);
    strcat(line_out, init_screen_choices[4]);
    add_eol_chars (line_out);
    fputs(line_out, fp);
   }
  concat(line_out, blank80, 0, margin, COPY);
  strcat(line_out, init_screen_choices[5]);
  strcat(line_out, ", ");
  strcat(line_out, init_screen_choices[6]);
  strcat(line_out, " ");
  strcat(line_out, init_screen_choices[7]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_dupe_file(const gchar *filnam, gint margin)
{
  FILE *fp;
  gint i, j, k, khold, ipage, npages, lml, lsci25, is, is1, ic;
  gint lines_per_mb[12], new_mb, idl;
  gchar blank80[81], msg[49], line_out[84], ipage_char[4], npages_char[4];
  gchar station[14], mode_line[6], dupe_line[2];
  gchar title_line[74], header_line1[74], call_hold[14], call_fill[14];
  gint iqsos[2] = {0, 0}, max_lines=54;
  gchar skip_line[3] = {'\r', '\n', '\0'};
  gchar form_feed[2] = {'\f', '\0'};

  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  memset(blank80, ' ', sizeof(blank80)-1);
  blank80[80] = '\0';
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    {
     iqsos[j] += score_mb.qsos[j][i];
     lines_per_mb[j*6+i] = (score_mb.qsos[j][i]-score_mb.dupes[j][i])/4;
     if((score_mb.qsos[j][i]-score_mb.dupes[j][i])%4)
      lines_per_mb[j*6+i]++;
    }
  if(!iqsos[CW])
   strcpy(mode_line, "Phone");
  else if(!iqsos[PH])
   strcpy(mode_line, "CW");
  else
   strcpy(mode_line, "");
  lml = strlen(mode_line);
  concat(title_line, full_year, 0, 1, COPY);
  concat(title_line, cr_array[1].date, 4, 5, !COPY);
  strcat(title_line, " ");
  strcat(title_line, select_contest_items[isel_contest]);
  lsci25 = 25-strlen(select_contest_items[isel_contest]);
  if(lsci25<0)
   lsci25 = 0;
  if(lml)
   {
    strcat(title_line, " ");
    strcat(title_line, mode_line);
   }
  strcat(title_line, ": ");
  strcpy(station, init_screen_choices[0]);
  right_blank_pad (station, sizeof(station));
  strcat(title_line, station);
  concat(title_line, blank80, 0, lsci25-lml+5, !COPY);
  strcat(title_line, "Page ");
  concat(header_line1, blank80, 0, 14, COPY);
  strcat(header_line1, "Dupe Sheet for ");
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    if(lines_per_mb[j*6+i])
     {
      new_mb = TRUE;
      npages = lines_per_mb[j*6+i]/max_lines;
      if(lines_per_mb[j*6+i]%max_lines)
       npages++;
      khold = 1;
      for(ipage=1; ipage<=npages; ipage++)
       {
        idl = 0;
        if(margin)
         {
          int_to_char (ipage, ipage_char);
          left_blank_pad (ipage_char, sizeof(ipage_char));
          concat(line_out, blank80, 0, margin, COPY);
          strcat(line_out, title_line);
          strcat(line_out, ipage_char);
          strcat(line_out, " of ");
          int_to_char (npages, npages_char);
          left_blank_pad (npages_char, sizeof(npages_char));
          strcat(line_out, npages_char);
          add_eol_chars (line_out);
          fputs(line_out, fp);
          fputs(skip_line, fp);
          fputs(skip_line, fp);
         }
        if(margin || new_mb)
         {
          new_mb = FALSE;
          concat(line_out, blank80, 0, margin, COPY);
          strcat(line_out, header_line1);
          strcat(line_out, bandw[i]);
          strcat(line_out, mode_char[j]);
          add_eol_chars (line_out);
          fputs(line_out, fp);
          fputs(skip_line, fp);
         }
        strcpy(call_hold, "");
        ic = 0;
        for(k=khold; k<=icall_count; k++)
         {
          is = call_tree.sort_order[k];
          if(!strcmp(cr_array[is].call, "NOQSO")) continue;
          if(cr_array[is].mode==j && cr_array[is].iband==i)
           if(strcmp(cr_array[is].call, call_hold))
            {
             if(!ic)
              concat(line_out, blank80, 0, margin, COPY);
             strcpy(call_fill, cr_array[is].call);
             right_blank_pad (call_fill, sizeof(call_fill));
             strcpy(dupe_line, " ");
             if(k<icall_count)
              {
               is1 = call_tree.sort_order[k+1];
               if(!strcmp(cr_array[is].call,
                  cr_array[is1].call) &&
                  cr_array[is].mode==cr_array[is1].mode &&
                  cr_array[is].iband==cr_array[is1].iband)
                {
                 strcpy(dupe_line, "*");
                 k++;
                }
              }
             strcat(line_out, dupe_line);
             strcat(line_out, call_fill);
             strcpy(call_hold, cr_array[is].call);
             ic++;
             ic %= 4;
             if(!ic)
              {
               add_eol_chars (line_out);
               fputs(line_out, fp);
               idl++;
              }
            }
          if(idl==max_lines)
           {
            khold = k+1;
            break;
           }
         }
        if(ic)
         {
          add_eol_chars (line_out);
          fputs(line_out, fp);
         }
        if(margin)
         fputs(form_feed, fp);
       }
      if(!margin)
       fputs(skip_line, fp);
     }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_log_file(const gchar *filnam, gint margin)
{
  FILE *fp;
  gint i, j, ipage, npages, new_page, lml, lsci25;
  gchar blank80[81], msg[49], line_out[84], ipage_char[4], npages_char[4];
  gchar station[14], call_line[14], mode_line[6], bm_line[6];
  gchar sent_line[8], received_line[8], zone_line[3], mult_line[8];
  gchar dupe_line[2], month_line[4], month_number[3], qso_char[5];
  gchar title_line[74], header_line1[74], header_line2[74];
  gchar footer_line1[74], footer_line2[74];
  gchar footer1_vars[4][6], footer2_vars[4][6];
  gchar hl2_zone[7], hl2_country[10];
  gint iqsos[2] = {0, 0}, nqsos[2] = {0, 0};
  gint nzones[2] = {0, 0}, nmults[2] = {0, 0}, npts[2] = {0, 0};
  gchar skip_line[3] = {'\r', '\n', '\0'};
  gchar form_feed[2] = {'\f', '\0'};

  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  memset(blank80, ' ', sizeof(blank80)-1);
  blank80[80] = '\0';
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    iqsos[j] += score_mb.qsos[j][i];
  if(!iqsos[CW])
   strcpy(mode_line, "Phone");
  else if(!iqsos[PH])
   strcpy(mode_line, "CW");
  else
   strcpy(mode_line, "");
  lml = strlen(mode_line);
  npages = icall_count/50;
  if(icall_count%50)
   npages++;
  int_to_char (npages, npages_char);
  left_blank_pad (npages_char, sizeof(npages_char));
  switch (isel_contest)
   {
    case DX :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "COUNTRY  ");
     break;
    case WPX :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "PREFIX   ");
     break;
    case FD :
     strcpy(header_line1, "MODE/");
     concat(header_line1, blank80, 0, 43, !COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "SECTION  ");
     break;
    case WW :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "ZONE  ");
     strcpy(hl2_country, "COUNTRY  ");
     break;
    case SS :
     concat(header_line1, blank80, 0, 31, COPY);
     strcat(header_line1, "SENT +");
     concat(header_line1, blank80, 0, 10, !COPY);
     strcpy(hl2_zone, "  ");
     strcpy(hl2_country, "SECTION  ");
     break;
    case A160 :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "SEC/CTY  ");
     break;
    case NYQP :
    case TEN :
     strcpy(header_line1, "MODE/");
     concat(header_line1, blank80, 0, 43, !COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "ST/PR/C  ");
     break;
    case WAA :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "RCVD2    ");
     break;
    default :
     concat(header_line1, blank80, 0, 48, COPY);
     strcpy(hl2_zone, "   ");
     strcpy(hl2_country, "HELP ME! ");
   }
  if(isel_contest!=FD && isel_contest!=WAA)
   strcat(header_line1, "NEW MULTS");
  concat(title_line, full_year, 0, 1, COPY);
  concat(title_line, cr_array[1].date, 4, 5, !COPY);
  strcat(title_line, " ");
  strcat(title_line, select_contest_items[isel_contest]);
  lsci25 = 25-strlen(select_contest_items[isel_contest]);
  if(lsci25<0)
   lsci25 = 0;
  if(lml)
   {
    strcat(title_line, " ");
    strcat(title_line, mode_line);
   }
  strcat(title_line, ": ");
  strcpy(station, init_screen_choices[0]);
  right_blank_pad (station, sizeof(station));
  strcat(title_line, station);
  concat(title_line, blank80, 0, lsci25-lml+5, !COPY);
  strcat(title_line, "Page ");
  concat(month_number, cr_array[1].date, 0, 1, COPY);
  strcpy(month_line, month_array[atoi(month_number)-1]);
  month_line[1] = toupper(month_line[1]);
  month_line[2] = toupper(month_line[2]);
  strcpy(header_line2, "BAND   ");
  strcat(header_line2, month_line);
  concat(header_line2, blank80, 0, 2, !COPY);
  strcat(header_line2, "UTC  ");
  strcat(header_line2, "STATION");
  concat(header_line2, blank80, 0, 6, !COPY);
  if(isel_contest==SS)
   {
    strcat(header_line2, set_screen_choices[0]);
    if(strlen(set_screen_choices[0])==6)
     concat(header_line2, blank80, 0, 1, !COPY);
    else
     concat(header_line2, blank80, 0, 2, !COPY);
    strcat(header_line2, "RECEIVED");
   }
  else
   {
    strcat(header_line2, "SENT    ");
    strcat(header_line2, "RCVD   ");
   }
  strcat(header_line2, hl2_zone);
  strcat(header_line2, hl2_country);
  strcat(header_line2, "PTS");
  strcpy(footer_line1, "THIS PAGE ");
  strcpy(footer_line2, "CUMULATIVE");
  for(ipage=1; ipage<=npages; ipage++)
   {
    new_page = TRUE;
    nqsos[0] = 0;
    nzones[0] = 0;
    nmults[0] = 0;
    npts[0] = 0;
    if(margin)
     {
      int_to_char (ipage, ipage_char);
      left_blank_pad (ipage_char, sizeof(ipage_char));
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, title_line);
      strcat(line_out, ipage_char);
      strcat(line_out, " of ");
      strcat(line_out, npages_char);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      fputs(skip_line, fp);
      fputs(skip_line, fp);
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, header_line1);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, header_line2);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      fputs(skip_line, fp);
     }
    for(i=(ipage-1)*50+1; i<=ipage*50; i++)
     {
      if(i<=icall_count)
       {
        switch (isel_contest)
         {
          case FD :
          case NYQP :
          case TEN :
           concat(line_out, blank80, 0, margin, COPY);
           if(!margin)
            {
             strcat(line_out, " ");
             strcpy(bm_line, bandw[cr_array[i].iband]);
            }
           else
            {
             if(cr_array[i].iband!=cr_array[i-1].iband ||
                cr_array[i].mode!=cr_array[i-1].mode || new_page)
              {
               strcpy(bm_line, bandw[cr_array[i].iband]);
               strcat(bm_line, mode_char[cr_array[i].mode]);
              }
             else
              strcpy(bm_line, "     ");
            }
           break;
          default :
           concat(line_out, blank80, 0, margin+1, COPY);
           if(!margin)
            strcpy(bm_line, bandw[cr_array[i].iband]);
           else
            {
             if(cr_array[i].iband!=cr_array[i-1].iband || new_page)
              {
               strcpy(bm_line, bandw[cr_array[i].iband]);
               strcat(bm_line, " ");
              }
             else
              strcpy(bm_line, "    ");
            }
         }
        strcat(line_out, bm_line);
        new_page = FALSE;
        if(margin)
         {
          concat(line_out, blank80, 0, 2, !COPY);
          concat(line_out, cr_array[i].date, 2, 3, !COPY);
          concat(line_out, blank80, 0, 1, !COPY);
         }
        else
         {
          concat(line_out, blank80, 0, 0, !COPY);
          strcat(line_out, mode_char[cr_array[i].mode]);
          concat(line_out, blank80, 0, 0, !COPY);
          concat(line_out, cr_array[i].date, 0, 1, !COPY);
          strcat(line_out, "-");
          concat(line_out, cr_array[i].date, 2, 3, !COPY);
          strcat(line_out, "-");
          concat(line_out, cr_array[i].date, 4, 5, !COPY);
          concat(line_out, blank80, 0, 0, !COPY);
         }
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        concat(line_out, blank80, 0, 0, !COPY);
        if(cr_array[i].dupe[0] && margin)
         strcpy(dupe_line, cr_array[i].dupe);
        else
         {
          strcpy(dupe_line, " ");
          nqsos[0]++;
          nqsos[1]++;
         }
        strcat(line_out, dupe_line);
        strcpy(call_line, cr_array[i].call);
        right_blank_pad (call_line, sizeof(call_line));
        strcat(line_out, call_line);
        concat(line_out, blank80, 0, 0, !COPY);
        switch (isel_contest)
         {
          case WPX :
           strcpy(sent_line, cr_array[i].sent);
           strcpy(qso_char, cr_array[i].qso);
           left_blank_unpad (qso_char);
           left_zero_pad (qso_char, sizeof(qso_char));
           strcat(sent_line, qso_char);
           break;
          case SS :
           strcpy(sent_line, cr_array[i].qso);
           break;
          default :
           strcpy(sent_line, cr_array[i].sent);
         }
        right_blank_pad (sent_line, sizeof(sent_line));
        strcat(line_out, sent_line);
        concat(line_out, blank80, 0, 0, !COPY);
        switch (isel_contest)
         {
          case WPX :
           concat(received_line, cr_array[i].rec_num, 0, 2-mode, COPY);
           concat(qso_char, cr_array[i].rec_num,
                  3-mode, strlen(cr_array[i].rec_num)-1, COPY);
           left_zero_pad (qso_char, sizeof(qso_char));
           strcat(received_line, qso_char);
           right_blank_pad (received_line, sizeof(received_line));
           strcat(line_out, received_line);
           break;
          case SS :
           if(!margin)
            {
             strcat(line_out, cr_array[i].sent);
             if(strlen(cr_array[i].sent)==6)
              concat(line_out, blank80, 0, 0, !COPY);
             else
              concat(line_out, blank80, 0, 1, !COPY);
            }
           concat(qso_char, cr_array[i].rec_num, 0, 3, COPY);
           left_blank_pad (qso_char, sizeof(qso_char));
           strcat(line_out, qso_char);
           concat(line_out, blank80, 0, 0, !COPY);
           strcat(line_out, cr_array[i].tx);
           concat(line_out, blank80, 0, 0, !COPY);
           strcat(line_out, cr_array[i].prefix);
           break;
          default :
           strcpy(received_line, cr_array[i].rec_num);
           right_blank_pad (received_line, sizeof(received_line));
           strcat(line_out, received_line);
         }
        concat(line_out, blank80, 0, 0, !COPY);
        if(isel_contest==WW)
         {
          if(cr_array[i].mult[1])
           {
            concat(zone_line, cr_array[i].rec_num, 3-mode, 4-mode, COPY);
            if(zone_line[0]=='0' && margin)
             zone_line[0] = ' ';
            nzones[0]++;
            nzones[1]++;
           }
          else
           strcpy(zone_line, "  ");
          strcat(line_out, zone_line);
          concat(line_out, blank80, 0, 2, !COPY);
         }
        else if(isel_contest!=SS)
         concat(line_out, blank80, 0, 1, !COPY);
        if(isel_contest==WPX)
         {
          if(cr_array[i].mult[1])
           {
            strcpy(mult_line, cr_array[i].prefix);
            right_blank_pad (mult_line, sizeof(mult_line));
            nmults[0]++;
            nmults[1]++;
           }
          else
           strcpy(mult_line, "       ");
         }
        else if(isel_contest==FD)
         {
          strcpy(mult_line, cr_array[i].rec_county);
          right_blank_pad (mult_line, sizeof(mult_line));
         }
        else if(isel_contest==NYQP)
         {
          if(cr_array[i].mult[0])
           {
            strcpy(mult_line, cr_array[i].rec_county);
            nmults[0]++;
            nmults[1]++;
            if(cr_array[i].mult[1])
             {
              strcat(mult_line, " ");
              strcat(mult_line, "NY");
              nmults[0]++;
              nmults[1]++;
             }
            right_blank_pad (mult_line, sizeof(mult_line));
           }
          else
           strcpy(mult_line, "       ");
         }
        else if(isel_contest==SS)
         {
          strcpy(mult_line, cr_array[i].rec_county);
          if(strlen(mult_line)==2)
           strcat(mult_line, "   ");
          else
           strcat(mult_line, "  ");
          if(cr_array[i].mult[0])
           {
            nmults[0]++;
            nmults[1]++;
            int_to_char (nmults[1], zone_line);
            left_blank_pad(zone_line, sizeof(zone_line));
            strcat(mult_line, zone_line);
           }
          right_blank_pad (mult_line, sizeof(mult_line));
         }
        else if(isel_contest==WAA)
         {
          strcpy(mult_line, cr_array[i].rec_county);
          right_blank_pad (mult_line, sizeof(mult_line));
         }
        else
         {
          if(cr_array[i].mult[0])
           {
            strcpy(mult_line, cr_array[i].rec_county);
            right_blank_pad (mult_line, sizeof(mult_line));
            nmults[0]++;
            nmults[1]++;
           }
          else
           strcpy(mult_line, "       ");
         }
        strcat(line_out, mult_line);
        concat(line_out, blank80, 0, 2, !COPY);
        npts[0] += atoi(cr_array[i].points);
        npts[1] += atoi(cr_array[i].points);
        strcat(line_out, cr_array[i].points);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
      else if(margin)
       fputs(skip_line, fp);
     }
    if(margin)
     {
      int_to_char (nqsos[0], footer1_vars[0]);
      left_blank_pad (footer1_vars[0], sizeof(footer1_vars[0]));
      int_to_char (nzones[0], footer1_vars[1]);
      left_blank_pad (footer1_vars[1], sizeof(footer1_vars[1]));
      int_to_char (nmults[0], footer1_vars[2]);
      left_blank_pad (footer1_vars[2], sizeof(footer1_vars[2]));
      int_to_char (npts[0], footer1_vars[3]);
      left_blank_pad (footer1_vars[3], sizeof(footer1_vars[3]));
      int_to_char (nqsos[1], footer2_vars[0]);
      left_blank_pad (footer2_vars[0], sizeof(footer2_vars[0]));
      int_to_char (nzones[1], footer2_vars[1]);
      left_blank_pad (footer2_vars[1], sizeof(footer2_vars[1]));
      int_to_char (nmults[1], footer2_vars[2]);
      left_blank_pad (footer2_vars[2], sizeof(footer2_vars[2]));
      int_to_char (npts[1], footer2_vars[3]);
      left_blank_pad (footer2_vars[3], sizeof(footer2_vars[3]));
      fputs(skip_line, fp);
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, footer_line1);
      concat(line_out, blank80, 0, 9, !COPY);
      strcat(line_out, footer1_vars[0]);
      concat(line_out, blank80, 0, 19, !COPY);
      if(isel_contest==WW)
       strcat(line_out, footer1_vars[1]);
      else
       concat(line_out, blank80, 0, 1, !COPY);
      concat(line_out, blank80, 0, 0, !COPY);
      if(isel_contest==FD)
       concat(line_out, blank80, 0, 4, !COPY);
      else
       strcat(line_out, footer1_vars[2]);
      concat(line_out, blank80, 0, 3, !COPY);
      strcat(line_out, footer1_vars[3]);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, footer_line2);
      concat(line_out, blank80, 0, 9, !COPY);
      strcat(line_out, footer2_vars[0]);
      concat(line_out, blank80, 0, 19, !COPY);
      if(isel_contest==WW)
       strcat(line_out, footer2_vars[1]);
      else
       concat(line_out, blank80, 0, 1, !COPY);
      concat(line_out, blank80, 0, 0, !COPY);
      if(isel_contest==FD)
       concat(line_out, blank80, 0, 4, !COPY);
      else
       strcat(line_out, footer2_vars[2]);
      concat(line_out, blank80, 0, 3, !COPY);
      strcat(line_out, footer2_vars[3]);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      fputs(form_feed, fp);
     }
   }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_multiplier_file(const gchar *filnam, gint margin)
{
  FILE *fp;
  gint i, j, ipage, npages, lml, lsci25, jfirst, jlast, ic;
  gchar blank80[81], msg[49], line_out[84], ipage_char[4], npages_char[4];
  gchar station[14], mode_line[6], px_line[6];
  gchar title_line[74], header_line1[74];
  gint max_lines=54;
  gint iqsos[2] = {0, 0};
  gchar skip_line[3] = {'\r', '\n', '\0'};
  gchar form_feed[2] = {'\f', '\0'};

  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  memset(blank80, ' ', sizeof(blank80)-1);
  blank80[80] = '\0';
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    iqsos[j] += score_mb.qsos[j][i];
  if(!iqsos[CW])
   strcpy(mode_line, "Phone");
  else if(!iqsos[PH])
   strcpy(mode_line, "CW");
  else
   strcpy(mode_line, "");
  lml = strlen(mode_line);
  npages = px_tree.npx/(max_lines*10);
  if(px_tree.npx%(max_lines*10))
   npages++;
  int_to_char (npages, npages_char);
  left_blank_pad (npages_char, sizeof(npages_char));
  concat(title_line, full_year, 0, 1, COPY);
  concat(title_line, cr_array[1].date, 4, 5, !COPY);
  strcat(title_line, " ");
  strcat(title_line, select_contest_items[isel_contest]);
  lsci25 = 25-strlen(select_contest_items[isel_contest]);
  if(lsci25<0)
   lsci25 = 0;
  if(lml)
   {
    strcat(title_line, " ");
    strcat(title_line, mode_line);
   }
  strcat(title_line, ": ");
  strcpy(station, init_screen_choices[0]);
  right_blank_pad (station, sizeof(station));
  strcat(title_line, station);
  concat(title_line, blank80, 0, lsci25-lml+5, !COPY);
  strcat(title_line, "Page ");
  concat(header_line1, blank80, 0, 14, COPY);
  strcat(header_line1, "Prefix Multiplier List");
  for(ipage=1; ipage<=npages; ipage++)
   {
    if(margin)
     {
      int_to_char (ipage, ipage_char);
      left_blank_pad (ipage_char, sizeof(ipage_char));
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, title_line);
      strcat(line_out, ipage_char);
      strcat(line_out, " of ");
      strcat(line_out, npages_char);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      fputs(skip_line, fp);
      fputs(skip_line, fp);
      concat(line_out, blank80, 0, margin, COPY);
      strcat(line_out, header_line1);
      add_eol_chars (line_out);
      fputs(line_out, fp);
      fputs(skip_line, fp);
     }
    jfirst = (ipage-1)*max_lines*10+1;
    jlast = ipage*max_lines*10<px_tree.npx ?
            ipage*max_lines*10 : px_tree.npx;
    ic = 0;
    for(j=jfirst; j<=jlast; j++)
     {
      if(!ic)
       concat(line_out, blank80, 0, margin+1, COPY);
      strcpy(px_line, px_tree.px_array[j]);
      right_blank_pad (px_line, sizeof(px_line));
      strcat(line_out, px_line);
      strcat(line_out, " ");
      ic++;
      ic %= 10;
      if(!ic)
       {
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     }
    if(ic)
     {
      add_eol_chars (line_out);
      fputs(line_out, fp);
     }
    if(margin)
     fputs(form_feed, fp);
   }
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}

static void write_cabrillo_file(const gchar *filnam)
{
  FILE *fp;
  gchar msg[49], line_out[84], section[4], contest_cab[13], score_cab[13];
  gchar category_cab[35], mode_cab[6], band_cab[5], power_cab[5];
  gchar station_cab[14], call_cab[14], exch_sent_cab[7], exch_rcvd_cab[7];
  gchar rst_sent_cab[4], rst_rcvd_cab[4], num_rcvd_cab[5], ck_rcvd_cab[3];
  gchar rn_rcvd_cab[8], rc_rcvd_cab[8];
  gint i, j, itotal_score, pm, lrc;
  gint points=0, mults=0, zones=0, icw_qsos=0, iph_qsos=0;
  const gchar *bandf_cab[6] = {" 1800", " 3500", " 7000",
                               "14000", "21000", "28000"};
  const gchar *category_cab_choices[8] = {"SINGLE-OP", "SINGLE-OP-ASSISTED",
               "SINGLE-OP", "MULTI-ONE", "MULTI-TWO", "MULTI-TWO",
               "MULTI-MULTI", "SCHOOL-CLUB"};

  fp = fopen(filnam, "w");
  if(fp==NULL)
   {
    strcpy(msg, "Error writing ");
    strcat(msg, filnam);
    rw_error_box (main_drawing_area, msg);
    return;
   }
  for(j=0; j<2; j++)
   for(i=0; i<6; i++)
    {
     points += score_mb.points[j][i];
     mults += score_mb.mults[j][i];
     zones += score_mb.zones[j][i];
    }
  if(isel_contest==FD)
   {
    if(!strcmp(init_screen_choices[12], power_bracket[0]))
     pm = 5;
    else if(!strcmp(init_screen_choices[12], power_bracket[1]))
     pm = 2;
    else
     pm = 1;
    itotal_score = points*pm;
   }
  else if(isel_contest==WAA)
    itotal_score = points;
  else
   itotal_score = points*(mults+zones);
  int_to_char (itotal_score, score_cab);
  for(i=0; i<6; i++)
   {
    icw_qsos += score_mb.qsos[0][i];
    iph_qsos += score_mb.qsos[1][i];
   }
  if(!icw_qsos)
   strcpy(mode_cab, "SSB");
  else if(!iph_qsos)
   strcpy(mode_cab, "CW");
  else
   strcpy(mode_cab, "MIXED");
  concat(power_cab, init_screen_choices[12], 1,
         strlen(init_screen_choices[12])-2, COPY);
  for(i=0; i<8; i++)
   if(!strcmp(init_screen_choices[11], category_bracket[i]))
    {
     strcpy(category_cab, category_cab_choices[i]);
     break;
    }
  strcpy(section, "");
  strcpy(band_cab, "ALL");
  strcpy(station_cab, init_screen_choices[0]);
  right_blank_pad (station_cab, sizeof(station_cab));
  if(isel_contest==SS)
   station_cab[10] = '\0';
  switch (isel_contest)
   {
    case DX :
     if(mode==0)
      strcpy(contest_cab, "ARRL-DX-CW");
     else
      strcpy(contest_cab, "ARRL-DX-SSB");
     break;
    case WPX :
     if(mode==0)
      strcpy(contest_cab, "CQ-WPX-CW");
     else
      strcpy(contest_cab, "CQ-WPX-SSB");
     break;
    case FD :
     strcpy(contest_cab, "ARRL-FD");
     break;
    case NYQP :
     strcpy(contest_cab, "NY-QSO-PARTY");
     break;
    case WW :
     if(mode==0)
      strcpy(contest_cab, "CQ-WW-CW");
     else
      strcpy(contest_cab, "CQ-WW-SSB");
     break;
    case SS :
     if(mode==0)
      strcpy(contest_cab, "ARRL-SS-CW");
     else
      strcpy(contest_cab, "ARRL-SS-SSB");
     concat(section, set_screen_choices[0], 3,
            strlen(set_screen_choices[0])-1, COPY);
     break;
    case A160 :
     strcpy(contest_cab, "ARRL-160");
     concat(section, set_screen_choices[0], 3,
            strlen(set_screen_choices[0])-1, COPY);
     strcpy(band_cab, "160M");
     break;
    case TEN :
     strcpy(contest_cab, "ARRL-10");
     strcpy(band_cab, "10M");
     break;
    case WAA :
     strcpy(contest_cab, "WAA");
     break;
   }
  strcat(category_cab, " ");
  strcat(category_cab, band_cab);
  strcat(category_cab, " ");
  strcat(category_cab, power_cab);
  strcat(category_cab, " ");
  strcat(category_cab, mode_cab);
  strcpy(line_out, "START-OF-LOG: 2.0");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  if(strlen(section)>0)
   {
   strcpy(line_out, "ARRL-SECTION: ");
   strcat(line_out, section);
   }
  else
   strcpy(line_out, "ARRL-SECTION:");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "CALLSIGN: ");
  strcat(line_out, init_screen_choices[0]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "CATEGORY: ");
  strcat(line_out, category_cab);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  if(isel_contest==WPX && !strcmp(init_screen_choices[11], "<SO-TRI/SE>"))
   {
    strcpy(line_out, "CATEGORY-OVERLAY: ");
    strcat(line_out, "TB-WIRES");
    add_eol_chars (line_out);
    fputs(line_out, fp);
   }
  strcpy(line_out, "CLAIMED-SCORE: ");
  strcat(line_out, score_cab);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "CLUB:");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "CONTEST: ");
  strcat(line_out, contest_cab);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "NAME: ");
  strcat(line_out, init_screen_choices[2]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "ADDRESS: ");
  strcat(line_out, init_screen_choices[3]);
  add_eol_chars (line_out);
  fputs(line_out, fp);
  if(strlen(init_screen_choices[4]))
   {
    strcpy(line_out, "ADDRESS: ");
    strcat(line_out, init_screen_choices[4]);
    add_eol_chars (line_out);
    fputs(line_out, fp);
    strcpy(line_out, "ADDRESS: ");
    strcat(line_out, init_screen_choices[5]);
    strcat(line_out, ", ");
    strcat(line_out, init_screen_choices[6]);
    strcat(line_out, " ");
    strcat(line_out, init_screen_choices[7]);
    add_eol_chars (line_out);
    fputs(line_out, fp);
   }
  else
   {
    strcpy(line_out, "ADDRESS: ");
    strcat(line_out, init_screen_choices[5]);
    strcat(line_out, ", ");
    strcat(line_out, init_screen_choices[6]);
    strcat(line_out, " ");
    strcat(line_out, init_screen_choices[7]);
    add_eol_chars (line_out);
    fputs(line_out, fp);
    strcpy(line_out, "ADDRESS:");
    add_eol_chars (line_out);
    fputs(line_out, fp);
   }
  strcpy(line_out, "OPERATORS:");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  strcpy(line_out, "SOAPBOX:");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fputs(line_out, fp);
  fputs(line_out, fp);
  fputs(line_out, fp);
  switch (isel_contest)
   {
    case WPX :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 0, 2-cr_array[i].mode, COPY);
        right_blank_pad (rst_sent_cab, sizeof(rst_sent_cab));
        strcpy(exch_sent_cab, cr_array[i].qso);
        left_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        concat(rst_rcvd_cab, cr_array[i].rec_num, 0,
               2-cr_array[i].mode, COPY);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        concat(exch_rcvd_cab, cr_array[i].rec_num, 3-cr_array[i].mode,
               strlen(cr_array[i].rec_num)-1, COPY);
        right_blank_pad (exch_rcvd_cab, sizeof(exch_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    case FD :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 0, 1, COPY);
        right_blank_pad (rst_sent_cab, sizeof(rst_sent_cab));
        concat(exch_sent_cab, cr_array[i].sent, 2,
               strlen(cr_array[i].sent)-1, COPY);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        strcpy(rst_rcvd_cab, cr_array[i].rec_num);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        strcpy(exch_rcvd_cab, cr_array[i].rec_county);
        right_blank_pad (exch_rcvd_cab, sizeof(exch_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    case NYQP :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 0, 2-cr_array[i].mode, COPY);
        right_blank_pad (rst_sent_cab, sizeof(rst_sent_cab));
        concat(exch_sent_cab, cr_array[i].sent, 3-cr_array[i].mode,
               strlen(cr_array[i].sent)-1, COPY);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        concat(rst_rcvd_cab, cr_array[i].rec_num, 0,
               2-cr_array[i].mode, COPY);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        concat(exch_rcvd_cab, cr_array[i].rec_num, 3-cr_array[i].mode,
               strlen(cr_array[i].rec_num)-1, COPY);
        if(!strcmp(exch_rcvd_cab, "MR"))
         strcpy(exch_rcvd_cab, "MAR");
        right_blank_pad (exch_rcvd_cab, sizeof(exch_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    case SS :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 1, 2, COPY);
        concat(exch_sent_cab, cr_array[i].sent, 3,
               strlen(cr_array[i].sent)-1, COPY);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        exch_sent_cab[3] = '\0';
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        call_cab[10] = '\0';
        strcpy(num_rcvd_cab, cr_array[i].rec_num);
        left_blank_pad (num_rcvd_cab, sizeof(num_rcvd_cab));
        strcpy(ck_rcvd_cab, cr_array[i].prefix);
        strcpy(rst_rcvd_cab, cr_array[i].rec_county);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].qso);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].sent, 0, 0, !COPY);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, num_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        strcat(line_out, " ");
        strcat(line_out, ck_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    case A160 :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 0, 2-cr_array[i].mode, COPY);
        right_blank_pad (rst_sent_cab, sizeof(rst_sent_cab));
        concat(exch_sent_cab, cr_array[i].sent, 3-cr_array[i].mode,
               strlen(cr_array[i].sent)-1, COPY);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        concat(rst_rcvd_cab, cr_array[i].rec_num, 0,
               2-cr_array[i].mode, COPY);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        if(isdigit(cr_array[i].rec_num[3-cr_array[i].mode]))
         {
          if((lrc = strlen(cr_array[i].rec_county))>6)
           lrc = 6;
          concat(exch_rcvd_cab, cr_array[i].rec_county, 0, lrc-1, COPY);
         }
        else
         concat(exch_rcvd_cab, cr_array[i].rec_num, 3-cr_array[i].mode,
                strlen(cr_array[i].rec_num)-1, COPY);
        right_blank_pad (exch_rcvd_cab, sizeof(exch_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    case WAA :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        strcpy(exch_sent_cab, cr_array[i].sent);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        strcpy(rn_rcvd_cab, cr_array[i].rec_num);
        right_blank_pad (rn_rcvd_cab, sizeof(rn_rcvd_cab));
        strcpy(rc_rcvd_cab, cr_array[i].rec_county);
        right_blank_pad (rc_rcvd_cab, sizeof(rc_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rn_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, rc_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
     break;
    default :
     for(i=1; i<=icall_count; i++)
      if(strcmp(cr_array[i].call, "NOQSO"))
       {
        concat(rst_sent_cab, cr_array[i].sent, 0, 2-cr_array[i].mode, COPY);
        right_blank_pad (rst_sent_cab, sizeof(rst_sent_cab));
        concat(exch_sent_cab, cr_array[i].sent, 3-cr_array[i].mode,
               strlen(cr_array[i].sent)-1, COPY);
        right_blank_pad (exch_sent_cab, sizeof(exch_sent_cab));
        strcpy(call_cab, cr_array[i].call);
        right_blank_pad (call_cab, sizeof(call_cab));
        concat(rst_rcvd_cab, cr_array[i].rec_num, 0,
               2-cr_array[i].mode, COPY);
        right_blank_pad (rst_rcvd_cab, sizeof(rst_rcvd_cab));
        concat(exch_rcvd_cab, cr_array[i].rec_num, 3-cr_array[i].mode,
               strlen(cr_array[i].rec_num)-1, COPY);
        right_blank_pad (exch_rcvd_cab, sizeof(exch_rcvd_cab));
        strcpy(line_out, "QSO: ");
        strcat(line_out, bandf_cab[cr_array[i].iband]);
        strcat(line_out, " ");
        strcat(line_out, mode_char[cr_array[i].mode]);
        strcat(line_out, " ");
        concat(line_out, full_year, 0, 1, !COPY);
        concat(line_out, cr_array[i].date, 4, 5, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 0, 1, !COPY);
        strcat(line_out, "-");
        concat(line_out, cr_array[i].date, 2, 3, !COPY);
        strcat(line_out, " ");
        concat(line_out, cr_array[i].time, 0, 3, !COPY);
        strcat(line_out, " ");
        strcat(line_out, station_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_sent_cab);
        strcat(line_out, " ");
        strcat(line_out, call_cab);
        strcat(line_out, " ");
        strcat(line_out, rst_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, exch_rcvd_cab);
        strcat(line_out, " ");
        strcat(line_out, cr_array[i].tx);
        add_eol_chars (line_out);
        fputs(line_out, fp);
       }
   }
  strcpy(line_out, "END-OF-LOG:");
  add_eol_chars (line_out);
  fputs(line_out, fp);
  fflush(fp);
  fsync(fileno(fp));
  fclose(fp);
}
