| | 1 | /* |
| | 2 | * This file is part of Jam - see jam.c for Copyright information. |
| | 3 | */ |
| | 4 | |
| | 5 | #include "jam.h" |
| | 6 | |
| | 7 | #if defined(OS_AMIGA) |
| | 8 | |
| | 9 | #include "pathsys.h" |
| | 10 | #include "strings.h" |
| | 11 | #include "newstr.h" |
| | 12 | #include "filesys.h" |
| | 13 | |
| | 14 | #include <dos/dosextens.h> |
| | 15 | #include <proto/exec.h> |
| | 16 | |
| | 17 | |
| | 18 | /* |
| | 19 | * pathamiga.c - manipulate file names on AmigaOS |
| | 20 | * |
| | 21 | * External routines: |
| | 22 | * |
| | 23 | * path_parse() - split a file name into dir/base/suffix/member |
| | 24 | * path_build() - build a filename given dir/base/suffix/member |
| | 25 | * path_parent() - make a PATHNAME point to its parent dir |
| | 26 | * |
| | 27 | * file_parse() and path_build() just manipuate a string and a structure; |
| | 28 | * they do not make system calls. |
| | 29 | */ |
| | 30 | |
| | 31 | |
| | 32 | /* |
| | 33 | * path_parse() - split a file name into dir/base/suffix/member |
| | 34 | */ |
| | 35 | void path_parse(char *file, PATHNAME *f) |
| | 36 | { |
| | 37 | char *p, *q; |
| | 38 | char *end; |
| | 39 | |
| | 40 | memset( (char *)f, 0, sizeof( *f ) ); |
| | 41 | |
| | 42 | /* Look for <grist> */ |
| | 43 | if( file[0] == '<' && ( p = strchr( file, '>' ) ) ) |
| | 44 | { |
| | 45 | f->f_grist.ptr = file; |
| | 46 | f->f_grist.len = p - file; |
| | 47 | file = p + 1; |
| | 48 | } |
| | 49 | |
| | 50 | /* Look for dir/ */ |
| | 51 | p = strrchr( file, '/' ); |
| | 52 | |
| | 53 | if( p ) |
| | 54 | { |
| | 55 | f->f_dir.ptr = file; |
| | 56 | f->f_dir.len = p - file; |
| | 57 | |
| | 58 | /* Special case for / - dirname is /, not "" */ |
| | 59 | if( !f->f_dir.len ) |
| | 60 | f->f_dir.len = 1; |
| | 61 | |
| | 62 | file = p + 1; |
| | 63 | } |
| | 64 | |
| | 65 | end = file + strlen( file ); |
| | 66 | |
| | 67 | /* Look for (member) */ |
| | 68 | if( ( p = strchr( file, '(' ) ) && end[-1] == ')' ) |
| | 69 | { |
| | 70 | f->f_member.ptr = p + 1; |
| | 71 | f->f_member.len = end - p - 2; |
| | 72 | end = p; |
| | 73 | } |
| | 74 | |
| | 75 | /* Look for .suffix */ |
| | 76 | /* This would be memrchr() */ |
| | 77 | p = 0; |
| | 78 | q = file; |
| | 79 | |
| | 80 | while( q = (char *)memchr( q, '.', end - q ) ) |
| | 81 | p = q++; |
| | 82 | |
| | 83 | if( p ) |
| | 84 | { |
| | 85 | f->f_suffix.ptr = p; |
| | 86 | f->f_suffix.len = end - p; |
| | 87 | end = p; |
| | 88 | } |
| | 89 | |
| | 90 | /* Leaves base */ |
| | 91 | f->f_base.ptr = file; |
| | 92 | f->f_base.len = end - file; |
| | 93 | } |
| | 94 | |
| | 95 | |
| | 96 | /* |
| | 97 | * is_path_delim() - true iff c is a path delimiter |
| | 98 | */ |
| | 99 | static int is_path_delim(char c) |
| | 100 | { |
| | 101 | return c == PATH_DELIM; |
| | 102 | } |
| | 103 | |
| | 104 | |
| | 105 | /* |
| | 106 | * as_path_delim() - convert c to a path delimiter if it isn't one |
| | 107 | * already |
| | 108 | */ |
| | 109 | static char as_path_delim( char c ) |
| | 110 | { |
| | 111 | return is_path_delim(c) ? c : PATH_DELIM; |
| | 112 | } |
| | 113 | |
| | 114 | |
| | 115 | /* |
| | 116 | * path_build() - build a filename given dir/base/suffix/member |
| | 117 | */ |
| | 118 | void path_build(PATHNAME *f, string *file, int binding) |
| | 119 | { |
| | 120 | file_build1( f, file ); |
| | 121 | |
| | 122 | /* Don't prepend root if it's . or directory is rooted */ |
| | 123 | if( f->f_root.len |
| | 124 | && !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' ) |
| | 125 | && !( f->f_dir.len && f->f_dir.ptr[0] == '/' ) ) |
| | 126 | { |
| | 127 | string_append_range( file, f->f_root.ptr, f->f_root.ptr + f->f_root.len ); |
| | 128 | /* If 'root' already ends with path delimeter, don't add yet another one. */ |
| | 129 | if( ! is_path_delim( f->f_root.ptr[f->f_root.len-1] ) ) |
| | 130 | string_push_back( file, as_path_delim( f->f_root.ptr[f->f_root.len] ) ); |
| | 131 | } |
| | 132 | |
| | 133 | if( f->f_dir.len ) |
| | 134 | { |
| | 135 | string_append_range( file, f->f_dir.ptr, f->f_dir.ptr + f->f_dir.len ); |
| | 136 | } |
| | 137 | |
| | 138 | /* Put / between dir and file */ |
| | 139 | if( f->f_dir.len && ( f->f_base.len || f->f_suffix.len ) ) |
| | 140 | { |
| | 141 | /* Special case for dir / : don't add another / */ |
| | 142 | if( !( f->f_dir.len == 1 && is_path_delim( f->f_dir.ptr[0] ) ) ) |
| | 143 | string_push_back( file, as_path_delim( f->f_dir.ptr[f->f_dir.len] ) ); |
| | 144 | } |
| | 145 | |
| | 146 | if( f->f_base.len ) |
| | 147 | { |
| | 148 | string_append_range( file, f->f_base.ptr, f->f_base.ptr + f->f_base.len ); |
| | 149 | } |
| | 150 | |
| | 151 | if( f->f_suffix.len ) |
| | 152 | { |
| | 153 | string_append_range( file, f->f_suffix.ptr, f->f_suffix.ptr + f->f_suffix.len ); |
| | 154 | } |
| | 155 | |
| | 156 | if( f->f_member.len ) |
| | 157 | { |
| | 158 | string_push_back( file, '(' ); |
| | 159 | string_append_range( file, f->f_member.ptr, f->f_member.ptr + f->f_member.len ); |
| | 160 | string_push_back( file, ')' ); |
| | 161 | } |
| | 162 | } |
| | 163 | |
| | 164 | |
| | 165 | /* |
| | 166 | * path_parent() - make a PATHNAME point to its parent dir |
| | 167 | */ |
| | 168 | void path_parent(PATHNAME *f) |
| | 169 | { |
| | 170 | f->f_base.ptr = ""; |
| | 171 | f->f_suffix.ptr = ""; |
| | 172 | f->f_member.ptr = ""; |
| | 173 | |
| | 174 | f->f_base.len = 0; |
| | 175 | f->f_suffix.len = 0; |
| | 176 | f->f_member.len = 0; |
| | 177 | } |
| | 178 | |
| | 179 | |
| | 180 | const char * path_tmpdir() |
| | 181 | { |
| | 182 | return "\t"; |
| | 183 | } |
| | 184 | |
| | 185 | |
| | 186 | const char * path_tmpnam(void) |
| | 187 | { |
| | 188 | static char tmpbuf[64]; |
| | 189 | uint32 pid = ((struct Process*)IExec->FindTask(0))->pr_ProcessID; |
| | 190 | snprintf(tmpbuf, sizeof(tmpbuf), "jam.%u.XXXXXX", pid); |
| | 191 | (void) mktemp(tmpbuf); |
| | 192 | |
| | 193 | return newstr(tmpbuf); |
| | 194 | } |
| | 195 | |
| | 196 | |
| | 197 | const char * path_tmpfile(void) |
| | 198 | { |
| | 199 | const char * result = 0; |
| | 200 | |
| | 201 | static char pathbuf[4096]; |
| | 202 | snprintf(pathbuf, sizeof(pathbuf), "%s%c%s", |
| | 203 | path_tmpdir(), PATH_DELIM, path_tmpnam()); |
| | 204 | |
| | 205 | return newstr(pathbuf); |
| | 206 | } |
| | 207 | |
| | 208 | #endif |