/*
 *  unfind
 *
 *  stdin is a list of file names generated by find(1) with
 *  it's -print option.
 *  unfind will remove any file names on stdin which exactly
 *  match the argument(s) of that beging with the argument
 *  and then have a slash as the next character.
 *  the slash condition will remove subdirectories.
 *
 */

/*
 *  define directives
 */

#define TRUE  1
#define FALSE 0

#define MAX_FILENAME_LENGTH 1023

/*
 *  include directives
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 *  global variables
 */

static char *MCLwhat = "@(#) MCL unfind, GENERIC, version 001, 13-MAR-96";

/*
 *  Main
 */

main(argc, argv)
  int   argc;
  char *argv[];
{
  /* local variables */
  char  filename [ MAX_FILENAME_LENGTH + sizeof(char) ];
  int   len_fn;
  int   len_arg;
  int   arg;
  int   outflag;

  /* while there are lines on stdin to read */
  while ( fgets(filename, MAX_FILENAME_LENGTH, stdin) != NULL )
  {
    /* calculate length of line */
    len_fn = strlen(filename);

    /* if it is zero then skip it */
    if (len_fn == 0)
      continue;

    /* is last character in filename a newline */
    if ( filename[ len_fn - 1 ] == '\n' )
    {
      /* yes, lose it */
      filename[ len_fn - 1 ] = '\0';

      /* update length of filename accordingly */
      len_fn--;
    }

    /* if filename length now zero then skip it */
    if (len_fn == 0)
      continue;

    /* assume file name needs to be output */
    outflag = TRUE;

    /* for each argument on the command line */
    for ( arg=1 ; arg < argc; arg++)
    {
      /* calulate length of argument */
      len_arg = strlen(argv[arg]);

      /* is the filename at least starting with the current argument */
      if ( strncmp(filename, argv[arg], len_arg) == 0 )
      {
        /* yes, is the next character a slash (/) or the null character  */
        if ( (filename[len_arg] == '/') || (filename[len_arg] == '\0') )
        {
          /* yes, ignore it */
          outflag = FALSE;

          /* and ditch from argument loop now */
          arg = argc;
        }
      }
    }

    /* does the file need to be output */
    if (outflag)
    {
      /* yes, so do so */
      printf("%s\n", filename);
    }
  }

  /* return */
  return(0);
}
