Exemple #1
0
//
// MIB_odom_lmget
//    Retrieve print queue table information from Lan Manager.
//    If not cached, sort it and then
//    cache it.
//
// Notes:
//
// Return Codes:
//    SNMPAPI_NOERROR
//    SNMPAPI_ERROR
//
// Error Codes:
//    None.
//
SNMPAPI MIB_odoms_lmget(
           )

{

DWORD totalentries;
LPBYTE bufptr;
unsigned lmCode;
WKSTA_USER_INFO_1 *DataTable;
DOM_OTHER_ENTRY *MIB_DomOtherDomainTableElement ;
char *p;
char *next;
time_t curr_time ;
unsigned i;
SNMPAPI nResult = SNMPAPI_NOERROR;



   time(&curr_time);    // get the time


   //
   //
   // If cached, return piece of info.
   //
   //


   if((NULL != cache_table[C_ODOM_TABLE].bufptr) &&
      (curr_time <
        (cache_table[C_ODOM_TABLE].acquisition_time
                 + cache_expire[C_ODOM_TABLE]              ) ) )
        { // it has NOT expired!

        goto Exit; // the global table is valid

        }

   //
   //
   // Do network call to gather information and put it in a nice array
   //
   //


     //
     // remember to free the existing data
     //

     MIB_DomOtherDomainTableElement = MIB_DomOtherDomainTable.Table ;

     // iterate over the whole table
     for(i=0; i<MIB_DomOtherDomainTable.Len ;i++)
     {
        // free any alloc'ed elements of the structure
        SnmpUtilOidFree(&(MIB_DomOtherDomainTableElement->Oid));
        SafeFree(MIB_DomOtherDomainTableElement->domOtherName.stream);

        MIB_DomOtherDomainTableElement ++ ;  // increment table entry
     }
     SafeFree(MIB_DomOtherDomainTable.Table) ;  // free the base Table
     MIB_DomOtherDomainTable.Table = NULL ;     // just for safety
     MIB_DomOtherDomainTable.Len = 0 ;          // just for safety

        lmCode =
        NetWkstaUserGetInfo(
                        0,                      // required
                        1,                      // level 0,
                        &bufptr                 // data structure to return
                        );


    DataTable = (WKSTA_USER_INFO_1 *) bufptr ;

    if((NERR_Success == lmCode) || (ERROR_MORE_DATA == lmCode))
        {  // valid so process it, otherwise error
        if(NULL==DataTable->wkui1_oth_domains) {
                totalentries = 0;

                // alloc the table space
                MIB_DomOtherDomainTable.Table = SnmpUtilMemAlloc(totalentries *
                                                sizeof(DOM_OTHER_ENTRY) );
        } else {  // compute it
        totalentries = chrcount((char *)DataTable->wkui1_oth_domains);
        if(0 == MIB_DomOtherDomainTable.Len) {  // 1st time, alloc the whole table
                // alloc the table space
                MIB_DomOtherDomainTable.Table = SnmpUtilMemAlloc(totalentries *
                                                sizeof(DOM_OTHER_ENTRY) );
        }

        MIB_DomOtherDomainTableElement = MIB_DomOtherDomainTable.Table  ;

        // make a pointer to the beginning of the string field

        #ifdef UNICODE
        SnmpUtilUnicodeToAnsi(
                &p,
                DataTable->wkui1_oth_domains,
                TRUE);
        #else
        p =  DataTable->wkui1_oth_domains  ;
        #endif

        // scan through the field, making an entry for each space
        // separated domain
        while(  (NULL != p ) &
                ('\0' != *p)  ) {  // once for each entry in the buffer

                // increment the entry number

                MIB_DomOtherDomainTable.Len ++;

                // find the end of this one
                next = strchr(p,' ');

                // if more to come, ready next pointer and mark end of this one
                if(NULL != next) {
                        *next='\0' ;    // replace space with EOS
                        next++ ;        // point to beginning of next domain
                }


                MIB_DomOtherDomainTableElement->domOtherName.stream = SnmpUtilMemAlloc (
                                strlen( p ) ) ;
                MIB_DomOtherDomainTableElement->domOtherName.length =
                                strlen( p ) ;
                MIB_DomOtherDomainTableElement->domOtherName.dynamic = TRUE;
                memcpy( MIB_DomOtherDomainTableElement->domOtherName.stream,
                        p,
                        strlen( p ) ) ;


                MIB_DomOtherDomainTableElement ++ ;  // and table entry

           DataTable ++ ;  // advance pointer to next sess entry in buffer

        } // while still more to do

        } // if there really were entries
        } // if data is valid to process

    else
       {
       // Signal error
       nResult = SNMPAPI_ERROR;
       goto Exit;
       }


   // free all of the lan man data
   SafeBufferFree( bufptr ) ;


    // iterate over the table populating the Oid field
    build_odom_entry_oids();

   // Sort the table information using MSC QuickSort routine
   qsort( (void *)&MIB_DomOtherDomainTable.Table[0], (size_t)MIB_DomOtherDomainTable.Len,
          (size_t)sizeof(DOM_OTHER_ENTRY), odom_entry_cmp );

   //
   //
   // Cache table
   //
   //


   if(0 != MIB_DomOtherDomainTable.Len) {

        cache_table[C_ODOM_TABLE].acquisition_time = curr_time ;

        cache_table[C_ODOM_TABLE].bufptr = bufptr ;
   }

   //
   //
   // Return piece of information requested
   //
   //

Exit:
   return nResult;
} // MIB_odom_get
int
main(int argc, char **argv)
{
    int n, m;
    COUNTS **vector = typeCalloc((size_t) argc, COUNTS *);
    int *widths = typeCalloc((size_t) (argc + 1), int);
    char **labels;
    int lwidth;

    while ((n = getopt(argc, argv, "mp")) != EOF) {
	switch (n) {
	case 'm':
	    m_opt = 1;
	    break;
	case 'p':
	    p_opt = 1;
	    break;
	default:
	    usage();
	}
    }
    if (optind >= argc)
	usage();

    /* count everything */
    labels = argv + optind;
    for (n = optind, m = 0; n < argc; n++) {
	widths[m] = (int) strlen(argv[n]) + 1;
	if (widths[m] < 8)
	    widths[m] = 8;
	vector[m++] = chrcount(argv[n]);
    }
    /* FIXME: should expand the first column to allow for names that appear
     * in other columns, but not in the first
     */

    /* compute the label-width */
    for (n = 0, lwidth = 8; vector[0][n].name != 0; n++) {
	if (lwidth < (int) strlen(vector[0][n].name) + 1)
	    lwidth = (int) strlen(vector[0][n].name) + 1;
    }

    /* write the header */
    printf("%-*.*s", lwidth, lwidth, "filename");
    for (n = 0; labels[n] != 0; n++)
	printf("%*s", widths[n], labels[n]);
    printf("\n");

    /* write the rows */
    for (n = 0; vector[0][n].name != 0; n++) {
	printf("%-*.*s", lwidth, lwidth, vector[0][n].name);
	for (m = 0; labels[m] != 0; m++) {
	    long value = find_count(vector[0][n].name, vector[m]);
	    if (value >= 0) {
		if (p_opt && vector[0][n].count) {
		    printf("%*.1f%%", widths[m] - 1,
			   (100.0 * (double) value) / (double) vector[0][n].count);
		} else {
		    printf("%*ld", widths[m], value);
		}
	    } else
		printf("%*s", widths[m], "n/a");
	}
	printf("\n");
    }
    free(vector);
    free(widths);
    return EXIT_SUCCESS;
}