/* crlf.c - convert text files to different line terminators */
/* Neil Franklin, Morgenweg 8, 8404 Winterthur */
/* version (last revision) 7.9.93 */
/* written in ANSI C */

#include <stdio.h>
#include <string.h>
#define CR 13
#define LF 10

void main(int argc, char *argv[]) {
  int target = ' ', c;
  if (argc == 2 && *argv[1] == '-' && !*(argv[1]+2)) {
    target = tolower(*(argv[1]+1)); }
  if (target != 'd' && target != 'u' && target != 'm') {
    fprintf(stderr, "usage: %s {-d|-u|-m} < input > output\n", argv[0]);
    fprintf(stderr, "       -d converts to DOS format (cr lf)\n");
    fprintf(stderr, "       -u converts to Unix format (lf)\n");
    fprintf(stderr, "       -m converts to Mac format (cr)\n");
    exit(1); }
  while ((c = getchar()) != EOF) {
    if (c == CR || c == LF) {
      if (c == CR) {
        c = getchar(); }
      if (c == LF) {
        c = getchar(); }
      ungetc(c,stdin);
      if (target == 'd') {
        putchar(CR); c = LF; }
      if (target == 'u') {
        c = LF; }
      if (target == 'm') {
        c = CR; } }
    putchar(c); }
  exit(0); }
