    /* this program is for those people using Procomm+ that move directories
       from one default area code to another, i.e. from work to home and back.
       If you find this program useful, register your Procomm+ program if you
       have'nt done so already!

       If you have any comments or suggestions please contact:

       Program author: Albert Stein
       Company:        Software Ideology
                       P.O. Box 305
                       Brooklyn, NY 11204
       Telephone:      1-718-236-3876

       Please do not modify and repost this code!  You are free to modify this
       code for your own use only.

       Program compiled using TurboC 2.0 on 04/05/90

    */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <ctype.h>

void syntax (void);             /* display command line syntax */
int get_args (char *argv[]);    /* parse/validate command line data */
int process (void);             /* create new .dir file */

    /* storage space */
unsigned char old_file[81];
unsigned char new_file[81];
unsigned char old_area[6];
unsigned char new_area[6];

void main (int argc, char *argv[])
{
    if (argc != 5)
    {   syntax ();
        exit (1);
    }
    if (get_args (argv) == -1)
        exit (1);

    if (process () == -1)
    {   printf ("Original file ok, no new file created!");
        exit (1);
    }

    exit (0);
}

void syntax ()
{   printf ("Syntax:  PCPAREA old-file new-file old-area new-area\n"
        "   File names default to a .DIR extension if not specified\n"
        "   Area codes must be in the form 1-nnn\n");
}

    /* get input from command line,
       verify input for accessibility/validity */
int get_args (char *argv[])
{
    int badform = 0;
    int i;

    if (strlen (argv[1]) > 76)
    {   printf ("Input (old) filename to long\n");
        return (-1);
    }
    else
    {   strcpy (old_file,argv[1]);
        if (strchr (old_file, '.') == NULL)
            strcat (old_file,".DIR");
    }

    if (access (old_file,0) != 0)
    {   printf ("Unable to access input file '%s'\n",old_file);
        return (-1);
    }

    if (strlen (argv[2]) > 76)
    {   printf ("Output (new) filename to long\n");
        return (-1);
    }
    else
    {   strcpy (new_file,argv[2]);
        if (strchr (new_file, '.') == NULL)
            strcat (new_file,".DIR");
    }

    if (strlen (argv[3]) != 5)
        badform = 1;
    else
    {   strcpy (old_area,argv[3]);
        if (strncmp (old_area,"1-",2) != 0)
            badform = 1;
        else
        for (i=2; i < 5; i++)   /* check the area code for numeric data */
        {   if (!isdigit (old_area[i]))
            {   badform = 1;
                break;
            }
        }
    }

    if (badform)
    {   printf ("Old area code not in '1-nnn' format\n");
        return (-1);
    }

    if (strlen (argv[4]) != 5)
        badform = 1;
    else
    {   strcpy (new_area,argv[4]);
        if (strncmp (new_area,"1-",2) != 0)
            badform = 1;
        else
        for (i=2; i < 5; i++)   /* check the area code for numeric data */
        {   if (!isdigit (new_area[i]))
            {   badform = 1;
                break;
            }
        }
    }

    if (badform)
    {   printf ("New area code not in '1-nnn' format\n");
        return (-1);
    }

    strupr (old_file);
    strupr (new_file);
    return (0);
}

int process()
{   
    FILE *in, *out;
    unsigned char entry[75];
    unsigned char header[251];
    unsigned char number[22];
    int ret = 0;
    int old = 0, new = 0;
    int i;

    if (( in = fopen (old_file,"rb")) == NULL)
    {   printf ("Open failure for old file '%s'\n",old_file);
        return (-1);
    }

    if (( out = fopen (new_file,"wb")) == NULL)
    {   printf ("Open failure for new file '%s'\n",new_file);
        fclose (in);
        return (-1);
    }

    if ( fread (header,250,1,in) != 1)
    {   printf ("Bad read of header\n");
        ret = -1;
    }
    else
    if ( fwrite (header,250,1,out) != 1)
    {   printf ("Bad write of header\n");
        ret = -1;
    }

    for (i=0; 1 ; i++)  /* after header, keep looping thru rest of input */
    {   
        if ( fread (entry,74,1,in) != 1)
        {   if (i < 200)
            {   printf ("Bad read of entry %d\n",i+1);
                ret = -1;
            }
            break;
        }
        if (strncmp (&entry[25],old_area,5) == 0)   /* found old, strip area */
        {   strcpy (number,&entry[31]);
            sprintf (&entry[25],"%s%c%c%c%c%c%c",number,0,0,0,0,0,0);
            old++;
        }
        else
        if (entry[28] == '-')   /* found no area code, insert new area */
        {   strcpy (number,&entry[25]);
            sprintf (&entry[25],"%s-%s",new_area,number);
            new++;
        }
        if ( fwrite (entry,74,1,out) != 1)
        {   printf ("Bad write of entry %d\n",i+1);
            ret = -1;
            break;
        }
    }

    fclose (in);
    fclose (out);
    if (ret == -1)
        unlink (new_file);
    else
        printf ("Successfully updated %d records\n"
                "%d %s entries changed to nulls\n"
                "%d null entries changed to %s\n",
                    i,old,old_area,new,new_area);

    return (ret);
}
