Category : C Source Code
Archive   : SMALLC.ZIP
Filename : FORMAT3.C

 
Output of file : FORMAT3.C contained in archive : SMALLC.ZIP
/* Text formatter: I/O routines
*
* text formatter transcribed from the book
* Software Tools by Kernighan and Plauger
*
* Source: format3.c
* Version: April 5, 1980.
*/




/* Define global I/O variables */


int input; /* input file number */
int output; /* output file number */


/* open output file out.txt */


openout()
{
char line[81]; /* CP/M assumes length is 81 */
while (1) {
/* comment out ..........
pl("Output filename ? ");
gets(line);
if (line[0]==0) {
output=0;
return;
}
.......... end comment out */
/* output file is always out.txt */
if ((output=fopen("out.txt","w"))!=NULL) {
return;
}
else {
pl("Open failure on out.txt");
output=0;
return;
}
}
}


/* open input file. return EOF if no more to open */


openin()
{
char line[81]; /* CP/M assumes length is 81 */
while (1) {
pl("Input filename ? ");
gets(line); /* get file name */
if (line[0]==0) {
return(EOF);
}
if ((input=fopen(line,"r"))!=NULL) {
return(OK);
}
else {
pl("Open failure");
}
}
}


/* close output file if it exists */


closeout()
{
if (output!=0) {
fclose(output);
output=0;
}
}


/* close input file */


closein()
{
if (fclose(input)==NULL) {
pl("Input close failed");
}
}


/* get next line from input file into buf.
* return EOF when all input is exhausted.
* terminate the input line with NEWLINE.
*/


getlin (buf) char *buf;
{
int k;
char c;
if (ineof==YES) {
if (openin()==EOF) {
return(EOF);
}
else {
ineof=NO;
}
}
k=0;
while (k c=getc(input);
if (c==-1) {
closein();
buf[k++]=NEWLINE;
buf[k]=EOS;
ineof=YES;
return(OK);
}
else if (c==NEWLINE) {
buf[k++]=NEWLINE;
buf[k]=EOS;
return(OK);
}
else {
buf[k++]=c;
}
}
buf[k]=EOS;
pl("line truncated");
pl(buf);
buf[k++]=NEWLINE;
buf[k]=EOS;
while (1) {
c=getc(input);
if ((c==NEWLINE)+(c==-1)) {
break;
}
}
return(OK);
}


/* print string on console on new line */


pl(s) char *s;
{
putchar(CR);
puts(s);
}


/* output one character to the output file */


putout(c) char c;
{
if (output==0) {
putchar(c);
return(c);
}
if (putc(c,output)==-1) {
pl("output file write error");
return(-1);
}
else {
return(c);
}
}


/* output line to output file */
/* NOTE: this routine replaces putlin(buf,STDOUT) */


putlnout(buf) char *buf;
{
int k;
k=0;
while(buf[k]!=EOS) {
if (putout(buf[k])==-1) {
break;
}
else {
k++;
}
}
}




  3 Responses to “Category : C Source Code
Archive   : SMALLC.ZIP
Filename : FORMAT3.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/