Home | History | Annotate | Line # | Download | only in ksh
misc.c revision 1.15.34.1
      1  1.15.34.1  pgoyette /*	$NetBSD: misc.c,v 1.15.34.1 2017/05/02 03:19:14 pgoyette Exp $	*/
      2        1.2       tls 
      3        1.1       jtc /*
      4        1.1       jtc  * Miscellaneous functions
      5        1.1       jtc  */
      6        1.8       agc #include <sys/cdefs.h>
      7        1.8       agc 
      8        1.8       agc #ifndef lint
      9  1.15.34.1  pgoyette __RCSID("$NetBSD: misc.c,v 1.15.34.1 2017/05/02 03:19:14 pgoyette Exp $");
     10        1.8       agc #endif
     11        1.8       agc 
     12        1.1       jtc 
     13        1.1       jtc #include "sh.h"
     14        1.1       jtc #include <ctype.h>	/* for FILECHCONV */
     15        1.1       jtc #ifdef HAVE_LIMITS_H
     16        1.1       jtc # include <limits.h>
     17        1.1       jtc #endif
     18        1.1       jtc 
     19        1.1       jtc #ifndef UCHAR_MAX
     20        1.1       jtc # define UCHAR_MAX	0xFF
     21        1.1       jtc #endif
     22        1.1       jtc 
     23        1.1       jtc short ctypes [UCHAR_MAX+1];	/* type bits for unsigned char */
     24        1.1       jtc 
     25        1.1       jtc static int	do_gmatch ARGS((const unsigned char *s, const unsigned char *p,
     26        1.1       jtc 			const unsigned char *se, const unsigned char *pe,
     27        1.1       jtc 			int isfile));
     28        1.1       jtc static const unsigned char *cclass ARGS((const unsigned char *p, int sub));
     29        1.1       jtc 
     30        1.1       jtc /*
     31        1.1       jtc  * Fast character classes
     32        1.1       jtc  */
     33        1.1       jtc void
     34        1.1       jtc setctypes(s, t)
     35        1.1       jtc 	register const char *s;
     36        1.1       jtc 	register int t;
     37        1.1       jtc {
     38        1.1       jtc 	register int i;
     39        1.1       jtc 
     40        1.1       jtc 	if (t & C_IFS) {
     41        1.1       jtc 		for (i = 0; i < UCHAR_MAX+1; i++)
     42        1.1       jtc 			ctypes[i] &= ~C_IFS;
     43        1.1       jtc 		ctypes[0] |= C_IFS; /* include \0 in C_IFS */
     44        1.1       jtc 	}
     45        1.1       jtc 	while (*s != 0)
     46        1.1       jtc 		ctypes[(unsigned char) *s++] |= t;
     47        1.1       jtc }
     48        1.1       jtc 
     49        1.1       jtc void
     50        1.1       jtc initctypes()
     51        1.1       jtc {
     52        1.1       jtc 	register int c;
     53        1.1       jtc 
     54        1.1       jtc 	for (c = 'a'; c <= 'z'; c++)
     55        1.1       jtc 		ctypes[c] |= C_ALPHA;
     56        1.1       jtc 	for (c = 'A'; c <= 'Z'; c++)
     57        1.1       jtc 		ctypes[c] |= C_ALPHA;
     58        1.1       jtc 	ctypes['_'] |= C_ALPHA;
     59        1.1       jtc 	setctypes("0123456789", C_DIGIT);
     60        1.1       jtc 	setctypes(" \t\n|&;<>()", C_LEX1); /* \0 added automatically */
     61        1.1       jtc 	setctypes("*@#!$-?", C_VAR1);
     62        1.1       jtc 	setctypes(" \t\n", C_IFSWS);
     63        1.1       jtc 	setctypes("=-+?", C_SUBOP1);
     64        1.1       jtc 	setctypes("#%", C_SUBOP2);
     65        1.1       jtc 	setctypes(" \n\t\"#$&'()*;<>?[\\`|", C_QUOTE);
     66        1.1       jtc }
     67        1.1       jtc 
     68        1.1       jtc /* convert unsigned long to base N string */
     69        1.1       jtc 
     70        1.1       jtc char *
     71        1.1       jtc ulton(n, base)
     72        1.1       jtc 	register unsigned long n;
     73        1.1       jtc 	int base;
     74        1.1       jtc {
     75        1.1       jtc 	register char *p;
     76        1.1       jtc 	static char buf [20];
     77        1.1       jtc 
     78        1.1       jtc 	p = &buf[sizeof(buf)];
     79        1.1       jtc 	*--p = '\0';
     80        1.1       jtc 	do {
     81        1.1       jtc 		*--p = "0123456789ABCDEF"[n%base];
     82        1.1       jtc 		n /= base;
     83        1.1       jtc 	} while (n != 0);
     84        1.1       jtc 	return p;
     85        1.1       jtc }
     86        1.1       jtc 
     87        1.1       jtc char *
     88        1.1       jtc str_save(s, ap)
     89        1.1       jtc 	register const char *s;
     90        1.1       jtc 	Area *ap;
     91        1.1       jtc {
     92        1.9   mycroft 	size_t len;
     93        1.9   mycroft 	char *p;
     94        1.9   mycroft 
     95        1.9   mycroft 	if (!s)
     96        1.9   mycroft 		return NULL;
     97        1.9   mycroft 	len = strlen(s)+1;
     98        1.9   mycroft 	p = alloc(len, ap);
     99       1.14     seanb 	strlcpy(p, s, len);
    100        1.9   mycroft 	return (p);
    101        1.1       jtc }
    102        1.1       jtc 
    103        1.1       jtc /* Allocate a string of size n+1 and copy upto n characters from the possibly
    104        1.1       jtc  * null terminated string s into it.  Always returns a null terminated string
    105        1.1       jtc  * (unless n < 0).
    106        1.1       jtc  */
    107        1.1       jtc char *
    108        1.1       jtc str_nsave(s, n, ap)
    109        1.1       jtc 	register const char *s;
    110        1.1       jtc 	int n;
    111        1.1       jtc 	Area *ap;
    112        1.1       jtc {
    113        1.1       jtc 	char *ns;
    114        1.1       jtc 
    115        1.1       jtc 	if (n < 0)
    116        1.1       jtc 		return 0;
    117        1.1       jtc 	ns = alloc(n + 1, ap);
    118        1.1       jtc 	ns[0] = '\0';
    119        1.1       jtc 	return strncat(ns, s, n);
    120        1.1       jtc }
    121        1.1       jtc 
    122        1.1       jtc /* called from expand.h:XcheckN() to grow buffer */
    123        1.1       jtc char *
    124        1.1       jtc Xcheck_grow_(xsp, xp, more)
    125        1.1       jtc 	XString *xsp;
    126        1.1       jtc 	char *xp;
    127        1.1       jtc 	int more;
    128        1.1       jtc {
    129        1.1       jtc 	char *old_beg = xsp->beg;
    130        1.1       jtc 
    131       1.13     lukem 	xsp->len += (size_t)more > xsp->len ? (size_t)more : xsp->len;
    132        1.1       jtc 	xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap);
    133        1.1       jtc 	xsp->end = xsp->beg + xsp->len;
    134        1.1       jtc 	return xsp->beg + (xp - old_beg);
    135        1.1       jtc }
    136        1.1       jtc 
    137       1.12  christos const struct option goptions[] = {
    138        1.1       jtc 	/* Special cases (see parse_args()): -A, -o, -s.
    139        1.1       jtc 	 * Options are sorted by their longnames - the order of these
    140        1.1       jtc 	 * entries MUST match the order of sh_flag F* enumerations in sh.h.
    141        1.1       jtc 	 */
    142        1.1       jtc 	{ "allexport",	'a',		OF_ANY },
    143        1.1       jtc #ifdef BRACE_EXPAND
    144        1.1       jtc 	{ "braceexpand",  0,		OF_ANY }, /* non-standard */
    145        1.1       jtc #endif
    146        1.1       jtc 	{ "bgnice",	  0,		OF_ANY },
    147        1.4   hubertf 	{ (char *) 0, 	'c',	    OF_CMDLINE },
    148        1.1       jtc #ifdef EMACS
    149        1.1       jtc 	{ "emacs",	  0,		OF_ANY },
    150        1.9   mycroft 	{ "emacs-usemeta",  0,		OF_ANY }, /* non-standard */
    151        1.1       jtc #endif
    152        1.1       jtc 	{ "errexit",	'e',		OF_ANY },
    153        1.1       jtc #ifdef EMACS
    154        1.1       jtc 	{ "gmacs",	  0,		OF_ANY },
    155        1.1       jtc #endif
    156        1.1       jtc 	{ "ignoreeof",	  0,		OF_ANY },
    157        1.1       jtc 	{ "interactive",'i',	    OF_CMDLINE },
    158        1.1       jtc 	{ "keyword",	'k',		OF_ANY },
    159        1.1       jtc 	{ "login",	'l',	    OF_CMDLINE },
    160        1.1       jtc 	{ "markdirs",	'X',		OF_ANY },
    161        1.1       jtc #ifdef JOBS
    162        1.1       jtc 	{ "monitor",	'm',		OF_ANY },
    163        1.1       jtc #else /* JOBS */
    164        1.4   hubertf 	{ (char *) 0,	'm',		     0 }, /* so FMONITOR not ifdef'd */
    165        1.1       jtc #endif /* JOBS */
    166        1.1       jtc 	{ "noclobber",	'C',		OF_ANY },
    167        1.1       jtc 	{ "noexec",	'n',		OF_ANY },
    168        1.1       jtc 	{ "noglob",	'f',		OF_ANY },
    169        1.1       jtc 	{ "nohup",	  0,		OF_ANY },
    170        1.1       jtc 	{ "nolog",	  0,		OF_ANY }, /* no effect */
    171        1.1       jtc #ifdef	JOBS
    172        1.1       jtc 	{ "notify",	'b',		OF_ANY },
    173        1.1       jtc #endif	/* JOBS */
    174        1.1       jtc 	{ "nounset",	'u',		OF_ANY },
    175        1.1       jtc 	{ "physical",	  0,		OF_ANY }, /* non-standard */
    176        1.1       jtc 	{ "posix",	  0,		OF_ANY }, /* non-standard */
    177        1.1       jtc 	{ "privileged",	'p',		OF_ANY },
    178        1.1       jtc 	{ "restricted",	'r',	    OF_CMDLINE },
    179        1.1       jtc 	{ "stdin",	's',	    OF_CMDLINE }, /* pseudo non-standard */
    180        1.1       jtc 	{ "trackall",	'h',		OF_ANY },
    181        1.1       jtc 	{ "verbose",	'v',		OF_ANY },
    182        1.1       jtc #ifdef VI
    183        1.1       jtc 	{ "vi",		  0,		OF_ANY },
    184        1.1       jtc 	{ "viraw",	  0,		OF_ANY }, /* no effect */
    185        1.1       jtc 	{ "vi-show8",	  0,		OF_ANY }, /* non-standard */
    186        1.1       jtc 	{ "vi-tabcomplete",  0, 	OF_ANY }, /* non-standard */
    187        1.1       jtc 	{ "vi-esccomplete",  0, 	OF_ANY }, /* non-standard */
    188        1.1       jtc #endif
    189        1.1       jtc 	{ "xtrace",	'x',		OF_ANY },
    190        1.4   hubertf 	/* Anonymous flags: used internally by shell only
    191        1.9   mycroft 	 * (not visible to user)
    192        1.4   hubertf 	 */
    193        1.4   hubertf 	{ (char *) 0,	0,		OF_INTERNAL }, /* FTALKING_I */
    194        1.1       jtc };
    195        1.1       jtc 
    196        1.1       jtc /*
    197        1.1       jtc  * translate -o option into F* constant (also used for test -o option)
    198        1.1       jtc  */
    199        1.1       jtc int
    200        1.1       jtc option(n)
    201        1.1       jtc 	const char *n;
    202        1.1       jtc {
    203        1.1       jtc 	int i;
    204        1.1       jtc 
    205       1.13     lukem 	for (i = 0; i < (int)NELEM(goptions); i++)
    206       1.12  christos 		if (goptions[i].name && strcmp(goptions[i].name, n) == 0)
    207        1.1       jtc 			return i;
    208        1.1       jtc 
    209        1.1       jtc 	return -1;
    210        1.1       jtc }
    211        1.1       jtc 
    212        1.1       jtc struct options_info {
    213        1.1       jtc 	int opt_width;
    214        1.1       jtc 	struct {
    215        1.1       jtc 		const char *name;
    216        1.1       jtc 		int	flag;
    217       1.12  christos 	} opts[NELEM(goptions)];
    218        1.1       jtc };
    219        1.1       jtc 
    220        1.1       jtc static char *options_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
    221        1.1       jtc static void printoptions ARGS((int verbose));
    222        1.1       jtc 
    223        1.1       jtc /* format a single select menu item */
    224        1.1       jtc static char *
    225        1.1       jtc options_fmt_entry(arg, i, buf, buflen)
    226        1.1       jtc 	void *arg;
    227        1.1       jtc 	int i;
    228        1.1       jtc 	char *buf;
    229        1.1       jtc 	int buflen;
    230        1.1       jtc {
    231        1.1       jtc 	struct options_info *oi = (struct options_info *) arg;
    232        1.1       jtc 
    233        1.1       jtc 	shf_snprintf(buf, buflen, "%-*s %s",
    234        1.1       jtc 		oi->opt_width, oi->opts[i].name,
    235        1.1       jtc 		Flag(oi->opts[i].flag) ? "on" : "off");
    236        1.1       jtc 	return buf;
    237        1.1       jtc }
    238        1.1       jtc 
    239        1.1       jtc static void
    240        1.1       jtc printoptions(verbose)
    241        1.1       jtc 	int verbose;
    242        1.1       jtc {
    243        1.1       jtc 	int i;
    244        1.1       jtc 
    245        1.1       jtc 	if (verbose) {
    246        1.1       jtc 		struct options_info oi;
    247        1.1       jtc 		int n, len;
    248        1.1       jtc 
    249        1.1       jtc 		/* verbose version */
    250        1.1       jtc 		shprintf("Current option settings\n");
    251        1.1       jtc 
    252       1.13     lukem 		for (i = n = oi.opt_width = 0; i < (int)NELEM(goptions); i++)
    253       1.12  christos 			if (goptions[i].name) {
    254       1.12  christos 				len = strlen(goptions[i].name);
    255       1.12  christos 				oi.opts[n].name = goptions[i].name;
    256        1.1       jtc 				oi.opts[n++].flag = i;
    257        1.1       jtc 				if (len > oi.opt_width)
    258        1.1       jtc 					oi.opt_width = len;
    259        1.1       jtc 			}
    260        1.1       jtc 		print_columns(shl_stdout, n, options_fmt_entry, &oi,
    261        1.6    provos 			      oi.opt_width + 5, 1);
    262        1.1       jtc 	} else {
    263        1.1       jtc 		/* short version ala ksh93 */
    264        1.1       jtc 		shprintf("set");
    265       1.13     lukem 		for (i = 0; i < (int)NELEM(goptions); i++)
    266       1.12  christos 			if (Flag(i) && goptions[i].name)
    267       1.12  christos 				shprintf(" -o %s", goptions[i].name);
    268       1.15     joerg 		shprintf("%s", newline);
    269        1.1       jtc 	}
    270        1.1       jtc }
    271        1.1       jtc 
    272        1.1       jtc char *
    273        1.1       jtc getoptions()
    274        1.1       jtc {
    275       1.13     lukem 	size_t i;
    276        1.4   hubertf 	char m[(int) FNFLAGS + 1];
    277        1.1       jtc 	register char *cp = m;
    278        1.1       jtc 
    279       1.12  christos 	for (i = 0; i < NELEM(goptions); i++)
    280       1.12  christos 		if (goptions[i].c && Flag(i))
    281       1.12  christos 			*cp++ = goptions[i].c;
    282        1.1       jtc 	*cp = 0;
    283        1.1       jtc 	return str_save(m, ATEMP);
    284        1.1       jtc }
    285        1.1       jtc 
    286        1.1       jtc /* change a Flag(*) value; takes care of special actions */
    287        1.1       jtc void
    288        1.1       jtc change_flag(f, what, newval)
    289        1.1       jtc 	enum sh_flag f;	/* flag to change */
    290        1.1       jtc 	int what;	/* what is changing the flag (command line vs set) */
    291        1.1       jtc 	int newval;
    292        1.1       jtc {
    293        1.1       jtc 	int oldval;
    294        1.1       jtc 
    295        1.1       jtc 	oldval = Flag(f);
    296        1.1       jtc 	Flag(f) = newval;
    297        1.1       jtc #ifdef JOBS
    298        1.1       jtc 	if (f == FMONITOR) {
    299        1.1       jtc 		if (what != OF_CMDLINE && newval != oldval)
    300        1.1       jtc 			j_change();
    301        1.1       jtc 	} else
    302        1.1       jtc #endif /* JOBS */
    303        1.1       jtc #ifdef EDIT
    304        1.1       jtc 	if (0
    305        1.1       jtc # ifdef VI
    306        1.1       jtc 	    || f == FVI
    307        1.1       jtc # endif /* VI */
    308        1.1       jtc # ifdef EMACS
    309        1.1       jtc 	    || f == FEMACS || f == FGMACS
    310        1.1       jtc # endif /* EMACS */
    311        1.1       jtc 	   )
    312        1.1       jtc 	{
    313        1.1       jtc 		if (newval) {
    314        1.1       jtc # ifdef VI
    315        1.1       jtc 			Flag(FVI) = 0;
    316        1.1       jtc # endif /* VI */
    317        1.1       jtc # ifdef EMACS
    318        1.1       jtc 			Flag(FEMACS) = Flag(FGMACS) = 0;
    319        1.1       jtc # endif /* EMACS */
    320        1.1       jtc 			Flag(f) = newval;
    321        1.1       jtc 		}
    322        1.1       jtc 	} else
    323        1.1       jtc #endif /* EDIT */
    324        1.1       jtc 	/* Turning off -p? */
    325        1.1       jtc 	if (f == FPRIVILEGED && oldval && !newval) {
    326        1.1       jtc #ifdef OS2
    327        1.1       jtc 		;
    328        1.1       jtc #else /* OS2 */
    329        1.9   mycroft 		seteuid(ksheuid = getuid());
    330        1.9   mycroft 		setuid(ksheuid);
    331        1.9   mycroft 		setegid(getgid());
    332        1.1       jtc 		setgid(getgid());
    333        1.1       jtc #endif /* OS2 */
    334        1.1       jtc 	} else if (f == FPOSIX && newval) {
    335        1.1       jtc #ifdef BRACE_EXPAND
    336        1.1       jtc 		Flag(FBRACEEXPAND) = 0
    337        1.1       jtc #endif /* BRACE_EXPAND */
    338        1.1       jtc 		;
    339        1.1       jtc 	}
    340        1.4   hubertf 	/* Changing interactive flag? */
    341        1.4   hubertf 	if (f == FTALKING) {
    342        1.4   hubertf 		if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
    343        1.4   hubertf 			Flag(FTALKING_I) = newval;
    344        1.4   hubertf 	}
    345        1.1       jtc }
    346        1.1       jtc 
    347        1.1       jtc /* parse command line & set command arguments.  returns the index of
    348        1.1       jtc  * non-option arguments, -1 if there is an error.
    349        1.1       jtc  */
    350        1.1       jtc int
    351        1.1       jtc parse_args(argv, what, setargsp)
    352        1.1       jtc 	char **argv;
    353        1.1       jtc 	int	what;		/* OF_CMDLINE or OF_SET */
    354        1.1       jtc 	int	*setargsp;
    355        1.1       jtc {
    356       1.12  christos 	static char cmd_opts[NELEM(goptions) + 3]; /* o:\0 */
    357       1.12  christos 	static char set_opts[NELEM(goptions) + 5]; /* Ao;s\0 */
    358        1.1       jtc 	char *opts;
    359        1.4   hubertf 	char *array = (char *) 0;
    360        1.1       jtc 	Getopt go;
    361        1.1       jtc 	int i, optc, set, sortargs = 0, arrayset = 0;
    362        1.1       jtc 
    363        1.1       jtc 	/* First call?  Build option strings... */
    364        1.1       jtc 	if (cmd_opts[0] == '\0') {
    365        1.4   hubertf 		char *p, *q;
    366        1.1       jtc 
    367        1.9   mycroft 		/* see cmd_opts[] declaration */
    368        1.9   mycroft 		strlcpy(cmd_opts, "o:", sizeof cmd_opts);
    369        1.1       jtc 		p = cmd_opts + strlen(cmd_opts);
    370        1.9   mycroft 		/* see set_opts[] declaration */
    371        1.9   mycroft 		strlcpy(set_opts, "A:o;s", sizeof set_opts);
    372        1.4   hubertf 		q = set_opts + strlen(set_opts);
    373       1.13     lukem 		for (i = 0; i < (int)NELEM(goptions); i++) {
    374       1.12  christos 			if (goptions[i].c) {
    375       1.12  christos 				if (goptions[i].flags & OF_CMDLINE)
    376       1.12  christos 					*p++ = goptions[i].c;
    377       1.12  christos 				if (goptions[i].flags & OF_SET)
    378       1.12  christos 					*q++ = goptions[i].c;
    379        1.4   hubertf 			}
    380        1.4   hubertf 		}
    381        1.1       jtc 		*p = '\0';
    382        1.4   hubertf 		*q = '\0';
    383        1.1       jtc 	}
    384        1.1       jtc 
    385        1.1       jtc 	if (what == OF_CMDLINE) {
    386        1.1       jtc 		char *p;
    387        1.1       jtc 		/* Set FLOGIN before parsing options so user can clear
    388        1.1       jtc 		 * flag using +l.
    389        1.1       jtc 		 */
    390        1.1       jtc 		Flag(FLOGIN) = (argv[0][0] == '-'
    391        1.1       jtc 				|| ((p = ksh_strrchr_dirsep(argv[0]))
    392        1.1       jtc 				     && *++p == '-'));
    393        1.1       jtc 		opts = cmd_opts;
    394        1.1       jtc 	} else
    395        1.1       jtc 		opts = set_opts;
    396        1.1       jtc 	ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT);
    397        1.1       jtc 	while ((optc = ksh_getopt(argv, &go, opts)) != EOF) {
    398        1.1       jtc 		set = (go.info & GI_PLUS) ? 0 : 1;
    399        1.1       jtc 		switch (optc) {
    400        1.1       jtc 		  case 'A':
    401        1.1       jtc 			arrayset = set ? 1 : -1;
    402        1.4   hubertf 			array = go.optarg;
    403        1.1       jtc 			break;
    404        1.1       jtc 
    405        1.1       jtc 		  case 'o':
    406        1.1       jtc 			if (go.optarg == (char *) 0) {
    407        1.1       jtc 				/* lone -o: print options
    408        1.1       jtc 				 *
    409        1.1       jtc 				 * Note that on the command line, -o requires
    410        1.1       jtc 				 * an option (ie, can't get here if what is
    411        1.1       jtc 				 * OF_CMDLINE).
    412        1.1       jtc 				 */
    413        1.1       jtc 				printoptions(set);
    414        1.1       jtc 				break;
    415        1.1       jtc 			}
    416        1.1       jtc 			i = option(go.optarg);
    417        1.1       jtc 			if (i >= 0 && set == Flag(i))
    418        1.1       jtc 				/* Don't check the context if the flag
    419        1.1       jtc 				 * isn't changing - makes "set -o interactive"
    420        1.1       jtc 				 * work if you're already interactive.  Needed
    421        1.1       jtc 				 * if the output of "set +o" is to be used.
    422        1.1       jtc 				 */
    423        1.1       jtc 				;
    424       1.12  christos 			else if (i >= 0 && (goptions[i].flags & what))
    425        1.1       jtc 				change_flag((enum sh_flag) i, what, set);
    426        1.1       jtc 			else {
    427        1.1       jtc 				bi_errorf("%s: bad option", go.optarg);
    428        1.1       jtc 				return -1;
    429        1.1       jtc 			}
    430        1.1       jtc 			break;
    431        1.1       jtc 
    432        1.1       jtc 		  case '?':
    433        1.1       jtc 			return -1;
    434        1.1       jtc 
    435        1.1       jtc 		  default:
    436        1.1       jtc 			/* -s: sort positional params (at&t ksh stupidity) */
    437        1.1       jtc 			if (what == OF_SET && optc == 's') {
    438        1.1       jtc 				sortargs = 1;
    439        1.1       jtc 				break;
    440        1.1       jtc 			}
    441       1.13     lukem 			for (i = 0; i < (int)NELEM(goptions); i++)
    442       1.12  christos 				if (optc == goptions[i].c
    443       1.12  christos 				    && (what & goptions[i].flags))
    444        1.1       jtc 				{
    445        1.1       jtc 					change_flag((enum sh_flag) i, what,
    446        1.1       jtc 						    set);
    447        1.1       jtc 					break;
    448        1.1       jtc 				}
    449       1.12  christos 			if (i == NELEM(goptions)) {
    450        1.1       jtc 				internal_errorf(1, "parse_args: `%c'", optc);
    451        1.1       jtc 				return -1; /* not reached */
    452        1.1       jtc 			}
    453        1.1       jtc 		}
    454        1.1       jtc 	}
    455        1.1       jtc 	if (!(go.info & GI_MINUSMINUS) && argv[go.optind]
    456        1.1       jtc 	    && (argv[go.optind][0] == '-' || argv[go.optind][0] == '+')
    457        1.1       jtc 	    && argv[go.optind][1] == '\0')
    458        1.1       jtc 	{
    459        1.1       jtc 		/* lone - clears -v and -x flags */
    460        1.1       jtc 		if (argv[go.optind][0] == '-' && !Flag(FPOSIX))
    461        1.1       jtc 			Flag(FVERBOSE) = Flag(FXTRACE) = 0;
    462        1.1       jtc 		/* set skips lone - or + option */
    463        1.1       jtc 		go.optind++;
    464        1.1       jtc 	}
    465        1.1       jtc 	if (setargsp)
    466        1.1       jtc 		/* -- means set $#/$* even if there are no arguments */
    467        1.1       jtc 		*setargsp = !arrayset && ((go.info & GI_MINUSMINUS)
    468        1.1       jtc 					  || argv[go.optind]);
    469        1.1       jtc 
    470        1.4   hubertf 	if (arrayset && (!*array || *skip_varname(array, FALSE))) {
    471        1.4   hubertf 		bi_errorf("%s: is not an identifier", array);
    472        1.4   hubertf 		return -1;
    473        1.4   hubertf 	}
    474        1.1       jtc 	if (sortargs) {
    475        1.1       jtc 		for (i = go.optind; argv[i]; i++)
    476        1.1       jtc 			;
    477        1.1       jtc 		qsortp((void **) &argv[go.optind], (size_t) (i - go.optind),
    478        1.1       jtc 			xstrcmp);
    479        1.1       jtc 	}
    480        1.1       jtc 	if (arrayset) {
    481        1.1       jtc 		set_array(array, arrayset, argv + go.optind);
    482        1.1       jtc 		for (; argv[go.optind]; go.optind++)
    483        1.1       jtc 			;
    484        1.1       jtc 	}
    485        1.1       jtc 
    486        1.1       jtc 	return go.optind;
    487        1.1       jtc }
    488        1.1       jtc 
    489        1.1       jtc /* parse a decimal number: returns 0 if string isn't a number, 1 otherwise */
    490        1.1       jtc int
    491        1.1       jtc getn(as, ai)
    492        1.1       jtc 	const char *as;
    493        1.1       jtc 	int *ai;
    494        1.1       jtc {
    495        1.9   mycroft 	char *p;
    496        1.9   mycroft 	long n;
    497        1.9   mycroft 
    498        1.9   mycroft 	n = strtol(as, &p, 10);
    499        1.1       jtc 
    500        1.9   mycroft 	if (!*as || *p || INT_MIN >= n || n >= INT_MAX)
    501        1.1       jtc 		return 0;
    502        1.9   mycroft 
    503        1.9   mycroft 	*ai = (int)n;
    504        1.1       jtc 	return 1;
    505        1.1       jtc }
    506        1.1       jtc 
    507        1.1       jtc /* getn() that prints error */
    508        1.1       jtc int
    509        1.1       jtc bi_getn(as, ai)
    510        1.1       jtc 	const char *as;
    511        1.1       jtc 	int *ai;
    512        1.1       jtc {
    513        1.1       jtc 	int rv = getn(as, ai);
    514        1.1       jtc 
    515        1.1       jtc 	if (!rv)
    516        1.1       jtc 		bi_errorf("%s: bad number", as);
    517        1.1       jtc 	return rv;
    518        1.1       jtc }
    519        1.1       jtc 
    520        1.1       jtc /* -------- gmatch.c -------- */
    521        1.1       jtc 
    522        1.1       jtc /*
    523        1.1       jtc  * int gmatch(string, pattern)
    524        1.1       jtc  * char *string, *pattern;
    525        1.1       jtc  *
    526        1.1       jtc  * Match a pattern as in sh(1).
    527        1.1       jtc  * pattern character are prefixed with MAGIC by expand.
    528        1.1       jtc  */
    529        1.1       jtc 
    530        1.1       jtc int
    531        1.1       jtc gmatch(s, p, isfile)
    532        1.1       jtc 	const char *s, *p;
    533        1.1       jtc 	int isfile;
    534        1.1       jtc {
    535        1.1       jtc 	const char *se, *pe;
    536        1.1       jtc 
    537        1.1       jtc 	if (s == NULL || p == NULL)
    538        1.1       jtc 		return 0;
    539        1.1       jtc 	se = s + strlen(s);
    540        1.1       jtc 	pe = p + strlen(p);
    541        1.1       jtc 	/* isfile is false iff no syntax check has been done on
    542        1.1       jtc 	 * the pattern.  If check fails, just to a strcmp().
    543        1.1       jtc 	 */
    544        1.1       jtc 	if (!isfile && !has_globbing(p, pe)) {
    545        1.1       jtc 		int len = pe - p + 1;
    546        1.1       jtc 		char tbuf[64];
    547       1.13     lukem 		char *t = len <= (int)sizeof(tbuf) ? tbuf
    548        1.1       jtc 				: (char *) alloc(len, ATEMP);
    549        1.9   mycroft 		debunk(t, p, len);
    550        1.1       jtc 		return !strcmp(t, s);
    551        1.1       jtc 	}
    552        1.1       jtc 	return do_gmatch((const unsigned char *) s, (const unsigned char *) se,
    553        1.1       jtc 			 (const unsigned char *) p, (const unsigned char *) pe,
    554        1.1       jtc 			 isfile);
    555        1.1       jtc }
    556        1.1       jtc 
    557        1.1       jtc /* Returns if p is a syntacticly correct globbing pattern, false
    558        1.1       jtc  * if it contains no pattern characters or if there is a syntax error.
    559        1.1       jtc  * Syntax errors are:
    560        1.1       jtc  *	- [ with no closing ]
    561        1.9   mycroft  *	- imbalanced $(...) expression
    562        1.1       jtc  *	- [...] and *(...) not nested (eg, [a$(b|]c), *(a[b|c]d))
    563        1.1       jtc  */
    564        1.1       jtc /*XXX
    565        1.1       jtc - if no magic,
    566        1.1       jtc 	if dest given, copy to dst
    567        1.1       jtc 	return ?
    568        1.1       jtc - if magic && (no globbing || syntax error)
    569        1.1       jtc 	debunk to dst
    570        1.1       jtc 	return ?
    571        1.1       jtc - return ?
    572        1.1       jtc */
    573        1.1       jtc int
    574        1.1       jtc has_globbing(xp, xpe)
    575        1.1       jtc 	const char *xp, *xpe;
    576        1.1       jtc {
    577        1.1       jtc 	const unsigned char *p = (const unsigned char *) xp;
    578        1.1       jtc 	const unsigned char *pe = (const unsigned char *) xpe;
    579        1.1       jtc 	int c;
    580        1.1       jtc 	int nest = 0, bnest = 0;
    581        1.1       jtc 	int saw_glob = 0;
    582        1.1       jtc 	int in_bracket = 0; /* inside [...] */
    583        1.1       jtc 
    584        1.1       jtc 	for (; p < pe; p++) {
    585        1.1       jtc 		if (!ISMAGIC(*p))
    586        1.1       jtc 			continue;
    587        1.1       jtc 		if ((c = *++p) == '*' || c == '?')
    588        1.1       jtc 			saw_glob = 1;
    589        1.1       jtc 		else if (c == '[') {
    590        1.1       jtc 			if (!in_bracket) {
    591        1.1       jtc 				saw_glob = 1;
    592        1.1       jtc 				in_bracket = 1;
    593        1.1       jtc 				if (ISMAGIC(p[1]) && p[2] == NOT)
    594        1.1       jtc 					p += 2;
    595        1.1       jtc 				if (ISMAGIC(p[1]) && p[2] == ']')
    596        1.1       jtc 					p += 2;
    597        1.1       jtc 			}
    598        1.1       jtc 			/* XXX Do we need to check ranges here? POSIX Q */
    599        1.1       jtc 		} else if (c == ']') {
    600        1.1       jtc 			if (in_bracket) {
    601        1.1       jtc 				if (bnest)		/* [a*(b]) */
    602        1.1       jtc 					return 0;
    603        1.1       jtc 				in_bracket = 0;
    604        1.1       jtc 			}
    605        1.4   hubertf 		} else if ((c & 0x80) && strchr("*+?@! ", c & 0x7f)) {
    606        1.1       jtc 			saw_glob = 1;
    607        1.1       jtc 			if (in_bracket)
    608        1.1       jtc 				bnest++;
    609        1.1       jtc 			else
    610        1.1       jtc 				nest++;
    611        1.1       jtc 		} else if (c == '|') {
    612        1.1       jtc 			if (in_bracket && !bnest)	/* *(a[foo|bar]) */
    613        1.1       jtc 				return 0;
    614        1.1       jtc 		} else if (c == /*(*/ ')') {
    615        1.1       jtc 			if (in_bracket) {
    616        1.1       jtc 				if (!bnest--)		/* *(a[b)c] */
    617        1.1       jtc 					return 0;
    618        1.1       jtc 			} else if (nest)
    619        1.1       jtc 				nest--;
    620        1.1       jtc 		}
    621        1.1       jtc 		/* else must be a MAGIC-MAGIC, or MAGIC-!, MAGIC--, MAGIC-]
    622        1.1       jtc 			 MAGIC-{, MAGIC-,, MAGIC-} */
    623        1.1       jtc 	}
    624        1.1       jtc 	return saw_glob && !in_bracket && !nest;
    625        1.1       jtc }
    626        1.1       jtc 
    627        1.1       jtc /* Function must return either 0 or 1 (assumed by code for 0x80|'!') */
    628        1.1       jtc static int
    629        1.1       jtc do_gmatch(s, se, p, pe, isfile)
    630        1.1       jtc 	const unsigned char *s, *p;
    631        1.1       jtc 	const unsigned char *se, *pe;
    632        1.1       jtc 	int isfile;
    633        1.1       jtc {
    634  1.15.34.1  pgoyette 	int sc, pc;
    635        1.1       jtc 	const unsigned char *prest, *psub, *pnext;
    636        1.1       jtc 	const unsigned char *srest;
    637  1.15.34.1  pgoyette 	const unsigned char *sNext, *pNext, *sStart;
    638        1.1       jtc 
    639        1.1       jtc 	if (s == NULL || p == NULL)
    640        1.1       jtc 		return 0;
    641  1.15.34.1  pgoyette 	sNext = sStart = s;
    642  1.15.34.1  pgoyette 	pNext = p;
    643  1.15.34.1  pgoyette 	while (p < pe || s < se) {
    644  1.15.34.1  pgoyette 		pc = *p;
    645        1.1       jtc 		sc = s < se ? *s : '\0';
    646        1.1       jtc 		if (isfile) {
    647       1.11    rillig 			sc = FILECHCONV((unsigned char)sc);
    648       1.11    rillig 			pc = FILECHCONV((unsigned char)pc);
    649        1.1       jtc 		}
    650        1.1       jtc 		if (!ISMAGIC(pc)) {
    651        1.1       jtc 			if (sc != pc)
    652  1.15.34.1  pgoyette 				goto backtrack;
    653  1.15.34.1  pgoyette 			p++;
    654  1.15.34.1  pgoyette 			s++;
    655        1.1       jtc 			continue;
    656  1.15.34.1  pgoyette 		} else
    657  1.15.34.1  pgoyette 			pc = *++p;
    658  1.15.34.1  pgoyette 		switch (pc) {
    659        1.1       jtc 		  case '[':
    660  1.15.34.1  pgoyette 			p++;
    661  1.15.34.1  pgoyette 			s++;
    662        1.1       jtc 			if (sc == 0 || (p = cclass(p, sc)) == NULL)
    663  1.15.34.1  pgoyette 				break;
    664  1.15.34.1  pgoyette 			continue;
    665        1.1       jtc 
    666        1.1       jtc 		  case '?':
    667        1.1       jtc 			if (sc == 0)
    668  1.15.34.1  pgoyette 				break;
    669  1.15.34.1  pgoyette 			p++;
    670  1.15.34.1  pgoyette 			s++;
    671  1.15.34.1  pgoyette 			continue;
    672        1.1       jtc 
    673        1.1       jtc 		  case '*':
    674  1.15.34.1  pgoyette 			pNext = p - 1;
    675  1.15.34.1  pgoyette 			sNext = s + 1;
    676  1.15.34.1  pgoyette 			p++;
    677  1.15.34.1  pgoyette 			continue;
    678        1.4   hubertf 		  /*
    679        1.4   hubertf 		   * [*+?@!](pattern|pattern|..)
    680        1.4   hubertf 		   *
    681        1.4   hubertf 		   * Not ifdef'd KSH as this is needed for ${..%..}, etc.
    682        1.4   hubertf 		   */
    683        1.1       jtc 		  case 0x80|'+': /* matches one or more times */
    684        1.1       jtc 		  case 0x80|'*': /* matches zero or more times */
    685  1.15.34.1  pgoyette 			if (!(prest = pat_scan(++p, pe, 0)))
    686  1.15.34.1  pgoyette 				break;
    687        1.1       jtc 			s--;
    688        1.1       jtc 			/* take care of zero matches */
    689        1.1       jtc 			if (p[-1] == (0x80 | '*')
    690        1.1       jtc 			    && do_gmatch(s, se, prest, pe, isfile))
    691  1.15.34.1  pgoyette 				continue;
    692        1.1       jtc 			for (psub = p; ; psub = pnext) {
    693        1.1       jtc 				pnext = pat_scan(psub, pe, 1);
    694        1.1       jtc 				for (srest = s; srest <= se; srest++) {
    695        1.1       jtc 					if (do_gmatch(s, srest,
    696        1.1       jtc 						psub, pnext - 2, isfile)
    697        1.1       jtc 					    && (do_gmatch(srest, se,
    698        1.1       jtc 							  prest, pe, isfile)
    699        1.1       jtc 						|| (s != srest
    700        1.1       jtc 						    && do_gmatch(srest, se,
    701        1.1       jtc 							p - 2, pe, isfile))))
    702        1.1       jtc 						return 1;
    703        1.1       jtc 				}
    704        1.1       jtc 				if (pnext == prest)
    705        1.1       jtc 					break;
    706        1.1       jtc 			}
    707  1.15.34.1  pgoyette 			break;
    708        1.1       jtc 
    709        1.1       jtc 		  case 0x80|'?': /* matches zero or once */
    710        1.1       jtc 		  case 0x80|'@': /* matches one of the patterns */
    711        1.4   hubertf 		  case 0x80|' ': /* simile for @ */
    712  1.15.34.1  pgoyette 			if (!(prest = pat_scan(++p, pe, 0)))
    713  1.15.34.1  pgoyette 				break;
    714        1.1       jtc 			s--;
    715        1.1       jtc 			/* Take care of zero matches */
    716        1.1       jtc 			if (p[-1] == (0x80 | '?')
    717        1.1       jtc 			    && do_gmatch(s, se, prest, pe, isfile))
    718  1.15.34.1  pgoyette 				continue;
    719        1.1       jtc 			for (psub = p; ; psub = pnext) {
    720        1.1       jtc 				pnext = pat_scan(psub, pe, 1);
    721        1.1       jtc 				srest = prest == pe ? se : s;
    722        1.1       jtc 				for (; srest <= se; srest++) {
    723        1.1       jtc 					if (do_gmatch(s, srest,
    724        1.1       jtc 						      psub, pnext - 2, isfile)
    725        1.1       jtc 					    && do_gmatch(srest, se,
    726        1.1       jtc 							 prest, pe, isfile))
    727  1.15.34.1  pgoyette 						continue;
    728        1.1       jtc 				}
    729        1.1       jtc 				if (pnext == prest)
    730        1.1       jtc 					break;
    731        1.1       jtc 			}
    732  1.15.34.1  pgoyette 			break;
    733        1.1       jtc 
    734        1.1       jtc 		  case 0x80|'!': /* matches none of the patterns */
    735  1.15.34.1  pgoyette 			if (!(prest = pat_scan(++p, pe, 0)))
    736  1.15.34.1  pgoyette 				break;
    737        1.1       jtc 			s--;
    738        1.1       jtc 			for (srest = s; srest <= se; srest++) {
    739        1.1       jtc 				int matched = 0;
    740        1.1       jtc 
    741        1.1       jtc 				for (psub = p; ; psub = pnext) {
    742        1.1       jtc 					pnext = pat_scan(psub, pe, 1);
    743        1.1       jtc 					if (do_gmatch(s, srest,
    744        1.1       jtc 						      psub, pnext - 2, isfile))
    745        1.1       jtc 					{
    746        1.1       jtc 						matched = 1;
    747        1.1       jtc 						break;
    748        1.1       jtc 					}
    749        1.1       jtc 					if (pnext == prest)
    750        1.1       jtc 						break;
    751        1.1       jtc 				}
    752        1.1       jtc 				if (!matched && do_gmatch(srest, se,
    753        1.1       jtc 							  prest, pe, isfile))
    754  1.15.34.1  pgoyette 					continue;
    755        1.1       jtc 			}
    756  1.15.34.1  pgoyette 			break;
    757        1.1       jtc 
    758        1.1       jtc 		  default:
    759  1.15.34.1  pgoyette 			if (sc != pc)
    760  1.15.34.1  pgoyette 				break;
    761  1.15.34.1  pgoyette 			p++;
    762  1.15.34.1  pgoyette 			s++;
    763  1.15.34.1  pgoyette 			continue;
    764        1.1       jtc 		}
    765  1.15.34.1  pgoyette backtrack:	if (sNext != sStart && sNext <= se) {
    766  1.15.34.1  pgoyette 			p = pNext;
    767  1.15.34.1  pgoyette 			s = sNext;
    768  1.15.34.1  pgoyette 			continue;
    769  1.15.34.1  pgoyette 		}
    770  1.15.34.1  pgoyette 		return 0;
    771        1.1       jtc 	}
    772  1.15.34.1  pgoyette 	return 1;
    773        1.1       jtc }
    774        1.1       jtc 
    775        1.1       jtc static const unsigned char *
    776        1.1       jtc cclass(p, sub)
    777        1.1       jtc 	const unsigned char *p;
    778        1.1       jtc 	register int sub;
    779        1.1       jtc {
    780        1.1       jtc 	register int c, d, not, found = 0;
    781        1.1       jtc 	const unsigned char *orig_p = p;
    782        1.1       jtc 
    783        1.1       jtc 	if ((not = (ISMAGIC(*p) && *++p == NOT)))
    784        1.1       jtc 		p++;
    785        1.1       jtc 	do {
    786        1.1       jtc 		c = *p++;
    787        1.1       jtc 		if (ISMAGIC(c)) {
    788        1.1       jtc 			c = *p++;
    789        1.4   hubertf 			if ((c & 0x80) && !ISMAGIC(c)) {
    790        1.1       jtc 				c &= 0x7f;/* extended pattern matching: *+?@! */
    791        1.4   hubertf 				/* XXX the ( char isn't handled as part of [] */
    792        1.4   hubertf 				if (c == ' ') /* simile for @: plain (..) */
    793        1.4   hubertf 					c = '(' /*)*/;
    794        1.4   hubertf 			}
    795        1.1       jtc 		}
    796        1.1       jtc 		if (c == '\0')
    797        1.1       jtc 			/* No closing ] - act as if the opening [ was quoted */
    798        1.1       jtc 			return sub == '[' ? orig_p : NULL;
    799        1.1       jtc 		if (ISMAGIC(p[0]) && p[1] == '-'
    800        1.1       jtc 		    && (!ISMAGIC(p[2]) || p[3] != ']'))
    801        1.1       jtc 		{
    802        1.1       jtc 			p += 2; /* MAGIC- */
    803        1.1       jtc 			d = *p++;
    804        1.1       jtc 			if (ISMAGIC(d)) {
    805        1.1       jtc 				d = *p++;
    806        1.1       jtc 				if ((d & 0x80) && !ISMAGIC(d))
    807        1.1       jtc 					d &= 0x7f;
    808        1.1       jtc 			}
    809        1.1       jtc 			/* POSIX says this is an invalid expression */
    810        1.1       jtc 			if (c > d)
    811        1.1       jtc 				return NULL;
    812        1.1       jtc 		} else
    813        1.1       jtc 			d = c;
    814        1.1       jtc 		if (c == sub || (c <= sub && sub <= d))
    815        1.1       jtc 			found = 1;
    816        1.1       jtc 	} while (!(ISMAGIC(p[0]) && p[1] == ']'));
    817        1.1       jtc 
    818        1.1       jtc 	return (found != not) ? p+2 : NULL;
    819        1.1       jtc }
    820        1.1       jtc 
    821        1.1       jtc /* Look for next ) or | (if match_sep) in *(foo|bar) pattern */
    822        1.1       jtc const unsigned char *
    823        1.1       jtc pat_scan(p, pe, match_sep)
    824        1.1       jtc 	const unsigned char *p;
    825        1.1       jtc 	const unsigned char *pe;
    826        1.1       jtc 	int match_sep;
    827        1.1       jtc {
    828        1.1       jtc 	int nest = 0;
    829        1.1       jtc 
    830        1.1       jtc 	for (; p < pe; p++) {
    831        1.1       jtc 		if (!ISMAGIC(*p))
    832        1.1       jtc 			continue;
    833        1.1       jtc 		if ((*++p == /*(*/ ')' && nest-- == 0)
    834        1.1       jtc 		    || (*p == '|' && match_sep && nest == 0))
    835        1.1       jtc 			return ++p;
    836        1.4   hubertf 		if ((*p & 0x80) && strchr("*+?@! ", *p & 0x7f))
    837        1.1       jtc 			nest++;
    838        1.1       jtc 	}
    839        1.1       jtc 	return (const unsigned char *) 0;
    840        1.1       jtc }
    841        1.1       jtc 
    842        1.1       jtc 
    843        1.1       jtc /* -------- qsort.c -------- */
    844        1.1       jtc 
    845        1.1       jtc /*
    846        1.1       jtc  * quick sort of array of generic pointers to objects.
    847        1.1       jtc  */
    848        1.1       jtc static void qsort1 ARGS((void **base, void **lim, int (*f)(void *, void *)));
    849        1.1       jtc 
    850        1.1       jtc void
    851        1.1       jtc qsortp(base, n, f)
    852        1.1       jtc 	void **base;				/* base address */
    853        1.1       jtc 	size_t n;				/* elements */
    854        1.1       jtc 	int (*f) ARGS((void *, void *));	/* compare function */
    855        1.1       jtc {
    856        1.1       jtc 	qsort1(base, base + n, f);
    857        1.1       jtc }
    858        1.1       jtc 
    859        1.1       jtc #define	swap2(a, b)	{\
    860        1.1       jtc 	register void *t; t = *(a); *(a) = *(b); *(b) = t;\
    861        1.1       jtc }
    862        1.1       jtc #define	swap3(a, b, c)	{\
    863        1.1       jtc 	register void *t; t = *(a); *(a) = *(c); *(c) = *(b); *(b) = t;\
    864        1.1       jtc }
    865        1.1       jtc 
    866        1.1       jtc static void
    867        1.1       jtc qsort1(base, lim, f)
    868        1.1       jtc 	void **base, **lim;
    869        1.1       jtc 	int (*f) ARGS((void *, void *));
    870        1.1       jtc {
    871        1.1       jtc 	register void **i, **j;
    872        1.1       jtc 	register void **lptr, **hptr;
    873        1.1       jtc 	size_t n;
    874        1.1       jtc 	int c;
    875        1.1       jtc 
    876        1.1       jtc   top:
    877        1.1       jtc 	n = (lim - base) / 2;
    878        1.1       jtc 	if (n == 0)
    879        1.1       jtc 		return;
    880        1.1       jtc 	hptr = lptr = base+n;
    881        1.1       jtc 	i = base;
    882        1.1       jtc 	j = lim - 1;
    883        1.1       jtc 
    884        1.1       jtc 	for (;;) {
    885        1.1       jtc 		if (i < lptr) {
    886        1.1       jtc 			if ((c = (*f)(*i, *lptr)) == 0) {
    887       1.10    simonb 				lptr--;
    888        1.1       jtc 				swap2(i, lptr);
    889        1.1       jtc 				continue;
    890        1.1       jtc 			}
    891        1.1       jtc 			if (c < 0) {
    892        1.1       jtc 				i += 1;
    893        1.1       jtc 				continue;
    894        1.1       jtc 			}
    895        1.1       jtc 		}
    896        1.1       jtc 
    897        1.1       jtc 	  begin:
    898        1.1       jtc 		if (j > hptr) {
    899        1.1       jtc 			if ((c = (*f)(*hptr, *j)) == 0) {
    900       1.10    simonb 				hptr++;
    901        1.1       jtc 				swap2(hptr, j);
    902        1.1       jtc 				goto begin;
    903        1.1       jtc 			}
    904        1.1       jtc 			if (c > 0) {
    905        1.1       jtc 				if (i == lptr) {
    906       1.10    simonb 					hptr++;
    907        1.1       jtc 					swap3(i, hptr, j);
    908        1.1       jtc 					i = lptr += 1;
    909        1.1       jtc 					goto begin;
    910        1.1       jtc 				}
    911        1.1       jtc 				swap2(i, j);
    912        1.1       jtc 				j -= 1;
    913        1.1       jtc 				i += 1;
    914        1.1       jtc 				continue;
    915        1.1       jtc 			}
    916        1.1       jtc 			j -= 1;
    917        1.1       jtc 			goto begin;
    918        1.1       jtc 		}
    919        1.1       jtc 
    920        1.1       jtc 		if (i == lptr) {
    921        1.1       jtc 			if (lptr-base >= lim-hptr) {
    922        1.1       jtc 				qsort1(hptr+1, lim, f);
    923        1.1       jtc 				lim = lptr;
    924        1.1       jtc 			} else {
    925        1.1       jtc 				qsort1(base, lptr, f);
    926        1.1       jtc 				base = hptr+1;
    927        1.1       jtc 			}
    928        1.1       jtc 			goto top;
    929        1.1       jtc 		}
    930        1.1       jtc 
    931        1.1       jtc 		lptr -= 1;
    932        1.1       jtc 		swap3(j, lptr, i);
    933        1.1       jtc 		j = hptr -= 1;
    934        1.1       jtc 	}
    935        1.1       jtc }
    936        1.1       jtc 
    937        1.1       jtc int
    938        1.1       jtc xstrcmp(p1, p2)
    939        1.1       jtc 	void *p1, *p2;
    940        1.1       jtc {
    941        1.1       jtc 	return (strcmp((char *)p1, (char *)p2));
    942        1.1       jtc }
    943        1.1       jtc 
    944        1.1       jtc /* Initialize a Getopt structure */
    945        1.1       jtc void
    946        1.1       jtc ksh_getopt_reset(go, flags)
    947        1.1       jtc 	Getopt *go;
    948        1.1       jtc 	int flags;
    949        1.1       jtc {
    950        1.1       jtc 	go->optind = 1;
    951        1.1       jtc 	go->optarg = (char *) 0;
    952        1.1       jtc 	go->p = 0;
    953        1.1       jtc 	go->flags = flags;
    954        1.1       jtc 	go->info = 0;
    955        1.1       jtc 	go->buf[1] = '\0';
    956        1.1       jtc }
    957        1.1       jtc 
    958        1.1       jtc 
    959        1.1       jtc /* getopt() used for shell built-in commands, the getopts command, and
    960        1.1       jtc  * command line options.
    961        1.1       jtc  * A leading ':' in options means don't print errors, instead return '?'
    962        1.1       jtc  * or ':' and set go->optarg to the offending option character.
    963        1.1       jtc  * If GF_ERROR is set (and option doesn't start with :), errors result in
    964        1.1       jtc  * a call to bi_errorf().
    965        1.1       jtc  *
    966        1.1       jtc  * Non-standard features:
    967        1.1       jtc  *	- ';' is like ':' in options, except the argument is optional
    968        1.1       jtc  *	  (if it isn't present, optarg is set to 0).
    969        1.1       jtc  *	  Used for 'set -o'.
    970        1.1       jtc  *	- ',' is like ':' in options, except the argument always immediately
    971        1.1       jtc  *	  follows the option character (optarg is set to the null string if
    972        1.1       jtc  *	  the option is missing).
    973        1.1       jtc  *	  Used for 'read -u2', 'print -u2' and fc -40.
    974        1.1       jtc  *	- '#' is like ':' in options, expect that the argument is optional
    975        1.1       jtc  *	  and must start with a digit.  If the argument doesn't start with a
    976        1.1       jtc  *	  digit, it is assumed to be missing and normal option processing
    977        1.1       jtc  *	  continues (optarg is set to 0 if the option is missing).
    978        1.1       jtc  *	  Used for 'typeset -LZ4'.
    979        1.1       jtc  *	- accepts +c as well as -c IF the GF_PLUSOPT flag is present.  If an
    980        1.1       jtc  *	  option starting with + is accepted, the GI_PLUS flag will be set
    981        1.4   hubertf  *	  in go->info.
    982        1.1       jtc  */
    983        1.1       jtc int
    984        1.1       jtc ksh_getopt(argv, go, options)
    985        1.1       jtc 	char **argv;
    986        1.1       jtc 	Getopt *go;
    987        1.1       jtc 	const char *options;
    988        1.1       jtc {
    989        1.1       jtc 	char c;
    990        1.1       jtc 	char *o;
    991        1.1       jtc 
    992        1.1       jtc 	if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') {
    993        1.1       jtc 		char *arg = argv[go->optind], flag = arg ? *arg : '\0';
    994        1.1       jtc 
    995        1.1       jtc 		go->p = 1;
    996        1.1       jtc 		if (flag == '-' && arg[1] == '-' && arg[2] == '\0') {
    997        1.1       jtc 			go->optind++;
    998        1.1       jtc 			go->p = 0;
    999        1.1       jtc 			go->info |= GI_MINUSMINUS;
   1000        1.1       jtc 			return EOF;
   1001        1.1       jtc 		}
   1002        1.1       jtc 		if (arg == (char *) 0
   1003        1.4   hubertf 		    || ((flag != '-' ) /* neither a - nor a + (if + allowed) */
   1004        1.4   hubertf 			&& (!(go->flags & GF_PLUSOPT) || flag != '+'))
   1005        1.1       jtc 		    || (c = arg[1]) == '\0')
   1006        1.1       jtc 		{
   1007        1.1       jtc 			go->p = 0;
   1008        1.1       jtc 			return EOF;
   1009        1.1       jtc 		}
   1010        1.1       jtc 		go->optind++;
   1011        1.4   hubertf 		go->info &= ~(GI_MINUS|GI_PLUS);
   1012        1.1       jtc 		go->info |= flag == '-' ? GI_MINUS : GI_PLUS;
   1013        1.1       jtc 	}
   1014        1.1       jtc 	go->p++;
   1015        1.1       jtc 	if (c == '?' || c == ':' || c == ';' || c == ',' || c == '#'
   1016        1.1       jtc 	    || !(o = strchr(options, c)))
   1017        1.1       jtc 	{
   1018        1.1       jtc 		if (options[0] == ':') {
   1019        1.1       jtc 			go->buf[0] = c;
   1020        1.1       jtc 			go->optarg = go->buf;
   1021        1.1       jtc 		} else {
   1022        1.1       jtc 			warningf(TRUE, "%s%s-%c: unknown option",
   1023        1.1       jtc 				(go->flags & GF_NONAME) ? "" : argv[0],
   1024        1.1       jtc 				(go->flags & GF_NONAME) ? "" : ": ", c);
   1025        1.1       jtc 			if (go->flags & GF_ERROR)
   1026       1.15     joerg 				bi_errorf("%s", null);
   1027        1.1       jtc 		}
   1028        1.1       jtc 		return '?';
   1029        1.1       jtc 	}
   1030        1.1       jtc 	/* : means argument must be present, may be part of option argument
   1031        1.1       jtc 	 *   or the next argument
   1032        1.1       jtc 	 * ; same as : but argument may be missing
   1033        1.1       jtc 	 * , means argument is part of option argument, and may be null.
   1034        1.1       jtc 	 */
   1035        1.1       jtc 	if (*++o == ':' || *o == ';') {
   1036        1.1       jtc 		if (argv[go->optind - 1][go->p])
   1037        1.1       jtc 			go->optarg = argv[go->optind - 1] + go->p;
   1038        1.1       jtc 		else if (argv[go->optind])
   1039        1.1       jtc 			go->optarg = argv[go->optind++];
   1040        1.1       jtc 		else if (*o == ';')
   1041        1.1       jtc 			go->optarg = (char *) 0;
   1042        1.1       jtc 		else {
   1043        1.1       jtc 			if (options[0] == ':') {
   1044        1.1       jtc 				go->buf[0] = c;
   1045        1.1       jtc 				go->optarg = go->buf;
   1046        1.1       jtc 				return ':';
   1047        1.1       jtc 			}
   1048        1.1       jtc 			warningf(TRUE, "%s%s-`%c' requires argument",
   1049        1.1       jtc 				(go->flags & GF_NONAME) ? "" : argv[0],
   1050        1.1       jtc 				(go->flags & GF_NONAME) ? "" : ": ", c);
   1051        1.1       jtc 			if (go->flags & GF_ERROR)
   1052       1.15     joerg 				bi_errorf("%s", null);
   1053        1.1       jtc 			return '?';
   1054        1.1       jtc 		}
   1055        1.1       jtc 		go->p = 0;
   1056        1.1       jtc 	} else if (*o == ',') {
   1057        1.9   mycroft 		/* argument is attached to option character, even if null */
   1058        1.1       jtc 		go->optarg = argv[go->optind - 1] + go->p;
   1059        1.1       jtc 		go->p = 0;
   1060        1.1       jtc 	} else if (*o == '#') {
   1061        1.9   mycroft 		/* argument is optional and may be attached or unattached
   1062        1.1       jtc 		 * but must start with a digit.  optarg is set to 0 if the
   1063        1.1       jtc 		 * argument is missing.
   1064        1.1       jtc 		 */
   1065        1.1       jtc 		if (argv[go->optind - 1][go->p]) {
   1066        1.1       jtc 			if (digit(argv[go->optind - 1][go->p])) {
   1067        1.1       jtc 				go->optarg = argv[go->optind - 1] + go->p;
   1068        1.1       jtc 				go->p = 0;
   1069        1.1       jtc 			} else
   1070        1.7    simonb 				go->optarg = (char *) 0;
   1071        1.1       jtc 		} else {
   1072        1.1       jtc 			if (argv[go->optind] && digit(argv[go->optind][0])) {
   1073        1.1       jtc 				go->optarg = argv[go->optind++];
   1074        1.1       jtc 				go->p = 0;
   1075        1.1       jtc 			} else
   1076        1.7    simonb 				go->optarg = (char *) 0;
   1077        1.1       jtc 		}
   1078        1.1       jtc 	}
   1079        1.1       jtc 	return c;
   1080        1.1       jtc }
   1081        1.1       jtc 
   1082        1.1       jtc /* print variable/alias value using necessary quotes
   1083        1.1       jtc  * (POSIX says they should be suitable for re-entry...)
   1084        1.1       jtc  * No trailing newline is printed.
   1085        1.1       jtc  */
   1086        1.1       jtc void
   1087        1.1       jtc print_value_quoted(s)
   1088        1.1       jtc 	const char *s;
   1089        1.1       jtc {
   1090        1.1       jtc 	const char *p;
   1091        1.1       jtc 	int inquote = 0;
   1092        1.1       jtc 
   1093        1.1       jtc 	/* Test if any quotes are needed */
   1094        1.1       jtc 	for (p = s; *p; p++)
   1095        1.1       jtc 		if (ctype(*p, C_QUOTE))
   1096        1.1       jtc 			break;
   1097        1.1       jtc 	if (!*p) {
   1098        1.1       jtc 		shprintf("%s", s);
   1099        1.1       jtc 		return;
   1100        1.1       jtc 	}
   1101        1.1       jtc 	for (p = s; *p; p++) {
   1102        1.1       jtc 		if (*p == '\'') {
   1103       1.15     joerg 			shprintf("%s", "'\\'" + 1 - inquote);
   1104        1.1       jtc 			inquote = 0;
   1105        1.1       jtc 		} else {
   1106        1.1       jtc 			if (!inquote) {
   1107        1.1       jtc 				shprintf("'");
   1108        1.1       jtc 				inquote = 1;
   1109        1.1       jtc 			}
   1110        1.1       jtc 			shf_putc(*p, shl_stdout);
   1111        1.1       jtc 		}
   1112        1.1       jtc 	}
   1113        1.1       jtc 	if (inquote)
   1114        1.1       jtc 		shprintf("'");
   1115        1.1       jtc }
   1116        1.1       jtc 
   1117        1.1       jtc /* Print things in columns and rows - func() is called to format the ith
   1118        1.1       jtc  * element
   1119        1.1       jtc  */
   1120        1.1       jtc void
   1121        1.6    provos print_columns(shf, n, func, arg, max_width, prefcol)
   1122        1.1       jtc 	struct shf *shf;
   1123        1.1       jtc 	int n;
   1124        1.1       jtc 	char *(*func) ARGS((void *, int, char *, int));
   1125        1.1       jtc 	void *arg;
   1126        1.1       jtc 	int max_width;
   1127        1.6    provos 	int prefcol;
   1128        1.1       jtc {
   1129        1.1       jtc 	char *str = (char *) alloc(max_width + 1, ATEMP);
   1130        1.1       jtc 	int i;
   1131        1.1       jtc 	int r, c;
   1132        1.1       jtc 	int rows, cols;
   1133        1.1       jtc 	int nspace;
   1134        1.1       jtc 
   1135        1.1       jtc 	/* max_width + 1 for the space.  Note that no space
   1136        1.1       jtc 	 * is printed after the last column to avoid problems
   1137        1.1       jtc 	 * with terminals that have auto-wrap.
   1138        1.1       jtc 	 */
   1139        1.1       jtc 	cols = x_cols / (max_width + 1);
   1140        1.1       jtc 	if (!cols)
   1141        1.1       jtc 		cols = 1;
   1142        1.1       jtc 	rows = (n + cols - 1) / cols;
   1143        1.6    provos 	if (prefcol && n && cols > rows) {
   1144        1.1       jtc 		int tmp = rows;
   1145        1.1       jtc 
   1146        1.1       jtc 		rows = cols;
   1147        1.1       jtc 		cols = tmp;
   1148        1.1       jtc 		if (rows > n)
   1149        1.1       jtc 			rows = n;
   1150        1.1       jtc 	}
   1151        1.1       jtc 
   1152        1.1       jtc 	nspace = (x_cols - max_width * cols) / cols;
   1153        1.1       jtc 	if (nspace <= 0)
   1154        1.1       jtc 		nspace = 1;
   1155        1.1       jtc 	for (r = 0; r < rows; r++) {
   1156        1.1       jtc 		for (c = 0; c < cols; c++) {
   1157        1.1       jtc 			i = c * rows + r;
   1158        1.1       jtc 			if (i < n) {
   1159        1.1       jtc 				shf_fprintf(shf, "%-*s",
   1160        1.1       jtc 					max_width,
   1161        1.1       jtc 					(*func)(arg, i, str, max_width + 1));
   1162        1.1       jtc 				if (c + 1 < cols)
   1163        1.1       jtc 					shf_fprintf(shf, "%*s", nspace, null);
   1164        1.1       jtc 			}
   1165        1.1       jtc 		}
   1166        1.1       jtc 		shf_putchar('\n', shf);
   1167        1.1       jtc 	}
   1168        1.1       jtc 	afree(str, ATEMP);
   1169        1.1       jtc }
   1170        1.1       jtc 
   1171        1.1       jtc /* Strip any nul bytes from buf - returns new length (nbytes - # of nuls) */
   1172        1.1       jtc int
   1173        1.1       jtc strip_nuls(buf, nbytes)
   1174        1.1       jtc 	char *buf;
   1175        1.1       jtc 	int nbytes;
   1176        1.1       jtc {
   1177        1.1       jtc 	char *dst;
   1178        1.1       jtc 
   1179        1.1       jtc 	/* nbytes check because some systems (older freebsd's) have a buggy
   1180        1.1       jtc 	 * memchr()
   1181        1.1       jtc 	 */
   1182        1.1       jtc 	if (nbytes && (dst = memchr(buf, '\0', nbytes))) {
   1183        1.1       jtc 		char *end = buf + nbytes;
   1184        1.1       jtc 		char *p, *q;
   1185        1.1       jtc 
   1186        1.1       jtc 		for (p = dst; p < end; p = q) {
   1187        1.1       jtc 			/* skip a block of nulls */
   1188        1.1       jtc 			while (++p < end && *p == '\0')
   1189        1.1       jtc 				;
   1190        1.1       jtc 			/* find end of non-null block */
   1191        1.1       jtc 			if (!(q = memchr(p, '\0', end - p)))
   1192        1.1       jtc 				q = end;
   1193        1.1       jtc 			memmove(dst, p, q - p);
   1194        1.1       jtc 			dst += q - p;
   1195        1.1       jtc 		}
   1196        1.1       jtc 		*dst = '\0';
   1197        1.1       jtc 		return dst - buf;
   1198        1.1       jtc 	}
   1199        1.1       jtc 	return nbytes;
   1200        1.1       jtc }
   1201        1.1       jtc 
   1202        1.1       jtc /* Copy at most dsize-1 bytes from src to dst, ensuring dst is null terminated.
   1203        1.1       jtc  * Returns dst.
   1204        1.1       jtc  */
   1205        1.1       jtc char *
   1206        1.1       jtc str_zcpy(dst, src, dsize)
   1207        1.1       jtc 	char *dst;
   1208        1.1       jtc 	const char *src;
   1209        1.1       jtc 	int dsize;
   1210        1.1       jtc {
   1211        1.1       jtc 	if (dsize > 0) {
   1212        1.1       jtc 		int len = strlen(src);
   1213        1.1       jtc 
   1214        1.1       jtc 		if (len >= dsize)
   1215        1.1       jtc 			len = dsize - 1;
   1216        1.1       jtc 		memcpy(dst, src, len);
   1217        1.1       jtc 		dst[len] = '\0';
   1218        1.1       jtc 	}
   1219        1.1       jtc 	return dst;
   1220        1.1       jtc }
   1221        1.1       jtc 
   1222        1.1       jtc /* Like read(2), but if read fails due to non-blocking flag, resets flag
   1223        1.1       jtc  * and restarts read.
   1224        1.1       jtc  */
   1225        1.1       jtc int
   1226        1.1       jtc blocking_read(fd, buf, nbytes)
   1227        1.1       jtc 	int fd;
   1228        1.1       jtc 	char *buf;
   1229        1.1       jtc 	int nbytes;
   1230        1.1       jtc {
   1231        1.1       jtc 	int ret;
   1232        1.1       jtc 	int tried_reset = 0;
   1233        1.1       jtc 
   1234        1.1       jtc 	while ((ret = read(fd, buf, nbytes)) < 0) {
   1235        1.1       jtc 		if (!tried_reset && (errno == EAGAIN
   1236        1.1       jtc #ifdef EWOULDBLOCK
   1237        1.1       jtc 				     || errno == EWOULDBLOCK
   1238        1.1       jtc #endif /* EWOULDBLOCK */
   1239        1.1       jtc 				    ))
   1240        1.1       jtc 		{
   1241        1.1       jtc 			int oerrno = errno;
   1242        1.1       jtc 			if (reset_nonblock(fd) > 0) {
   1243        1.1       jtc 				tried_reset = 1;
   1244        1.1       jtc 				continue;
   1245        1.1       jtc 			}
   1246        1.1       jtc 			errno = oerrno;
   1247        1.1       jtc 		}
   1248        1.1       jtc 		break;
   1249        1.1       jtc 	}
   1250        1.1       jtc 	return ret;
   1251        1.1       jtc }
   1252        1.1       jtc 
   1253        1.1       jtc /* Reset the non-blocking flag on the specified file descriptor.
   1254        1.1       jtc  * Returns -1 if there was an error, 0 if non-blocking wasn't set,
   1255        1.1       jtc  * 1 if it was.
   1256        1.1       jtc  */
   1257        1.1       jtc int
   1258        1.1       jtc reset_nonblock(fd)
   1259        1.1       jtc 	int fd;
   1260        1.1       jtc {
   1261        1.1       jtc 	int flags;
   1262        1.1       jtc 	int blocking_flags;
   1263        1.1       jtc 
   1264        1.1       jtc 	if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
   1265        1.1       jtc 		return -1;
   1266        1.1       jtc 	/* With luck, the C compiler will reduce this to a constant */
   1267        1.1       jtc 	blocking_flags = 0;
   1268        1.1       jtc #ifdef O_NONBLOCK
   1269        1.1       jtc 	blocking_flags |= O_NONBLOCK;
   1270        1.1       jtc #endif /* O_NONBLOCK */
   1271        1.1       jtc #ifdef O_NDELAY
   1272        1.1       jtc 	blocking_flags |= O_NDELAY;
   1273        1.1       jtc #else /* O_NDELAY */
   1274        1.1       jtc # ifndef O_NONBLOCK
   1275        1.1       jtc 	blocking_flags |= FNDELAY; /* hope this exists... */
   1276        1.1       jtc # endif /* O_NONBLOCK */
   1277        1.1       jtc #endif /* O_NDELAY */
   1278        1.1       jtc 	if (!(flags & blocking_flags))
   1279        1.1       jtc 		return 0;
   1280        1.1       jtc 	flags &= ~blocking_flags;
   1281        1.1       jtc 	if (fcntl(fd, F_SETFL, flags) < 0)
   1282        1.1       jtc 		return -1;
   1283        1.1       jtc 	return 1;
   1284        1.1       jtc }
   1285        1.1       jtc 
   1286        1.1       jtc 
   1287        1.1       jtc #ifdef HAVE_SYS_PARAM_H
   1288        1.1       jtc # include <sys/param.h>
   1289        1.1       jtc #endif /* HAVE_SYS_PARAM_H */
   1290        1.1       jtc #ifndef MAXPATHLEN
   1291        1.1       jtc # define MAXPATHLEN PATH
   1292        1.1       jtc #endif /* MAXPATHLEN */
   1293        1.1       jtc 
   1294        1.4   hubertf #ifdef HPUX_GETWD_BUG
   1295        1.4   hubertf # include "ksh_dir.h"
   1296        1.4   hubertf 
   1297        1.4   hubertf /*
   1298        1.4   hubertf  * Work around bug in hpux 10.x C library - getwd/getcwd dump core
   1299        1.4   hubertf  * if current directory is not readable.  Done in macro 'cause code
   1300        1.4   hubertf  * is needed in GETWD and GETCWD cases.
   1301        1.4   hubertf  */
   1302        1.4   hubertf # define HPUX_GETWD_BUG_CODE \
   1303        1.4   hubertf 	{ \
   1304        1.4   hubertf 	    DIR *d = ksh_opendir("."); \
   1305        1.4   hubertf 	    if (!d) \
   1306        1.4   hubertf 		return (char *) 0; \
   1307        1.4   hubertf 	    closedir(d); \
   1308        1.4   hubertf 	}
   1309        1.4   hubertf #else /* HPUX_GETWD_BUG */
   1310        1.4   hubertf # define HPUX_GETWD_BUG_CODE
   1311        1.4   hubertf #endif /* HPUX_GETWD_BUG */
   1312        1.4   hubertf 
   1313        1.1       jtc /* Like getcwd(), except bsize is ignored if buf is 0 (MAXPATHLEN is used) */
   1314        1.1       jtc char *
   1315        1.1       jtc ksh_get_wd(buf, bsize)
   1316        1.1       jtc 	char *buf;
   1317        1.1       jtc 	int bsize;
   1318        1.1       jtc {
   1319        1.4   hubertf #ifdef HAVE_GETCWD
   1320        1.4   hubertf 	char *b;
   1321        1.4   hubertf 	char *ret;
   1322        1.4   hubertf 
   1323        1.4   hubertf 	/* Before memory allocated */
   1324        1.4   hubertf 	HPUX_GETWD_BUG_CODE
   1325        1.4   hubertf 
   1326        1.4   hubertf 	/* Assume getcwd() available */
   1327        1.4   hubertf 	if (!buf) {
   1328        1.4   hubertf 		bsize = MAXPATHLEN;
   1329        1.4   hubertf 		b = alloc(MAXPATHLEN + 1, ATEMP);
   1330        1.4   hubertf 	} else
   1331        1.4   hubertf 		b = buf;
   1332        1.4   hubertf 
   1333        1.4   hubertf 	ret = getcwd(b, bsize);
   1334        1.4   hubertf 
   1335        1.4   hubertf 	if (!buf) {
   1336        1.4   hubertf 		if (ret)
   1337        1.4   hubertf 			ret = aresize(b, strlen(b) + 1, ATEMP);
   1338        1.4   hubertf 		else
   1339        1.4   hubertf 			afree(b, ATEMP);
   1340        1.4   hubertf 	}
   1341        1.4   hubertf 
   1342        1.4   hubertf 	return ret;
   1343        1.4   hubertf #else /* HAVE_GETCWD */
   1344        1.9   mycroft 	extern char *getwd ARGS((char *));
   1345        1.1       jtc 	char *b;
   1346        1.1       jtc 	int len;
   1347        1.1       jtc 
   1348        1.4   hubertf 	/* Before memory allocated */
   1349        1.4   hubertf 	HPUX_GETWD_BUG_CODE
   1350        1.4   hubertf 
   1351        1.1       jtc 	if (buf && bsize > MAXPATHLEN)
   1352        1.1       jtc 		b = buf;
   1353        1.1       jtc 	else
   1354        1.1       jtc 		b = alloc(MAXPATHLEN + 1, ATEMP);
   1355        1.3       erh 	if (!getcwd(b, MAXPATHLEN)) {
   1356        1.1       jtc 		errno = EACCES;
   1357        1.1       jtc 		if (b != buf)
   1358        1.1       jtc 			afree(b, ATEMP);
   1359        1.1       jtc 		return (char *) 0;
   1360        1.1       jtc 	}
   1361        1.1       jtc 	len = strlen(b) + 1;
   1362        1.1       jtc 	if (!buf)
   1363        1.1       jtc 		b = aresize(b, len, ATEMP);
   1364        1.1       jtc 	else if (buf != b) {
   1365        1.1       jtc 		if (len > bsize) {
   1366        1.1       jtc 			errno = ERANGE;
   1367        1.1       jtc 			return (char *) 0;
   1368        1.1       jtc 		}
   1369        1.1       jtc 		memcpy(buf, b, len);
   1370        1.1       jtc 		afree(b, ATEMP);
   1371        1.1       jtc 		b = buf;
   1372        1.1       jtc 	}
   1373        1.1       jtc 
   1374        1.1       jtc 	return b;
   1375        1.4   hubertf #endif /* HAVE_GETCWD */
   1376        1.1       jtc }
   1377