home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / xmodem / del.c < prev    next >
Encoding:
Text File  |  1986-11-30  |  1.0 KB  |  61 lines

  1. #
  2. # include <file.h>
  3.  
  4. # define CR    13
  5. # define NL    10
  6. # define CTRL_Z    26
  7. # define BLOCK    512
  8.  
  9. DELCR(file)
  10. char *file; {
  11.     int id, oid;
  12.     char buf[BLOCK+10], *index();
  13.     static char temp[11];
  14.     register char *ptr = buf, *od, *cz;
  15.     register int len;
  16.  
  17.     strcpy(temp,"tempXXXXXX");
  18.     mktemp(temp);
  19.     if ((oid = open(temp,O_CREAT | O_WRONLY,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.     while ((len = read(id,ptr,BLOCK)) > 0) {
  32.         *(ptr + len) = 0;
  33.         while ( od = index(ptr,CR) ) {
  34.             *od = '\0';
  35.             if (cz = index(ptr,CTRL_Z)) {
  36.                 *cz = '\0';
  37.                 write(oid, ptr, strlen(ptr));
  38.                  goto OUT;
  39.             }
  40.             write(oid, ptr, strlen(ptr));
  41.             ptr = ++od;
  42.         }
  43.         if (cz = index(ptr,CTRL_Z)) {
  44.             *cz = '\0';
  45.             write(oid, ptr, strlen(ptr));
  46.              goto OUT;
  47.         }
  48.         if (len = strlen(ptr))
  49.         write(oid, ptr, len);
  50.         ptr = buf;
  51.     }
  52.  
  53. OUT:
  54.     close(oid); close(id);
  55.     strcpy(buf,"mv ");
  56.     strcat(buf,temp);
  57.     strcat(buf," ");
  58.     strcat(buf,file);
  59.     system(buf);
  60. }
  61.