Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 May 1999 21:25:37 +0200 (CEST)
From:      "Michael C. Vergallen" <mvergall@mail.double-barrel.be>
To:        freebsd-stable@FreeBSD.ORG
Subject:   Dos2unix 
Message-ID:  <Pine.LNX.4.04.9905032123010.5274-100000@ws3.double-barrel.be>

next in thread | raw e-mail | index | archive | help
Here it is ... someone gave me this routine to use ...

------------- dud.c starts here ---------------------
/*
===============================================================================

    DUD  Dos to Unix to Dos Ascii conversion

22/12/95              TC20
Ported to Linux 07/01/99 By DDU
Surely there's some bad code in here
But it works for me
===============================================================================

*/

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>

unsigned char getbyte();
void outbyte(unsigned char kar);

int hdl1,hdl2;

void main(int argc,char *argv[])
{
    unsigned char kar,kar2,flag;

    if(argc<4)
     {
 puts("dud -d infile outfile  [Dos to Unix conversion]");
 puts("dud -u infile outfile  [Unix to Dos conversion]");
 exit(0);
     }
    if( (hdl1=open(argv[2],O_RDONLY ))==-1)
        { puts("INFILE OPEN FAILED."); exit(0); }
    if( (hdl2=open(argv[3],O_CREAT | O_RDWR,S_IWRITE ))==-1)
        { puts("OUTFILE OPEN FAILED."); exit(0); }

/* Dos to Unix conversion */
    if(strcmp(argv[1],"-d")==0)
     {
 while(1)
         {
         kar=getbyte();
         if(kar==0xff) { outbyte(0); close(hdl1); close(hdl2); exit(0);
}
         if(kar==13)
             {
         if( (kar=getbyte() )==10)
                 {
                 outbyte(10);
                 }
             else { outbyte(kar); }
             }
         else outbyte(kar);
         }
     }

/* Unix to Dos conversion */
     else if(strcmp(argv[1],"-u")==0)
     {
 while(1)
         {
         kar=getbyte();
         if(kar==0xff) { outbyte(0); close(hdl1); close(hdl2); exit(0);
}
         if(kar==10)
             {
         outbyte(13);
  outbyte(10);
             }
            else outbyte(kar);
         }
     }
    else { puts("invalid option."); exit(0); }

}

/*------------------------------------------------------------------------*/

unsigned char getbyte() /* Get next byte from our buffer */
{
    static unsigned char first;
    static int off,rval;
    static unsigned char buf[4096],out;
    static unsigned char *kar=buf;

    if( (first==0) || (off==4096) )     /* If end of buffer */
     {
        rval=read(hdl1,buf,4096);     /* Read in next block */
        first=1;
        off=0;
     }
    out=*(kar+off);
    if( (rval<4096) && (rval==off) ) { return(0xff); }
    off++;
    return (out);
}

/*-----------------------------------------------------------------------*/

void outbyte(unsigned char kar)
{
    static unsigned char buf[4096];
    static unsigned char *ptr=buf;
    static int off,rval;
    if(kar==0) { write(hdl2,ptr,off); return; }

    if(off==4096)
     {
        rval=write(hdl2,ptr,4096);
        off=0;
     }
     *(ptr+off)=kar;
     off++;
}

----end-----
Michael
---
Michael C. Vergallen A.k.A. Mad Mike, 
Sportstraat 28			http://www.double-barrel.be/mvergall/
B 9000 Gent			ftp://ftp.double-barrel.be/pub/linux/
Belgium				tel : 32-9-2227764 Fax : 32-9-2224976
			



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-stable" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.LNX.4.04.9905032123010.5274-100000>