home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.ZIP / VOLLABEL.C < prev   
C/C++ Source or Header  |  1993-02-25  |  2KB  |  110 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "tools.h"
  4. #include <direct.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9. #ifndef FA_LABEL
  10. #define FA_LABEL  0x08
  11. #endif
  12.  
  13. static int near vol_kill( int drive, char *vol )
  14.  
  15. { union REGS regs;
  16.   struct SREGS sregs;
  17.   struct xfcb buf;
  18.   char cdir[_MAX_PATH], dir[] = "x:\\\0           ";
  19.   void far *ptr;
  20.  
  21.   if ( getcurdir(drive + 1, cdir) ) return -1;
  22.   *dir = drive + 'A';
  23.   if ( chdir(dir) ) { chdir( cdir ); return -1; }
  24.  
  25.   strncpy( &dir[2], vol, strlen(vol) );
  26.  
  27.   /* Parse the filename into an FCB */
  28.   regs.h.ah = 0x29;
  29.   regs.h.al = 0;
  30.   sregs.ds  = FP_SEG( dir );
  31.   regs.x.si = FP_OFF( dir );
  32.   ptr = &buf.xfcb_fcb;
  33.   sregs.es  = FP_SEG( ptr );
  34.   regs.x.di = FP_OFF( ptr );
  35.   intdosx( ®s, ®s, &sregs );
  36.  
  37.   if ( ! regs.h.al )
  38.      {
  39.        /* Volume labels require extended FCB's */
  40.        buf.xfcb_flag = 0xff;
  41.        buf.xfcb_attr = FA_LABEL;
  42.  
  43.        /* Delete the old label */
  44.        regs.h.ah = 0x13;
  45.        ptr = &buf;
  46.        sregs.ds  = FP_SEG( ptr );
  47.        regs.x.dx = FP_OFF( ptr );
  48.        intdosx( ®s, ®s, &sregs );
  49.      }
  50.  
  51.   chdir( cdir );
  52.  
  53.   return regs.h.al;
  54. }
  55.  
  56.  
  57.  
  58. /***
  59.  *
  60.  *  Function   :    vollabel
  61.  *
  62.  *  Topics     :    Get/Set disk volume label
  63.  *
  64.  *  Parameters :    in       int drive         0 = A:, 1 = B:
  65.  *                  in/out   char *vol         volume label
  66.  *
  67.  *  Decisions  :    - If *vol = '\0', it is filled with disk volume label.
  68.  *                  - Otherwise, vol is copied onto disk volume label.
  69.  *
  70.  *  Return code:    0 if OK
  71.  *                 -1 if error
  72.  ***/
  73.  
  74. int vollabel( int drive, char *vol )
  75.  
  76. { int result, fd;
  77.   struct find_t finfo;
  78.   char buf[20] = "A:\\*.*";
  79.  
  80.   *buf = drive + 'A';
  81.  
  82.   result = _dos_findfirst( buf, FA_LABEL, &finfo );
  83.  
  84.   if ( ! *vol )       /* only get the volume label */
  85.      {
  86.        if ( ! result ) strcpy( vol, finfo.name );
  87.        return 0;
  88.      }
  89.  
  90.   /* Delete volume label */
  91.   if ( ! result )
  92.      if ( vol_kill(drive, finfo.name) ) return -1;
  93.  
  94.   /* Construct absolute filename */
  95.   strncpy( &buf[3], vol, 11 );
  96.  
  97.   /* Insert a '.' between 8th and 9th character */
  98.   buf[14] = buf[13];
  99.   buf[13] = buf[12];
  100.   buf[12] = buf[11];
  101.   buf[11] = '.';
  102.  
  103.   /* Create new volume label */
  104.   fd = _creat( buf, FA_LABEL );
  105.   if ( fd < 0 ) return -1;
  106.   close( fd );
  107.  
  108.   return 0;
  109. }
  110.