1 /*
2 *   $Id: qdos.c 443 2006-05-30 04:37:13Z darren $
3 *
4 *   Copyright (c) 1999, Thierry Godefroy <godefroy@imaginet.fr>
5 *
6 *   This source code is released for free distribution under the terms of the
7 *   GNU General Public License.
8 *
9 *   This module contains functions to handle wildcard expansion and file name
10 *   conversion under QDOS.
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <qdos.h>
18 #include <string.h>
19 #include <errno.h>
20 #include "ctags.h"
21 
22 /* Translate the filenames from UNIX to QDOS conventions on open calls */
23 int (*_Open) (const char *, int, ...) = qopen;
24 
25 long _stack          = 24576;  /* Plenty of stack space */
26 long _memincr        = 10240;  /* Big increments to cut fragmentation */
27 char _prog_name []   = "ctags";
28 char _version []     = PROGRAM_VERSION;
29 char _copyright [32] = __DATE__;
30 char *_endmsg        = "\nPress a key to exit.";
31 int  custom_expand (char * param, char ***argvptr, int *argcptr);
32 int  (*_cmdwildcard) ()  = custom_expand;
33 
34 
35 struct WINDOWDEF _condetails = { 208, 1, 0, 7, 512, 256, 0, 0};
36 void (*_consetup) ()         = consetup_title;
37 
38 /* custom cmdexpand: also expands directory names */
39 
40 #define FILEBUF_INIT    1024  /* Initial allocation size for buffer */
41 #define FILEBUF_INCR    1024  /* Increment size for buffer */
42 
custom_expand(char * param,char *** argvptr,int * argcptr)43 int custom_expand (char * param, char ***argvptr, int *argcptr)
44 {
45 	int     count,sl;
46 	size_t  bufsize;
47 	char    *filenamebuf;
48 	char    *ptr,*safeptr;
49 
50 	/*
51 	 *  Check to see if we should do wild card expansion.
52 	 *  We only perform wildcard expansion if the parameter
53 	 *  was not a string and if it contains one of the
54 	 *  wild card characters.
55 	 *
56 	 *  We also do not expand any option that starts with '-'
57 	 *  as we then assume that it is a unix stylew option.
58 	 */
59 	if ((*param == '-') ||  (strpbrk (param,"*?") == NULL) ) {
60 	    return 0;
61 	}
62 
63 	if ((filenamebuf = malloc (bufsize = FILEBUF_INIT)) == NULL) {
64 	    return -1;
65 	}
66 TRYAGAIN:
67 	count = getfnl (param, filenamebuf, bufsize, QDR_ALL);
68 	if (count == -1  && errno == ENOMEM) {
69 	    /*
70 	     *  We have overflowed the buffer, so we try
71 	     *  to get a bigger buffer and try again.
72 	     */
73 	    bufsize += FILEBUF_INCR;
74 	    if ((filenamebuf = realloc (filenamebuf, bufsize)) == NULL) {
75 	        return -1;
76 	    } else {
77 	        goto TRYAGAIN;
78 	    }
79 	}
80 	/*
81 	 *  If no files were found, then return unexpanded.
82 	 */
83 	if (count == 0) {
84 	    free (filenamebuf);
85 	    return 0;
86 	}
87 	/*
88 	 *  Files were found, so add these to the list instead
89 	 *  of the original parameter typed by the user.
90 	 */
91 	for ( ptr=filenamebuf ; count > 0 ; count -- ) {
92 		*argvptr = (char **) realloc (*argvptr, (size_t) (((*argcptr) + 2) * sizeof (char *)));
93 		safeptr= (char *) malloc ((size_t) (sl=strlen (ptr) + 1));
94 		if (safeptr == NULL || *argvptr == NULL) {
95 			return -1;
96 		}
97 		(void) memcpy (safeptr,ptr, (size_t) sl);
98 		(*argvptr) [*argcptr] = safeptr;
99 		*argcptr += 1;
100 		ptr += sl;
101 	}
102 	free (filenamebuf);
103 	return *argcptr;
104 }
105 
106 /* vi:set tabstop=4 shiftwidth=4: */
107