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

  1. /****************************************************************************
  2. **    File:    XAFP1.C
  3. **
  4. **    Desc:    Example of AFP file access.
  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.     #include <io.h>        /* close(), read() */
  52.  
  53.     /*------------------------------------------------------------------------
  54.     **    NetWare
  55.     */
  56.     #include <nwafp.h>    /* AFPOpenFileFork()        */
  57.     #include <nwconn.h>    /* GetFileServerID()        */
  58.     #include <nwenvrn.h>    /* GetFileServerName()    */
  59.     #include <nwdir.h>    /* GetVolumeNumber();    */
  60.     #include <errno.h>    /* ESUCCESS                    */
  61.     #include <niterror.h>/*    UNKNOWN_FILE_SERVER    */    
  62.  
  63.     /*------------------------------------------------------------------------
  64.     **    xafp2
  65.     */
  66.     #define    AFP_MODE_READ            0x01
  67.     #define    AFP_MODE_WRITE            0x02
  68.     #define    AFP_MODE_DENYREAD        0x04
  69.     #define    AFP_MODE_DENYWRITE    0x08
  70.  
  71.     #define    AFP_SEARCH_HIDDEN        0x02
  72.     #define    AFP_SEARCH_SYSTEM        0x04
  73.     #define    AFP_SEARCH_DIR            0x08
  74.     #define    AFP_SEARCH_FILES        0x10
  75.  
  76.     #define    AFP_REQUEST_ALL        0x3FFF
  77.  
  78.     #define  AFP_ENTRYID_VOLROOT     0x00000001L
  79.  
  80.     #define    AFP_DATA_FORK            0x00
  81.     #define    AFP_RESOURCE_FORK        0x01
  82.  
  83. /****************************************************************************
  84. **    Global storage
  85. */
  86.  
  87. /****************************************************************************
  88. ** Convert a DOS style path to a Apple style path.
  89. **
  90. **    Shifts string right one byte, NOT including NULL terminator.
  91. **        (terminator not needed in an apple string.)
  92. **    Inserts length of string in first byte.
  93. **    Converts slashes to '\0's.
  94. **
  95. **    Return values:
  96. **
  97. **            0    Success.
  98. **            1    String too long.
  99. */
  100. int StrDOStoAFP(char *outStr, char *inStr)
  101.     {
  102.     char    *oldStr    = NULL;
  103.     int     cCode;
  104.     int     sLen;
  105.     char    *cp;
  106.  
  107.     /*------------------------------------------------------------------------
  108.     **    Determine length of string and verify length bounds.
  109.     */
  110.     sLen=strlen(inStr);
  111.     if(sLen > 254)
  112.         {
  113.         cCode = 1;
  114.         goto END;
  115.         }
  116.  
  117.     /*------------------------------------------------------------------------
  118.     **    Set string length and move string back to origional buffer (+1).
  119.     */
  120.     *outStr = (char)sLen;
  121.     ++outStr;
  122.     strncpy(outStr, inStr, sLen);
  123.     
  124.     /*------------------------------------------------------------------------
  125.     **    Convert forward slashes to backslashes,
  126.     **    then back slashes to 0x00 from the right side first.
  127.     */
  128.     while((cp=strchr(outStr, '/')) != NULL) *cp = '\\';
  129.     while((cp=strrchr(outStr, '\\')) != NULL) *cp = '\0';
  130.  
  131.     cCode=0;
  132.  
  133. END:
  134.     /*------------------------------------------------------------------------
  135.     **    Return temp buffer to OS.
  136.     */
  137.     if(oldStr != NULL) free(oldStr);
  138.  
  139.     return(cCode);
  140.     }
  141.  
  142. /****************************************************************************
  143. ** Copy the data portion of a file to the screen.
  144. */
  145. int AFP_TypeFile(WORD fileServerID, int volumeNumber, char *afpPathString)
  146.     {
  147.     int    cCode;
  148.     int     fileHandle;
  149.     LONG    bytesRead;
  150.     char    buffer[256];
  151.  
  152.     /*------------------------------------------------------------------------
  153.     **    Open the file.
  154.     */
  155.     cCode=AFPOpenFileFork(
  156.         /* I-    fileServerID    */ fileServerID,
  157.         /* I-    volumeNum        */    volumeNumber,
  158.         /*    I-    AFPEntryID        */    AFP_ENTRYID_VOLROOT,    
  159.         /*    I-    forkIndicator    */    AFP_DATA_FORK,    
  160.         /*    I-    accessMode        */    AFP_MODE_READ,
  161.         /*    I-    AFPPathString    */    afpPathString,
  162.         /*    -O    fileID            */    NULL,
  163.         /*    -O    forkLength        */    NULL,
  164.         /*    -O    NetWareHandle    */    NULL,
  165.         /*    -O    fileHandle        */    &fileHandle
  166.         );
  167.     switch(cCode)
  168.         {
  169.         case ESUCCESS:
  170.             break;
  171.  
  172.         case ERR_INVALID_PATH:
  173.         default:
  174.             goto END;
  175.         }
  176.  
  177.     /*------------------------------------------------------------------------
  178.     **    Copy file to screen.
  179.     */
  180.     do    {
  181.         bytesRead=read(
  182.             /*    I-    handle    */    fileHandle,
  183.             /*    -O    buffer    */    buffer,
  184.             /*    I-    len        */    sizeof(buffer)
  185.             );
  186.   
  187.         printf("%*.*s", bytesRead, bytesRead, buffer);
  188.         } while(bytesRead == sizeof(buffer));
  189.     
  190.     if(bytesRead == (-1))
  191.         {
  192.         cCode=errno;
  193.         goto END;
  194.         }
  195.  
  196.     /*------------------------------------------------------------------------
  197.     **    Close the file.
  198.     */
  199.     cCode=close(fileHandle);
  200.     if(cCode) cCode=errno;
  201.  
  202. END:
  203.     return(cCode);
  204.     }
  205.  
  206. /****************************************************************************
  207. ** Print program's usage (help screen).
  208. */
  209. void Usage(void)
  210.     {
  211.     printf("\n");
  212.     printf("USAGE:     LOAD xafp1 \"{volumeName}:{path}\\{filename}\"\n");
  213.     printf("\n");
  214.     printf("                volumeName  Name of a volume where file is located.\n");
  215.     printf("\n");
  216.     printf("                path        Directory path where file is located.\n");
  217.     printf("\n");
  218.     printf("                fileName    Name of file type to display.\n");
  219.     printf("\n");
  220.     printf("NOTE:  Be aware that the MAC name space allows spaces in the path\n");
  221.     printf("       and filename.  Therefore, the entire (arg[1]) must be in quotes\n");
  222.     printf("       so that if it contains spaces, NetWare will not parse it into\n");
  223.     printf("       multiple args.\n");
  224.     printf("\n");
  225.     return;
  226.     }
  227.  
  228. /****************************************************************************
  229. ** Program start.
  230. */
  231. void main(int argC, char *argV[])
  232.     {
  233.     int     cCode;
  234.     WORD    fileServerID        =    0;                    /* Assume local file server.    */
  235.     
  236.     char    *nwVol;
  237.     int    volumeNumber;
  238.     
  239.     char  *nwPath;
  240.     char    afpPath[255+1];
  241.  
  242.     /*------------------------------------------------------------------------
  243.     **    Determine if AFP calls are supported on fileserver.
  244.     */
  245.       cCode=AFPSupported(fileServerID);
  246.     if(cCode == FALSE)
  247.         {
  248.         printf("ERROR:  AFP system calls are not supported on this file server.\n");
  249.         goto END;
  250.         }
  251.  
  252.     /*------------------------------------------------------------------------
  253.     **    Parse and validate command line args.
  254.     */
  255.     if(argC != 2)                                        /* make sure we have an arg. */
  256.         {
  257.         Usage();
  258.         goto END;
  259.         }
  260.     nwVol=argV[1];
  261.     if((nwPath=strchr(nwVol, ':')) == NULL)    /* make sure we have a volume name. */
  262.         {
  263.         Usage();
  264.         goto END;
  265.         }
  266.     *nwPath='\0';
  267.     ++nwPath;
  268.     if(strlen(nwVol) > 15)                            /* make sure volume name is 15 chars or less */
  269.         {
  270.         Usage();
  271.         goto END;
  272.         }
  273.  
  274.     /*------------------------------------------------------------------------
  275.     **    Get the specified volume number.
  276.     */
  277.     cCode=GetVolumeNumber(
  278.         /*    I-    volumeName        */ nwVol,
  279.         /* -O    volumeNumber    */    &volumeNumber
  280.         );
  281.     switch(cCode)
  282.         {
  283.         case ESUCCESS:
  284.             break;
  285.  
  286.         case ERR_VOLUME_DOES_NOT_EXIST:
  287.             printf("ERROR:  Volume %s: does not exist.\n", nwVol);
  288.             goto END;
  289.  
  290.         default:
  291.             printf("ERROR:  Unknown error returned by GetVolumeNumber(): %d\n", cCode);
  292.             goto END;
  293.         }
  294.  
  295.     /*------------------------------------------------------------------------
  296.     **    Convert DOS style path to AFP path.
  297.     */
  298.     cCode=StrDOStoAFP(afpPath, nwPath);
  299.     switch(cCode)
  300.         {
  301.         case 0:
  302.             break;
  303.  
  304.         case 1:
  305.             printf("ERROR:  StrDOStoAFP() reports that path [%s] is too long.\n", nwPath);
  306.             goto END;
  307.  
  308.         default:
  309.             printf("ERROR:  Unknown error returned by STRDOStoAFP(): %d\n", cCode);
  310.             goto END;
  311.         }
  312.  
  313.  
  314.     cCode=AFP_TypeFile(fileServerID, volumeNumber, afpPath);
  315.     switch(cCode)
  316.         {
  317.         case ESUCCESS:
  318.             break;
  319.  
  320.         case ERR_INVALID_PATH:
  321.             printf("ERROR:  AFP_TypeFile() reports: Invalid path.\n");
  322.             goto END;
  323.  
  324.         case EBADF:
  325.             printf("ERROR:  Bad file handle reported by AFP_TypeFile[close()].\n");
  326.             break;
  327.  
  328.         default:
  329.             printf("ERROR:  AFP_TypeFile() returned unknown error: %d.\n", cCode);
  330.             goto END;
  331.         }
  332.  
  333. END:
  334.     exit(0);
  335.     }
  336.  
  337.  
  338.