home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / setfattr.c < prev    next >
C/C++ Source or Header  |  1993-10-14  |  6KB  |  171 lines

  1. /*
  2.  * File......: SETFATTR.C
  3.  * Author....: Dave Pearson
  4.  * BBS.......: The Dark Knight Returns
  5.  * Net/Node..: 050/069
  6.  * User Name.: Dave Pearson
  7.  * Date......: $Date$
  8.  * Revision..: $Revision$
  9.  * Log file..: $Logfile$
  10.  *
  11.  * This is an original work by Dave Pearson and is placed in the public
  12.  * domain.
  13.  *
  14.  * Modification history:
  15.  * ---------------------
  16.  *
  17.  * $Log$
  18.  *
  19.  */
  20.  
  21. // NOTE: This code has been written for and compiled with Borland C++
  22. //       Version 3.1
  23. //
  24.  
  25. #include "gt_mem.h"
  26. #include <extend.h>
  27.  
  28. // Prototype internal functions.
  29.  
  30. int     _GetFileAttrs(char *);
  31. Boolean _SetFileAttrs(char *, int);
  32.  
  33. /*  $DOC$
  34.  *  $FUNCNAME$
  35.  *      GT_FATTRIB()
  36.  *  $CATEGORY$
  37.  *      File I/O
  38.  *  $ONELINER$
  39.  *      Set the attributes of a file.
  40.  *  $SYNTAX$
  41.  *      GT_FAttrib(<cFileName>,<cAttributes>) --> lChangedOk
  42.  *  $ARGUMENTS$
  43.  *      <cFileName> is the name of the file who's attributes are to be
  44.  *      changed.
  45.  *
  46.  *      <cAttributes> are the attributes to be set.
  47.  *  $RETURNS$
  48.  *      A logical value, .T. if the file attributes were changed ok, .F.
  49.  *      if not.
  50.  *  $DESCRIPTION$
  51.  *      GT_FAttrib() can be used to set the attributes of a file.
  52.  *
  53.  *      The available attributes that can be changed are:
  54.  *
  55.  *                     ─────────────────────────
  56.  *                     Character       Attribute
  57.  *                     ─────────────────────────
  58.  *                     A               Archive
  59.  *                     H               Hidden
  60.  *                     S               System
  61.  *                     R               Read Only
  62.  *                     ─────────────────────────
  63.  *
  64.  *      To set an attribute on use the upper case letter, to set the
  65.  *      attribute off use the lower case letter. Any attribute that
  66.  *      is not included in the attribute string is left as it is.
  67.  *  $EXAMPLES$
  68.  *      // Make a file read only.
  69.  *
  70.  *      lOk := GT_FAttrib("Life.42","R")
  71.  *
  72.  *      // Turn on the archive attribute and the hidden attrubite, and
  73.  *      // turn off the read only attribute.
  74.  *
  75.  *      lOk := GT_FAttrib("Life.42","AHr")
  76.  *  $END$
  77.  */
  78.  
  79. CLIPPER GT_FAttrib()
  80. {
  81.         Boolean AllOk       = FALSE;
  82.         int     Attrs       = 0;
  83.         char    *FileName   = NULL;
  84.         char    *Attributes = NULL;
  85.         int     CharCount;
  86.         int     MaxAttr;
  87.  
  88.         if (PCOUNT >= 2 && ISCHAR(1) && ISCHAR(2))
  89.         {
  90.                 FileName   = _parc(1);
  91.                 Attributes = _parc(2);
  92.                 MaxAttr    = _parclen(2);
  93.                 Attrs      = _GetFileAttrs(FileName);
  94.                 if (Attrs != -1)
  95.                 {
  96.                         for (CharCount = 0; CharCount < MaxAttr; CharCount++)
  97.                         {
  98.                                 switch (Attributes[CharCount])
  99.                                 {
  100.                                         case 'a' : Attrs &= (~0x20) ; break;
  101.                                         case 'A' : Attrs |= 0x20    ; break;
  102.                                         case 'h' : Attrs &= (~0x2)  ; break;
  103.                                         case 'H' : Attrs |= 0x2     ; break;
  104.                                         case 'r' : Attrs &= (~0x1)  ; break;
  105.                                         case 'R' : Attrs |= 0x1     ; break;
  106.                                         case 's' : Attrs &= (~0x4)  ; break;
  107.                                         case 'S' : Attrs |= 0x4     ; break;
  108.                                 }
  109.                         }
  110.                         AllOk = _SetFileAttrs(FileName,Attrs);
  111.                 }
  112.         }
  113.         _retl(AllOk);
  114. }
  115.  
  116. /*****************************************************************************
  117. * Function: _GetFileAttrs()                                                  *
  118. * Syntax..: int _GetFileAttrs(char *FileName)                                *
  119. * Usage...: Internal function to get the attributes of a file.               *
  120. * By......: David A Pearson                                                  *
  121. *****************************************************************************/
  122.  
  123. static int _GetFileAttrs(char *FileName)
  124. {
  125.         int     FileNameSeg = _GT_FP_SEG(FileName);
  126.         int     FileNameOff = _GT_FP_OFF(FileName);
  127.         int     Attrs       = 0;
  128.  
  129.         asm     Push    DS
  130.         asm     Mov     AH,0x43
  131.         asm     Mov     AL,0h
  132.         asm     Mov     DS,FileNameSeg
  133.         asm     Mov     DX,FileNameOff
  134.         asm     Int     0x21
  135.         asm     Jc      BadFile
  136.         asm     Mov     Attrs,CX
  137.         asm     Jmp     AllDone
  138. BadFile:
  139.         Attrs = -1;
  140. AllDone:
  141.         asm     Pop     DS
  142.         return(Attrs);
  143. }
  144.  
  145. /*****************************************************************************
  146. * Function: _SetFileAttrs()                                                  *
  147. * Syntax..: int _SetFileAttrs(char *FileName, int Attrs)                     *
  148. * Usage...: Internal function to set the attributes of a file.               *
  149. * By......: David A Pearson                                                  *
  150. *****************************************************************************/
  151.  
  152. static Boolean _SetFileAttrs(char *FileName, int Attrs)
  153. {
  154.         int     FileNameSeg = _GT_FP_SEG(FileName);
  155.         int     FileNameOff = _GT_FP_OFF(FileName);
  156.         Boolean AllOk       = FALSE;
  157.  
  158.         asm     Push    DS
  159.         asm     Mov     AH,0x43
  160.         asm     Mov     AL,0x01
  161.         asm     Mov     CX,Attrs
  162.         asm     Mov     DS,FileNameSeg
  163.         asm     Mov     DX,FileNameOff
  164.         asm     Int     0x21
  165.         asm     Jc      BadFile
  166.         AllOk = TRUE;
  167. BadFile:
  168.         asm     Pop     DS
  169.         return(AllOk);
  170. }
  171.