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

  1. /***************************************************************************/
  2. /* File:             WHATHAS.C                                             */
  3. /*                                                                         */
  4. /* Function:         List all files that are owned by the specified user.  */
  5. /*                                                                         */
  6. /* Usage:            whathas <username>                                    */
  7. /*                                                                         */
  8. /* Functions Called: GetBinderyObjectID                                    */
  9. /*                   GetVolumeName                                         */
  10. /*                   ScanDirectoryInformation                              */
  11. /*                   ScanFileInformation                                   */
  12. /*                   GetPreferredConnectionID                              */
  13. /*                   GetDefaultConnectionID                                */
  14. /*                   GetPrimaryConnectionID                                */
  15. /*                   SetPreferredConnectionID                              */
  16. /*                   ISShellLoaded                                         */
  17. /*                                                                         */
  18. /***************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #ifndef TURBOC
  25. #include <malloc.h>
  26. #endif
  27.  
  28. #include "netware.h"
  29.  
  30. void usage ( void );
  31. void display_dir( char *this_dir );
  32. void do_files( byte dirhandle,char *this_dir,char *scandir );
  33. void do_directory( char *this_dir , int dirname_length );
  34.  
  35. char drive_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_,";
  36. char requested_user[256];
  37. long requserid;
  38. byte fileattr,extfileattr;
  39. long filesize;
  40. long fileowner;
  41. int sequence=-1;
  42. char fname[15];
  43. byte maxrightsmask;
  44. char dname[17];
  45. char ownername[49];
  46. word objecttype;
  47. int dir_len;
  48. long dirownerid;
  49.  
  50. nw_date creationdate,lastaccessdate,lastupdatedate,
  51.         lastarchivedate;
  52. nw_time lastupdatetime,lastarchivetime,creationtime;
  53.  
  54. unsigned long totalfilesize,totalfiles;
  55.  
  56. char default_output[] = "whathas.txt";
  57. char output_name[256];
  58.  
  59. FILE *ofile;
  60.  
  61. void display_dir( char *this_dir )
  62. {
  63. char dir_to_show[81];
  64. char *d = this_dir;
  65.  
  66.      while (strlen(d) > 79)
  67.         d=(strchr(d,'\\')+1);
  68.      sprintf(dir_to_show,"%-79.79s\r",d);
  69.      printf(dir_to_show);
  70. }
  71.  
  72. void do_files( byte dirhandle,char *this_dir,char *scandir )
  73. {
  74. int dirshown = 0;
  75. char allfiles[2] = "*";
  76. char *dirtoscan;
  77.  
  78. unsigned long loctotalfilesize=0,loctotalfiles=0;
  79.     
  80.      if (dirhandle == 0)
  81.         dirtoscan = scandir;
  82.      else
  83.         dirtoscan = allfiles;
  84.         
  85.      sequence = -1;
  86.      
  87.      while( ScanFileInformation( dirhandle,dirtoscan,6,&sequence,fname,
  88.                                  &fileattr,&extfileattr,
  89.                                  &filesize,&creationdate,
  90.                                  &lastaccessdate,
  91.                                  &lastupdatedate,&lastupdatetime,
  92.                                  &lastarchivedate,&lastarchivetime,
  93.                                  &fileowner ) == 0)
  94.      {
  95.         if (fileowner == requserid)
  96.         {
  97.             if (!dirshown)
  98.             {
  99.                 fprintf(ofile,"\ndirectory: %s\n\n",this_dir);
  100.                 dirshown = 1;
  101.             }
  102.             totalfiles++;
  103.             totalfilesize += filesize;
  104.             loctotalfiles++;
  105.             loctotalfilesize += filesize;
  106.             fprintf(ofile,"      %-13.13s    %10ld\n",fname,filesize);
  107.         }
  108.      }
  109.      if (loctotalfiles)
  110.      {
  111.          fprintf(ofile,"                       ==========\n");
  112.          fprintf(ofile,"             Total:    %10ld  (%ld files)\n",
  113.                        loctotalfilesize,loctotalfiles);
  114.          fprintf(ofile,"                       ==========\n");
  115.      }
  116. }
  117.  
  118. void do_directory( char *this_dir , int dirname_length )
  119. {
  120. int rcode=0;
  121. int subdirno=1;
  122. char *scandir;
  123. char *nextdir;
  124. byte dirhandle,effrights;
  125.  
  126. /* This procedure is called recursively in order to process every directory */
  127. /* and subdirectory found. Therefore we use malloc as much as possible in   */
  128. /* order to allocate required storage, this will stop us blowing the stack. */
  129.  
  130.    scandir = (char *)malloc(dirname_length+3); /* Get enough space to store */
  131.                                                /* current directory name +  */
  132.                                                /* '\*' + terminating NULL   */
  133.  
  134.    strcpy(scandir,this_dir);                   /* Construct scan dir path   */
  135.  
  136.    if (AllocTemporaryDirectoryHandle(0,scandir,'?',&dirhandle,
  137.                                      &effrights) != 0)
  138.    {
  139.       printf("Failed to allocate handle\n");
  140.       dirhandle = 0;
  141.    }
  142.  
  143.    strcpy(scandir+dirname_length,"\\*");
  144.  
  145.    display_dir( this_dir );                    /* Display directory name    */
  146.                                                /* on console                */
  147.    
  148.    do_files( dirhandle,this_dir,scandir );     /* Display files in this     */
  149.                                                /* directory                 */
  150.    DeallocateDirectoryHandle(dirhandle);
  151.  
  152.    while ( rcode == 0 )      /* Now get all sub_directories */
  153.    {
  154.       rcode = ScanDirectoryInformation( 0,scandir,&subdirno,
  155.                                         dname,&creationdate,
  156.                                         &creationtime,
  157.                                         &dirownerid,
  158.                                         &maxrightsmask);
  159.       if (rcode == 0)
  160.       {
  161.          dir_len = strlen(dname);
  162.          
  163.          /* Get enough space to store full path name of found sub_directory */
  164.          
  165.          nextdir = (char *)malloc(dirname_length+dir_len+2);
  166.  
  167.          strcpy(nextdir,this_dir);            /* Construct full path name */
  168.          *(nextdir+dirname_length) = '\\';
  169.          strcpy(nextdir+dirname_length+1,dname);
  170.  
  171.          do_directory(nextdir,dirname_length + 1 + dir_len); /* do it !! */
  172.          
  173.          free(nextdir);
  174.       }
  175.    }
  176.    free(scandir);
  177. }
  178.  
  179. void usage ( void )
  180. {
  181.    printf("\nWhathas - (c) Adrian M. Cunnelly 1993\n\n");
  182.    printf("        Scan all directories on all volumes");
  183.    printf(" attached to the current server\n");
  184.    printf("        and produce a listing of all files owned by the");
  185.    printf(" specified Netware User.\n\n");
  186.    printf("        Usage: Whathas <switches> Netware_User\n\n");
  187.    printf("               Switches:\n");
  188.    printf("                        -ofile_name - Produce output to 'file_name'\n");
  189.    printf("                                      (default WHATHAS.TXT)\n");
  190.    printf("\n");
  191. }
  192.  
  193. void main(int argc,char *argv[])
  194. {
  195. char volume[17];
  196. int this_one;
  197. char *this_param;
  198. int param=1;
  199. char *o_name = default_output;   /* Set output name to default */
  200. char underline[80];
  201. int prefserver;
  202. int thisserver;
  203.  
  204.    totalfilesize = totalfiles = 0;
  205.  
  206.    requested_user[0]='\0';
  207.    output_name[0] = '\0';
  208.  
  209.    if (IsShellLoaded() != SUCCESS)
  210.    {
  211.       printf("*** No netware shell loaded ***\n");
  212.       exit(255);
  213.    }
  214.    else
  215.    if ((prefserver = GetPreferredConnectionID()) == 0)
  216.    {
  217.       if ((thisserver = GetDefaultConnectionID()) == 0)
  218.          thisserver = GetPrimaryConnectionID();
  219.       SetPreferredConnectionID( thisserver );
  220.    }
  221.    else
  222.       thisserver = prefserver;
  223.  
  224.    if (argc < 2)       /* Not enough parameters */
  225.    {
  226.       usage();
  227.       if (thisserver != prefserver)   /* reset preferred server */
  228.          SetPreferredConnectionID( prefserver );
  229.       exit(0);
  230.    }
  231.  
  232.    while(--argc)
  233.    {
  234.       this_param = argv[param++];
  235.  
  236.       if(*this_param == '-' || *this_param == '/')
  237.       {
  238.          switch(toupper(*(++this_param)))
  239.          {
  240.              case '?':
  241.                       usage();
  242.                       if (thisserver != prefserver)
  243.                          SetPreferredConnectionID( prefserver );
  244.                       exit(0);
  245.                       break;
  246.              case 'O':
  247.                       if (output_name[0])
  248.                       {
  249.                          printf("Output name already specified\n");
  250.                          if (thisserver != prefserver)
  251.                             SetPreferredConnectionID( prefserver );
  252.                          exit(255);
  253.                       }
  254.                       else
  255.                       {
  256.                          strcpy(output_name,this_param+1);
  257.                          o_name = output_name;
  258.                       }
  259.                       break;
  260.              default:
  261.                       printf("Invalid switch specified\n\n");
  262.                       usage();
  263.                       if (thisserver != prefserver)
  264.                          SetPreferredConnectionID( prefserver );
  265.                       exit(255);
  266.          }
  267.       }
  268.       else
  269.       if (requested_user[0])
  270.       {
  271.          printf("Username already specified\n");
  272.          if (thisserver != prefserver)
  273.             SetPreferredConnectionID( prefserver );
  274.          exit(255);
  275.       }
  276.       else
  277.       {
  278.          strcpy(requested_user,this_param);
  279.          strupr(requested_user);
  280.          if (GetBinderyObjectID(USER,requested_user,&requserid) != 0)
  281.          {
  282.             printf("\nUser: %s - not found\n",requested_user);
  283.             if (thisserver != prefserver)
  284.                SetPreferredConnectionID( prefserver );
  285.             exit(255);
  286.          }
  287.       }
  288.    }
  289.  
  290.    if (!requested_user[0])
  291.    {
  292.       printf("A username must be specified\n\n");
  293.       usage();
  294.       if (thisserver != prefserver)
  295.           SetPreferredConnectionID( prefserver );
  296.       exit(255);
  297.    }
  298.  
  299.    strupr(o_name);    /* Convert output filename to uppercase */
  300.    
  301.    if ((ofile = fopen(o_name,"w")) == NULL)
  302.    {
  303.       printf("Failed to open output file: %s\n",o_name);
  304.       if (thisserver != prefserver)
  305.           SetPreferredConnectionID( prefserver );
  306.       exit(255);
  307.    }
  308.  
  309.    /* Write headings to output file */
  310.    
  311.    fprintf(ofile,"\fFiles owned by Netware user %s\n",requested_user);
  312.    fputs(          "============================",ofile );
  313.    memset((void *)&underline,'\0',80);
  314.    memset((void *)&underline,'=',strlen(requested_user));
  315.    fputs(underline,ofile);
  316.    fputc('\n',ofile);
  317.  
  318.    /* Loop round all volumes */
  319.  
  320.    for(this_one=0;this_one<31;this_one++)
  321.        if ((GetVolumeName(this_one,volume) == 0) && (volume[0]))
  322.        {
  323.           strcat(volume,":");
  324.           do_directory(volume,strlen(volume));
  325.        }
  326.        
  327.    display_dir(" ");          /* Clear display line on console */
  328.    
  329.    fprintf(ofile,"\n%s has:\n",requested_user);
  330.    fprintf(ofile,"    %lu bytes in %lu files\n",totalfilesize,totalfiles);
  331.  
  332.    fclose(ofile);
  333.  
  334.    printf("File: %s - produced OK for User: %s\n",o_name,requested_user);
  335.  
  336.    if (thisserver != prefserver)
  337.       SetPreferredConnectionID( prefserver );
  338.  
  339.    exit(0);
  340. }
  341.