home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / remove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-31  |  1.2 KB  |  51 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <dpmi.h>
  8. #include <go32.h>
  9. #include <libc/dosio.h>
  10.  
  11. int
  12. remove(const char *fn)
  13. {
  14.   __dpmi_regs r;
  15.   unsigned attr;
  16.   int directory_p;
  17.   int use_lfn = _USE_LFN;
  18.  
  19.   /* Get the file attribute byte.  */
  20.   attr = _chmod(fn, 0);
  21.   directory_p = attr & 0x10;
  22.  
  23.   /* Now, make the file writable.  We must reset Vol, Dir, Sys and Hidden bits 
  24.      in addition to the Read-Only bit, or else 214301 will fail.  */
  25.   _chmod(fn, 1, attr & 0xffe0);
  26.  
  27.   /* Now delete it.  Note, _chmod leaves dir name in tranfer buffer. */
  28.   if (directory_p)
  29.     r.h.ah = 0x3a;        /* DOS Remove Directory function */
  30.   else
  31.     r.h.ah = 0x41;        /* DOS Remove File function */
  32.   if(use_lfn) {
  33.     r.h.al = r.h.ah;
  34.     r.h.ah = 0x71;
  35.     r.x.si = 0;            /* No Wildcards */
  36.   }
  37.   r.x.dx = __tb_offset;
  38.   r.x.ds = __tb_segment;
  39.   __dpmi_int(0x21, &r);
  40.   if(r.x.flags & 1)
  41.   {
  42.     /* We failed.  Leave the things as we've found them.  */
  43.     int e = __doserr_to_errno(r.x.ax);
  44.  
  45.     _chmod(fn, 1, attr & 0xffe7);
  46.     errno = e;
  47.     return -1;
  48.   }
  49.   return 0;
  50. }
  51.