Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hoptoad!pozar From: pozar@hoptoad.uucp (Tim Pozar) Newsgroups: comp.sys.ibm.pc Subject: Re: Text file conversion between UNIX and MSDOS Message-ID: <6489@hoptoad.uucp> Date: 10 Feb 89 21:10:20 GMT References: <89Feb9.123853est.2662@godzilla.eecg.toronto.edu> Distribution: na Organization: Late Night Software (San Francisco) Lines: 277 noworol@eecg.toronto.edu (Mark Noworolski) wrote: > Very frequently when I get stuff off the net I run into the problem of > no carriage returns. It appears that UNIX stores text a little differently > from Messdos. > > Bafore writing something to fix this problem... I figure somebody's probably > already done it. > > Can somebody send me a program to do this? A number of people asked me for the programme... So here is the source. If you want the binaries you can download the programme from my bbs at +1 415 695 0759. Tim --- #include /* * TOTXT * * Changes UNIX, MAC, or ASCII newline characters in their text files * into the local enviorment's newline convention. * */ /* * REVISION HISTORY * * 3.2 5.Dec.88 * Added ROT13 * * 3.1 9.Dec.87 * Added code to handle form-feeds (^L) properly. * * 3.0 29.Nov.87 * Added -p switch for printer margin handling. * * 2.0 21.Oct.87 * Added switches for tab handling and tab handling routines. * */ int ver = 3; /* Current version and revision numbers. */ int rev = 2; #define FALSE 0 #define TRUE 1 #define WIDTH 80 /* output width */ #define PL 66 /* total page length */ #define MT 3 /* top margin */ #define MB 3 /* bottom margin */ #define PO 8 /* page offset */ #define TABSPACE 8 /* tab positions */ /* Globals ... */ int hpos = 1; /* pointer to cursor position */ int vpos = 1; /* pointer to line number */ int rot13 = FALSE; /* Rotate the characters 13 places for encryption or decryption. */ int tabstrip = FALSE; /* flag to indicate if we are striping the tabs */ int printer = FALSE; /* printer mode */ main(argc, argv) int argc; char *argv[]; { FILE *fp, *fopen(); int i; if(argc == 1){ banner(); exit(0); } /* scan command line arguments, and look for files to work on. */ for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { if((fp = fopen(argv[i],"rb")) == NULL) { printf("\rTOTXT: can't open %s \n",argv[i]); break; } else { filecopy(fp); fclose(fp); } } else { switch (argv[i][1]){ case 'R': case 'r': rot13 = TRUE; break; case 'T': case 't': tabstrip = TRUE; break; case 'P': case 'p': tabstrip = TRUE; printer = TRUE; break; default: printf(" I don't know the meaning of -%c.\n",argv[i][1]); banner(); exit(1); } } } } banner() { printf("TOTXT ver %d.%d Copyright 1987-1988 Timothy Pozar\n",ver,rev); printf("USAGE:\n"); printf("TOTXT [-[prt] filename.ext [filename.ext filename.ext ...]\n"); printf("\n"); printf(" Changes UNIX, MAC, IBM-PC, or CP/M newlines in their text files\n"); printf("into the local enviorment's newline convention. Also strips the high\n"); printf("bit off of the characters for word processed files, such as the output\n"); printf("files from WordStar(TM) MicroPro.\n"); printf(" Output is via the standard output device.\n"); printf(" Non-printing control characters will show as '^c'. Where c is the\n"); printf("control character's name in upper-case (eg. ^G = bell). ^@ and ^Z are\n"); printf("not displayed, but are tossed.\n"); printf(" Currently does not support wild cards.\n"); printf("\n"); printf("SWITCHES:\n"); printf(" -p = Printer mode:\n"); printf(" Strip tabs and replace with appropriate number of spaces.\n"); printf(" Inserts a top, bottom, and left hand margin.\n"); printf(" -t = strip tabs and replace with appropriate number of spaces.\n"); printf(" -r = ROT13 encryption or decryption.\n"); printf("\n"); printf(" Files will be processed in the order of the command line. If an\n"); printf("action switch is put after a file name, the file will not be processed\n"); printf("with the action specified by the switch.\n"); printf("eg. totxt FOO -t BOZO\n"); printf(" The file named FOO will not have it's tabs striped out, but the\n"); printf("file BOZO will.\n"); } filecopy(fp) FILE *fp; { int c; /* char to test and output */ int i,n; /* All around variable */ int CRFLAG = FALSE; /* 'NEWLINE' flags ... */ int LFFLAG = FALSE; while ((c = getc(fp)) != EOF){ if ((vpos == PL - MB + 1) && (printer)){ for (i = 0; i < MB; i++){ /* Do bottom margin */ printf("\n"); } vpos = 1; /* At top of page again */ hpos = 1; } if ((vpos == 1) && (printer)){ for (i = 0; i < MT; i++){ /* Create top margin */ printf("\n"); } vpos = MT + 1; /* At start of text vertical position */ hpos = 1; } if ((hpos == 1) && (printer)){ for (i = 0; i < PO; i++){ /* Do page offset */ printf(" "); } hpos = PO + 1; /* At start of text cursor position */ /* printf("%d",vpos); */ /* Start of line number hack */ } c = c & 0x7F; switch (c) { case 0: /* ^@ and */ case 26: /* ^Z Throw these guys out... */ break; case 9: /* Tab test */ n = totab(); if (!tabstrip) /* if not striping tabs then put out a real tab... */ putchar(c); while (n > 0) { if (tabstrip) /* ... and forget putting out spaces */ putchar(' '); ++hpos; --n; } break; case 10: /* Line feed test */ if (!CRFLAG){ printf("\n"); LFFLAG = TRUE; hpos = 1; vpos++; } break; case 12: /* Form feed test */ if (printer){ /* Eject the page */ eject(); } else { /* Just stick it out there */ putchar(c); CRFLAG = FALSE; LFFLAG = FALSE; ++hpos; } break; case 13: /* Carriage return test */ if (!LFFLAG){ printf("\n"); CRFLAG = TRUE; hpos = 1; vpos++; } break; default: if (((c >= 0) && (c <= 0x8)) || ((c >= 0xD) && (c <= 0x1F))){ putchar('^'); putchar(c + 0x40); ++hpos; } else { if (rot13){ if (((c >= 'A') && (c <= 'M')) || ((c >= 'a') && (c <= 'm'))){ putchar(c + 0xd); } if (((c >= 'N') && (c <= 'Z')) || ((c >= 'n') && (c <= 'z'))){ putchar(c - 0xd); } if (!(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))){ putchar(c); } } else { putchar(c); } CRFLAG = FALSE; LFFLAG = FALSE; ++hpos; } break; } } if (printer) { /* Eject paper from printer */ eject(); } } totab() { int i; i = hpos - 1; while (i >= TABSPACE){ i = i - TABSPACE; } return (TABSPACE - i); } /* * Ejects the page via newlines. * */ eject() { int i; for (i = vpos; i < PL + 1; i++) { printf("\n"); } vpos = 1; hpos = 1; } -- ...sun!hoptoad!\ Tim Pozar >fidogate!pozar Fido: 1:125/406 ...lll-winken!/ PaBell: (415) 788-3904 USNail: KKSF / 77 Maiden Lane / San Francisco CA 94108