home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dslist.exe / DSLIST.C next >
Text File  |  1994-11-29  |  9KB  |  384 lines

  1. /****************************************************************************
  2. **    File:    DSLIST.C
  3. **
  4. **    Desc:    This program lists all the objects that exist at 
  5. **           the current context in the tree and then demonstrates how to
  6. **            read the attribute values of "Common Name" and "Surname" of each
  7. **            object. Be aware that the "Surname" attribute is only applicable
  8. **            for "User" objects and will not be displayed for other types of
  9. **            objects.        
  10. **
  11. **        DISCLAIMER  
  12. **  
  13. **    Novell, Inc. makes no representations or warranties with respect to
  14. **    any NetWare software, and specifically disclaims any express or
  15. **    implied warranties of merchantability, title, or fitness for a
  16. **    particular purpose.  
  17. **
  18. **    Distribution of any NetWare software is forbidden without the
  19. **    express written consent of Novell, Inc.  Further, Novell reserves
  20. **    the right to discontinue distribution of any NetWare software.
  21. **    
  22. **    Novell is not responsible for lost profits or revenue, loss of use
  23. **    of the software, loss of data, costs of re-creating lost data, the
  24. **    cost of any substitute equipment or program, or claims by any party
  25. **    other than you.  Novell strongly recommends a backup be made before
  26. **    any software is installed.   Technical support for this software
  27. **    may be provided at the discretion of Novell.
  28. **
  29. **    Programmers:
  30. **
  31. **        Ini    Who                        Firm
  32. **        -----------------------------------------------------------------------
  33. **        KLB    Karl Bunnell                Novell Developer Support.
  34. **
  35. **    History:
  36. **
  37. **        When        Who    What
  38. **        -----------------------------------------------------------------------
  39. **        09-07-94    klb    First code.
  40. */
  41.  
  42. /****************************************************************************
  43. **    Include headers, macros, function prototypes, etc.
  44. */
  45.  
  46.     /*------------------------------------------------------------------------
  47.     **    MACROS
  48.     */
  49.     #define NWDOS
  50.  
  51.     /*------------------------------------------------------------------------
  52.     **    ANSI
  53.     */
  54.     #include <stdio.h>
  55.     #include <conio.h>
  56.  
  57.  
  58.     /*------------------------------------------------------------------------
  59.     **    NetWare
  60.     */
  61.     #include <nwnet.h>
  62.     #include <nwcalls.h>
  63.     #include <nwlocale.h>
  64.  
  65.  
  66.     /*------------------------------------------------------------------------
  67.     **    Prototypes
  68.     */
  69.     int ReadAttributes(NWDSContextHandle dContext, char *objectName);
  70.     int DisplayAttributes(NWDSContextHandle dContext, NWDS_BUFFER *buf);
  71.  
  72. /****************************************************************************
  73. **    The entire program is contained in Function main().
  74. **    
  75. */
  76.  
  77. void main(int argc, char *argv[])
  78. {
  79.     NWDSContextHandle    dContext;
  80.     NWDS_ITERATION        iterHandle = -1L;
  81.     NWOBJECT_INFO        objectInfo;
  82.     NWDS_BUFFER            *outBuf, *inBuf;
  83.     NWDSCCODE            cCode;
  84.     NWCOUNT                objectCount, attrCount;
  85.       LCONV                lconvInfo;
  86.     char                    objectName[MAX_DN_CHARS];
  87.     char                    nContext[MAX_DN_CHARS];
  88.       char far             *countryPtr;
  89.     int                    i;
  90.     int                    rval;
  91.     char                    c;
  92.  
  93.     cCode = NWCallsInit(NULL, NULL);
  94.     if (cCode)
  95.         {
  96.         printf("\nCall to NWCallsInit returned: %04X", cCode);
  97.         exit(1);
  98.         }
  99.  
  100.       countryPtr = NWLsetlocale(LC_ALL, "");
  101.  
  102.       NWLlocaleconv(&lconvInfo);
  103.  
  104.       cCode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  105.  
  106.       if(cCode)
  107.         {
  108.           printf("NWInitUnicodeTables() returned: %04X\n", cCode);
  109.         goto _FreeUnicodeTables;
  110.         }
  111.  
  112.     dContext = NWDSCreateContext();
  113.  
  114.     if (dContext == ERR_CONTEXT_CREATION)
  115.         {
  116.           printf("NWDSCreateContext returned: %04X\n", cCode);
  117.         goto _FreeContext; 
  118.         }
  119.  
  120.     if (argc == 2)
  121.         {
  122.         cCode = NWDSSetContext(
  123.                     /* Context handle */ dContext,
  124.                     /* Key             */ DCK_NAME_CONTEXT,
  125.                     /* Context value  */ argv[1]
  126.                     );
  127.  
  128.         if (cCode < 0)
  129.             {
  130.               printf("NWDSSetContext returned: %04X\n", cCode);
  131.             goto _FreeContext;
  132.             }
  133.  
  134.         }
  135.  
  136.     cCode = NWDSGetContext(
  137.                 /* Context to query */ dContext,
  138.                 /* Context variable */ DCK_NAME_CONTEXT,
  139.                 /* Context returned */ (void *) nContext
  140.                 );
  141.  
  142.     if (cCode)
  143.             {
  144.               printf("NWDSGetContext returned: %04X\n", cCode);
  145.             goto _FreeContext;
  146.             }
  147.  
  148.     cCode = NWDSAllocBuf(
  149.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  150.                 /* Buff. Point.*/ &outBuf
  151.                 );
  152.  
  153.     if (cCode < 0)
  154.             {
  155.               printf("NWDSAllocBuf returned: %04X\n", cCode);
  156.             goto _FreeContext;
  157.             }
  158.  
  159.     do
  160.         {
  161.         cCode = NWDSList(
  162.                     /* Req. Context */ dContext,
  163.                     /* Object Name  */ "",
  164.                     /* Iter. handle */ &iterHandle,
  165.                     /* Subordinates */ outBuf
  166.                     );
  167.         
  168.         if (cCode < 0)
  169.             {
  170.             printf("\nNWDSList returned : %04X\n", cCode);
  171.             break;
  172.             }
  173.  
  174.         cCode = NWDSGetObjectCount(
  175.                     /* Req. Context   */ dContext,
  176.                     /* P. to read buf.*/ outBuf,
  177.                     /* Num. of OBJs   */ &objectCount
  178.                     );
  179.  
  180.         if (cCode < 0)
  181.             {
  182.             printf("\nNWDSGetObjectCount returned : %04X\n", cCode);
  183.             break;
  184.             }
  185.         
  186.         printf("\nObjects found under %s : \n", nContext);
  187.         for (i = 0; i < objectCount; i++)
  188.             {
  189.             cCode = NWDSGetObjectName(
  190.                         /* Req. Context     */ dContext,
  191.                         /* P. to result buf */ outBuf,
  192.                         /* object name      */ objectName,
  193.                         /* Attribute count  */ &attrCount,
  194.                         /* object Info      */ &objectInfo
  195.                         );
  196.  
  197.             if (cCode < 0)
  198.                 {
  199.                 printf("\nNWDSGetObjectName returned: %04X\n", cCode);
  200.                 goto _FreeContext;
  201.                 }
  202.  
  203.             printf("%d. %s\n", (i + 1), objectName);
  204.  
  205.             rval = ReadAttributes(dContext, objectName);
  206.  
  207.             c = getch();
  208.  
  209.             }
  210.  
  211.  
  212.         } while(iterHandle != -1);
  213.  
  214.  
  215. _FreeContext:
  216.     NWDSFreeContext(dContext);
  217. _FreeUnicodeTables:
  218.     NWFreeUnicodeTables();
  219. }
  220.  
  221.  
  222. int ReadAttributes(NWDSContextHandle dContext, char *objectName)
  223. {
  224.     NWDSCCODE            cCode;
  225.     NWDS_BUFFER            *outBuf, *inBuf;
  226.     NWDS_ITERATION        iterHandle = -1L;
  227.     char                    *attrName[] = { "Common Name",
  228.                                                  "Surname"      };
  229.     int                    i;
  230.     int                    rval = 0;
  231.  
  232.     cCode = NWDSAllocBuf(
  233.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  234.                 /* Buff. Point.*/ &inBuf
  235.                 );
  236.  
  237.     if (cCode < 0)
  238.             {
  239.               printf("NWDSAllocBuf returned: %04X\n", cCode);
  240.             return(1);
  241.             }
  242.  
  243.    cCode = NWDSInitBuf(
  244.                 /* Context  */ dContext,
  245.                 /* Operation*/ DSV_READ,
  246.                 /* buffer   */ inBuf
  247.                 );
  248.  
  249.  
  250.     if (cCode < 0)
  251.             {
  252.               printf("NWDSInitBuf returned: %04X\n", cCode);
  253.             NWDSFreeBuf(inBuf);
  254.             return(1);
  255.             }
  256.  
  257.     cCode = NWDSAllocBuf(
  258.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  259.                 /* Buff. Point.*/ &outBuf
  260.                 );
  261.  
  262.     if (cCode < 0)
  263.             {
  264.               printf("NWDSAllocBuf returned: %04X\n", cCode);
  265.             NWDSFreeBuf(inBuf);
  266.             return(1);
  267.             }
  268.  
  269.     for(i = 0; i < 2; i++)
  270.         {
  271.         cCode = NWDSPutAttrName(
  272.                     /* context   */ dContext,
  273.                     /* in buffer */ inBuf,
  274.                     /* attr. name*/ (char NWFAR*)attrName[i]
  275.                     );
  276.  
  277.         if (cCode < 0)
  278.                 {
  279.                   printf("NWDSPutAttrName returned: %04X\n", cCode);
  280.                 NWDSFreeBuf(inBuf);
  281.                 NWDSFreeBuf(outBuf);
  282.                 return(1);
  283.                 }
  284.  
  285.         }
  286.  
  287.     cCode = NWDSRead(
  288.                 /* Context     */ dContext,
  289.                 /* Object name    */ objectName,
  290.                 /* Info. Type  */ DS_ATTRIBUTE_VALUES,
  291.                 /* All Attrib ?*/ FALSE,
  292.                 /* Attri. names*/ inBuf,
  293.                 /* Iter. Handle*/ &iterHandle,
  294.                 /* Object info */ outBuf
  295.                 );
  296.  
  297.     if (cCode < 0)
  298.         {
  299.         printf("\nNWDSRead returned: %04X\n", cCode);
  300.         NWDSFreeBuf(inBuf);
  301.         NWDSFreeBuf(outBuf);
  302.         return(1);
  303.         }
  304.  
  305.     rval = DisplayAttributes(dContext, outBuf);
  306.  
  307.     NWDSFreeBuf(inBuf);
  308.     NWDSFreeBuf(outBuf);
  309.  
  310.     if (rval)
  311.         return(1);
  312.  
  313.     return(0);
  314.  
  315. }
  316.  
  317.  
  318. int DisplayAttributes(NWDSContextHandle dContext, NWDS_BUFFER *buf)
  319. {
  320.     NWSYNTAX_ID        syntax;
  321.     NWDSCCODE        cCode;
  322.     NWCOUNT            attrCount;
  323.     NWCOUNT            valCount;
  324.     char                attrName[MAX_DN_CHARS + 1];
  325.     char                attrVal[MAX_DN_CHARS + 1];
  326.     int                i, j;
  327.  
  328.  
  329.     cCode = NWDSGetAttrCount(
  330.                 /* Context    */ dContext,
  331.                 /* attr. buff */ buf,
  332.                 /* num of attr*/ &attrCount
  333.                 );
  334.  
  335.     if (cCode < 0)
  336.         {
  337.         printf("\nNWDSGetAttrCount returned: %04X\n", cCode);
  338.         return(1);
  339.         }
  340.     
  341.     for (i = 0; i < attrCount; i++)
  342.         {
  343.         cCode = NWDSGetAttrName(
  344.                     /* Context        */ dContext,
  345.                     /* attrib. buf    */ buf,
  346.                     /* attrib name    */ attrName,
  347.                     /* attr. val. cnt    */ &valCount,
  348.                     /* Syntax ID      */ &syntax
  349.                     );
  350.  
  351.         if (cCode < 0)
  352.             {
  353.             printf("\nNWDSGetAttrName returned: %04X\n", cCode);
  354.             return(1);
  355.             }
  356.  
  357.         printf("\nAttribute Name : %s", attrName);
  358.  
  359.         printf("\nAttribute Value(s):\n");
  360.  
  361.         for (j = 0; j < valCount; j++)
  362.             {
  363.             cCode = NWDSGetAttrVal(
  364.                         /* Context  */ dContext,
  365.                         /* read buf */ buf,
  366.                         /* syntax id*/ syntax,
  367.                         /* attr. val*/ attrVal
  368.                         );
  369.  
  370.             if (cCode < 0)
  371.                 {
  372.                 printf("\nNWDSGetAttrVal returned: %04X\n", cCode);
  373.                 return(1);
  374.                 }
  375.  
  376.             printf("%s\n ", attrVal);
  377.  
  378.             free(attrVal);
  379.  
  380.             }
  381.  
  382.         }
  383. }
  384.