home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / NETCLB35.ZIP / NETCLB35.EXE / EXAMPLES / FLIST.C < prev    next >
C/C++ Source or Header  |  1996-01-03  |  10KB  |  302 lines

  1. /***************************************************************************/
  2. /* File:             FLIST.C - Copyright (C) Adrian M. Cunnelly 1993       */
  3. /*                                                                         */
  4. /* Function:         For all connected workstations list all files that    */
  5. /*                   are open on the current server.                       */
  6. /*                                                                         */
  7. /*                   This has been tested with Netware 2.15 and 3.11       */
  8. /*                                                                         */
  9. /* Usage:            flist                                                 */
  10. /*                                                                         */
  11. /* Functions Called: CheckConsolePrivileges                                */
  12. /*                   GetBinderyObjectID                                    */
  13. /*                   GetConnectionInformation                              */
  14. /*                   GetConnectionNumber                                   */
  15. /*                   GetConnectionsOpenFiles                               */
  16. /*                   GetFileServerInformation                              */
  17. /*                   GetObjectConnectionNumbers                            */
  18. /*                   GetPathFromDirectoryEntry                             */
  19. /*                   GetPreferredConnectionID                              */
  20. /*                   GetDefaultConnectionID                                */
  21. /*                   GetPrimaryConnectionID                                */
  22. /*                   SetPreferredConnectionID                              */
  23. /*                   ISShellLoaded                                         */
  24. /*                                                                         */
  25. /***************************************************************************/
  26.  
  27. #include <sys\types.h>
  28. #include <sys\stat.h>
  29. #include <io.h>
  30. #include <errno.h>
  31. #include <conio.h>
  32. #include <dos.h>
  33.  
  34. #ifndef TURBOC
  35. #include <search.h>
  36. #endif
  37.  
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <fcntl.h>
  43. #include <ctype.h>
  44.  
  45. #include "netware.h"
  46.  
  47. void show_usage( void );
  48. int get_max_connections( void );
  49. void do_connection( int station );
  50. int do_it( int argc, char *argv[]);
  51.  
  52. static int this_con,max_con;
  53. static OPEN_FILES_INFO openfiles[64];
  54.  
  55. int main(int argc,char *argv[])
  56. {
  57. int thisserver,prefserver;
  58. int rcode;
  59.  
  60.    printf("\nFLIST (v2.0) - Copyright (C) Adrian M. Cunnelly 1993\n\n");
  61.  
  62.    if (IsShellLoaded() != SUCCESS)
  63.    {
  64.       printf("*** No netware shell loaded ***\n");
  65.       exit(255);
  66.    }
  67.  
  68.    if ((prefserver = GetPreferredConnectionID()) == 0)
  69.    {
  70.       if ((thisserver = GetDefaultConnectionID()) == 0)
  71.          thisserver = GetPrimaryConnectionID();
  72.       SetPreferredConnectionID( thisserver );
  73.    }
  74.    else
  75.       thisserver = prefserver;
  76.  
  77.    if ((this_con = GetConnectionNumber()) == 0)
  78.    {
  79.      printf("*** No current connection ***\n");
  80.      if (thisserver != prefserver)   /* reset preferred server */
  81.         SetPreferredConnectionID( prefserver );
  82.      exit(255);
  83.    }
  84.    if (CheckConsolePrivileges() != 0)
  85.    {
  86.       printf("FLIST requires user to have Console Operator rights");
  87.       printf("... aborted\n");
  88.       if (thisserver != prefserver)   /* reset preferred server */
  89.          SetPreferredConnectionID( prefserver );
  90.       exit(255);
  91.    }
  92.    if ((max_con = get_max_connections()) == 0)
  93.    {
  94.       if (thisserver != prefserver)   /* reset preferred server */
  95.          SetPreferredConnectionID( prefserver );
  96.       exit(255);
  97.    }
  98.  
  99.    rcode = do_it( argc, argv );
  100.    if (thisserver != prefserver)   /* reset preferred server */
  101.        SetPreferredConnectionID( prefserver );
  102.    return( rcode );
  103. }
  104.  
  105. int do_it( int argc, char *argv[])
  106. {
  107. unsigned int    station;
  108. char *this_param;
  109. int param=1;
  110. int req_connection = -1;
  111. long req_user = 0L;
  112. char req_uname[OBJECT_LENGTH];
  113. word conn_count;
  114. byte connections[100];
  115. int r;
  116.  
  117.    while(--argc)
  118.    {
  119.       this_param = argv[param++];
  120.  
  121.       if(*this_param == '-' || *this_param == '/')
  122.       {
  123.          switch(toupper(*(++this_param)))
  124.          {
  125.              case 'C':
  126.                       if (req_user > 0L)
  127.                       {
  128.                          printf("Connection number cannot be specified if");
  129.                          printf(" username is used\n\n");
  130.                          show_usage();
  131.                          return(255);
  132.                       }
  133.                       if ((req_connection = atoi(this_param+1)) < 1)
  134.                       {
  135.                          printf("Connection number [%d] is invalid\n\n",
  136.                                 req_connection);
  137.                          show_usage();
  138.                          return(255);
  139.                       }
  140.                       break;
  141.              case 'U':
  142.                       if (req_connection > 0)
  143.                       {
  144.                          printf("Username cannot be specified if connection");
  145.                          printf(" number is used\n\n");
  146.                          show_usage();
  147.                          return(255);
  148.                       }
  149.                       if ( GetBinderyObjectID( USER ,
  150.                                                this_param+1 ,
  151.                                                &req_user ) != SUCCESS)
  152.                       {
  153.                          printf("Invalid username specified: %s\n\n",
  154.                                 this_param+1);
  155.                          show_usage();
  156.                          return(255);
  157.                       }
  158.                       else
  159.                          strcpy(req_uname,this_param+1);
  160.                       break;
  161.              case '?':
  162.                       show_usage();
  163.                       return(0);
  164.  
  165.              default:
  166.                       printf("Invalid option specified\n\n");
  167.                       show_usage();
  168.                       return(255);
  169.          }
  170.       }
  171.       else
  172.       {
  173.          printf("Invalid parameter specified\n\n");
  174.          show_usage();
  175.          return(255);
  176.       }
  177.    }
  178.  
  179.    if (req_connection > max_con)
  180.    {
  181.       printf("Connection number [%d] out of range, max allowed is %d\n",
  182.              req_connection,max_con);
  183.       return(255);
  184.    }
  185.  
  186.    if (req_connection > 0)
  187.          do_connection(req_connection);
  188.    else
  189.    if (req_user > 0L)
  190.    {
  191.       if ( ( r = GetObjectConnectionNumbers( USER , req_uname , &conn_count ,
  192.                                              connections ) ) != SUCCESS )
  193.       {
  194.          printf("GetObjectConnectionNumbers returned:  %03d--%#02x\n",
  195.                 r,r);
  196.          return(255);
  197.       }
  198.       for (station=0; station < conn_count; station++)
  199.           do_connection( connections[ station ] );
  200.    }
  201.    else
  202.       for (station=1; station<=max_con; station++)
  203.           do_connection(station);
  204.  
  205.    return(0);
  206. }
  207.  
  208. int get_max_connections( void )
  209. {
  210. FILE_SERVER_INFO sinfo;
  211. int r;
  212.  
  213.     if ((r=GetFileServerInformation( &sinfo )) != SUCCESS)
  214.     {
  215.        printf("GetFileServerInformation returned:  %03d--%#02x\n",r,r);
  216.        return (0);
  217.     }
  218.  
  219.     return( sinfo.connections_supported );
  220. }
  221.  
  222. void do_connection( int station )
  223. {
  224. long object_id;
  225. int  object_type;
  226. char object_name[OBJECT_LENGTH];
  227. byte logintime[7];
  228. int r,r1,i;
  229. char path[256];
  230. int fcount;
  231.  
  232.   if((r1=GetConnectionInformation( (word)station , object_name,
  233.                                    &object_type,&object_id,
  234.                                    logintime)) != SUCCESS)
  235.   {
  236.     printf("GetConnectionInformation returned:  %03d--%#02x\n",r1,r1);
  237.     return;
  238.   }
  239.   else
  240.   if ( (object_id == 0) || (object_name[0] == '\0') )
  241.      return;
  242.   else
  243.   {
  244.      if (station != this_con)
  245.         printf("   %2u-%-16s\n",station , object_name );
  246.      else
  247.         printf(" \376 %2u-%-16s\n",station , object_name );
  248.  
  249.      r=GetConnectionsOpenFiles(station,64,&openfiles[0],
  250.                                &fcount);
  251.      if (r == NOT_ENOUGH_ITEMS)
  252.      {
  253.         printf("Warning: more than 64 files open\n");
  254.         fcount=64;
  255.         r = SUCCESS;
  256.      }
  257.  
  258.      if (r != SUCCESS)
  259.         printf("GetConnectionsOpenFiles returned:  %03d--%#02x\n",
  260.                r,r);
  261.      else
  262.      {
  263.         for (i=0;i<fcount;i++)
  264.         {
  265.           if (openfiles[i].directory_entry != 0)
  266.           {                     /* Must be on Netware 3.11 */
  267.              if ((r1=GetPathFromDirectoryEntry(openfiles[i].name_space,
  268.                      openfiles[i].volume_number,
  269.                      openfiles[i].directory_entry,path)) == SUCCESS)
  270.                 printf("       %s\n",path);
  271.           }
  272.           else
  273.           {                     /* Must be Netware 2.15 */
  274.              if ((r1=GetPathFromDirectoryEntry(openfiles[i].name_space,
  275.                      openfiles[i].volume_number,
  276.                      openfiles[i].parent_dir_entry,path)) == SUCCESS)
  277.                 printf("       %s/%s\n",path,
  278.                        openfiles[i].file_name);
  279.           }
  280.           if (r1 != SUCCESS)
  281.              printf("GetPathFromDirectoryEntry returned:  %03d--%#02x\n",
  282.                     r1,r1);
  283.         }
  284.      }
  285.  
  286.      if (fcount == 0)
  287.         printf("       ** No Open Files **\n");
  288.   }
  289. }
  290.  
  291. void show_usage( void )
  292. {
  293.    printf("Usage: Flist <options>\n\n");
  294.    printf("   Options:\n");
  295.    printf("            -cn  - Show open files for connection number 'n'\n");
  296.    printf("                                [ default: all connections ]\n");
  297.    printf("            -uxx - Show open files for all connections logged in");
  298.    printf(" as user xx\n");
  299.    printf("            -?   - This screen of information\n");
  300.    printf("\n");
  301. }
  302.