RCS_ID_C "$Id: ls.c,v 1.13 1993/10/21 04:02:00 ppessi Exp $";
/*
 * ls.c
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * Copyright © 1991, 1993 Pekka Pessi. All rights reserved
 *
 * Last modified: Thu Oct 21 05:59:44 1993 ppessi
 *
 */

#include "ls_rev.h"
static const char version[] = VERSTAG;

#include <stdlib.h>

#include "ls.h"

char copyright[] =
  "Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  "Helsinki University of Technology, Finland.\n";

struct options options = { FALSE };

BPTR Stdin, Stdout, Stderr;
struct Library *UtilityBase = NULL;
APTR WinPtr;

int errno;
struct Library *SocketBase = NULL;
STRPTR SOCKETNAME = "bsdsocket.library";
#define SOCKETVERSION 3		/* minimum bsdsocket version to use */

#include <proto/socket.h>

static void resume_amiga_stdio(void)
{
  struct Process* me = (struct Process *)FindTask(NULL);
  me->pr_WindowPtr = WinPtr;
  if (UtilityBase)
    CloseLibrary(UtilityBase);
  UtilityBase = NULL;
  if (SocketBase)
    CloseLibrary(SocketBase);
  SocketBase = NULL;
}

static void startup_amiga_stdio(void)
{
  struct Process* me = (struct Process *)FindTask(NULL);
  Stdin = me -> pr_CIS;
  Stdout = me -> pr_COS;
  Stderr = (me -> pr_CES ? me -> pr_CES : Stdout);
  if (!UtilityBase) {
    UtilityBase = OpenLibrary("utility.library", 0);
    /* Remove requesters */
    WinPtr = me->pr_WindowPtr;
    me->pr_WindowPtr = (void *)-1L;
    atexit(resume_amiga_stdio);
  }
  /* Open SocketBase */
  if (!SocketBase) {
    if ((SocketBase = OpenLibrary(SOCKETNAME, SOCKETVERSION)) != NULL) {
      SetErrnoPtr(&errno, sizeof(errno));
    }
  }
}

int
main(int argc, char **argv)
{
  struct ExAllList *listing = NULL;
  char *path = malloc(MAXPATHLEN);
  char *option;
  int optionerror = 0;
  int optind = 0;

  startup_amiga_stdio();

  while (++optind < argc && (argv[optind])[0] == '-') {
    option = argv[optind] + 1;
    while (*option)
      switch(*option++) {
      case 'l':			/* long */
	options.longformat = TRUE;
	break;
      case 'g':
	options.group = TRUE;
	break;
      case 't': 
	options.sort_time = TRUE;
	break;
      case 'a':
	options.all = TRUE;
	break;
      case 'A':
	options.dotted = TRUE;
	break;
      case 's':
	options.kilos = TRUE;
	break;
      case 'd':
	options.dir = TRUE;
	break;
      case 'r':
	options.reverse = TRUE;
	break;
      case 'i':
	options.inode = TRUE;
	break;
      case 'f':
	options.fast = TRUE;
	break;
      case 'F':
	options.filetype = TRUE;
	break;
      case 'L':
	options.symbolic = TRUE;
	break;
      case 'R':
	options.recursion = TRUE;
	break;
      case 'q':
	options.nongraph = TRUE;
	break;
      case 'C':
	options.multicolumn = TRUE;
	if (options.singlecolumn) optionerror++;
	break;
      case '1':			/* one */
	options.singlecolumn = TRUE;
	if (options.multicolumn) optionerror++;
	break;
      case 'p':
	options.pathname = TRUE;
	break;
      case '?':
      default:
	optionerror++;
      }
    if (optionerror != 0) {
      FPrintf(Stderr, "Usage: %s [-lgtasdrifFLRC1q] [patterns...]\n", argv[0]);
      exit(RETURN_ERROR);
    }
  }

  if (!path) 
    exit(RETURN_ERROR);
  path[0] = '\0';
  
  if (IsInteractive(Stdout))
    options.nongraph = TRUE;
  if (optind >= argc) {
    listing = listpatterns(1, &path, options);
  } else {
    listing = listpatterns(argc - optind, argv + optind, options);
  }	

  if (listing)
    doprint(path, listing, options);

  exit(RETURN_OK);
  /*NOTREACHED*/
}
