aboutsummaryrefslogtreecommitdiff
path: root/gnu/libexec/uucp/libunix/wldcrd.c
blob: 13faa29b9c43fef5ed18c240f71204bcfb29a317 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* wldcrd.c
   Expand wildcards.

   Copyright (C) 1991, 1992, 1993 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, Building 200, 1 Kendall Square, Cambridge, MA 02139.
   */

#include "uucp.h"

#include "uudefs.h"
#include "sysdep.h"
#include "system.h"

#include <ctype.h>
#include <errno.h>

#if HAVE_GLOB && ! HAVE_GLOB_H
#undef HAVE_GLOB
#define HAVE_GLOB 0
#endif

#if HAVE_GLOB
#include <glob.h>
#endif

/* Local variables to hold the wildcard in progress.  */

#if HAVE_GLOB
static glob_t sSglob;
static int iSglob;
#else
static char *zSwildcard_alloc;
static char *zSwildcard;
#endif

/* Start getting a wildcarded file spec.  Use the glob function if it
   is available, and otherwise use the shell.  */

boolean
fsysdep_wildcard_start (zfile)
     const char *zfile;
{
#if HAVE_GLOB

#if DEBUG > 0
  if (*zfile != '/')
    ulog (LOG_FATAL, "fsysdep_wildcard: %s: Can't happen", zfile);
#endif

  if (glob (zfile, 0, (int (*) ()) NULL, &sSglob) != 0)
    sSglob.gl_pathc = 0;
  iSglob = 0;
  return TRUE;

#else /* ! HAVE_GLOB */

  char *zcmd, *zto;
  const char *zfrom;
  size_t c;
  const char *azargs[4];
  FILE *e;
  pid_t ipid;

#if DEBUG > 0
  if (*zfile != '/')
    ulog (LOG_FATAL, "fsysdep_wildcard: %s: Can't happen", zfile);
#endif

  zSwildcard_alloc = NULL;
  zSwildcard = NULL;

  zcmd = zbufalc (sizeof ECHO_PROGRAM + sizeof " " + 2 * strlen (zfile));
  memcpy (zcmd, ECHO_PROGRAM, sizeof ECHO_PROGRAM - 1);
  zto = zcmd + sizeof ECHO_PROGRAM - 1;
  *zto++ = ' ';
  zfrom = zfile;
  while (*zfrom != '\0')
    {
      /* To avoid shell trickery, we quote all characters except
	 letters, digits, and wildcard specifiers.  We don't quote '/'
	 to avoid an Ultrix sh bug.  */
      if (! isalnum (*zfrom)
	  && *zfrom != '*'
	  && *zfrom != '?'
	  && *zfrom != '['
	  && *zfrom != ']'
	  && *zfrom != '/')
	*zto++ = '\\';
      *zto++ = *zfrom++;
    }
  *zto = '\0';

  azargs[0] = "/bin/sh";
  azargs[1] = "-c";
  azargs[2] = zcmd;
  azargs[3] = NULL;

  e = espopen (azargs, TRUE, &ipid);

  ubuffree (zcmd);

  if (e == NULL)
    {
      ulog (LOG_ERROR, "espopen: %s", strerror (errno));
      return FALSE;
    }

  zSwildcard_alloc = NULL;
  c = 0;
  if (getline (&zSwildcard_alloc, &c, e) <= 0)
    {
      xfree ((pointer) zSwildcard_alloc);
      zSwildcard_alloc = NULL;
    }

  if (ixswait ((unsigned long) ipid, ECHO_PROGRAM) != 0)
    {
      xfree ((pointer) zSwildcard_alloc);
      return FALSE;
    }

  if (zSwildcard_alloc == NULL)
    return FALSE;

  DEBUG_MESSAGE1 (DEBUG_EXECUTE,
		  "fsysdep_wildcard_start: got \"%s\"",
		  zSwildcard_alloc);

  zSwildcard = zSwildcard_alloc;

  return TRUE;

#endif /* ! HAVE_GLOB */
}

/* Get the next wildcard spec.  */

/*ARGSUSED*/
char *
zsysdep_wildcard (zfile)
     const char *zfile;
{
#if HAVE_GLOB

  char *zret;

  if (iSglob >= sSglob.gl_pathc)
    return NULL;
  zret = zbufcpy (sSglob.gl_pathv[iSglob]);
  ++iSglob;
  return zret;
  
#else /* ! HAVE_GLOB */

  char *zret;

  if (zSwildcard_alloc == NULL || zSwildcard == NULL)
    return NULL;

  zret = zSwildcard;

  while (*zSwildcard != '\0' && ! isspace (BUCHAR (*zSwildcard)))
    ++zSwildcard;

  if (*zSwildcard != '\0')
    {
      *zSwildcard = '\0';
      ++zSwildcard;
      while (*zSwildcard != '\0' && isspace (BUCHAR (*zSwildcard)))
	++zSwildcard;
    }

  if (*zSwildcard == '\0')
    zSwildcard = NULL;

  return zbufcpy (zret);

#endif /* ! HAVE_GLOB */
}

/* Finish up getting wildcard specs.  */

boolean
fsysdep_wildcard_end ()
{
#if HAVE_GLOB
  globfree (&sSglob);
  return TRUE;
#else /* ! HAVE_GLOB */
  xfree ((pointer) zSwildcard_alloc);
  zSwildcard_alloc = NULL;
  zSwildcard = NULL;
  return TRUE;
#endif /* ! HAVE_GLOB */
}