OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [posix/] [wordexp.c] - Blame information for rev 520

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* Copyright (C) 2002 by  Red Hat, Incorporated. All rights reserved.
2
 *
3
 * Permission to use, copy, modify, and distribute this software
4
 * is freely granted, provided that this notice is preserved.
5
 */
6
#ifndef _NO_WORDEXP
7
 
8
#include <sys/param.h>
9
#include <sys/stat.h>
10
 
11
#include <ctype.h>
12
#include <dirent.h>
13
#include <errno.h>
14
#include <glob.h>
15
#include <pwd.h>
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <unistd.h>
20
#include <sys/wait.h>
21
 
22
#include <wordexp.h>
23
 
24
#define MAXLINELEN 500
25
 
26
/* Note: This implementation of wordexp requires a version of bash
27
   that supports the --wordexp and --protected arguments to be present
28
   on the system.  It does not support the WRDE_UNDEF flag. */
29
int
30
wordexp(const char *words, wordexp_t *pwordexp, int flags)
31
{
32
  FILE *f;
33
  FILE *f_err;
34
  char tmp[MAXLINELEN];
35
  int i = 0;
36
  int offs = 0;
37
  char *iter;
38
  pid_t pid;
39
  int num_words = 0;
40
  int num_bytes = 0;
41
  int fd[2];
42
  int fd_err[2];
43
  int err = 0;
44
 
45
  if (pwordexp == NULL)
46
    {
47
      return WRDE_NOSPACE;
48
    }
49
 
50
  if (flags & WRDE_REUSE)
51
    wordfree(pwordexp);
52
 
53
  if ((flags & WRDE_APPEND) == 0)
54
    {
55
      pwordexp->we_wordc = 0;
56
      pwordexp->we_wordv = NULL;
57
    }
58
 
59
  if (flags & WRDE_DOOFFS)
60
    {
61
      offs = pwordexp->we_offs;
62
 
63
      if(!(pwordexp->we_wordv = (char **)realloc(pwordexp->we_wordv, (pwordexp->we_wordc + offs + 1) * sizeof(char *))))
64
        return WRDE_NOSPACE;
65
 
66
      for (i = 0; i < offs; i++)
67
        pwordexp->we_wordv[i] = NULL;
68
    }
69
 
70
  pipe(fd);
71
  pipe(fd_err);
72
  pid = fork();
73
 
74
  if (pid > 0)
75
    {
76
      /* In parent process. */
77
 
78
      /* Close write end of parent's pipe. */
79
      close(fd[1]);
80
      close(fd_err[1]);
81
 
82
      /* f_err is the standard error from the shell command. */
83
      f_err = fdopen(fd_err[0], "r");
84
 
85
      /* Check for errors. */
86
      if (fgets(tmp, MAXLINELEN, f_err))
87
        {
88
          if (strstr(tmp, "EOF"))
89
            err = WRDE_SYNTAX;
90
          else if (strstr(tmp, "`\n'") || strstr(tmp, "`|'")
91
                   || strstr(tmp, "`&'") || strstr(tmp, "`;'")
92
                   || strstr(tmp, "`<'") || strstr(tmp, "`>'")
93
                   || strstr(tmp, "`('") || strstr(tmp, "`)'")
94
                   || strstr(tmp, "`{'") || strstr(tmp, "`}'"))
95
            err = WRDE_BADCHAR;
96
          else if (strstr(tmp, "command substitution"))
97
            err = WRDE_CMDSUB;
98
          else
99
            err = WRDE_SYNTAX;
100
 
101
          if (flags & WRDE_SHOWERR)
102
            {
103
              fprintf(stderr, tmp);
104
              while(fgets(tmp, MAXLINELEN, f_err))
105
                fprintf(stderr, tmp);
106
            }
107
 
108
          return err;
109
        }
110
 
111
      /* f is the standard output from the shell command. */
112
      f = fdopen(fd[0], "r");
113
 
114
      /* Get number of words expanded by shell. */
115
      fgets(tmp, MAXLINELEN, f);
116
 
117
      if((iter = strchr(tmp, '\n')))
118
          *iter = '\0';
119
 
120
      num_words = atoi(tmp);
121
 
122
      if(!(pwordexp->we_wordv = (char **)realloc(pwordexp->we_wordv,
123
                                                 (pwordexp->we_wordc + num_words + offs + 1) * sizeof(char *))))
124
        return WRDE_NOSPACE;
125
 
126
      /* Get number of bytes required for storage of num_words words. */
127
      fgets(tmp, MAXLINELEN, f);
128
 
129
      if((iter = strchr(tmp, '\n')))
130
          *iter = '\0';
131
 
132
      num_bytes = atoi(tmp) + pwordexp->we_wordc;
133
 
134
      /* Get each expansion from the shell output, and store each in
135
         pwordexp's we_wordv vector. */
136
      for(i = 0; i < num_words; i++)
137
        {
138
          fgets(tmp, MAXLINELEN, f);
139
 
140
          if((iter = strchr(tmp, '\n')))
141
            *iter = '\0';
142
 
143
          pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = strdup(tmp);
144
        }
145
 
146
      pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL;
147
      pwordexp->we_wordc += num_words;
148
 
149
      close(fd[0]);
150
      close(fd_err[0]);
151
 
152
      /* Wait for child to finish. */
153
      waitpid (pid, NULL, 0);
154
 
155
      return WRDE_SUCCESS;
156
    }
157
  else
158
    {
159
      /* In child process. */
160
 
161
      /* Close read end of child's pipe. */
162
      close(fd[0]);
163
      close(fd_err[0]);
164
 
165
      /* Pipe standard output to parent process via fd. */
166
      if (fd[1] != STDOUT_FILENO)
167
        {
168
          dup2(fd[1], STDOUT_FILENO);
169
          /* fd[1] no longer required. */
170
          close(fd[1]);
171
        }
172
 
173
      /* Pipe standard error to parent process via fd_err. */
174
      if (fd_err[1] != STDERR_FILENO)
175
        {
176
          dup2(fd_err[1], STDERR_FILENO);
177
          /* fd_err[1] no longer required. */
178
          close(fd_err[1]);
179
        }
180
 
181
      if (flags & WRDE_NOCMD)
182
        execl("/bin/bash", "bash", "--protected", "--wordexp", words, (char *)0);
183
      else
184
        execl("/bin/bash", "bash", "--wordexp", words, (char *)0);
185
    }
186
  return WRDE_SUCCESS;
187
}
188
#endif /* !_NO_WORDEXP  */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.