Snapshot of upstream SQLite 3.37.2
[sqlcipher.git] / autoconf / tea / win / nmakehlp.c
blob2dc33cc657b43dd57937fe1f5e58f740c8dfcbe7
1 /*
2 * ----------------------------------------------------------------------------
3 * nmakehlp.c --
5 * This is used to fix limitations within nmake and the environment.
7 * Copyright (c) 2002 by David Gravereaux.
8 * Copyright (c) 2006 by Pat Thoyts
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * ----------------------------------------------------------------------------
15 #define _CRT_SECURE_NO_DEPRECATE
16 #include <windows.h>
17 #ifdef _MSC_VER
18 #pragma comment (lib, "user32.lib")
19 #pragma comment (lib, "kernel32.lib")
20 #endif
21 #include <stdio.h>
22 #include <math.h>
25 * This library is required for x64 builds with _some_ versions of MSVC
27 #if defined(_M_IA64) || defined(_M_AMD64)
28 #if _MSC_VER >= 1400 && _MSC_VER < 1500
29 #pragma comment(lib, "bufferoverflowU")
30 #endif
31 #endif
33 /* ISO hack for dumb VC++ */
34 #ifdef _MSC_VER
35 #define snprintf _snprintf
36 #endif
39 /* protos */
41 static int CheckForCompilerFeature(const char *option);
42 static int CheckForLinkerFeature(char **options, int count);
43 static int IsIn(const char *string, const char *substring);
44 static int SubstituteFile(const char *substs, const char *filename);
45 static int QualifyPath(const char *path);
46 static int LocateDependency(const char *keyfile);
47 static const char *GetVersionFromFile(const char *filename, const char *match, int numdots);
48 static DWORD WINAPI ReadFromPipe(LPVOID args);
50 /* globals */
52 #define CHUNK 25
53 #define STATICBUFFERSIZE 1000
54 typedef struct {
55 HANDLE pipe;
56 char buffer[STATICBUFFERSIZE];
57 } pipeinfo;
59 pipeinfo Out = {INVALID_HANDLE_VALUE, ""};
60 pipeinfo Err = {INVALID_HANDLE_VALUE, ""};
63 * exitcodes: 0 == no, 1 == yes, 2 == error
66 int
67 main(
68 int argc,
69 char *argv[])
71 char msg[300];
72 DWORD dwWritten;
73 int chars;
74 const char *s;
77 * Make sure children (cl.exe and link.exe) are kept quiet.
80 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
83 * Make sure the compiler and linker aren't effected by the outside world.
86 SetEnvironmentVariable("CL", "");
87 SetEnvironmentVariable("LINK", "");
89 if (argc > 1 && *argv[1] == '-') {
90 switch (*(argv[1]+1)) {
91 case 'c':
92 if (argc != 3) {
93 chars = snprintf(msg, sizeof(msg) - 1,
94 "usage: %s -c <compiler option>\n"
95 "Tests for whether cl.exe supports an option\n"
96 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
97 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
98 &dwWritten, NULL);
99 return 2;
101 return CheckForCompilerFeature(argv[2]);
102 case 'l':
103 if (argc < 3) {
104 chars = snprintf(msg, sizeof(msg) - 1,
105 "usage: %s -l <linker option> ?<mandatory option> ...?\n"
106 "Tests for whether link.exe supports an option\n"
107 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
108 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
109 &dwWritten, NULL);
110 return 2;
112 return CheckForLinkerFeature(&argv[2], argc-2);
113 case 'f':
114 if (argc == 2) {
115 chars = snprintf(msg, sizeof(msg) - 1,
116 "usage: %s -f <string> <substring>\n"
117 "Find a substring within another\n"
118 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
119 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
120 &dwWritten, NULL);
121 return 2;
122 } else if (argc == 3) {
124 * If the string is blank, there is no match.
127 return 0;
128 } else {
129 return IsIn(argv[2], argv[3]);
131 case 's':
132 if (argc == 2) {
133 chars = snprintf(msg, sizeof(msg) - 1,
134 "usage: %s -s <substitutions file> <file>\n"
135 "Perform a set of string map type substutitions on a file\n"
136 "exitcodes: 0\n",
137 argv[0]);
138 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
139 &dwWritten, NULL);
140 return 2;
142 return SubstituteFile(argv[2], argv[3]);
143 case 'V':
144 if (argc != 4) {
145 chars = snprintf(msg, sizeof(msg) - 1,
146 "usage: %s -V filename matchstring\n"
147 "Extract a version from a file:\n"
148 "eg: pkgIndex.tcl \"package ifneeded http\"",
149 argv[0]);
150 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
151 &dwWritten, NULL);
152 return 0;
154 s = GetVersionFromFile(argv[2], argv[3], *(argv[1]+2) - '0');
155 if (s && *s) {
156 printf("%s\n", s);
157 return 0;
158 } else
159 return 1; /* Version not found. Return non-0 exit code */
161 case 'Q':
162 if (argc != 3) {
163 chars = snprintf(msg, sizeof(msg) - 1,
164 "usage: %s -Q path\n"
165 "Emit the fully qualified path\n"
166 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
167 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
168 &dwWritten, NULL);
169 return 2;
171 return QualifyPath(argv[2]);
173 case 'L':
174 if (argc != 3) {
175 chars = snprintf(msg, sizeof(msg) - 1,
176 "usage: %s -L keypath\n"
177 "Emit the fully qualified path of directory containing keypath\n"
178 "exitcodes: 0 == success, 1 == not found, 2 == error\n", argv[0]);
179 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
180 &dwWritten, NULL);
181 return 2;
183 return LocateDependency(argv[2]);
186 chars = snprintf(msg, sizeof(msg) - 1,
187 "usage: %s -c|-f|-l|-Q|-s|-V ...\n"
188 "This is a little helper app to equalize shell differences between WinNT and\n"
189 "Win9x and get nmake.exe to accomplish its job.\n",
190 argv[0]);
191 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, &dwWritten, NULL);
192 return 2;
195 static int
196 CheckForCompilerFeature(
197 const char *option)
199 STARTUPINFO si;
200 PROCESS_INFORMATION pi;
201 SECURITY_ATTRIBUTES sa;
202 DWORD threadID;
203 char msg[300];
204 BOOL ok;
205 HANDLE hProcess, h, pipeThreads[2];
206 char cmdline[100];
208 hProcess = GetCurrentProcess();
210 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
211 ZeroMemory(&si, sizeof(STARTUPINFO));
212 si.cb = sizeof(STARTUPINFO);
213 si.dwFlags = STARTF_USESTDHANDLES;
214 si.hStdInput = INVALID_HANDLE_VALUE;
216 ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
217 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
218 sa.lpSecurityDescriptor = NULL;
219 sa.bInheritHandle = FALSE;
222 * Create a non-inheritible pipe.
225 CreatePipe(&Out.pipe, &h, &sa, 0);
228 * Dupe the write side, make it inheritible, and close the original.
231 DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE,
232 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
235 * Same as above, but for the error side.
238 CreatePipe(&Err.pipe, &h, &sa, 0);
239 DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE,
240 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
243 * Base command line.
246 lstrcpy(cmdline, "cl.exe -nologo -c -TC -Zs -X -Fp.\\_junk.pch ");
249 * Append our option for testing
252 lstrcat(cmdline, option);
255 * Filename to compile, which exists, but is nothing and empty.
258 lstrcat(cmdline, " .\\nul");
260 ok = CreateProcess(
261 NULL, /* Module name. */
262 cmdline, /* Command line. */
263 NULL, /* Process handle not inheritable. */
264 NULL, /* Thread handle not inheritable. */
265 TRUE, /* yes, inherit handles. */
266 DETACHED_PROCESS, /* No console for you. */
267 NULL, /* Use parent's environment block. */
268 NULL, /* Use parent's starting directory. */
269 &si, /* Pointer to STARTUPINFO structure. */
270 &pi); /* Pointer to PROCESS_INFORMATION structure. */
272 if (!ok) {
273 DWORD err = GetLastError();
274 int chars = snprintf(msg, sizeof(msg) - 1,
275 "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err);
277 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|
278 FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPSTR)&msg[chars],
279 (300-chars), 0);
280 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL);
281 return 2;
285 * Close our references to the write handles that have now been inherited.
288 CloseHandle(si.hStdOutput);
289 CloseHandle(si.hStdError);
291 WaitForInputIdle(pi.hProcess, 5000);
292 CloseHandle(pi.hThread);
295 * Start the pipe reader threads.
298 pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID);
299 pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);
302 * Block waiting for the process to end.
305 WaitForSingleObject(pi.hProcess, INFINITE);
306 CloseHandle(pi.hProcess);
309 * Wait for our pipe to get done reading, should it be a little slow.
312 WaitForMultipleObjects(2, pipeThreads, TRUE, 500);
313 CloseHandle(pipeThreads[0]);
314 CloseHandle(pipeThreads[1]);
317 * Look for the commandline warning code in both streams.
318 * - in MSVC 6 & 7 we get D4002, in MSVC 8 we get D9002.
321 return !(strstr(Out.buffer, "D4002") != NULL
322 || strstr(Err.buffer, "D4002") != NULL
323 || strstr(Out.buffer, "D9002") != NULL
324 || strstr(Err.buffer, "D9002") != NULL
325 || strstr(Out.buffer, "D2021") != NULL
326 || strstr(Err.buffer, "D2021") != NULL);
329 static int
330 CheckForLinkerFeature(
331 char **options,
332 int count)
334 STARTUPINFO si;
335 PROCESS_INFORMATION pi;
336 SECURITY_ATTRIBUTES sa;
337 DWORD threadID;
338 char msg[300];
339 BOOL ok;
340 HANDLE hProcess, h, pipeThreads[2];
341 int i;
342 char cmdline[255];
344 hProcess = GetCurrentProcess();
346 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
347 ZeroMemory(&si, sizeof(STARTUPINFO));
348 si.cb = sizeof(STARTUPINFO);
349 si.dwFlags = STARTF_USESTDHANDLES;
350 si.hStdInput = INVALID_HANDLE_VALUE;
352 ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
353 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
354 sa.lpSecurityDescriptor = NULL;
355 sa.bInheritHandle = TRUE;
358 * Create a non-inheritible pipe.
361 CreatePipe(&Out.pipe, &h, &sa, 0);
364 * Dupe the write side, make it inheritible, and close the original.
367 DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE,
368 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
371 * Same as above, but for the error side.
374 CreatePipe(&Err.pipe, &h, &sa, 0);
375 DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE,
376 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
379 * Base command line.
382 lstrcpy(cmdline, "link.exe -nologo ");
385 * Append our option for testing.
388 for (i = 0; i < count; i++) {
389 lstrcat(cmdline, " \"");
390 lstrcat(cmdline, options[i]);
391 lstrcat(cmdline, "\"");
394 ok = CreateProcess(
395 NULL, /* Module name. */
396 cmdline, /* Command line. */
397 NULL, /* Process handle not inheritable. */
398 NULL, /* Thread handle not inheritable. */
399 TRUE, /* yes, inherit handles. */
400 DETACHED_PROCESS, /* No console for you. */
401 NULL, /* Use parent's environment block. */
402 NULL, /* Use parent's starting directory. */
403 &si, /* Pointer to STARTUPINFO structure. */
404 &pi); /* Pointer to PROCESS_INFORMATION structure. */
406 if (!ok) {
407 DWORD err = GetLastError();
408 int chars = snprintf(msg, sizeof(msg) - 1,
409 "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err);
411 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|
412 FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPSTR)&msg[chars],
413 (300-chars), 0);
414 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL);
415 return 2;
419 * Close our references to the write handles that have now been inherited.
422 CloseHandle(si.hStdOutput);
423 CloseHandle(si.hStdError);
425 WaitForInputIdle(pi.hProcess, 5000);
426 CloseHandle(pi.hThread);
429 * Start the pipe reader threads.
432 pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID);
433 pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);
436 * Block waiting for the process to end.
439 WaitForSingleObject(pi.hProcess, INFINITE);
440 CloseHandle(pi.hProcess);
443 * Wait for our pipe to get done reading, should it be a little slow.
446 WaitForMultipleObjects(2, pipeThreads, TRUE, 500);
447 CloseHandle(pipeThreads[0]);
448 CloseHandle(pipeThreads[1]);
451 * Look for the commandline warning code in the stderr stream.
454 return !(strstr(Out.buffer, "LNK1117") != NULL ||
455 strstr(Err.buffer, "LNK1117") != NULL ||
456 strstr(Out.buffer, "LNK4044") != NULL ||
457 strstr(Err.buffer, "LNK4044") != NULL ||
458 strstr(Out.buffer, "LNK4224") != NULL ||
459 strstr(Err.buffer, "LNK4224") != NULL);
462 static DWORD WINAPI
463 ReadFromPipe(
464 LPVOID args)
466 pipeinfo *pi = (pipeinfo *) args;
467 char *lastBuf = pi->buffer;
468 DWORD dwRead;
469 BOOL ok;
471 again:
472 if (lastBuf - pi->buffer + CHUNK > STATICBUFFERSIZE) {
473 CloseHandle(pi->pipe);
474 return (DWORD)-1;
476 ok = ReadFile(pi->pipe, lastBuf, CHUNK, &dwRead, 0L);
477 if (!ok || dwRead == 0) {
478 CloseHandle(pi->pipe);
479 return 0;
481 lastBuf += dwRead;
482 goto again;
484 return 0; /* makes the compiler happy */
487 static int
488 IsIn(
489 const char *string,
490 const char *substring)
492 return (strstr(string, substring) != NULL);
496 * GetVersionFromFile --
497 * Looks for a match string in a file and then returns the version
498 * following the match where a version is anything acceptable to
499 * package provide or package ifneeded.
502 static const char *
503 GetVersionFromFile(
504 const char *filename,
505 const char *match,
506 int numdots)
508 static char szBuffer[100];
509 char *szResult = NULL;
510 FILE *fp = fopen(filename, "rt");
512 if (fp != NULL) {
514 * Read data until we see our match string.
517 while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
518 LPSTR p, q;
520 p = strstr(szBuffer, match);
521 if (p != NULL) {
523 * Skip to first digit after the match.
526 p += strlen(match);
527 while (*p && !isdigit((unsigned char)*p)) {
528 ++p;
532 * Find ending whitespace.
535 q = p;
536 while (*q && (strchr("0123456789.ab", *q)) && (((!strchr(".ab", *q)
537 && !strchr("ab", q[-1])) || --numdots))) {
538 ++q;
541 *q = 0;
542 szResult = p;
543 break;
546 fclose(fp);
548 return szResult;
552 * List helpers for the SubstituteFile function
555 typedef struct list_item_t {
556 struct list_item_t *nextPtr;
557 char * key;
558 char * value;
559 } list_item_t;
561 /* insert a list item into the list (list may be null) */
562 static list_item_t *
563 list_insert(list_item_t **listPtrPtr, const char *key, const char *value)
565 list_item_t *itemPtr = (list_item_t *)malloc(sizeof(list_item_t));
566 if (itemPtr) {
567 itemPtr->key = strdup(key);
568 itemPtr->value = strdup(value);
569 itemPtr->nextPtr = NULL;
571 while(*listPtrPtr) {
572 listPtrPtr = &(*listPtrPtr)->nextPtr;
574 *listPtrPtr = itemPtr;
576 return itemPtr;
579 static void
580 list_free(list_item_t **listPtrPtr)
582 list_item_t *tmpPtr, *listPtr = *listPtrPtr;
583 while (listPtr) {
584 tmpPtr = listPtr;
585 listPtr = listPtr->nextPtr;
586 free(tmpPtr->key);
587 free(tmpPtr->value);
588 free(tmpPtr);
593 * SubstituteFile --
594 * As windows doesn't provide anything useful like sed and it's unreliable
595 * to use the tclsh you are building against (consider x-platform builds -
596 * eg compiling AMD64 target from IX86) we provide a simple substitution
597 * option here to handle autoconf style substitutions.
598 * The substitution file is whitespace and line delimited. The file should
599 * consist of lines matching the regular expression:
600 * \s*\S+\s+\S*$
602 * Usage is something like:
603 * nmakehlp -S << $** > $@
604 * @PACKAGE_NAME@ $(PACKAGE_NAME)
605 * @PACKAGE_VERSION@ $(PACKAGE_VERSION)
606 * <<
609 static int
610 SubstituteFile(
611 const char *substitutions,
612 const char *filename)
614 static char szBuffer[1024], szCopy[1024];
615 list_item_t *substPtr = NULL;
616 FILE *fp, *sp;
618 fp = fopen(filename, "rt");
619 if (fp != NULL) {
622 * Build a list of substutitions from the first filename
625 sp = fopen(substitutions, "rt");
626 if (sp != NULL) {
627 while (fgets(szBuffer, sizeof(szBuffer), sp) != NULL) {
628 unsigned char *ks, *ke, *vs, *ve;
629 ks = (unsigned char*)szBuffer;
630 while (ks && *ks && isspace(*ks)) ++ks;
631 ke = ks;
632 while (ke && *ke && !isspace(*ke)) ++ke;
633 vs = ke;
634 while (vs && *vs && isspace(*vs)) ++vs;
635 ve = vs;
636 while (ve && *ve && !(*ve == '\r' || *ve == '\n')) ++ve;
637 *ke = 0, *ve = 0;
638 list_insert(&substPtr, (char*)ks, (char*)vs);
640 fclose(sp);
643 /* debug: dump the list */
644 #ifndef NDEBUG
646 int n = 0;
647 list_item_t *p = NULL;
648 for (p = substPtr; p != NULL; p = p->nextPtr, ++n) {
649 fprintf(stderr, "% 3d '%s' => '%s'\n", n, p->key, p->value);
652 #endif
655 * Run the substitutions over each line of the input
658 while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
659 list_item_t *p = NULL;
660 for (p = substPtr; p != NULL; p = p->nextPtr) {
661 char *m = strstr(szBuffer, p->key);
662 if (m) {
663 char *cp, *op, *sp;
664 cp = szCopy;
665 op = szBuffer;
666 while (op != m) *cp++ = *op++;
667 sp = p->value;
668 while (sp && *sp) *cp++ = *sp++;
669 op += strlen(p->key);
670 while (*op) *cp++ = *op++;
671 *cp = 0;
672 memcpy(szBuffer, szCopy, sizeof(szCopy));
675 printf("%s", szBuffer);
678 list_free(&substPtr);
680 fclose(fp);
681 return 0;
684 BOOL FileExists(LPCTSTR szPath)
686 #ifndef INVALID_FILE_ATTRIBUTES
687 #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
688 #endif
689 DWORD pathAttr = GetFileAttributes(szPath);
690 return (pathAttr != INVALID_FILE_ATTRIBUTES &&
691 !(pathAttr & FILE_ATTRIBUTE_DIRECTORY));
696 * QualifyPath --
698 * This composes the current working directory with a provided path
699 * and returns the fully qualified and normalized path.
700 * Mostly needed to setup paths for testing.
703 static int
704 QualifyPath(
705 const char *szPath)
707 char szCwd[MAX_PATH + 1];
709 GetFullPathName(szPath, sizeof(szCwd)-1, szCwd, NULL);
710 printf("%s\n", szCwd);
711 return 0;
715 * Implements LocateDependency for a single directory. See that command
716 * for an explanation.
717 * Returns 0 if found after printing the directory.
718 * Returns 1 if not found but no errors.
719 * Returns 2 on any kind of error
720 * Basically, these are used as exit codes for the process.
722 static int LocateDependencyHelper(const char *dir, const char *keypath)
724 HANDLE hSearch;
725 char path[MAX_PATH+1];
726 size_t dirlen;
727 int keylen, ret;
728 WIN32_FIND_DATA finfo;
730 if (dir == NULL || keypath == NULL)
731 return 2; /* Have no real error reporting mechanism into nmake */
732 dirlen = strlen(dir);
733 if ((dirlen + 3) > sizeof(path))
734 return 2;
735 strncpy(path, dir, dirlen);
736 strncpy(path+dirlen, "\\*", 3); /* Including terminating \0 */
737 keylen = strlen(keypath);
739 #if 0 /* This function is not available in Visual C++ 6 */
741 * Use numerics 0 -> FindExInfoStandard,
742 * 1 -> FindExSearchLimitToDirectories,
743 * as these are not defined in Visual C++ 6
745 hSearch = FindFirstFileEx(path, 0, &finfo, 1, NULL, 0);
746 #else
747 hSearch = FindFirstFile(path, &finfo);
748 #endif
749 if (hSearch == INVALID_HANDLE_VALUE)
750 return 1; /* Not found */
752 /* Loop through all subdirs checking if the keypath is under there */
753 ret = 1; /* Assume not found */
754 do {
755 int sublen;
757 * We need to check it is a directory despite the
758 * FindExSearchLimitToDirectories in the above call. See SDK docs
760 if ((finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
761 continue;
762 sublen = strlen(finfo.cFileName);
763 if ((dirlen+1+sublen+1+keylen+1) > sizeof(path))
764 continue; /* Path does not fit, assume not matched */
765 strncpy(path+dirlen+1, finfo.cFileName, sublen);
766 path[dirlen+1+sublen] = '\\';
767 strncpy(path+dirlen+1+sublen+1, keypath, keylen+1);
768 if (FileExists(path)) {
769 /* Found a match, print to stdout */
770 path[dirlen+1+sublen] = '\0';
771 QualifyPath(path);
772 ret = 0;
773 break;
775 } while (FindNextFile(hSearch, &finfo));
776 FindClose(hSearch);
777 return ret;
781 * LocateDependency --
783 * Locates a dependency for a package.
784 * keypath - a relative path within the package directory
785 * that is used to confirm it is the correct directory.
786 * The search path for the package directory is currently only
787 * the parent and grandparent of the current working directory.
788 * If found, the command prints
789 * name_DIRPATH=<full path of located directory>
790 * and returns 0. If not found, does not print anything and returns 1.
792 static int LocateDependency(const char *keypath)
794 size_t i;
795 int ret;
796 static const char *paths[] = {"..", "..\\..", "..\\..\\.."};
798 for (i = 0; i < (sizeof(paths)/sizeof(paths[0])); ++i) {
799 ret = LocateDependencyHelper(paths[i], keypath);
800 if (ret == 0)
801 return ret;
803 return ret;
808 * Local variables:
809 * mode: c
810 * c-basic-offset: 4
811 * fill-column: 78
812 * indent-tabs-mode: t
813 * tab-width: 8
814 * End: