codec cleanup
[platform/upstream/boost-jam.git] / filemac.c
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 /*  This file is ALSO:
8  *  Copyright 2001-2004 David Abrahams.
9  *  Distributed under the Boost Software License, Version 1.0.
10  *  (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
11  */
12
13 # include "jam.h"
14 # include "filesys.h"
15 # include "pathsys.h"
16
17 # ifdef OS_MAC
18
19 #include <Files.h>
20 #include <Folders.h>
21
22 # include <:sys:stat.h>
23
24 /*
25  * filemac.c - manipulate file names and scan directories on macintosh
26  *
27  * External routines:
28  *
29  *  file_dirscan() - scan a directory for files
30  *  file_time() - get timestamp of file, if not done by file_dirscan()
31  *  file_archscan() - scan an archive for files
32  *
33  * File_dirscan() and file_archscan() call back a caller provided function
34  * for each file found.  A flag to this callback function lets file_dirscan()
35  * and file_archscan() indicate that a timestamp is being provided with the
36  * file.   If file_dirscan() or file_archscan() do not provide the file's
37  * timestamp, interested parties may later call file_time().
38  *
39  * 04/08/94 (seiwald) - Coherent/386 support added.
40  * 12/19/94 (mikem) - solaris string table insanity support
41  * 02/14/95 (seiwald) - parse and build /xxx properly
42  * 05/03/96 (seiwald) - split into pathunix.c
43  * 11/21/96 (peterk) - BEOS does not have Unix-style archives
44  */
45
46
47 void CopyC2PStr( char const * cstr, StringPtr pstr )
48 {
49     int len;
50     for ( len = 0; *cstr && ( len < 255 ); pstr[ ++len ] = *cstr++ );
51     pstr[ 0 ] = len;
52 }
53
54
55 /*
56  * file_dirscan() - scan a directory for files.
57  */
58
59 void file_dirscan( char * dir, scanback func, void * closure )
60 {
61     PATHNAME f;
62     string filename[ 1 ];
63     unsigned char fullPath[ 512 ];
64
65     FSSpec spec;
66     WDPBRec vol;
67     Str63 volName;
68     CInfoPBRec lastInfo;
69     int index = 1;
70
71     /* First enter directory itself. */
72
73     memset( (char *)&f, '\0', sizeof( f ) );
74
75     f.f_dir.ptr = dir;
76     f.f_dir.len = strlen(dir);
77
78     if ( DEBUG_BINDSCAN )
79         printf( "scan directory %s\n", dir );
80
81     /* Special case ":" - enter it */
82
83     if ( ( f.f_dir.len == 1 ) && ( f.f_dir.ptr[0] == ':' ) )
84         (*func)( closure, dir, 0 /* not stat()'ed */, (time_t)0 );
85
86     /* Now enter contents of directory */
87
88     vol.ioNamePtr = volName;
89
90     if ( PBHGetVolSync( &vol ) )
91         return;
92
93     CopyC2PStr( dir, fullPath );
94
95     if ( FSMakeFSSpec( vol.ioWDVRefNum, vol.ioWDDirID, fullPath, &spec ) )
96         return;
97
98     lastInfo.dirInfo.ioVRefNum   = spec.vRefNum;
99     lastInfo.dirInfo.ioDrDirID   = spec.parID;
100     lastInfo.dirInfo.ioNamePtr   = spec.name;
101     lastInfo.dirInfo.ioFDirIndex = 0;
102     lastInfo.dirInfo.ioACUser    = 0;
103
104     if ( PBGetCatInfoSync( &lastInfo ) )
105         return;
106
107     if ( !( lastInfo.dirInfo.ioFlAttrib & 0x10 ) )
108         return;
109
110     /* ioDrDirID must be reset each time. */
111     spec.parID = lastInfo.dirInfo.ioDrDirID;
112
113     string_new( filename );
114     for ( ; ; )
115     {
116         lastInfo.dirInfo.ioVRefNum  = spec.vRefNum;
117         lastInfo.dirInfo.ioDrDirID  = spec.parID;
118         lastInfo.dirInfo.ioNamePtr  = fullPath;
119         lastInfo.dirInfo.ioFDirIndex = index++;
120
121         if ( PBGetCatInfoSync( &lastInfo ) )
122             return;
123
124         f.f_base.ptr = (char *)fullPath + 1;
125         f.f_base.len = *fullPath;
126
127         string_truncate( filename, 0 );
128         path_build( &f, filename, 0 );
129         (*func)( closure, filename->value, 0 /* not stat()'ed */, (time_t)0 );
130     }
131     string_free( filename );
132 }
133
134
135 /*
136  * file_time() - get timestamp of file, if not done by file_dirscan().
137  */
138
139 int file_time( char * filename, time_t * time )
140 {
141     struct stat statbuf;
142
143     if ( stat( filename, &statbuf ) < 0 )
144         return -1;
145
146     *time = statbuf.st_mtime;
147
148     return 0;
149 }
150
151
152 int file_is_file( char * filename )
153 {
154     struct stat statbuf;
155     if ( stat( filename, &statbuf ) < 0 )
156         return -1;
157     return S_ISREG( statbuf.st_mode ) ? 1 : 0;
158 }
159
160 int file_mkdir(char *pathname)
161 {
162     return mkdir(pathname, 0766);
163 }
164
165
166 /*
167  * file_archscan() - scan an archive for files.
168  */
169
170 void file_archscan( char * archive, scanback func, void * closure )
171 {
172 }
173
174
175 # endif /* macintosh */