/* slist.c - list known NetWare file servers, DOS mars-dosutils version */

#include "net.h"

static int usage(void)
{
  fprintf(stderr, "usage:\t%s [pattern]\n", funcname);
  return(-1);
}

static void print_net_node(uint8 *addr)
{
  fprintf(stdout, "%08lX  %02X%02X%02X%02X%02X%02X",
          (unsigned long)GET_BE32(addr),
          addr[4], addr[5], addr[6], addr[7], addr[8], addr[9]);
}

int func_slist(int argc, char *argv[], int mode)
{
  BINDERY_OBJECT obj;
  uint32 last_id = MAX_U32;
  uint8 pattern[50];
  int found = 0;
  int result;

  (void)mode;

  if (argc > 2) return(usage());

  if (argc == 2) strmaxcpy(pattern, argv[1], sizeof(pattern) - 1);
  else strcpy(pattern, "*");
  upstr(pattern);

  fprintf(stdout, "\n%-52s%-10s%-12s\n",
          "Known NetWare File Servers", "Network", "Node Address");
  fprintf(stdout,
          "-----------------------------------------------"
          "---------------------------\n");

  while ((result = ncp_17_37(last_id, NCP_BINDERY_FSERVER,
                             pattern, &obj)) == 0) {
    NW_PROPERTY prop;

    found = 1;
    last_id = obj.object_id;

    fprintf(stdout, "%-52s", obj.object_name);

    if (!ncp_17_3d(NCP_BINDERY_FSERVER, obj.object_name,
                   1, "NET_ADDRESS", &prop)) {
      print_net_node(prop.value);
    }

    fprintf(stdout, "\n");

    if (last_id == MAX_U32) break;
  }

  if (!found)
    fprintf(stdout, "No servers found\n");

  return(0);
}
