/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ /* modified for unixish paths, and direct conversion by jeffdb@netzone.com Mikey */ #include #include #include #include #include #include #include #include #include #define BUFFSIZE 4096 #ifndef O_BINARY # define O_BINARY 0 #endif static int cvt(char *fname, int is_dtou) { int sf, df, il, ol, end; register char *iptr, *optr; char ibuf[BUFFSIZE], obuf[BUFFSIZE * 2 + 1], ctrlz[1]; char tfname[FILENAME_MAX]; struct utimbuf ftime; struct stat *mystat, oldstat; mystat = &oldstat; if (*fname == '-') { sf = 0; df = 1; } else { sprintf(tfname, "cvt$$"); sf = open(fname, O_RDONLY|O_BINARY); /* O_TEXT dosen't work in cygwin32 b17.1 w/binary mounted filesystems so we have to do it all ourselves. */ if (sf < 1) { perror(fname); return 1; } df = open(tfname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); if (df < 1) { perror(tfname); close(sf); return 1; } } end = 0; while (((il=read(sf, ibuf, BUFFSIZE)) > 0) && (!end)) { iptr = &ibuf; optr = &obuf; /* if (is_dtou) il++; */ ol = il; if (is_dtou) { while(il > 0) { switch (*iptr) { case '\r': *iptr++; ol--; il--; break; case '\032': *optr++ = '\n'; ol -= il; il = 0; end++; break; default: *optr++ = *iptr++; il--; break; } } } else { while(il > 0) { while ((*iptr != '\n') && (il > 0)) { *optr++ = *iptr++; il--; } if (il) { *optr++ = '\r'; *optr++ = *iptr++; ol++; il--; } } } write(df, obuf, ol); } if (*fname != '-') { fstat(sf, mystat); ftime.actime = oldstat.st_atime; ftime.modtime = oldstat.st_mtime; utime(tfname, &ftime); close(sf); close(df); remove(fname); rename(tfname, fname); } return 0; } int main(int argc, char **argv) { int i; int is_dtou = 0; int rv = 0; char *temp, *progname; progname = argv[0]; temp = strrchr(progname, '/'); if (temp == NULL) { temp = progname; } else { ++temp; } if (strlen (temp) >= 4 && strcmp (temp + strlen (temp) - 4, ".exe") == 0) *(temp + strlen (temp) - 4) = '\0'; /* dump .exe suffix if necessary */ if (strcmp (temp + strlen (temp) - 4, "dtou") == 0) is_dtou = 1; if (argc == 1) { rv += cvt((char *)"-\0", is_dtou); return rv; } for (argc--, argv++; argc; argc--, argv++) rv += cvt(*argv, is_dtou); return rv; }