Remove all occurrences of occurence, except for one (in the
[p5sagit/p5-mst-13.2.git] / ext / File / Glob / bsd_glob.c
CommitLineData
72b16652 1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Guido van Rossum.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
0e950d83 16 * 3. Neither the name of the University nor the names of its contributors
72b16652 17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
72b16652 33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
bac331f5 35/* most changes between the version above and the one below have been ported:
36static char sscsid[]= "$OpenBSD: glob.c,v 1.8.10.1 2001/04/10 jason Exp $";
37 */
72b16652 38#endif /* LIBC_SCCS and not lint */
39
40/*
41 * glob(3) -- a superset of the one defined in POSIX 1003.2.
42 *
43 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
44 *
45 * Optional extra services, controlled by flags not defined by POSIX:
46 *
47 * GLOB_QUOTE:
48 * Escaping convention: \ inhibits any special meaning the following
49 * character might have (except \ at end of string is retained).
50 * GLOB_MAGCHAR:
51 * Set in gl_flags if pattern contained a globbing character.
52 * GLOB_NOMAGIC:
53 * Same as GLOB_NOCHECK, but it will only append pattern if it did
54 * not contain any magic characters. [Used in csh style globbing]
55 * GLOB_ALTDIRFUNC:
56 * Use alternately specified directory access functions.
57 * GLOB_TILDE:
58 * expand ~user/foo to the /home/dir/of/user/foo
59 * GLOB_BRACE:
60 * expand {1,2}{a,b} to 1a 1b 2a 2b
61 * gl_matchc:
62 * Number of matches in the current invocation of glob.
2d5e9e5d 63 * GLOB_ALPHASORT:
64 * sort alphabetically like csh (case doesn't matter) instead of in ASCII
65 * order
72b16652 66 */
67
68#include <EXTERN.h>
69#include <perl.h>
4f49e16e 70#include <XSUB.h>
71
72b16652 72#include "bsd_glob.h"
73#ifdef I_PWD
74# include <pwd.h>
75#else
fd8cd3a3 76#if defined(HAS_PASSWD) && !defined(VMS)
72b16652 77 struct passwd *getpwnam(char *);
78 struct passwd *getpwuid(Uid_t);
79#endif
80#endif
81
82#ifndef MAXPATHLEN
83# ifdef PATH_MAX
84# define MAXPATHLEN PATH_MAX
7369a524 85# ifdef MACOS_TRADITIONAL
86# define MAXPATHLEN 255
87# else
88# define MAXPATHLEN 1024
89# endif
72b16652 90# endif
91#endif
92
b8ef571c 93#ifdef I_LIMITS
94#include <limits.h>
95#endif
96
97#ifndef ARG_MAX
6eb630b7 98# ifdef MACOS_TRADITIONAL
99# define ARG_MAX 65536 /* Mac OS is actually unlimited */
b8ef571c 100# else
6eb630b7 101# ifdef _SC_ARG_MAX
102# define ARG_MAX (sysconf(_SC_ARG_MAX))
b8ef571c 103# else
6eb630b7 104# ifdef _POSIX_ARG_MAX
105# define ARG_MAX _POSIX_ARG_MAX
b8ef571c 106# else
6eb630b7 107# ifdef WIN32
108# define ARG_MAX 14500 /* from VC's limits.h */
109# else
110# define ARG_MAX 4096 /* from POSIX, be conservative */
111# endif
b8ef571c 112# endif
113# endif
114# endif
115#endif
116
3e5d0dec 117#define BG_DOLLAR '$'
118#define BG_DOT '.'
119#define BG_EOS '\0'
120#define BG_LBRACKET '['
121#define BG_NOT '!'
122#define BG_QUESTION '?'
123#define BG_QUOTE '\\'
124#define BG_RANGE '-'
125#define BG_RBRACKET ']'
7369a524 126#ifdef MACOS_TRADITIONAL
127# define BG_SEP ':'
128#else
129# define BG_SEP '/'
130#endif
220398a0 131#ifdef DOSISH
132#define BG_SEP2 '\\'
133#endif
3e5d0dec 134#define BG_STAR '*'
135#define BG_TILDE '~'
136#define BG_UNDERSCORE '_'
137#define BG_LBRACE '{'
138#define BG_RBRACE '}'
139#define BG_SLASH '/'
140#define BG_COMMA ','
72b16652 141
142#ifndef GLOB_DEBUG
143
144#define M_QUOTE 0x8000
145#define M_PROTECT 0x4000
146#define M_MASK 0xffff
147#define M_ASCII 0x00ff
148
149typedef U16 Char;
150
151#else
152
153#define M_QUOTE 0x80
154#define M_PROTECT 0x40
155#define M_MASK 0xff
156#define M_ASCII 0x7f
157
158typedef U8 Char;
159
160#endif /* !GLOB_DEBUG */
161
162
163#define CHAR(c) ((Char)((c)&M_ASCII))
164#define META(c) ((Char)((c)|M_QUOTE))
165#define M_ALL META('*')
166#define M_END META(']')
167#define M_NOT META('!')
168#define M_ONE META('?')
169#define M_RNG META('-')
170#define M_SET META('[')
171#define ismeta(c) (((c)&M_QUOTE) != 0)
172
173
174static int compare(const void *, const void *);
220398a0 175static int ci_compare(const void *, const void *);
b8ef571c 176static int g_Ctoc(const Char *, char *, STRLEN);
72b16652 177static int g_lstat(Char *, Stat_t *, glob_t *);
178static DIR *g_opendir(Char *, glob_t *);
179static Char *g_strchr(Char *, int);
72b16652 180static int g_stat(Char *, Stat_t *, glob_t *);
181static int glob0(const Char *, glob_t *);
b8ef571c 182static int glob1(Char *, Char *, glob_t *, size_t *);
183static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
184 glob_t *, size_t *);
185static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
186 Char *, Char *, glob_t *, size_t *);
187static int globextend(const Char *, glob_t *, size_t *);
188static const Char *
189 globtilde(const Char *, Char *, size_t, glob_t *);
72b16652 190static int globexp1(const Char *, glob_t *);
191static int globexp2(const Char *, const Char *, glob_t *, int *);
220398a0 192static int match(Char *, Char *, Char *, int);
72b16652 193#ifdef GLOB_DEBUG
194static void qprintf(const char *, Char *);
195#endif /* GLOB_DEBUG */
196
4f49e16e 197#ifdef PERL_IMPLICIT_CONTEXT
198static Direntry_t * my_readdir(DIR*);
199
200static Direntry_t *
201my_readdir(DIR *d)
202{
011f1a1a 203#ifndef NETWARE
4f49e16e 204 return PerlDir_read(d);
011f1a1a 205#else
206 return (DIR *)PerlDir_read(d);
207#endif
4f49e16e 208}
209#else
31d9e88b 210
211/* ReliantUNIX (OS formerly known as SINIX) defines readdir
212 * in LFS-mode to be a 64-bit version of readdir. */
213
214# ifdef sinix
215static Direntry_t * my_readdir(DIR*);
216
217static Direntry_t *
218my_readdir(DIR *d)
219{
220 return readdir(d);
221}
222# else
223
224# define my_readdir readdir
225
226# endif
227
4f49e16e 228#endif
229
be708cc0 230#ifdef MACOS_TRADITIONAL
231#include <Files.h>
232#include <Types.h>
233#include <string.h>
234
235#define NO_UPDIR_ERR 1 /* updir resolving failed */
236
237static Boolean g_matchVol; /* global variable */
238static short updir(char *path);
239static short resolve_updirs(char *new_pattern);
240static void remove_trColon(char *path);
241static short glob_mark_Mac(Char *pathbuf, Char *pathend, Char *pathend_last);
242static OSErr GetVolInfo(short volume, Boolean indexed, FSSpec *spec);
243static void name_f_FSSpec(StrFileName volname, FSSpec *spec);
244
245#endif
246
72b16652 247int
248bsd_glob(const char *pattern, int flags,
249 int (*errfunc)(const char *, int), glob_t *pglob)
250{
251 const U8 *patnext;
252 int c;
b8ef571c 253 Char *bufnext, *bufend, patbuf[MAXPATHLEN];
72b16652 254
be708cc0 255#ifdef MACOS_TRADITIONAL
256 char *new_pat, *p, *np;
257 int err;
258 size_t len;
259#endif
260
261#ifndef MACOS_TRADITIONAL
72b16652 262 patnext = (U8 *) pattern;
be708cc0 263#endif
72b16652 264 if (!(flags & GLOB_APPEND)) {
265 pglob->gl_pathc = 0;
266 pglob->gl_pathv = NULL;
267 if (!(flags & GLOB_DOOFFS))
268 pglob->gl_offs = 0;
269 }
270 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
271 pglob->gl_errfunc = errfunc;
272 pglob->gl_matchc = 0;
273
274 bufnext = patbuf;
b8ef571c 275 bufend = bufnext + MAXPATHLEN - 1;
220398a0 276#ifdef DOSISH
277 /* Nasty hack to treat patterns like "C:*" correctly. In this
278 * case, the * should match any file in the current directory
279 * on the C: drive. However, the glob code does not treat the
280 * colon specially, so it looks for files beginning "C:" in
281 * the current directory. To fix this, change the pattern to
282 * add an explicit "./" at the start (just after the drive
c6c619a9 283 * letter and colon - ie change to "C:./").
220398a0 284 */
285 if (isalpha(pattern[0]) && pattern[1] == ':' &&
286 pattern[2] != BG_SEP && pattern[2] != BG_SEP2 &&
287 bufend - bufnext > 4) {
288 *bufnext++ = pattern[0];
289 *bufnext++ = ':';
290 *bufnext++ = '.';
291 *bufnext++ = BG_SEP;
292 patnext += 2;
293 }
294#endif
be708cc0 295
296#ifdef MACOS_TRADITIONAL
297 /* Check if we need to match a volume name (e.g. '*HD:*') */
298 g_matchVol = false;
299 p = (char *) pattern;
300 if (*p != BG_SEP) {
301 p++;
302 while (*p != BG_EOS) {
303 if (*p == BG_SEP) {
304 g_matchVol = true;
305 break;
306 }
307 p++;
308 }
309 }
310
311 /* Transform the pattern:
312 * (a) Resolve updirs, e.g.
313 * '*:t*p::' -> '*:'
314 * ':a*:tmp::::' -> '::'
315 * ':base::t*p:::' -> '::'
316 * '*HD::' -> return 0 (error, quit silently)
317 *
318 * (b) Remove a single trailing ':', unless it's a "match volume only"
319 * pattern like '*HD:'; e.g.
320 * '*:tmp:' -> '*:tmp' but
321 * '*HD:' -> '*HD:'
322 * (If we don't do that, even filenames will have a trailing ':' in
323 * the result.)
324 */
325
326 /* We operate on a copy of the pattern */
327 len = strlen(pattern);
328 New(0, new_pat, len + 1, char);
329 if (new_pat == NULL)
330 return (GLOB_NOSPACE);
331
332 p = (char *) pattern;
333 np = new_pat;
334 while (*np++ = *p++) ;
335
336 /* Resolve updirs ... */
337 err = resolve_updirs(new_pat);
338 if (err) {
339 Safefree(new_pat);
340 /* The pattern is incorrect: tried to move
341 up above the volume root, see above.
342 We quit silently. */
343 return 0;
344 }
345 /* remove trailing colon ... */
346 remove_trColon(new_pat);
347 patnext = (U8 *) new_pat;
348
349#endif /* MACOS_TRADITIONAL */
350
72b16652 351 if (flags & GLOB_QUOTE) {
352 /* Protect the quoted characters. */
3e5d0dec 353 while (bufnext < bufend && (c = *patnext++) != BG_EOS)
354 if (c == BG_QUOTE) {
220398a0 355#ifdef DOSISH
356 /* To avoid backslashitis on Win32,
357 * we only treat \ as a quoting character
358 * if it precedes one of the
359 * metacharacters []-{}~\
360 */
361 if ((c = *patnext++) != '[' && c != ']' &&
362 c != '-' && c != '{' && c != '}' &&
363 c != '~' && c != '\\') {
364#else
3e5d0dec 365 if ((c = *patnext++) == BG_EOS) {
220398a0 366#endif
3e5d0dec 367 c = BG_QUOTE;
72b16652 368 --patnext;
369 }
370 *bufnext++ = c | M_PROTECT;
b8ef571c 371 } else
72b16652 372 *bufnext++ = c;
b8ef571c 373 } else
374 while (bufnext < bufend && (c = *patnext++) != BG_EOS)
375 *bufnext++ = c;
3e5d0dec 376 *bufnext = BG_EOS;
72b16652 377
be708cc0 378#ifdef MACOS_TRADITIONAL
379 if (flags & GLOB_BRACE)
380 err = globexp1(patbuf, pglob);
381 else
382 err = glob0(patbuf, pglob);
383 Safefree(new_pat);
384 return err;
385#else
72b16652 386 if (flags & GLOB_BRACE)
387 return globexp1(patbuf, pglob);
388 else
389 return glob0(patbuf, pglob);
be708cc0 390#endif
72b16652 391}
392
393/*
394 * Expand recursively a glob {} pattern. When there is no more expansion
395 * invoke the standard globbing routine to glob the rest of the magic
396 * characters
397 */
b8ef571c 398static int
399globexp1(const Char *pattern, glob_t *pglob)
72b16652 400{
401 const Char* ptr = pattern;
402 int rv;
403
404 /* Protect a single {}, for find(1), like csh */
3e5d0dec 405 if (pattern[0] == BG_LBRACE && pattern[1] == BG_RBRACE && pattern[2] == BG_EOS)
72b16652 406 return glob0(pattern, pglob);
407
3e5d0dec 408 while ((ptr = (const Char *) g_strchr((Char *) ptr, BG_LBRACE)) != NULL)
72b16652 409 if (!globexp2(ptr, pattern, pglob, &rv))
410 return rv;
411
412 return glob0(pattern, pglob);
413}
414
415
416/*
417 * Recursive brace globbing helper. Tries to expand a single brace.
418 * If it succeeds then it invokes globexp1 with the new pattern.
419 * If it fails then it tries to glob the rest of the pattern and returns.
420 */
b8ef571c 421static int
422globexp2(const Char *ptr, const Char *pattern,
423 glob_t *pglob, int *rv)
72b16652 424{
425 int i;
426 Char *lm, *ls;
427 const Char *pe, *pm, *pl;
b8ef571c 428 Char patbuf[MAXPATHLEN];
72b16652 429
430 /* copy part up to the brace */
431 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
b8ef571c 432 ;
433 *lm = BG_EOS;
72b16652 434 ls = lm;
435
436 /* Find the balanced brace */
437 for (i = 0, pe = ++ptr; *pe; pe++)
3e5d0dec 438 if (*pe == BG_LBRACKET) {
72b16652 439 /* Ignore everything between [] */
3e5d0dec 440 for (pm = pe++; *pe != BG_RBRACKET && *pe != BG_EOS; pe++)
b8ef571c 441 ;
3e5d0dec 442 if (*pe == BG_EOS) {
72b16652 443 /*
3e5d0dec 444 * We could not find a matching BG_RBRACKET.
445 * Ignore and just look for BG_RBRACE
72b16652 446 */
447 pe = pm;
448 }
b8ef571c 449 } else if (*pe == BG_LBRACE)
72b16652 450 i++;
3e5d0dec 451 else if (*pe == BG_RBRACE) {
72b16652 452 if (i == 0)
453 break;
454 i--;
455 }
456
457 /* Non matching braces; just glob the pattern */
3e5d0dec 458 if (i != 0 || *pe == BG_EOS) {
72b16652 459 *rv = glob0(patbuf, pglob);
460 return 0;
461 }
462
b8ef571c 463 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
72b16652 464 switch (*pm) {
3e5d0dec 465 case BG_LBRACKET:
72b16652 466 /* Ignore everything between [] */
3e5d0dec 467 for (pl = pm++; *pm != BG_RBRACKET && *pm != BG_EOS; pm++)
b8ef571c 468 ;
3e5d0dec 469 if (*pm == BG_EOS) {
72b16652 470 /*
3e5d0dec 471 * We could not find a matching BG_RBRACKET.
472 * Ignore and just look for BG_RBRACE
72b16652 473 */
474 pm = pl;
475 }
476 break;
477
3e5d0dec 478 case BG_LBRACE:
72b16652 479 i++;
480 break;
481
3e5d0dec 482 case BG_RBRACE:
72b16652 483 if (i) {
b8ef571c 484 i--;
485 break;
72b16652 486 }
487 /* FALLTHROUGH */
3e5d0dec 488 case BG_COMMA:
489 if (i && *pm == BG_COMMA)
72b16652 490 break;
491 else {
492 /* Append the current string */
493 for (lm = ls; (pl < pm); *lm++ = *pl++)
b8ef571c 494 ;
495
72b16652 496 /*
497 * Append the rest of the pattern after the
498 * closing brace
499 */
b8ef571c 500 for (pl = pe + 1; (*lm++ = *pl++) != BG_EOS; )
501 ;
72b16652 502
503 /* Expand the current pattern */
504#ifdef GLOB_DEBUG
505 qprintf("globexp2:", patbuf);
506#endif /* GLOB_DEBUG */
507 *rv = globexp1(patbuf, pglob);
508
509 /* move after the comma, to the next string */
510 pl = pm + 1;
511 }
512 break;
513
514 default:
515 break;
516 }
b8ef571c 517 }
72b16652 518 *rv = 0;
519 return 0;
520}
521
522
523
524/*
525 * expand tilde from the passwd file.
526 */
527static const Char *
bac331f5 528globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
72b16652 529{
72b16652 530 char *h;
531 const Char *p;
bac331f5 532 Char *b, *eb;
72b16652 533
3e5d0dec 534 if (*pattern != BG_TILDE || !(pglob->gl_flags & GLOB_TILDE))
72b16652 535 return pattern;
536
537 /* Copy up to the end of the string or / */
bac331f5 538 eb = &patbuf[patbuf_len - 1];
539 for (p = pattern + 1, h = (char *) patbuf;
7c436af3 540 h < (char*)eb && *p && *p != BG_SLASH; *h++ = (char)*p++)
b8ef571c 541 ;
72b16652 542
3e5d0dec 543 *h = BG_EOS;
72b16652 544
b8ef571c 545#if 0
546 if (h == (char *)eb)
547 return what;
548#endif
549
3e5d0dec 550 if (((char *) patbuf)[0] == BG_EOS) {
72b16652 551 /*
552 * handle a plain ~ or ~/ by expanding $HOME
553 * first and then trying the password file
554 */
555 if ((h = getenv("HOME")) == NULL) {
556#ifdef HAS_PASSWD
91f3b821 557 struct passwd *pwd;
72b16652 558 if ((pwd = getpwuid(getuid())) == NULL)
559 return pattern;
560 else
561 h = pwd->pw_dir;
562#else
563 return pattern;
564#endif
565 }
b8ef571c 566 } else {
72b16652 567 /*
568 * Expand a ~user
569 */
570#ifdef HAS_PASSWD
91f3b821 571 struct passwd *pwd;
72b16652 572 if ((pwd = getpwnam((char*) patbuf)) == NULL)
573 return pattern;
574 else
575 h = pwd->pw_dir;
576#else
577 return pattern;
578#endif
579 }
580
581 /* Copy the home directory */
bac331f5 582 for (b = patbuf; b < eb && *h; *b++ = *h++)
b8ef571c 583 ;
72b16652 584
585 /* Append the rest of the pattern */
bac331f5 586 while (b < eb && (*b++ = *p++) != BG_EOS)
b8ef571c 587 ;
bac331f5 588 *b = BG_EOS;
72b16652 589
590 return patbuf;
591}
592
593
594/*
595 * The main glob() routine: compiles the pattern (optionally processing
596 * quotes), calls glob1() to do the real pattern matching, and finally
597 * sorts the list (unless unsorted operation is requested). Returns 0
598 * if things went well, nonzero if errors occurred. It is not an error
599 * to find no matches.
600 */
601static int
602glob0(const Char *pattern, glob_t *pglob)
603{
604 const Char *qpat, *qpatnext;
605 int c, err, oldflags, oldpathc;
b8ef571c 606 Char *bufnext, patbuf[MAXPATHLEN];
607 size_t limit = 0;
72b16652 608
7369a524 609#ifdef MACOS_TRADITIONAL
610 if ( (*pattern == BG_TILDE) && (pglob->gl_flags & GLOB_TILDE) ) {
6eb630b7 611 return(globextend(pattern, pglob, &limit));
7369a524 612 }
613#endif
614
b8ef571c 615 qpat = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
72b16652 616 qpatnext = qpat;
617 oldflags = pglob->gl_flags;
618 oldpathc = pglob->gl_pathc;
619 bufnext = patbuf;
620
621 /* We don't need to check for buffer overflow any more. */
3e5d0dec 622 while ((c = *qpatnext++) != BG_EOS) {
72b16652 623 switch (c) {
3e5d0dec 624 case BG_LBRACKET:
72b16652 625 c = *qpatnext;
3e5d0dec 626 if (c == BG_NOT)
72b16652 627 ++qpatnext;
3e5d0dec 628 if (*qpatnext == BG_EOS ||
629 g_strchr((Char *) qpatnext+1, BG_RBRACKET) == NULL) {
630 *bufnext++ = BG_LBRACKET;
631 if (c == BG_NOT)
72b16652 632 --qpatnext;
633 break;
634 }
635 *bufnext++ = M_SET;
3e5d0dec 636 if (c == BG_NOT)
72b16652 637 *bufnext++ = M_NOT;
638 c = *qpatnext++;
639 do {
640 *bufnext++ = CHAR(c);
3e5d0dec 641 if (*qpatnext == BG_RANGE &&
642 (c = qpatnext[1]) != BG_RBRACKET) {
72b16652 643 *bufnext++ = M_RNG;
644 *bufnext++ = CHAR(c);
645 qpatnext += 2;
646 }
3e5d0dec 647 } while ((c = *qpatnext++) != BG_RBRACKET);
72b16652 648 pglob->gl_flags |= GLOB_MAGCHAR;
649 *bufnext++ = M_END;
650 break;
3e5d0dec 651 case BG_QUESTION:
72b16652 652 pglob->gl_flags |= GLOB_MAGCHAR;
653 *bufnext++ = M_ONE;
654 break;
3e5d0dec 655 case BG_STAR:
72b16652 656 pglob->gl_flags |= GLOB_MAGCHAR;
657 /* collapse adjacent stars to one,
658 * to avoid exponential behavior
659 */
660 if (bufnext == patbuf || bufnext[-1] != M_ALL)
b8ef571c 661 *bufnext++ = M_ALL;
72b16652 662 break;
663 default:
664 *bufnext++ = CHAR(c);
665 break;
666 }
667 }
3e5d0dec 668 *bufnext = BG_EOS;
72b16652 669#ifdef GLOB_DEBUG
670 qprintf("glob0:", patbuf);
671#endif /* GLOB_DEBUG */
672
b8ef571c 673 if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0) {
72b16652 674 pglob->gl_flags = oldflags;
675 return(err);
676 }
677
678 /*
679 * If there was no match we are going to append the pattern
680 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
681 * and the pattern did not contain any magic characters
682 * GLOB_NOMAGIC is there just for compatibility with csh.
683 */
684 if (pglob->gl_pathc == oldpathc &&
685 ((pglob->gl_flags & GLOB_NOCHECK) ||
686 ((pglob->gl_flags & GLOB_NOMAGIC) &&
687 !(pglob->gl_flags & GLOB_MAGCHAR))))
688 {
689#ifdef GLOB_DEBUG
690 printf("calling globextend from glob0\n");
691#endif /* GLOB_DEBUG */
692 pglob->gl_flags = oldflags;
b8ef571c 693 return(globextend(qpat, pglob, &limit));
72b16652 694 }
695 else if (!(pglob->gl_flags & GLOB_NOSORT))
696 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
be708cc0 697 pglob->gl_pathc - oldpathc, sizeof(char *),
2d5e9e5d 698 (pglob->gl_flags & (GLOB_ALPHASORT|GLOB_NOCASE))
699 ? ci_compare : compare);
72b16652 700 pglob->gl_flags = oldflags;
701 return(0);
702}
703
704static int
220398a0 705ci_compare(const void *p, const void *q)
706{
b8ef571c 707 const char *pp = *(const char **)p;
708 const char *qq = *(const char **)q;
709 int ci;
710 while (*pp && *qq) {
3f939f22 711 if (toLOWER(*pp) != toLOWER(*qq))
b8ef571c 712 break;
713 ++pp;
714 ++qq;
715 }
3f939f22 716 ci = toLOWER(*pp) - toLOWER(*qq);
b8ef571c 717 if (ci == 0)
718 return compare(p, q);
719 return ci;
220398a0 720}
721
722static int
72b16652 723compare(const void *p, const void *q)
724{
725 return(strcmp(*(char **)p, *(char **)q));
726}
727
728static int
b8ef571c 729glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
72b16652 730{
b8ef571c 731 Char pathbuf[MAXPATHLEN];
72b16652 732
733 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
3e5d0dec 734 if (*pattern == BG_EOS)
72b16652 735 return(0);
b8ef571c 736 return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
737 pathbuf, pathbuf+MAXPATHLEN-1,
738 pattern, pattern_last, pglob, limitp));
72b16652 739}
740
741/*
742 * The functions glob2 and glob3 are mutually recursive; there is one level
743 * of recursion for each segment in the pattern that contains one or more
744 * meta characters.
745 */
746static int
b8ef571c 747glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
748 Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
72b16652 749{
750 Stat_t sb;
751 Char *p, *q;
752 int anymeta;
753
754 /*
755 * Loop over pattern segments until end of pattern or until
756 * segment with meta character found.
757 */
758 for (anymeta = 0;;) {
3e5d0dec 759 if (*pattern == BG_EOS) { /* End of pattern? */
760 *pathend = BG_EOS;
72b16652 761 if (g_lstat(pathbuf, &sb, pglob))
762 return(0);
72b16652 763
764 if (((pglob->gl_flags & GLOB_MARK) &&
220398a0 765 pathend[-1] != BG_SEP
766#ifdef DOSISH
767 && pathend[-1] != BG_SEP2
768#endif
b8ef571c 769 ) && (S_ISDIR(sb.st_mode) ||
770 (S_ISLNK(sb.st_mode) &&
72b16652 771 (g_stat(pathbuf, &sb, pglob) == 0) &&
772 S_ISDIR(sb.st_mode)))) {
be708cc0 773#ifdef MACOS_TRADITIONAL
774 short err;
775 err = glob_mark_Mac(pathbuf, pathend, pathend_last);
776 if (err)
777 return (err);
778#else
b8ef571c 779 if (pathend+1 > pathend_last)
780 return (1);
3e5d0dec 781 *pathend++ = BG_SEP;
782 *pathend = BG_EOS;
be708cc0 783#endif
72b16652 784 }
785 ++pglob->gl_matchc;
786#ifdef GLOB_DEBUG
787 printf("calling globextend from glob2\n");
788#endif /* GLOB_DEBUG */
b8ef571c 789 return(globextend(pathbuf, pglob, limitp));
72b16652 790 }
791
792 /* Find end of next segment, copy tentatively to pathend. */
793 q = pathend;
794 p = pattern;
220398a0 795 while (*p != BG_EOS && *p != BG_SEP
796#ifdef DOSISH
797 && *p != BG_SEP2
798#endif
799 ) {
72b16652 800 if (ismeta(*p))
801 anymeta = 1;
b8ef571c 802 if (q+1 > pathend_last)
803 return (1);
72b16652 804 *q++ = *p++;
805 }
806
807 if (!anymeta) { /* No expansion, do next segment. */
808 pathend = q;
809 pattern = p;
220398a0 810 while (*pattern == BG_SEP
811#ifdef DOSISH
812 || *pattern == BG_SEP2
813#endif
b8ef571c 814 ) {
815 if (pathend+1 > pathend_last)
816 return (1);
72b16652 817 *pathend++ = *pattern++;
b8ef571c 818 }
819 } else
820 /* Need expansion, recurse. */
821 return(glob3(pathbuf, pathbuf_last, pathend,
822 pathend_last, pattern, pattern_last,
823 p, pattern_last, pglob, limitp));
72b16652 824 }
825 /* NOTREACHED */
826}
827
828static int
b8ef571c 829glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
830 Char *pattern, Char *pattern_last,
831 Char *restpattern, Char *restpattern_last, glob_t *pglob, size_t *limitp)
72b16652 832{
833 register Direntry_t *dp;
834 DIR *dirp;
835 int err;
220398a0 836 int nocase;
72b16652 837 char buf[MAXPATHLEN];
838
839 /*
840 * The readdirfunc declaration can't be prototyped, because it is
841 * assigned, below, to two functions which are prototyped in glob.h
842 * and dirent.h as taking pointers to differently typed opaque
843 * structures.
844 */
cb359b41 845 Direntry_t *(*readdirfunc)(DIR*);
72b16652 846
b8ef571c 847 if (pathend > pathend_last)
848 return (1);
3e5d0dec 849 *pathend = BG_EOS;
72b16652 850 errno = 0;
851
f0963acb 852#ifdef VMS
853 {
bac331f5 854 Char *q = pathend;
855 if (q - pathbuf > 5) {
856 q -= 5;
857 if (q[0] == '.' &&
858 tolower(q[1]) == 'd' && tolower(q[2]) == 'i' &&
859 tolower(q[3]) == 'r' && q[4] == '/')
860 {
861 q[0] = '/';
862 q[1] = BG_EOS;
863 pathend = q+1;
864 }
865 }
f0963acb 866 }
867#endif
be708cc0 868
869#ifdef MACOS_TRADITIONAL
870 if ((!*pathbuf) && (g_matchVol)) {
871 FSSpec spec;
872 short index;
873 StrFileName vol_name; /* unsigned char[64] on MacOS */
874
875 err = 0;
876 nocase = ((pglob->gl_flags & GLOB_NOCASE) != 0);
877
878 /* Get and match a list of volume names */
879 for (index = 0; !GetVolInfo(index+1, true, &spec); ++index) {
880 register U8 *sc;
881 register Char *dc;
882
883 name_f_FSSpec(vol_name, &spec);
884
885 /* Initial BG_DOT must be matched literally. */
886 if (*vol_name == BG_DOT && *pattern != BG_DOT)
887 continue;
888 dc = pathend;
889 sc = (U8 *) vol_name;
890 while (dc < pathend_last && (*dc++ = *sc++) != BG_EOS)
891 ;
892 if (dc >= pathend_last) {
893 *dc = BG_EOS;
894 err = 1;
895 break;
896 }
897
898 if (!match(pathend, pattern, restpattern, nocase)) {
899 *pathend = BG_EOS;
900 continue;
901 }
902 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
903 restpattern, restpattern_last, pglob, limitp);
904 if (err)
905 break;
906 }
907 return(err);
908
909 } else { /* open dir */
910#endif /* MACOS_TRADITIONAL */
911
72b16652 912 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
913 /* TODO: don't call for ENOENT or ENOTDIR? */
914 if (pglob->gl_errfunc) {
b8ef571c 915 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
916 return (GLOB_ABEND);
72b16652 917 if (pglob->gl_errfunc(buf, errno) ||
918 (pglob->gl_flags & GLOB_ERR))
919 return (GLOB_ABEND);
920 }
921 return(0);
922 }
923
924 err = 0;
220398a0 925 nocase = ((pglob->gl_flags & GLOB_NOCASE) != 0);
72b16652 926
927 /* Search directory for matching names. */
928 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
b8ef571c 929 readdirfunc = (Direntry_t *(*)(DIR *))pglob->gl_readdir;
72b16652 930 else
4f49e16e 931 readdirfunc = my_readdir;
72b16652 932 while ((dp = (*readdirfunc)(dirp))) {
933 register U8 *sc;
934 register Char *dc;
935
3e5d0dec 936 /* Initial BG_DOT must be matched literally. */
937 if (dp->d_name[0] == BG_DOT && *pattern != BG_DOT)
72b16652 938 continue;
b8ef571c 939 dc = pathend;
940 sc = (U8 *) dp->d_name;
941 while (dc < pathend_last && (*dc++ = *sc++) != BG_EOS)
942 ;
943 if (dc >= pathend_last) {
944 *dc = BG_EOS;
945 err = 1;
946 break;
947 }
948
220398a0 949 if (!match(pathend, pattern, restpattern, nocase)) {
3e5d0dec 950 *pathend = BG_EOS;
72b16652 951 continue;
952 }
b8ef571c 953 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
954 restpattern, restpattern_last, pglob, limitp);
72b16652 955 if (err)
956 break;
957 }
958
959 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
960 (*pglob->gl_closedir)(dirp);
961 else
4f49e16e 962 PerlDir_close(dirp);
72b16652 963 return(err);
be708cc0 964
965#ifdef MACOS_TRADITIONAL
966 }
967#endif
72b16652 968}
969
970
971/*
972 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
973 * add the new item, and update gl_pathc.
974 *
975 * This assumes the BSD realloc, which only copies the block when its size
976 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
977 * behavior.
978 *
979 * Return 0 if new item added, error code if memory couldn't be allocated.
980 *
981 * Invariant of the glob_t structure:
982 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
983 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
984 */
985static int
b8ef571c 986globextend(const Char *path, glob_t *pglob, size_t *limitp)
72b16652 987{
988 register char **pathv;
989 register int i;
b8ef571c 990 STRLEN newsize, len;
72b16652 991 char *copy;
992 const Char *p;
993
994#ifdef GLOB_DEBUG
995 printf("Adding ");
996 for (p = path; *p; p++)
997 (void)printf("%c", CHAR(*p));
998 printf("\n");
3e5d0dec 999#endif /* GLOB_DEBUG */
72b16652 1000
b8ef571c 1001 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
4f49e16e 1002 if (pglob->gl_pathv)
b8ef571c 1003 pathv = Renew(pglob->gl_pathv,newsize,char*);
4f49e16e 1004 else
b8ef571c 1005 New(0,pathv,newsize,char*);
1006 if (pathv == NULL) {
1007 if (pglob->gl_pathv) {
1008 Safefree(pglob->gl_pathv);
1009 pglob->gl_pathv = NULL;
1010 }
72b16652 1011 return(GLOB_NOSPACE);
b8ef571c 1012 }
72b16652 1013
1014 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
1015 /* first time around -- clear initial gl_offs items */
1016 pathv += pglob->gl_offs;
1017 for (i = pglob->gl_offs; --i >= 0; )
1018 *--pathv = NULL;
1019 }
1020 pglob->gl_pathv = pathv;
1021
1022 for (p = path; *p++;)
b8ef571c 1023 ;
1024 len = (STRLEN)(p - path);
bac331f5 1025 *limitp += len;
4f49e16e 1026 New(0, copy, p-path, char);
1027 if (copy != NULL) {
b8ef571c 1028 if (g_Ctoc(path, copy, len)) {
1029 Safefree(copy);
1030 return(GLOB_NOSPACE);
1031 }
72b16652 1032 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
1033 }
1034 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
b8ef571c 1035
1036 if ((pglob->gl_flags & GLOB_LIMIT) &&
1037 newsize + *limitp >= ARG_MAX) {
1038 errno = 0;
1039 return(GLOB_NOSPACE);
1040 }
bac331f5 1041
72b16652 1042 return(copy == NULL ? GLOB_NOSPACE : 0);
1043}
1044
1045
1046/*
1047 * pattern matching function for filenames. Each occurrence of the *
1048 * pattern causes a recursion level.
1049 */
1050static int
220398a0 1051match(register Char *name, register Char *pat, register Char *patend, int nocase)
72b16652 1052{
1053 int ok, negate_range;
1054 Char c, k;
1055
1056 while (pat < patend) {
1057 c = *pat++;
1058 switch (c & M_MASK) {
1059 case M_ALL:
1060 if (pat == patend)
1061 return(1);
1062 do
220398a0 1063 if (match(name, pat, patend, nocase))
72b16652 1064 return(1);
b8ef571c 1065 while (*name++ != BG_EOS)
1066 ;
72b16652 1067 return(0);
1068 case M_ONE:
3e5d0dec 1069 if (*name++ == BG_EOS)
72b16652 1070 return(0);
1071 break;
1072 case M_SET:
1073 ok = 0;
3e5d0dec 1074 if ((k = *name++) == BG_EOS)
72b16652 1075 return(0);
3e5d0dec 1076 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != BG_EOS)
72b16652 1077 ++pat;
1078 while (((c = *pat++) & M_MASK) != M_END)
1079 if ((*pat & M_MASK) == M_RNG) {
220398a0 1080 if (nocase) {
1081 if (tolower(c) <= tolower(k) && tolower(k) <= tolower(pat[1]))
1082 ok = 1;
1083 } else {
1084 if (c <= k && k <= pat[1])
1085 ok = 1;
1086 }
72b16652 1087 pat += 2;
220398a0 1088 } else if (nocase ? (tolower(c) == tolower(k)) : (c == k))
72b16652 1089 ok = 1;
1090 if (ok == negate_range)
1091 return(0);
1092 break;
1093 default:
220398a0 1094 k = *name++;
1095 if (nocase ? (tolower(k) != tolower(c)) : (k != c))
72b16652 1096 return(0);
1097 break;
1098 }
1099 }
3e5d0dec 1100 return(*name == BG_EOS);
72b16652 1101}
1102
1103/* Free allocated data belonging to a glob_t structure. */
1104void
1105bsd_globfree(glob_t *pglob)
1106{
1107 register int i;
1108 register char **pp;
1109
1110 if (pglob->gl_pathv != NULL) {
1111 pp = pglob->gl_pathv + pglob->gl_offs;
1112 for (i = pglob->gl_pathc; i--; ++pp)
1113 if (*pp)
4f49e16e 1114 Safefree(*pp);
1115 Safefree(pglob->gl_pathv);
b8ef571c 1116 pglob->gl_pathv = NULL;
72b16652 1117 }
1118}
1119
1120static DIR *
1121g_opendir(register Char *str, glob_t *pglob)
1122{
1123 char buf[MAXPATHLEN];
1124
7369a524 1125 if (!*str) {
1126#ifdef MACOS_TRADITIONAL
1127 strcpy(buf, ":");
1128#else
72b16652 1129 strcpy(buf, ".");
7369a524 1130#endif
1131 } else {
b8ef571c 1132 if (g_Ctoc(str, buf, sizeof(buf)))
1133 return(NULL);
7369a524 1134 }
72b16652 1135
1136 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1137 return((*pglob->gl_opendir)(buf));
bac331f5 1138
1139 return(PerlDir_open(buf));
72b16652 1140}
1141
72b16652 1142static int
1143g_lstat(register Char *fn, Stat_t *sb, glob_t *pglob)
1144{
1145 char buf[MAXPATHLEN];
1146
b8ef571c 1147 if (g_Ctoc(fn, buf, sizeof(buf)))
1148 return(-1);
72b16652 1149 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1150 return((*pglob->gl_lstat)(buf, sb));
4f49e16e 1151#ifdef HAS_LSTAT
1152 return(PerlLIO_lstat(buf, sb));
1153#else
1154 return(PerlLIO_stat(buf, sb));
72b16652 1155#endif /* HAS_LSTAT */
4f49e16e 1156}
72b16652 1157
1158static int
1159g_stat(register Char *fn, Stat_t *sb, glob_t *pglob)
1160{
1161 char buf[MAXPATHLEN];
1162
b8ef571c 1163 if (g_Ctoc(fn, buf, sizeof(buf)))
1164 return(-1);
72b16652 1165 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
1166 return((*pglob->gl_stat)(buf, sb));
4f49e16e 1167 return(PerlLIO_stat(buf, sb));
72b16652 1168}
1169
1170static Char *
1171g_strchr(Char *str, int ch)
1172{
1173 do {
1174 if (*str == ch)
1175 return (str);
1176 } while (*str++);
1177 return (NULL);
1178}
1179
b8ef571c 1180static int
1181g_Ctoc(register const Char *str, char *buf, STRLEN len)
72b16652 1182{
b8ef571c 1183 while (len--) {
7c436af3 1184 if ((*buf++ = (char)*str++) == BG_EOS)
b8ef571c 1185 return (0);
1186 }
1187 return (1);
72b16652 1188}
1189
1190#ifdef GLOB_DEBUG
1191static void
1192qprintf(const char *str, register Char *s)
1193{
1194 register Char *p;
1195
1196 (void)printf("%s:\n", str);
1197 for (p = s; *p; p++)
1198 (void)printf("%c", CHAR(*p));
1199 (void)printf("\n");
1200 for (p = s; *p; p++)
1201 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
1202 (void)printf("\n");
1203 for (p = s; *p; p++)
1204 (void)printf("%c", ismeta(*p) ? '_' : ' ');
1205 (void)printf("\n");
1206}
1207#endif /* GLOB_DEBUG */
be708cc0 1208
1209
1210#ifdef MACOS_TRADITIONAL
1211
fb8eeed8 1212/* Replace the last occurrence of the pattern ":[^:]+::", e.g. ":lib::",
be708cc0 1213 with a single ':', if possible. It is not an error, if the pattern
1214 doesn't match (we return -1), but if there are two consecutive colons
1215 '::', there must be a preceding ':[^:]+'. Hence, a volume path like
1216 "HD::" is considered to be an error (we return 1), that is, it can't
1217 be resolved. We return 0 on success.
1218*/
1219
1220static short
1221updir(char *path)
1222{
1223 char *pb, *pe, *lastchar;
1224 char *bgn_mark, *end_mark;
1225 char *f, *m, *b; /* front, middle, back */
1226 size_t len;
1227
1228 len = strlen(path);
1229 lastchar = path + (len-1);
1230 b = lastchar;
1231 m = lastchar-1;
1232 f = lastchar-2;
1233
1234 /* find a '[^:]::' (e.g. b::) pattern ... */
1235 while ( !( (*f != BG_SEP) && (*m == BG_SEP) && (*b == BG_SEP) )
1236 && (f >= path)) {
1237 f--;
1238 m--;
1239 b--;
1240 }
1241
1242 if (f < path) { /* no (more) match */
1243 return -1;
1244 }
1245
1246 end_mark = b;
1247
1248 /* ... and now find its preceding colon ':' */
1249 while ((*f != BG_SEP) && (f >= path)) {
1250 f--;
1251 }
1252 if (f < path) {
1253 /* No preceding colon found, must be a
1254 volume path. We can't move up the
1255 tree and that's an error */
1256 return 1;
1257 }
1258 bgn_mark = f;
1259
1260 /* Shrink path, i.e. exclude all characters between
1261 bgn_mark and end_mark */
1262
1263 pb = bgn_mark;
1264 pe = end_mark;
1265 while (*pb++ = *pe++) ;
1266 return 0;
1267}
1268
1269
1270/* Resolve all updirs in pattern. */
1271
1272static short
1273resolve_updirs(char *new_pattern)
1274{
1275 short err;
1276
1277 do {
1278 err = updir(new_pattern);
1279 } while (!err);
1280 if (err == 1) {
1281 return NO_UPDIR_ERR;
1282 }
1283 return 0;
1284}
1285
1286
1287/* Remove a trailing colon from the path, but only if it's
1288 not a volume path (e.g. HD:) and not a path consisting
1289 solely of colons. */
1290
1291static void
1292remove_trColon(char *path)
1293{
1294 char *lastchar, *lc;
1295
1296 /* if path matches the pattern /:[^:]+:$/, we can
1297 remove the trailing ':' */
1298
1299 lc = lastchar = path + (strlen(path) - 1);
1300 if (*lastchar == BG_SEP) {
1301 /* there's a trailing ':', there must be at least
1302 one preceding char != ':' and a preceding ':' */
1303 lc--;
1304 if ((*lc != BG_SEP) && (lc >= path)) {
1305 lc--;
1306 } else {
1307 return;
1308 }
1309 while ((*lc != BG_SEP) && (lc >= path)) {
1310 lc--;
1311 }
1312 if (lc >= path) {
1313 /* ... there's a preceding ':', we remove
1314 the trailing colon */
1315 *lastchar = BG_EOS;
1316 }
1317 }
1318}
1319
1320
1321/* With the GLOB_MARK flag on, we append a colon, if pathbuf
1322 is a directory. If the directory name contains no colons,
1323 e.g. 'lib', we can't simply append a ':', since this (e.g.
1324 'lib:') is not a valid (relative) path on Mac OS. Instead,
1325 we add a leading _and_ trailing ':'. */
1326
1327static short
1328glob_mark_Mac(Char *pathbuf, Char *pathend, Char *pathend_last)
1329{
1330 Char *p, *pe;
1331 Boolean is_file = true;
1332
1333 /* check if pathbuf contains a ':',
1334 i.e. is not a file name */
1335 p = pathbuf;
1336 while (*p != BG_EOS) {
1337 if (*p == BG_SEP) {
1338 is_file = false;
1339 break;
1340 }
1341 p++;
1342 }
1343
1344 if (is_file) {
1345 if (pathend+2 > pathend_last) {
1346 return (1);
1347 }
1348 /* right shift one char */
1349 pe = p = pathend;
1350 p--;
1351 pathend++;
1352 while (p >= pathbuf) {
1353 *pe-- = *p--;
1354 }
1355 /* first char becomes a colon */
1356 *pathbuf = BG_SEP;
1357 /* append a colon */
1358 *pathend++ = BG_SEP;
1359 *pathend = BG_EOS;
1360
1361 } else {
1362 if (pathend+1 > pathend_last) {
1363 return (1);
1364 }
1365 *pathend++ = BG_SEP;
1366 *pathend = BG_EOS;
1367 }
1368 return 0;
1369}
1370
1371
1372/* Return a FSSpec record for the specified volume
1373 (borrowed from MacPerl.xs). */
1374
1375static OSErr
1376GetVolInfo(short volume, Boolean indexed, FSSpec* spec)
1377{
1378 OSErr err; /* OSErr: 16-bit integer */
1379 HParamBlockRec pb;
1380
1381 pb.volumeParam.ioNamePtr = spec->name;
1382 pb.volumeParam.ioVRefNum = indexed ? 0 : volume;
1383 pb.volumeParam.ioVolIndex = indexed ? volume : 0;
1384
1385 if (err = PBHGetVInfoSync(&pb))
1386 return err;
1387
1388 spec->vRefNum = pb.volumeParam.ioVRefNum;
1389 spec->parID = 1;
1390
1391 return noErr; /* 0 */
1392}
1393
1394/* Extract a C name from a FSSpec. Note that there are
1395 no leading or trailing colons. */
1396
1397static void
1398name_f_FSSpec(StrFileName name, FSSpec *spec)
1399{
1400 unsigned char *nc;
1401 const short len = spec->name[0];
1402 short i;
1403
1404 /* FSSpec.name is a Pascal string,
1405 convert it to C ... */
1406 nc = name;
1407 for (i=1; i<=len; i++) {
1408 *nc++ = spec->name[i];
1409 }
1410 *nc = BG_EOS;
1411}
1412
1413#endif /* MACOS_TRADITIONAL */