home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l440 / 2.ddi / CHAP4 / MOV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-14  |  2.9 KB  |  101 lines

  1. /*
  2. MOV.C
  3. demonstrate wildcard pathed renames via DOS indirect 
  4. function call
  5. by Ralf Brown, with thanks to Dan Lanciani for pointing out that 
  6. indirect function call enables wildcards on rename and delete
  7.  
  8. This file contains a number of minor modifications from the version
  9. printed in UNDOCUMENTED DOS
  10.  
  11. Usage: MOV old-filespec new-filespec
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <dos.h>
  17.  
  18. typedef struct
  19. {
  20.    unsigned ax,bx,cx,dx,si,di,ds,es,reserved,computerID,processID ;
  21. } DPL ;     /* DOS parameter list */
  22.  
  23. union REGS regs ;
  24. struct SREGS segregs ;
  25.  
  26. void canonicalize(filespec,canonical,errorlevel)
  27. char *filespec, *canonical ;
  28. int errorlevel ;
  29. {
  30.    void far *lvalue;
  31.    regs.h.ah = 0x60 ;
  32.    lvalue = filespec;
  33.    regs.x.si = FP_OFF(lvalue) ;
  34.    segregs.ds = FP_SEG(lvalue) ;
  35.    lvalue = canonical;
  36.    regs.x.di = FP_OFF(lvalue) ;
  37.    segregs.es = FP_SEG(lvalue) ;
  38.    intdosx(®s,®s,&segregs) ;
  39.    if (regs.x.cflag)
  40.    {
  41.       puts("invalid filespec") ;
  42.       exit(errorlevel) ;
  43.    }
  44. }
  45.  
  46. int errorlevel = 0 ;
  47. void ERROR(char *s, int x)     { puts(s); errorlevel = (x); }
  48.  
  49. int main(argc,argv)
  50. int argc ;
  51. char **argv ;
  52. {
  53.    DPL dpl ;
  54.    void far *ptr ;
  55.    char source[128], target[128] ;
  56.  
  57.    if (argc != 3)
  58.    {
  59.       puts("usage: MOV old-filespec new-filespec") ;
  60.       puts("where old-filespec and new-filespec may contain") ;
  61.       puts("wildcards. Wildcards in the new-filespec indicate") ;
  62.       puts("that new name should contain same characters as") ;
  63.       puts("old name in those positions.") ;
  64.       errorlevel = 1 ;
  65.    }
  66.    else if (_osmajor < 3 || (_osmajor == 3 && _osminor < 10))
  67.       ERROR("MOV requires DOS 3.10 or higher", 5);
  68.    else
  69.    {
  70.       canonicalize(argv[1],source,3) ;
  71.       canonicalize(argv[2],target,4) ;
  72.       if (source[0] != target[0])    /* are they on the same drive? */
  73.         ERROR("Source and target must be on the same drive", 6);
  74.       else /* do the move/rename */
  75.       {
  76.         void far *lvalue;
  77.         dpl.ax = 0x5600 ;   /* indirect function is rename */
  78.         lvalue = &source;
  79.         dpl.dx = FP_OFF(lvalue) ;
  80.         dpl.ds = FP_SEG(lvalue) ; /* DS:DX old filespec */
  81.         lvalue = ⌖
  82.         dpl.di = FP_OFF(lvalue) ;
  83.         dpl.es = FP_SEG(lvalue) ; /* ES:DI new filespec */
  84.         dpl.bx = dpl.cx = dpl.si = 0 ;
  85.         dpl.computerID = 0 ;    /* local machine */
  86.         dpl.processID = 0 ;     /* current process */
  87.  
  88.         regs.x.ax = 0x5D00 ;  /* invoke server function call */
  89.         ptr = (void far *)&dpl ;
  90.         regs.x.dx = FP_OFF(ptr) ;
  91.         segregs.ds = FP_SEG(ptr) ;
  92.         intdosx(®s,®s,&segregs) ;
  93.         /* rename returns error 12h (no more files) on success */
  94.         if (regs.x.cflag && regs.x.ax != 0x12)
  95.             ERROR("rename failed", 2);
  96.         /* NOTE: fails in OS/2 DOS box */
  97.       }
  98.    }
  99.    return errorlevel ;
  100. }
  101.