home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / SAAREXX.ZIP / XMPFXDLL.C < prev    next >
Text File  |  1991-08-02  |  15KB  |  348 lines

  1. /*******************************************************************/
  2. /*                                                                 */
  3. /* Module Name:        XMPFXDLL                                    */
  4. /*                                                                 */
  5. /* Description:        Example external functio for REXX/2.        */
  6. /*                                                                 */
  7. /* Function:           File directory utility function.            */
  8. /*                                                                 */
  9. /* Entry points:       RexDir                                      */
  10. /*                                                                 */
  11. /* Notes:                                                          */
  12. /*                                                                 */
  13. /*   The entry points obey the calling conventions for REXX        */
  14. /*   external functions.  Please use the following switches        */
  15. /*   when you compile this module:                                 */
  16. /*                                                                 */
  17. /*       -Alfu -G2s                                                */
  18. /*                                                                 */
  19. /*******************************************************************/
  20. /*   Modifications:                                                */
  21. /*                                                                 */
  22. /*    Date    Who        Reason                                    */
  23. /*  --------  ---  ----------------------------------------------  */
  24. /*  08/01/91  WDA  Original issue.                                 */
  25. /*                                                                 */
  26. /*******************************************************************/
  27.  
  28. #define     INCL_DOS
  29. #define     INCL_RXSHV
  30. #include    <os2.h>
  31. #include    <stdlib.h>
  32. #include    <string.h>
  33. #include    <rexxsaa.h>
  34. #include    "xmpfxdll.h"
  35.  
  36.  
  37. /*******************************************************************/
  38. /*                                                                 */
  39. /* Global definitions follow.                                      */
  40. /*                                                                 */
  41. /*******************************************************************/
  42.  
  43. /* none */
  44.  
  45.  
  46. /*******************************************************************/
  47. /*                                                                 */
  48. /* Global variables follow.                                        */
  49. /*                                                                 */
  50. /*******************************************************************/
  51.  
  52. static PCHAR   szVersion    = "1.00";
  53. static CHAR    szCompDate[] = __DATE__;
  54.  
  55.  
  56. /*******************************************************************/
  57. /*                                                                 */
  58. /* Internal function prototypes follow.                            */
  59. /*                                                                 */
  60. /*******************************************************************/
  61.  
  62. PSZ   Rx2psz    (RXSTRING rxsSource);
  63. SHORT SetRexVar (PSZ pszVar, PVOID pValue, ULONG ulLen);
  64.  
  65.  
  66. /*******************************************************************/
  67. /*                                                                 */
  68. /* Function:            RexDir()                                   */
  69. /*                                                                 */
  70. /* Description:         Retrieve file specs.                       */
  71. /*                                                                 */
  72. /* Input:               File search specification                  */
  73. /*                      Stem variable name                         */
  74. /*                      Search option(s) (optional)                */
  75. /*                                                                 */
  76. /* Returns:             Return code: '' = invalid arguments        */
  77. /*                                   0  = success                  */
  78. /*                                   1  = REXX variable pool error */
  79. /*                                   2  = memory error             */
  80. /*                                                                 */
  81. /* External References: DosFindFirst()                             */
  82. /*                      DosFindNext()                              */
  83. /*                      DosFindClose()                             */
  84. /*                                                                 */
  85. /* Internal References: Rx2psz()                                   */
  86. /*                      SetRexVar()                                */
  87. /*                                                                 */
  88. /* Notes:                                                          */
  89. /*                                                                 */
  90. /*   This routine takes 2 or 3 parameters. The form of the call is:*/
  91. /*                                                                 */
  92. /*   retc = RexDir(filespec, stem);                                */
  93. /*                    or                                           */
  94. /*   retc = RexDir(filespec, stem, options);                       */
  95. /*                                                                 */
  96. /*******************************************************************/
  97.  
  98. SHORT APIENTRY RexDir (
  99.          PSZ       function_name,      /* Function invocation name.*/
  100.          SHORT     argc,               /* Number of arguments.     */
  101.          PRXSTRING argv,               /* Function arguments.      */
  102.          PSZ       queue_name,         /* Current queue name.      */
  103.          PRXSTRING retval)  {          /* Value returned by funct. */
  104.  
  105.    PSZ         pszFname, pszStem, pszOptions;
  106.    SHORT       i;
  107.    SHORT       retc;
  108.    HDIR        hdir = HDIR_CREATE;
  109.    USHORT      usSearchCount = 1;
  110.    FILEFINDBUF findbuf;
  111.    USHORT      usOptions = 0;
  112.    CHAR        cOutline [256];
  113.    CHAR        cTail [18], cStem [251], cSymbol [251];
  114.  
  115.    /*****************************************************************/
  116.    /* Initialize the return buffer to 0 and the return value to     */
  117.    /* empty.                                                        */
  118.    /*                                                               */
  119.    /* Please note that the REXX interpreter allocates 250 bytes in  */
  120.    /* the return string.  This will suffice for us.                 */
  121.    /*****************************************************************/
  122.  
  123.    retval -> strlength = 0;
  124.  
  125.    /*****************************************************************/
  126.    /* If the argument count is valid (i.e. 2 or 3) and the argument */
  127.    /* is not too short, perform function.                           */
  128.    /*****************************************************************/
  129.  
  130.    if ((argc >= 2) && (argc <= 3) &&   /* Arg count valid?          */
  131.     (argv[0].strlength > 0) &&         /* Filespec > 0?             */
  132.     (argv[1].strlength > 0)) {         /* Stem variable name > 0?   */
  133.  
  134.       /* Extract the caller's arguments from the RXSTRING */
  135.       /* in argv into a null-terminated C strings.        */
  136.       pszFname = Rx2psz (argv[0]);
  137.       if (pszFname == NULL) {
  138.          *(retval -> strptr) = '2';
  139.          retval -> strlength = 1L;
  140.          return (0);
  141.          }
  142.       pszStem = Rx2psz (argv[1]);
  143.       if (pszFname == NULL) {
  144.          *(retval -> strptr) = '2';
  145.          retval -> strlength = 1L;
  146.          free (pszFname);
  147.          return (0);
  148.          }
  149.       if ((argc == 3) && (argv[2].strlength > 0)) {
  150.          pszOptions = Rx2psz (argv[2]);
  151.          if (pszFname == NULL) {
  152.             free (pszFname);
  153.             free (pszStem);
  154.             *(retval -> strptr) = '2';
  155.             retval -> strlength = 1L;
  156.             return (0);
  157.             }
  158.          strupr (pszOptions);
  159.          if (strchr (pszOptions, 'F') != NULL)
  160.             usOptions |= FILE_NORMAL;
  161.          else if (strchr (pszOptions, 'D') != NULL)
  162.             usOptions |= FILE_DIRECTORY;
  163.          else if (strchr (pszOptions, 'B') != NULL)
  164.             usOptions |= FILE_DIRECTORY | FILE_NORMAL;
  165.          else
  166.             usOptions |= FILE_NORMAL;
  167.          if (strchr (pszOptions, 'S') != NULL)
  168.             usOptions |= FILE_SYSTEM;
  169.          if (strchr (pszOptions, 'H') != NULL)
  170.             usOptions |= FILE_HIDDEN;
  171.          free (pszOptions);
  172.          }
  173.       else
  174.          usOptions |= FILE_NORMAL;
  175.  
  176.       /* Initialize stem */
  177.       strcpy (cStem, pszStem);
  178.       if ((CHAR) *(pszStem + strlen (pszStem) - 1) != '.')
  179.          strcat (cStem, ".");
  180.  
  181.       /* Find first file */
  182.       i = 0;
  183.       retc = DosFindFirst (pszFname, &hdir, usOptions, &findbuf,
  184.        sizeof (findbuf), &usSearchCount, 0L);
  185.  
  186.       /* If retc == 0 then keep getting next file specs */
  187.       while (retc == 0) {
  188.          i += 1;
  189.          /* set up stem.tail REXX variable */
  190.          sprintf (cOutline, "%2d-%02d-%04d  %02d:%02d  %8d  ",
  191.           findbuf.fdateLastWrite.month,
  192.           findbuf.fdateLastWrite.day,
  193.           findbuf.fdateLastWrite.year + 1980,
  194.           findbuf.ftimeLastWrite.hours,
  195.           findbuf.ftimeLastWrite.minutes,
  196.           findbuf.cbFileAlloc);
  197.          if (findbuf.attrFile & FILE_HIDDEN)
  198.             strcat (cOutline, "H");
  199.          else
  200.             strcat (cOutline, " ");
  201.          if (findbuf.attrFile & FILE_SYSTEM)
  202.             strcat (cOutline, "S");
  203.          else
  204.             strcat (cOutline, " ");
  205.          if (findbuf.attrFile & FILE_DIRECTORY)
  206.             strcat (cOutline, "<DIR>  ");
  207.          else
  208.             strcat (cOutline, "       ");
  209.          strcat (cOutline, findbuf.achName);
  210.          /* set up stem.tail name including trailer */
  211.          strcpy (cSymbol, cStem);
  212.          itoa (i, cTail, 10);
  213.          strcat (cSymbol, cTail);
  214.          /* set variable in pool */
  215.          retc = SetRexVar (cSymbol, cOutline,
  216.                            (ULONG) strlen (cOutline));
  217.          /* test return codes */
  218.          if (retc != 0) {
  219.             DosFindClose (hdir);
  220.             free (pszFname);
  221.             free (pszStem);
  222.             *(retval -> strptr) = '1';
  223.             retval -> strlength = 1L;
  224.             return (0);
  225.             }
  226.          retc = DosFindNext (hdir, &findbuf, sizeof (findbuf),
  227.           &usSearchCount);
  228.          }
  229.       DosFindClose (hdir);
  230.  
  231.       /* Set number of lines into stem.0 variable */
  232.       itoa (i, cOutline, 10);
  233.       /* set up stem.0 name including trailer */
  234.       strcpy (cSymbol, cStem);
  235.       strcat (cSymbol, "0");
  236.       /* set variable in pool */
  237.       retc = SetRexVar (cSymbol, cOutline, (ULONG) strlen (cOutline));
  238.       /* test return codes */
  239.       if (retc != 0) {
  240.          free (pszFname);
  241.          free (pszStem);
  242.          *(retval -> strptr) = '1';
  243.          retval -> strlength = 1L;
  244.          return (0);
  245.          }
  246.  
  247.       /* Free memory */
  248.       free (pszFname);
  249.       free (pszStem);
  250.  
  251.       /* set up return value for caller */
  252.       *(retval -> strptr) = '0';
  253.       retval -> strlength = 1L;
  254.       }
  255.  
  256.    return (0);
  257.    }
  258.  
  259.  
  260. /*******************************************************************/
  261. /*                                                                 */
  262. /* Function:            Rx2psz()                                   */
  263. /*                                                                 */
  264. /* Description:         Create PSZ from a RXSTRING. The caller is  */
  265. /*                      responsible for freeing the memory         */
  266. /*                      allocated by this function via free().     */
  267. /*                                                                 */
  268. /* Input:               RXSTRING - string to be converted          */
  269. /*                                                                 */
  270. /* Returns:             PSZ - converted string                     */
  271. /*                                                                 */
  272. /* References:          memmove(), malloc()                        */
  273. /*                                                                 */
  274. /* Notes:                                                          */
  275. /*                                                                 */
  276. /*    None.                                                        */
  277. /*                                                                 */
  278. /*******************************************************************/
  279.  
  280. PSZ Rx2psz (
  281.          RXSTRING rxsSource) {         /* Source struc             */
  282.  
  283.    PSZ pszTemp;
  284.  
  285.    /* Calculate destination buffer size and allocate buffer */
  286.    pszTemp = malloc ((USHORT) rxsSource.strlength + 1);
  287.    if (pszTemp == NULL)
  288.       return (NULL);
  289.    /* Move source into dest buffer */
  290.    memmove (pszTemp, rxsSource.strptr, (USHORT) rxsSource.strlength);
  291.    /* Zero-terminate dest buffer */
  292.    pszTemp[rxsSource.strlength] = '\0';
  293.  
  294.    return (pszTemp);
  295.    }
  296.  
  297.  
  298. /*******************************************************************/
  299. /*                                                                 */
  300. /* Function:            SetRexVar()                                */
  301. /*                                                                 */
  302. /* Description:         Sets the contents of a variable in the     */
  303. /*                      Rexx Variable Pool.                        */
  304. /*                                                                 */
  305. /* Input:               PSZ - Rexx variable name                   */
  306. /*                      PVOID - Value                              */
  307. /*                      ULONG - Value length                       */
  308. /*                                                                 */
  309. /* Returns:             Return code from RxVar()                   */
  310. /*                                                                 */
  311. /* References:          RxVar()                                    */
  312. /*                                                                 */
  313. /* Notes:                                                          */
  314. /*                                                                 */
  315. /*   None.                                                         */
  316. /*                                                                 */
  317. /*******************************************************************/
  318.  
  319. SHORT SetRexVar (
  320.          PSZ       pszVar,             /* Variable name            */
  321.          PVOID     pValue,             /* Ptr to value             */
  322.          ULONG     ulLen) {            /* Value length             */
  323.  
  324.    SHVBLOCK RxVarBlock;
  325.    SHORT    retc;
  326.  
  327.    /* Initialize RxVarBlock */
  328.    RxVarBlock.shvnext = NULL;
  329.    RxVarBlock.shvname.strptr = pszVar;
  330.    RxVarBlock.shvname.strlength = (ULONG) strlen (pszVar);
  331.    RxVarBlock.shvnamelen = RxVarBlock.shvname.strlength;
  332.    RxVarBlock.shvvalue.strptr = pValue;
  333.    RxVarBlock.shvvalue.strlength = ulLen;
  334.    RxVarBlock.shvvaluelen = ulLen;
  335.    RxVarBlock.shvcode = RXSHV_SYSET;
  336.    RxVarBlock.shvret = RXSHV_OK;
  337.  
  338.    /* set variable in pool */
  339.    retc = RxVar (&RxVarBlock);
  340.  
  341.    /* test return codes */
  342.    if (retc == RXSHV_NEWV)
  343.       return (RXSHV_OK);
  344.  
  345.    return (retc);
  346.    }
  347.  
  348.