home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xafp2.exe / XAFP2.C next >
Text File  |  1994-08-30  |  10KB  |  344 lines

  1. /****************************************************************************
  2. **    File:    xafp2.C
  3. **
  4. **    Desc:    Example use of AFPScanFileInformation.
  5. **
  6. **    Disclaimer:
  7. **
  8. **        Novell, Inc. makes no representations or warranties with respect to
  9. **        any NetWare software, and specifically disclaims any express or
  10. **        implied warranties of merchantability, title, or fitness for a
  11. **        particular purpose.  
  12. **
  13. **        Distribution of any NetWare software is forbidden without the
  14. **        express written consent of Novell, Inc.  Further, Novell reserves
  15. **        the right to discontinue distribution of any NetWare software.
  16. **
  17. **        Novell is not responsible for lost profits or revenue, loss of use
  18. **        of the software, loss of data, costs of re-creating lost data, the
  19. **        cost of any substitute equipment or program, or claims by any party
  20. **        other than you.  Novell strongly recommends a backup be made before
  21. **        any software is installed.   Technical support for this software
  22. **        may be provided at the discretion of Novell.
  23. **
  24. **
  25. **    QMK386 options used:
  26. **
  27. **        /ip - Appletalk
  28. **
  29. **    Programmers:
  30. **
  31. **        Ini    Who                        Firm
  32. **        -----------------------------------------------------------------------
  33. **        ABJ    Adam B. Jerome            Novell Developer Support.
  34. **
  35. **    History:
  36. **
  37. **        When        Who    What
  38. **        -----------------------------------------------------------------------
  39. **        08-24-94    ABJ    First code.
  40. */
  41.  
  42. /****************************************************************************
  43. **    Include headers, macros, function prototypes, etc.
  44. */
  45.     /*------------------------------------------------------------------------
  46.     **    ANSI
  47.     */
  48.     #include <stdlib.h>    /*    exit()    */
  49.     #include <stdio.h>    /* printf()    */
  50.     #include <string.h>    /* strlen(), strchr() strrchr() */
  51.  
  52.     /*------------------------------------------------------------------------
  53.     **    NetWare
  54.     */
  55.     #include <nwafp.h>    /* AFPOpenFileFork()        */
  56.     #include <nwconn.h>    /* GetFileServerID()        */
  57.     #include <nwenvrn.h>    /* GetFileServerName()    */
  58.     #include <nwdir.h>    /* GetVolumeNumber();    */
  59.     #include <errno.h>    /* ESUCCESS                    */
  60.     #include <niterror.h>/*    UNKNOWN_FILE_SERVER    */    
  61.  
  62.     /*------------------------------------------------------------------------
  63.     **    xafp2
  64.     */
  65.     #define    AFP_MODE_READ            0x01
  66.     #define    AFP_MODE_WRITE            0x02
  67.     #define    AFP_MODE_DENYREAD        0x04
  68.     #define    AFP_MODE_DENYWRITE    0x08
  69.  
  70.     #define    AFP_SEARCH_HIDDEN        0x02
  71.     #define    AFP_SEARCH_SYSTEM        0x04
  72.     #define    AFP_SEARCH_DIR            0x08
  73.     #define    AFP_SEARCH_FILES        0x10
  74.  
  75.     #define    AFP_REQUEST_ALL        0x3FFF
  76.  
  77.     #define  AFP_ENTRYID_VOLROOT     0x00000001L
  78.  
  79. /****************************************************************************
  80. ** Convert a DOS style path to a Apple style path.
  81. **
  82. **    Shifts string right one byte, NOT including NULL terminator.
  83. **        (terminator not needed in an apple string.)
  84. **    Inserts length of string in first byte.
  85. **    Converts slashes to '\0's.
  86. **
  87. **    Return values:
  88. **
  89. **            0    Success.
  90. **            1    String too long.
  91. */
  92. int StrDOStoAFP(char *outStr, char *inStr)
  93.     {
  94.     char    *oldStr    = NULL;
  95.     int     cCode;
  96.     int     sLen;
  97.     char    *cp;
  98.  
  99.     /*------------------------------------------------------------------------
  100.     **    Determine length of string and verify length bounds.
  101.     */
  102.     sLen=strlen(inStr);
  103.     if(sLen > 254)
  104.         {
  105.         cCode = 1;
  106.         goto END;
  107.         }
  108.  
  109.     /*------------------------------------------------------------------------
  110.     **    Set string length and move string back to origional buffer (+1).
  111.     */
  112.     *outStr = (char)sLen;
  113.     ++outStr;
  114.     strncpy(outStr, inStr, sLen);
  115.     
  116.     /*------------------------------------------------------------------------
  117.     **    Convert forward slashes to backslashes,
  118.     **    then back slashes to 0x00 from the right side first.
  119.     */
  120.     while((cp=strchr(outStr, '/')) != NULL) *cp = '\\';
  121.     while((cp=strrchr(outStr, '\\')) != NULL) *cp = '\0';
  122.  
  123.     cCode=0;
  124.  
  125. END:
  126.     /*------------------------------------------------------------------------
  127.     **    Return temp buffer to OS.
  128.     */
  129.     if(oldStr != NULL) free(oldStr);
  130.  
  131.     return(cCode);
  132.     }
  133.  
  134. /****************************************************************************
  135. ** Directory listing.
  136. **
  137. **    Input:    fileserverID    ID of fileserver to scan.
  138. **
  139. **                volumeNumber    Volume number to scan.
  140. **
  141. **                pathName            AFP style directory path to scan.
  142. */
  143. int AFP_Dir(WORD fileServerID, int volumeNumber, char *afpPathString)
  144.     {
  145.     int     cCode;
  146.     long  AFPLastSeenID;
  147.     AFPFILEINFO AFPScanInfo;
  148.     LONG    files=0L;
  149.     LONG    bytes=0L;
  150.  
  151.     printf("%-12s %-33s %10s %10s\n",
  152.         "DOSname",
  153.         "MACname",
  154.         "DataFork",
  155.         "ResFork"
  156.         );
  157.     printf("-----------------------------------------------------------------------------\n");
  158.  
  159.     /*------------------------------------------------------------------------
  160.     ** Scan for first file.
  161.     */
  162.     AFPLastSeenID = (-1L);
  163.     cCode=AFPScanFileInformation(
  164.         /* I-    fileServerID    */    fileServerID,
  165.         /*    I-    volumeNum        */    volumeNumber,
  166.         /*    I-    AFPEntryID        */    AFP_ENTRYID_VOLROOT,
  167.         /*    I-    AFPLastSeenID    */    &AFPLastSeenID,
  168.         /*    I-    searchBitMap    */    AFP_SEARCH_FILES|AFP_SEARCH_HIDDEN|AFP_SEARCH_SYSTEM|AFP_SEARCH_DIR,
  169.         /*    I- requestBitMap    */    AFP_REQUEST_ALL,
  170.         /*    I-    AFPPathString    */    afpPathString,
  171.         /*    I-    structSize        */    sizeof(AFPScanInfo),
  172.         /*    -O    AFPScanInfo        */ &AFPScanInfo
  173.         );
  174.     while(cCode == ESUCCESS)
  175.         {
  176.         /*---------------------------------------------------------------------
  177.         **    Update stats.
  178.         */
  179.         ++files;
  180.         bytes += AFPScanInfo.dataForkLength + AFPScanInfo.resourceForkLength;
  181.  
  182.         /*---------------------------------------------------------------------
  183.         **    Print file information.
  184.         */
  185.         printf("%-12.12s %-33.33s %10lu %10lu\n",
  186.             AFPScanInfo.shortName,
  187.             AFPScanInfo.longName,
  188.             AFPScanInfo.dataForkLength,
  189.             AFPScanInfo.resourceForkLength
  190.             );
  191.  
  192.         /*---------------------------------------------------------------------
  193.         **    Scan for next file.
  194.         */
  195.         cCode=AFPScanFileInformation(
  196.             /* I-    fileServerID    */    fileServerID,
  197.             /*    I-    volumeNum        */    volumeNumber,
  198.             /*    I-    AFPEntryID        */    AFP_ENTRYID_VOLROOT,
  199.             /*    I-    AFPLastSeenID    */    &AFPLastSeenID,
  200.             /*    I-    searchBitMap    */    AFP_SEARCH_FILES|AFP_SEARCH_HIDDEN|AFP_SEARCH_SYSTEM|AFP_SEARCH_DIR,
  201.             /*    I- requestBitMap    */    AFP_REQUEST_ALL,
  202.             /*    I-    AFPPathString    */    afpPathString,
  203.             /*    I-    structSize        */    sizeof(AFPScanInfo),
  204.             /*    -O    AFPScanInfo        */ &AFPScanInfo
  205.             );
  206.         }
  207.     switch(cCode)
  208.         {
  209.        case ERR_FAILURE:    /* No more files found.  Normal exit. */
  210.             break;
  211.  
  212.         default:
  213.             printf("ERROR:  Unknown error returned by AFPScanFileInformation(): %d\n", cCode);
  214.             goto END;
  215.         }
  216.  
  217.     printf("%10lu file(s)  %10lu bytes used\n", files, bytes);
  218.  
  219. END:
  220.  
  221.     return(cCode);
  222.     }
  223.  
  224. /****************************************************************************
  225. ** Print program's usage (help screen).
  226. */
  227. void Usage(void)
  228.     {
  229.     printf("\n");
  230.     printf("USAGE:     LOAD xafp2 \"{volumeName}:{path}\"\n");
  231.     printf("\n");
  232.     printf("                volumeName  Name of a volume which has an AFP name space.\n");
  233.     printf("\n");
  234.     printf("                path        Directory path to scan.\n");
  235.     printf("\n");
  236.     printf("NOTE:  Be aware that the MAC name space allows spaces in the path\n");
  237.     printf("       and filename.  Therefore, the entire (arg[1]) must be in quotes\n");
  238.     printf("       so that if it contains spaces, NetWare will not parse it into\n");
  239.     printf("       multiple args.\n");
  240.     printf("\n");
  241.     return;
  242.     }
  243.  
  244. /****************************************************************************
  245. ** Program start.
  246. */
  247. void main(int argC, char *argV[])
  248.     {
  249.     int     cCode;
  250.     WORD    fileServerID        =    0;                    /* Assume local file server.    */
  251.     
  252.     char    *nwVol;
  253.     int    volumeNumber;
  254.     
  255.     char  *nwPath;
  256.     char    afpPath[255+1];
  257.  
  258.     /*------------------------------------------------------------------------
  259.     **    Determine if AFP calls are supported on fileserver.
  260.     */
  261.       cCode=AFPSupported(fileServerID);
  262.     if(cCode == FALSE)
  263.         {
  264.         printf("ERROR:  AFP system calls are not supported on this file server.\n");
  265.         goto END;
  266.         }
  267.  
  268.     /*------------------------------------------------------------------------
  269.     **    Parse and validate command line args.
  270.     */
  271.     if(argC != 2)                                        /* make sure we have an arg. */
  272.         {
  273.         Usage();
  274.         goto END;
  275.         }
  276.     nwVol=argV[1];
  277.     if((nwPath=strchr(nwVol, ':')) == NULL)    /* make sure we have a volume name. */
  278.         {
  279.         Usage();
  280.         goto END;
  281.         }
  282.     *nwPath='\0';
  283.     ++nwPath;
  284.     if(strlen(nwVol) > 15)                            /* make sure volume name is 15 chars or less */
  285.         {
  286.         Usage();
  287.         goto END;
  288.         }
  289.  
  290.     /*------------------------------------------------------------------------
  291.     **    Get the specified volume number.
  292.     */
  293.     cCode=GetVolumeNumber(
  294.         /*    I-    volumeName        */ nwVol,
  295.         /* -O    volumeNumber    */    &volumeNumber
  296.         );
  297.     switch(cCode)
  298.         {
  299.         case ESUCCESS:
  300.             break;
  301.  
  302.         case ERR_VOLUME_DOES_NOT_EXIST:
  303.             printf("ERROR:  Volume %s: does not exist.\n", nwVol);
  304.             goto END;
  305.  
  306.         default:
  307.             printf("ERROR:  Unknown error returned by GetVolumeNumber(): %d\n", cCode);
  308.             goto END;
  309.         }
  310.  
  311.     /*------------------------------------------------------------------------
  312.     **    Convert DOS style path to AFP path.
  313.     */
  314.     cCode=StrDOStoAFP(afpPath, nwPath);
  315.     switch(cCode)
  316.         {
  317.         case 0:
  318.             break;
  319.  
  320.         case 1:
  321.             printf("ERROR:  StrDOStoAFP() reports that path [%s] is too long.\n", nwPath);
  322.             goto END;
  323.  
  324.         default:
  325.             printf("ERROR:  Unknown error returned by STRDOStoAFP(): %d\n", cCode);
  326.             goto END;
  327.         }
  328.  
  329.  
  330.     /*------------------------------------------------------------------------
  331.     ** Print DIR header and do the DIR.
  332.     */
  333.     printf("\n");
  334.     printf("\n Directory of %s:%s\n", nwVol, nwPath);
  335.     printf("\n");
  336.  
  337.     AFP_Dir(fileServerID, volumeNumber, afpPath);
  338.  
  339. END:
  340.     exit(0);
  341.     }
  342.  
  343.  
  344.