lseek(fi,0L,0);
lockf(fi,1,0);
read(fi,m1,12);
m1[12]=0;
sscanf(m1,"%ld%ld",&rgh1,&rgd);
for (i=0;i<nr;i++) if (doc[i][0][0]!=0 && doc[i][0][0]!=32 &&p[i]< 0) p[i]=rgd++;
if (flag <0)
{
	if(folio <0) for(count=rgh;count<rgh1;count++)
	{
		fseek(fh,count*lh,0);
		fgets(ptr[0][count],ph[0][2]+1,fh);
		if(!strncmp(ptr[0][count],head[0],ph[0][2])) flag=count;
	}
	if (flag <0 )
	{
		rgh=rgh1;
		flag =rgh1++;
		if (folio > -1)
		{
			sprintf(m1,"%%.%dld",ph[0][2]);
			sprintf(head[0],m1,flag+folio);
			printf("%s",head[0]);

		}
		sprintf(ptr[0][flag],"%s",head[0]);
		if ((fr=open("H",O_RDWR)) >0)
		{
			lseek(fr, 0L, 2);
			sprintf(m1,"%%-%ds",ph[0][2]);
			sprintf(m2,m1,head[0]);
			write(fr,m2,ph[0][2]);
		}
		else fprintf(stderr,"NO PUEDO ABRIR H en %s",argv[1]);
		
		close(fr);
		if (fuser) 
		{
			sprintf(m1,"%s",ctime(&tp));
			sprintf(m2,"%s:ING:%2.2s%2.2s%2.2s\n",head[0],m1+11,m1+14,m1+17);
			write(fuser,m2,strlen(m2));
		}

	}
	else 
	{
		sprintf(m1,"%s REGISTRO DUPLICADO ... ABORTADO EL INGRESO ",head[0]);
		coment(m1);
		getin1();
		flag = -1;
	}
}
else if(fuser) write(fuser,"RET\n",4);
fseek(fh,flag*lh,0);
for (i=0;i<nh;i++) if (ph[i][3]<30 || ph[i][3]>49)
{
	sprintf(m1,"%%-%d.%ds",ph[i][2],ph[i][2]);
	fprintf(fh,m1,head[i]);
}
for (i=0;i<nr;i++)
{
	if (p[i]== -1) fprintf (fh,"-00001");
	else fprintf(fh,"%.6ld",p[i]);
}
fprintf(fh,"\n");
for (i=0;i<nr;i++)
{
	if(p[i]> -1)
	{
		fseek(fd,p[i]*ld,0);
		for (j=0;j<nc;j++) if (pd[i][j][3] <30 || pd[i][j][3]>49)
		{
			sprintf(m1,"%%-%d.%ds",pd[i][j][2],pd[i][j][2]);
			fprintf(fd,m1,doc[i][j]);
		}
		fprintf(fd,"%5d\n",flag);
	}
}
fflush(fh);
fflush(fd);
sprintf(m1,"%5ld %6ld",rgh1,rgd);
lseek(fi,0L,0);
write(fi,m1,12);
lseek(fi,0L,0);
lockf(fi,0,0);
