1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: filesrch.c 1257 2016-09-20 17:14:21Z wesleyjohnson $
5 //
6 // Copyright (C) 1998-2016 by DooM Legacy Team.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 //-----------------------------------------------------------------------------
19
20 // Because of WINVER redefine, doomtype.h (via doomincl.h) is before any
21 // other include that might define WINVER
22 #include "doomincl.h"
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <direct.h>
27 // io.h -> access, _findnext, _A_SUBDIR, chdir
28
29 #if 0
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #ifndef __WIN32__
33 #include <unistd.h>
34 #else
35 #include <windows.h>
36 #endif
37 #endif
38
39 #include "d_netfil.h"
40 #include "m_misc.h"
41
42 //
43 // sys_filesearch:
44 //
45 // filename : the filename to be found
46 // wantedmd5sum : NULL for no md5 check
47 // completepath : when not NULL, return the full path and name
48 // must be a buffer of MAX_WADPATH
49 // maxsearchdepth : dir depth, when 0 only search given directory
50 // return FS_NOTFOUND
51 // FS_MD5SUMBAD
52 // FS_FOUND
53
sys_filesearch(const char * filename,const char * startpath,const byte * wantedmd5sum,int maxsearchdepth,char * completepath)54 filestatus_e sys_filesearch( const char * filename, const char * startpath,
55 const byte * wantedmd5sum, int maxsearchdepth,
56 /*OUT*/ char * completepath )
57 {
58 filestatus_e fs;
59 struct _finddata_t dta;
60 int searchhandle;
61
62 searchhandle=access(filename,4);
63 if(searchhandle!=-1)
64 {
65 // take care of gmt timestamp conversion
66 fs = checkfile_md5(filename,wantedmd5sum);
67 if( (fs==FS_FOUND) && completepath )
68 {
69 // completepath may be same buffer as filename
70 char orig_name[MAX_WADPATH];
71 strncpy( orig_name, filename, MAX_WADPATH-1 );
72 orig_name[MAX_WADPATH-1] = '\0';
73 cat_filename(completepath, dta.name, orig_name);
74 }
75 return fs;
76 }
77
78 if( maxsearchdepth < 1 )
79 return FS_NOTFOUND;
80
81 // Search subdirectories
82 if((searchhandle=_findfirst("*.*",&dta))!=-1)
83 {
84 do
85 {
86 if((dta.name[0]!='.') && (dta.attrib & _A_SUBDIR ))
87 {
88 if( chdir(dta.name)==0 ) { // can fail if we haven't the right
89 fs = sys_filesearch( filename, NULL, wantedmd5sum,
90 maxsearchdepth-1,
91 /*OUT*/ completepath );
92 chdir("..");
93 if( fs == FS_FOUND )
94 {
95 _findclose(searchhandle);
96 return fs;
97 }
98 }
99 }
100 } while(_findnext(searchhandle,&dta)==0);
101 }
102 _findclose(searchhandle);
103 return FS_NOTFOUND;
104 }
105
106