home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / xmodem / add.c next >
C/C++ Source or Header  |  1986-11-30  |  881b  |  54 lines

  1. #
  2. # include <file.h>
  3.  
  4. # define CR    13
  5. # define NL    10
  6. # define CTRL_Z 26
  7.  
  8. char *
  9. ADDCR(file)
  10. char *file; {
  11.     int id, oid;
  12.     char buf[512], *index();
  13.     static char temp[11];
  14.     char *end = "\r\n";
  15.     int ln, cnt;
  16.     register char *ptr = buf, *od;
  17.     register i, count;
  18.  
  19.     strcpy(temp,"tempXXXXXX");
  20.     mktemp(temp);
  21.     if ((oid = open(temp,O_CREAT | O_WRONLY,0644)) < 0) {
  22.         perror(temp);
  23.         unlink(temp);
  24.         exit(-1);
  25.     }
  26.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  27.         perror(file);
  28.         unlink(temp);
  29.         exit(-1);
  30.     }
  31.             
  32.     while (ln = read(id,ptr,512)) {
  33.         count = 0;
  34.         while ( od = index(ptr,NL) ) {
  35.             if (od > &buf[ln]) break;
  36.             *od = '\0';
  37.             cnt = strlen(ptr);
  38.             count += cnt;
  39.             write(oid, ptr, cnt);
  40.             write(oid, end, 2);
  41.             ptr = ++od;
  42.             count++;
  43.         }
  44.         if (count = ln - count)
  45.             write(oid,ptr,count);
  46.         ptr = buf;
  47.     }
  48.     *ptr = CTRL_Z;
  49.     write(oid,ptr,1);
  50.  
  51.     close(oid); close(id);
  52.     return(temp);
  53. }
  54.