/* Written by RTGregory 31-jan-91

   Distributed under license by the Free Software Foundation, Inc.

This file is part of RCS.

RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

Report problems and direct all questions to:

    current DECUS author

*/

/* BY is used to search the work files and determine if a specified user
** has locked any files.  It is quicker to do it with a program than with
** grep because the lock info is always within 10 line of the top of the
** file.  It was written for VMS and uses VMS specific calls to get all the
** file names.
*/

#include <stdio.h>

#ifdef VMS
#include "ccs.h"
#endif

#define LONGEST_USERNAME	(int) 22
#define FILE_NAME_LENGTH	(int) 100
#define LONGEST_LINE		(int) 100
#define RCS_WILDCARD_STRING	"*.*_V"
#define LOCK_KEYWORD_STRING	"locks"
#define LOCK_KEYWORD_LENGTH	(int) 5
#define LOCKER_START_POSITION	(int) 9
#define SCREEN_SIZE		(int) 22

static char *rcs_by_c_id = "$Revision: 1.2 $ $RCSfile: ccs:[rich.work]by.c_v $";

void screen_more( int *i)
{
char in_string[ 100];

	if( *i >= SCREEN_SIZE)
	{
	  *i = 0;
	  printf( "\n--more--");
	  gets( in_string);
	  printf( "\n");
	}
	else
	{
	  (*i)++;
	}
} /* screen more */

main( argc, argv)
  int argc;
  char *argv[];

{
#ifdef VMS

#else
  some kind of fxn that gets all the files that match the wildcard
#endif

FILE *fp;
char 	*fstr,
	str[FILE_NAME_LENGTH],
	path_name[FILE_NAME_LENGTH], 
	who[ LONGEST_USERNAME], *strptr;
int number_of_files, i, lines = 0;

	redirect_stdout( &argc, argv);

	if( argc != 2)
	{
	  printf( "usage: by username\nList files locked by username.");
	  exit( 1);
	}

	upper( who, argv[1]);

	/* get the symbol that points to the RCS directory */
	strcpy( path_name, getenv( "RCS_REFERENCE_DIRECTORY"));
	if( ISNULLSTRING( path_name))
	{
	  printf( "RCS_REFERENCE_DIRECTORY not set.\n");
	  exit( 1);
	}

	/* get all the RCS file names */
	strcat( path_name, RCS_WILDCARD_STRING); 
	c_next_file_init( path_name);
	fstr = c_next_file();

	while( fstr != NULL)
	{
	  fp = fopen( fstr, "r");

	  if( fp == NULL)
	  {
	     printf( ".");
	  }
	  else
	  {
	    /* find the line that holds the lock info */
	    fgets( str, LONGEST_LINE, fp);
	    while( strncmp( LOCK_KEYWORD_STRING, str,
			  LOCK_KEYWORD_LENGTH) != 0)
	    {
	      fgets( str, LONGEST_LINE, fp);
	    }
	  }

	  /* see if that line contains the username specified */
	  if( strstr( str, who) != NULL)
	  {
	    screen_more( &lines);
	    printf( "\n%s %s",  fstr, trim_string(str, str));
	  }
	  else
	  {
	     printf( ".");
	  }
	  fclose( fp);
	  fstr = c_next_file();
	}
} /* main */


