1/*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
3 *
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6
7/*
8 * search.c - find a target along $(SEARCH) or $(LOCATE)
9 *
10 * 11/04/02 (seiwald) - const-ing for string literals
11 */
12
13# include "jam.h"
14# include "lists.h"
15# include "search.h"
16# include "timestamp.h"
17# include "pathsys.h"
18# include "variable.h"
19# include "newstr.h"
20
21const char *
22search(
23	const char *target,
24	time_t	*time )
25{
26	PATHNAME f[1];
27	LIST	*varlist;
28	char	buf[ MAXJPATH ];
29
30	/* Parse the filename */
31
32	path_parse( target, f );
33
34	f->f_grist.ptr = 0;
35	f->f_grist.len = 0;
36
37	if( varlist = var_get( "LOCATE" ) )
38	{
39	    f->f_root.ptr = varlist->string;
40	    f->f_root.len = strlen( varlist->string );
41
42	    path_build( f, buf, 1 );
43
44	    if( DEBUG_SEARCH )
45		printf( "locate %s: %s\n", target, buf );
46
47	    timestamp( buf, time );
48
49	    return newstr( buf );
50	}
51	else if( varlist = var_get( "SEARCH" ) )
52	{
53	    while( varlist )
54	    {
55		f->f_root.ptr = varlist->string;
56		f->f_root.len = strlen( varlist->string );
57
58		path_build( f, buf, 1 );
59
60		if( DEBUG_SEARCH )
61		    printf( "search %s: %s\n", target, buf );
62
63		timestamp( buf, time );
64
65		if( *time )
66		    return newstr( buf );
67
68		varlist = list_next( varlist );
69	    }
70	}
71
72	/* Look for the obvious */
73	/* This is a questionable move.  Should we look in the */
74	/* obvious place if SEARCH is set? */
75
76	f->f_root.ptr = 0;
77	f->f_root.len = 0;
78
79	path_build( f, buf, 1 );
80
81	if( DEBUG_SEARCH )
82	    printf( "search %s: %s\n", target, buf );
83
84	timestamp( buf, time );
85
86	return newstr( buf );
87}
88