home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
UTILITY
/
SORTREGS.ZIP
/
SORTREGS.C
< prev
Wrap
Text File
|
1995-12-20
|
3KB
|
111 lines
The Black Dragon 1@1180
Mon Dec 18 14:30:18 1995 PST
This program is designed to sort and merge a regions file. It will operate
on REGIONS.DAT and on the extended regions files. Should it find two assignments
for the same area code (or prefix for the extended files), it will take the
LATER occurance. When it writes the data back to the file, it is written
in alphabetical order for the locations and numerical order within the location
for the area codes or prefixes.
It is distributed in SOURCE code to demonstrate to certain paranoid individuals
on the various networks that there is nothing covert going on here. Regardless,
I still retain the copyright and ask that anyone who modifies this code give me
the credit I am due for being the original author.
The program was compiled using Turbo C++ 1.0 to a COM file (Tiny model).
It should work with any other compilation environment.....
--------------------------------------------
#include <alloc.h>
#include <conio.h>
#include <ctype.h>
#include <dir.h>
#include <dos.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>
#include <mem.h>
#include <process.h>
#include <stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stat.h>
#define MAX_VALUE 1000
typedef struct prefix_rec {
struct prefix_rec *next;
unsigned int start;
unsigned int count;
unsigned char text;
} prefix_rec;
prefix_rec *first=NULL;
prefix_rec *exchange[MAX_VALUE];
void main(int argc, char *argv[])
{
FILE *f;
prefix_rec *p2;
prefix_rec *p=NULL;
unsigned char *q, s[80];
signed int i;
if (argc!=2) {
fprintf(stderr,"%s <regionsfile>\n",argv[0]);
exit(1);
}
if ((f=fopen(argv[1],"rt"))==NULL) {
fprintf(stderr,"%s: Could not read file \"%s\"\n",argv[0],argv[1]);
exit(2);
}
for(i=0;i<MAX_VALUE;++i) exchange[i]=NULL;
while(fgets(s,sizeof(s),f)!=NULL) {
s[sizeof(s)-1]=0;
if ((q=strchr(s,'\n'))!=NULL) *q=0;
if (strlen(s)<3) continue;
if (isdigit(s[0])) {
if ((i=atol(s))>=MAX_VALUE) continue;
if (exchange[i]!=NULL) --exchange[i]->count;
if ((exchange[i]=p)==NULL) continue;
if (i<p->start) p->start=i;
++p->count;
} else {
p2=(prefix_rec *)(((p!=NULL)&&(stricmp(&p->text,s)<0))?p:&first);
for(;p2!=NULL;p2=p2->next) {
if ((p=p2->next)!=NULL) {
if (!(i=stricmp(&p->text,s))) break;
if (i<0) continue;
}
if ((p=(prefix_rec *)farmalloc(sizeof(prefix_rec)+strlen(s)))==NULL) {
fprintf(stderr,"%s: Out of memory\n",argv[0]);
exit(3);
}
strcpy(&p->text,s);
p->start=MAX_VALUE;
p->next=p2->next;
p2->next=p;
p->count=0;
break;
}
}
}
fclose(f);
if ((f=fopen(argv[1],"wt+"))==NULL) {
fprintf(stderr,"%s: Could not write file \"%s\"\n",argv[0],argv[1]);
exit(4);
}
for(p=first;p!=NULL;p=p->next) {
if (!p->count) continue;
fprintf(f,"%s\n",&p->text);
for(i=p->start;i<MAX_VALUE;++i) {
if (exchange[i]==p) {
exchange[i]=NULL;
fprintf(f,"%03u\n",i);
if (!--p->count) break;
}
}
}
fclose(f);
}
--------------------------------------------