
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#include<string.h>
	char l[]={" .,!?(){};\n\t"};
	int lr,rr,bl,wcount;
	char word[300],sym[300],buf[90];

int letter(char x)
{       int q=0,i=0;
	while(l[i]!=NULL) {q+=(l[i]==x);i++;}
	return q;}
// leter ----------------------
int gword(FILE *fh)
{	char x;int i=0;

	x=fgetc(fh);
	while (((!feof(fh)))&&(!letter(x)))
			{word[i]=x;i++;x=fgetc(fh);}
		word[i]=NULL;
		i=0;
	while ((!feof(fh))&&letter(x)&&x!='\n'){if(x==9)x=' ';sym[i]=x;i++;x=fgetc(fh);}
	if(x=='\n') {sym[i]='\n';i++;}
		sym[i]=NULL;
	if(!feof(fh)&&x!='\n') fseek(fh,-1,1);

	return !feof(fh);}
// get word -----------
int getbuf(FILE *fh)
{
	int i=0,j=0,f=0,sw,ss;

   do {
	if(!word[0]&&!sym[0]) gword(fh);
		ss=strlen(sym);sw=strlen(word);
	if(sw>=rr-lr) {printf("\n ERROR ws range");exit(0);}

	f=(i+sw>=rr-lr);
	f+=2*((!f)&&(i+sw+ss>=rr-lr));

	j=0;while(sym[j]&&sym[j]!='\n') j++;
	if(f!=1&&sym[j]=='\n') {f=3;sym[j]=NULL;}

	if(f!=1)
	       { if(word[0]) wcount++;
		 j=0;while(word[j]){buf[i]=word[j];j++;i++;}
		 for (j=0;(i<rr-lr)&&sym[j];j++,i++) buf[i]=sym[j];
		 word[0]=NULL;sym[0]=NULL;
		}
	}
    while(!f&&!feof(fh));

	      if(f==2) fseek(fh,j-ss,1);

	      bl=rr-lr-i;
	      while(i<rr-lr) {buf[i]=' ';i++;}
	      buf[i]=NULL;

	      return !feof(fh);
}
//--------GET BUF------
int conv(char *str)
{
	int i,v=0,ss=strlen(str),j,o;

	for (i=0,o=1;i<ss;i++,o*=10)
      {	v+=(str[ss-1-i]-'0')*o;
   if(str[ss-1-i]>'9'||str[ss-1-i]<'0'){printf("\nnumber error");exit(0);}
       }
	return v;}
//---------CONV---------
int bufcount()
{	int i=rr-lr-1;
	while((buf[i]==' ')&&i) i--;
	return i;
}
//-----SPACE COUNT
void leftpos()
{
	int i=0,j=0,k=0;
	i=bufcount();
	while(buf[j]==' ') j++;
	while(j<=i){buf[k]=buf[j];k++;j++;}
	while(k<=i){buf[k]=' ';k++;}
}
//-------leftpos--
void rightpos()
{	int i,j=rr-lr;

	i=bufcount()+1;
	while(i>=0) {buf[j]=buf[i];j--;i--;}
	while(j>=0) {buf[j]=' ';j--;}

}
//-----RIGHT POS
void centrepos()
{
	int i,j=0,n,k=0;char sb[81];

	i=bufcount();
	n=rr-lr-i;while((buf[j]==' ')&&j<i) j++;
	n+=j;
	while(i>=j){sb[k]=buf[j];j++;k++;}j=0;sb[k]=NULL;k=0;
	while(j<n/2){buf[j]=' ';j++;}
	while(sb[k]) {buf[j]=sb[k];k++;j++;}
	while(j<rr-lr) {buf[j]=' ';j++;}
}
//---centerpos
//void spsins(int,int);
int spins(int,int,int);

void doublepos()
{
	int scs=0,sc=0,wsc=0,i,j=0,scnt=0;
	leftpos();

	i=rr-lr-1;
	while(buf[i]==' ') i--;
	sc=rr-lr-i;

	while(j<i)
    {
	while(j<i&&buf[i]!=' ') j++;
	if(j<i) scnt++;
	while(j<i&&buf[j]==' ') j++;
    }
	j=0;
	printf("**%d**",scnt);

	if(scnt)
   {
	scs=sc/scnt+(sc%scnt!=0);

	while(j<i)
     {	while(j<i&&buf[j]!=' ') j++;
	i=spins(j,scs,i);
	while(j<i&&buf[i]==' ') j++;
     }
   }

}
/*
void spsins(int i,int wsc)
{
	int j=i,scount,scs;

	while(letter(buf[j])) j++;
	scount=j-i-1;j=i;
	if(scount>0)
    {	scs=wsc/scount+(wsc%scount!=0);
	while(scount>0&&wsc>0)
      {	scount--;
	spins(j,scs);
	wsc-=scs;
	j+=scs+1;
      }
     }
     else spins(i,wsc);
}
  */
int spins(int j,int scs,int ii)
{	int i=rr-lr-1;
	while(buf[i]==' ') i--;
	if(i+scs>rr-lr-1) scs=rr-lr-1-i;
	while(i>j){buf[i+scs]=buf[i];i--;}
	for(i++;i<=j+scs;i++) buf[i]=' ';

	return (ii+scs);
}
//---double pos---
void prbuf()
{	int i;
	printf("\n");
	for(i=0;i<lr;i++) putch('-');
	for(i=0;i<rr-lr;i++) putch(buf[i]);
	putch('-');
}
//--------PRINT BUF-------
void cbuf(char h)
{
	switch (h)
	{
		case 'l':
			leftpos();break;
		case 'r':
			rightpos();break;
		case 'c':
			centrepos();break;
		case 'd':
			doublepos();break;
		default:
			break;
	}
}
//---CONVERT BUF
int main(int argc,char **argv)
{       FILE *fh;

	if (argc!=5) {printf("\n COMMAND LINE ERROR \n");exit(0);}

	lr=conv(argv[2]);rr=conv(argv[3]);
     if(lr<0||lr>77||rr<1||rr>77||lr>=rr){printf("\nERROR RANGES");exit(0);}
  if((fh=fopen(argv[1],"r"))==NULL){printf("\n ERROR OPENING FILE");exit(0);}

	 buf[rr-lr]='\n';

	 int q=1;
	 clrscr();int rows=0;

	 while(q)
	{ q=getbuf(fh);
	  cbuf(argv[4][0]);wcount=0;
	  prbuf();
	  rows++;if(rows==23) {rows=0;if(getch()=='q') exit(0);}
	 }
	 if(word[0]) {getbuf(fh);cbuf(argv[4][0]);prbuf();}

	 if(fclose(fh)==EOF) {printf("\nERROR CLOSING FILE");exit(0);}
		 return 1;
 }
//---------END PROG---