home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / lanserv / fileen32 / fileen32.c < prev    next >
C/C++ Source or Header  |  1998-10-01  |  13KB  |  356 lines

  1. /***************************************************************************/
  2. /*
  3.  *   PROGRAM NAME: FILEEN32
  4.  *   ------------
  5.  *
  6.  *   What this program does:
  7.  *               It gets information on all files within a given
  8.  *               filepath that have been opened by a specific user.
  9.  *               Both the path and the userid may be left unspecified
  10.  *               to get information on all files opened by all users
  11.  *               on the specified server.
  12.  *
  13.  *               The program must be executed by an administrator
  14.  *               and may execute either locally or remotely.
  15.  *
  16.  *   REQUIRED FILES:
  17.  *   --------------
  18.  *   SAMPLE32.MAK    - IBM C SET/2 makefile
  19.  *   FILEEN32.C      - C source code for the main program.
  20.  *   ERRMSG.C        - C source code for the Error_Message function.
  21.  *
  22.  *   REQUIRED LIBRARIES:
  23.  *   ------------------
  24.  *   NETAPI32.LIB   -  32-bit Netapi library for OS/2
  25.  *                     (in the IBMLAN\NETSRC\OS2\LIB directory).
  26.  *
  27.  *   NetAPI32 functions used in this program:
  28.  *   ---------------------------------------
  29.  *   Net32FileEnum2
  30.  *
  31.  ***************************************************************************/
  32.  
  33. /*------------------ OS/2 Include definitions -----------------------------*/
  34.  
  35. #define  INCL_BASE
  36.  
  37. #include <os2.h>
  38. #include <limits.h>
  39. #include <netcons.h>
  40. #include <shares.h>
  41. #include <neterr.h>
  42.  
  43. /*--------------------- C Include definitions -----------------------------*/
  44.  
  45. #include <stdio.h>
  46.  
  47. #ifdef __WATCOMC__
  48. #include <mem.h>
  49. #else
  50. #include <memory.h>
  51. #endif
  52.  
  53. #include <string.h>
  54.  
  55. /*--------------------- Function Declarations -----------------------------*/
  56.  
  57. VOID Error_Message(USHORT, PSZ);
  58. VOID Syntax(VOID);
  59.  
  60. /***************************************************************************/
  61. /* Main C Function                                                         */
  62. /*-------------------------------------------------------------------------*/
  63.  
  64. VOID
  65. main(USHORT argc, PCHAR argv[])
  66. {
  67.     USHORT                      usRc = 0,         /* API return code */
  68.                                 usEntriesprinted,
  69.                                 usMaxDataSize;
  70.     ULONG                       ulBuflen,         /* Size of FileInfo buffer */
  71.                                 ulEntriesRead,    /* Number of entries read  */
  72.                                 ulEntriesRemaining;/*Total entries available*/
  73.     PCHAR                       pchServername;    /* Server name             */
  74.     PCHAR                       pchBasepath;      /* Root of path for files  */
  75.     PCHAR                       pchUsername;      /* Userid                  */
  76.     struct file_info_3 LSFAR   *EnumBuf;          /* FileInfo buffer         */
  77.     struct file_info_3 LSFAR   *pchBuf;           /* Ptr. to current entry   */
  78.     FRK                         frkResume_key;    /* Resume key: defined in  */
  79.                                                   /* shares.h                */
  80.     /*
  81.      * Check the validity of the input parameters.
  82.      * Up to 4 parameters are allowed.
  83.      */
  84.     if (argc > 4)
  85.         Syntax();
  86.  
  87.     if (argc == 2 &&
  88.         (!strcmp(argv[1], "?") || !stricmp(argv[1], "/HELP")))
  89.             Syntax();
  90.  
  91.     /*
  92.      * Command line parameters are positional; the servername, if any,
  93.      * must precede the basepath (if any), which must precede the username
  94.      * (if any).  Servername must begin with two backslashes; basepath
  95.      * must be an absolute path beginning with a drive letter.
  96.      */
  97.     pchServername = pchBasepath = pchUsername = (PCHAR)NULL;
  98.     if (argc == 2)
  99.     {
  100.         if (*argv[1] == '\\')
  101.             pchServername = argv[1];
  102.         else if (*(argv[1]+1) == ':')
  103.             pchBasepath = argv[1];
  104.         else
  105.             pchUsername = argv[1];
  106.     }
  107.     else if (argc == 3)
  108.     {
  109.         /* argv[1] must be either servername or basepath */
  110.         if (*argv[1] == '\\')
  111.         {
  112.             pchServername = argv[1];
  113.             if (*(argv[2]+1) == ':')
  114.                 pchBasepath = argv[2];
  115.             else
  116.                 pchUsername = argv[2];
  117.         }
  118.         else if (*(argv[1]+1) == ':')
  119.         {
  120.             pchBasepath = argv[1];
  121.             pchUsername = argv[2];
  122.         }
  123.         else
  124.             Syntax();
  125.     }
  126.     else if (argc == 4)
  127.     {
  128.         pchServername = argv[1];
  129.         pchBasepath = argv[2];
  130.         pchUsername = argv[3];
  131.     }
  132.  
  133.     if (pchServername && (*pchServername != '\\' || *(pchServername+1) != '\\'))
  134.     {
  135.         printf("Invalid server name specified: %s.\n", pchServername);
  136.         Syntax();
  137.     }
  138.  
  139.     if (pchBasepath &&
  140.         (*(pchBasepath+1) != ':' || *(pchBasepath+2) != '\\'))
  141.     {
  142.         printf("Invalid basepath specified: %s.\n", pchBasepath);
  143.         Syntax();
  144.     }
  145.  
  146.     if (pchUsername &&
  147.         (*pchUsername == '\\' || *(pchUsername+1) == ':'))
  148.     {
  149.         printf("Invalid username specified: %s.\n", pchUsername);
  150.         Syntax();
  151.     }
  152.  
  153.     /*
  154.      * Initialize the resume key.
  155.      * (FRK_INIT is a macro that is defined in shares.h.)
  156.      */
  157.     FRK_INIT(frkResume_key);
  158.  
  159.     /*
  160.      * Call NetFileEnum2 with a NULL buffer pointer and a
  161.      * zero length buffer.  The function can't return any
  162.      * file_info_3 data since the buffer length is 0, but
  163.      * will return the total number of file_info_3 entries
  164.      * (i.e., the total number of open files).  A buffer
  165.      * will then be allocated based on the amount of
  166.      * information available.
  167.      */
  168.     usRc = Net32FileEnum2(pchServername,
  169.                           pchBasepath,
  170.                           pchUsername,
  171.                           3,                    /* Information level 3 */
  172.                           (unsigned char *)EnumBuf,
  173.                           0L,
  174.                           (PULONG)&ulEntriesRead,
  175.                           (PULONG)&ulEntriesRemaining,
  176.                           (PVOID)&frkResume_key);
  177.  
  178.     /*
  179.      * Exit if the return code is not NERR_Success
  180.      * or ERROR_MORE_DATA.
  181.      */
  182.     if (usRc && usRc != ERROR_MORE_DATA)
  183.     {
  184.         Error_Message(usRc, "Net32FileEnum2");
  185.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  186.     }
  187.     else if (usRc == NERR_Success && ulEntriesRemaining == 0)
  188.     {
  189.         printf("There are no open files on %s.\n",
  190.                 pchServername ? pchServername : "the local server");
  191.         DosExit(EXIT_PROCESS, 0L);
  192.     }
  193.  
  194.     /*
  195.      * Calculate the maximum number of bytes of data that
  196.      * could be returned per file_info_3 structure.
  197.      */
  198.     usMaxDataSize = sizeof(struct file_info_3) +
  199.                     UNLEN + 1 +
  200.                     PATHLEN + 1;
  201.  
  202.     /*
  203.      * Calculate the size of the buffer needed to hold
  204.      * <ulTotalEntries> file_info_3 structures.  Note
  205.      * that Net32FileEnum2 cannot return more than 64KB;
  206.      * if ulTotalEntries indicates that there is more than
  207.      * 64KB of data, a 64KB buffer is allocated and
  208.      * Net32FileEnum2 is called multiple times.
  209.      */
  210.     if ((USHORT)(usMaxDataSize * ulEntriesRemaining) > USHRT_MAX)
  211.         ulBuflen = 64 * 1024;
  212.     else
  213.         ulBuflen = usMaxDataSize * ulEntriesRemaining;
  214.  
  215.     usRc = DosAllocMem((PPVOID)&EnumBuf,
  216.                         ulBuflen,
  217.                         PAG_READ | PAG_WRITE | PAG_COMMIT);
  218.  
  219.     if (usRc)
  220.     {
  221.         printf("FILEEN32: Unable to allocate memory.\n");
  222.         Error_Message(usRc, "DosAllocMem");
  223.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  224.     }
  225.  
  226.     if (ulBuflen == 64 * 1024)
  227.         ulBuflen = USHRT_MAX;
  228.  
  229.     /* Re-initialize the resume key. */
  230.     FRK_INIT( frkResume_key);
  231.  
  232.     /*
  233.      * If the buffer supplied to the Net32FileEnum2 function is too
  234.      * small to hold all the available data, ERROR_MORE_DATA will
  235.      * be returned.  In this case, the function can be called
  236.      * repeatedly until all data is obtained (NERR_Success is
  237.      * returned).  The buffer might be too small to hold all
  238.      * the available data if (1) more files have been opened since
  239.      * the first call to Net32FileEnum2, or (2) the maximum buffer
  240.      * size (64K) has been allocated and there is more than
  241.      * 64K of data available.
  242.      */
  243.     do
  244.     {
  245.         /* Set the buffer to zeroes. */
  246.         memset(EnumBuf, '\x00', ulBuflen);
  247.  
  248.         usRc = Net32FileEnum2(pchServername,
  249.                               pchBasepath,
  250.                               pchUsername,
  251.                               3,
  252.                               (unsigned char *)EnumBuf,
  253.                               ulBuflen,
  254.                               &ulEntriesRead,
  255.                               &ulEntriesRemaining,
  256.                               &frkResume_key);
  257.  
  258.         switch (usRc)
  259.         {
  260.             case NERR_Success:
  261.             case ERROR_MORE_DATA:
  262.             {
  263.                 printf("Servername       : %s\n",
  264.                         pchServername ? pchServername : "NULL");
  265.                 printf("Basepath         : %s\n",
  266.                         pchBasepath ? pchBasepath : "NULL");
  267.                 printf("Username         : %s\n",
  268.                         pchUsername ? pchUsername : "NULL");
  269.                 printf("Level            : 3\n");
  270.                 printf("Return code from Net32FileEnum2 : %d\n", usRc);
  271.                 printf("Entries read     : %d\n", ulEntriesRead);
  272.                 printf("Entries remaining: %d\n", ulEntriesRemaining);
  273.  
  274.                 /* If there is information on open files, print it. */
  275.                 if (ulEntriesRead != 0)
  276.                 {
  277.                     usEntriesprinted = 1;
  278.                     pchBuf = EnumBuf;
  279.  
  280.                     do
  281.                     {
  282.                         printf("\n/----------------\\\n");
  283.                         printf("| File ID Number : %ld\n", pchBuf->fi3_id);
  284.  
  285.                         printf("| App.Permissions: %02X(hex) =>",
  286.                                 pchBuf->fi3_permissions);
  287.  
  288.                         if ((pchBuf->fi3_permissions & PERM_FILE_READ) != 0)
  289.                             printf(" Read");
  290.  
  291.                         if ((pchBuf->fi3_permissions & PERM_FILE_WRITE) != 0)
  292.                            printf(" Write");
  293.  
  294.                         if ((pchBuf->fi3_permissions & PERM_FILE_CREATE) != 0)
  295.                             printf(" Create");
  296.  
  297.                         printf("\n");
  298.  
  299.                         printf("| # of File locks: %d\n",
  300.                                 pchBuf->fi3_num_locks);
  301.  
  302.                         printf("| Pathname       : %s\n",
  303.                                 pchBuf->fi3_pathname);
  304.  
  305.                         printf("| Username       : %s\n",
  306.                                 pchBuf->fi3_username);
  307.  
  308.                         printf("\\----------------/\n");
  309.  
  310.                         pchBuf++;
  311.                     } while (++usEntriesprinted <= (USHORT)ulEntriesRead);
  312.                 }
  313.                 break;
  314.             }
  315.             default:
  316.             {
  317.                 /*
  318.                  * If any error besides ERROR_MORE_DATA was returned,
  319.                  * print the error code and drop out of the do loop.
  320.                  */
  321.                 Error_Message(usRc,"Net32FileEnum2");
  322.                 break;
  323.             }
  324.         }
  325.  
  326.     } while (usRc == ERROR_MORE_DATA);
  327.  
  328.     /* Free the buffer and return. */
  329.     (void)DosFreeMem((PVOID)EnumBuf);
  330.     DosExit(EXIT_PROCESS, (USHORT)usRc);
  331.  
  332. }
  333.  
  334. VOID
  335. Syntax()
  336. {
  337.     printf("\nUSAGE: FILEEN32 enumerates open files on a server.\n\n");
  338.     printf("Syntax:\n");
  339.     printf("  FILEEN32 \\\\servername basepath username\n");
  340.     printf("  where:\n");
  341.     printf("  - \\\\servername indicates the machine where open files will\n");
  342.     printf("    be enumerated.  If a servername is specified, it must be the\n");
  343.     printf("    first parameter specified.  If no servername is specified,\n");
  344.     printf("    open files at the local server will be enumerated.\n");
  345.     printf("  - basepath is in the form of <drive>:\\pathname.  Only open files\n");
  346.     printf("    matching this basepath will be enumerated.  If no basepath is\n");
  347.     printf("    specified, all open files are enumerated.\n");
  348.     printf("  - username is the name of an existing user.  If username is\n");
  349.     printf("    specified, only files opened by this user are enumerated.\n");
  350.     printf("    Username must be specified as the last parameter.\n\n");
  351.     printf("  If no parameters are specified, all open files at the local\n");
  352.     printf("  server are enumerated.\n");
  353.  
  354.     DosExit(EXIT_PROCESS, 0);
  355. }
  356.