home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / xmodem / lf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.2 KB  |  76 lines

  1. #
  2. #include <file.h>
  3.  
  4. #define CR    13
  5. #define NL    10
  6. #define BLOCK    512
  7. #define STOP    0xff
  8.  
  9. CR2LF(file)
  10. char *file; {
  11.     int id, oid;
  12.     char buf[BLOCK+1], *index();
  13.     char temp[11], temp1[11];
  14.     register char *ptr = buf;
  15.     register int len;
  16.  
  17.     strcpy(temp,"tempXXXXXX");
  18.     mktemp(temp);
  19.     if ((oid = open(temp,O_CREAT | O_RDWR,0644)) < 0) {
  20.         perror(temp);
  21.         unlink(temp);
  22.         exit(-1);
  23.     }
  24.  
  25.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  26.         perror(file);
  27.         unlink(temp);
  28.         exit(-1);
  29.     }
  30.  
  31.     buf[BLOCK] = 0;
  32.     while ((len = read(id,ptr,BLOCK)) > 0) {
  33.         while ( ptr = index(ptr,CR) ) {
  34.             *ptr = NL;
  35.         }
  36.         write(oid, ptr = buf, len);
  37.     }
  38.     
  39.     close(id);
  40.     lseek(oid,1,L_XTND);
  41.     do {
  42.         lseek(oid,-2,L_INCR);
  43.         read(oid,ptr,1);
  44.     } while ( *ptr != NL );
  45.     *ptr = STOP;
  46.     write(oid,ptr,1);
  47.     lseek(oid,0,L_SET);
  48.  
  49.     strcpy(temp1,"tempXXXXXX");
  50.     mktemp(temp1);
  51.     if ((id = open(temp1,O_CREAT | O_WRONLY,0644)) < 0) {
  52.         perror(temp1);
  53.         unlink(temp1);
  54.         unlink(temp);
  55.         exit(-1);
  56.     }
  57.  
  58.     ptr = buf;
  59.     while ((len = read(oid,ptr,BLOCK)) > 0) {
  60.         if (ptr = index(ptr,STOP))  {
  61.             *ptr = 0;
  62.             write(id, buf, strlen(buf));
  63.             break;
  64.         }
  65.         write(id, ptr = buf, len);
  66.     }
  67.  
  68.     close(oid); close(id);
  69.     strcpy(buf,"mv ");
  70.     strcat(buf,temp1);
  71.     strcat(buf," ");
  72.     strcat(buf,file);
  73.     system(buf);
  74.     unlink(temp);
  75. }
  76.