Bash  5.0-beta2
Bash - Bourne Again shell
fdflags.c File Reference
#include <config.h>
#include <fcntl.h>
#include <errno.h>
#include "bashansi.h"
#include <stdio.h>
#include "loadables.h"

Go to the source code of this file.

Macros

#define N_FLAGS   (sizeof (file_flags) / sizeof (file_flags[0]))
 

Functions

static int getallflags ()
 
static int getflags (int fd, int p)
 
static void printone (int fd, int p, int verbose)
 
static int parseflags (char *s, int *p, int *n)
 
static void setone (int fd, char *v, int verbose)
 
static int getmaxfd ()
 
int fdflags_builtin (WORD_LIST *list)
 

Variables

struct {
const char * name
 
int value
 
file_flags []
 
int errno
 
char * fdflags_doc []
 
struct builtin fdflags_struct
 

Macro Definition Documentation

◆ N_FLAGS

#define N_FLAGS   (sizeof (file_flags) / sizeof (file_flags[0]))

Definition at line 79 of file fdflags.c.

Referenced by getallflags(), parseflags(), and printone().

Function Documentation

◆ getallflags()

static int getallflags ( )
static

Definition at line 87 of file fdflags.c.

References file_flags, i, and N_FLAGS.

Referenced by getflags().

88 {
89  int i, allflags;
90 
91  for (i = allflags = 0; i < N_FLAGS; i++)
92  allflags |= file_flags[i].value;
93  return allflags;
94 }
#define N_FLAGS
Definition: fdflags.c:79
static nls_uint32 nls_uint32 i
Definition: gettextP.h:74
static const struct @1 file_flags[]
Here is the caller graph for this function:

◆ getflags()

static int getflags ( int  fd,
int  p 
)
static

Definition at line 97 of file fdflags.c.

References builtin_error(), c, errno, getallflags(), and strerror().

Referenced by printone(), and setone().

98 {
99  int c, f;
100  int allflags;
101 
102  if ((c = fcntl(fd, F_GETFD)) == -1)
103  {
104  if (p)
105  builtin_error("can't get status for fd %d: %s", fd, strerror(errno));
106  return -1;
107  }
108 
109  if ((f = fcntl(fd, F_GETFL)) == -1)
110  {
111  if (p)
112  builtin_error("Can't get flags for fd %d: %s", fd, strerror(errno));
113  return -1;
114  }
115 
116  if (c)
117  f |= O_CLOEXEC;
118 
119  return f & getallflags();
120 }
char * strerror()
register GCHAR c
Definition: glob_loop.c:26
p
Definition: glob_loop.c:31
void builtin_error(char *format, va_alist) const
Definition: common.c:106
int errno
static int getallflags()
Definition: fdflags.c:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ printone()

static void printone ( int  fd,
int  p,
int  verbose 
)
static

Definition at line 123 of file fdflags.c.

References file_flags, getflags(), i, N_FLAGS, and builtin::name.

Referenced by fdflags_builtin().

124 {
125  int f;
126  size_t i;
127 
128  if ((f = getflags(fd, p)) == -1)
129  return;
130 
131  printf ("%d:", fd);
132 
133  for (i = 0; i < N_FLAGS; i++)
134  {
135  if (f & file_flags[i].value)
136  {
137  printf ("%s%s", verbose ? "+" : "", file_flags[i].name);
138  f &= ~file_flags[i].value;
139  }
140  else if (verbose)
141  printf ( "-%s", file_flags[i].name);
142  else
143  continue;
144 
145  if (f || (verbose && i != N_FLAGS - 1))
146  putchar (',');
147  }
148  printf ("\n");
149 }
#define N_FLAGS
Definition: fdflags.c:79
static nls_uint32 nls_uint32 i
Definition: gettextP.h:74
p
Definition: glob_loop.c:31
static int getflags(int fd, int p)
Definition: fdflags.c:97
static const struct @1 file_flags[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseflags()

static int parseflags ( char *  s,
int *  p,
int *  n 
)
static

Definition at line 152 of file fdflags.c.

References builtin_error(), file_flags, i, n, N_FLAGS, builtin::name, NULL, and p.

Referenced by setone().

153 {
154  int f, *v;
155  size_t i;
156 
157  f = 0;
158  *p = *n = 0;
159 
160  for (s = strtok(s, ","); s; s = strtok(NULL, ","))
161  {
162  switch (*s)
163  {
164  case '+':
165  v = p;
166  s++;
167  break;
168  case '-':
169  v = n;
170  s++;
171  break;
172  default:
173  v = &f;
174  break;
175  }
176 
177  for (i = 0; i < N_FLAGS; i++)
178  if (strcmp(s, file_flags[i].name) == 0)
179  {
180  *v |= file_flags[i].value;
181  break;
182  }
183  if (i == N_FLAGS)
184  builtin_error("invalid flag `%s'", s);
185  }
186 
187  return f;
188 }
#define N_FLAGS
Definition: fdflags.c:79
unsigned long int n
Definition: eval-plural.h:35
static nls_uint32 nls_uint32 i
Definition: gettextP.h:74
p
Definition: glob_loop.c:31
void builtin_error(char *format, va_alist) const
Definition: common.c:106
#define NULL
Definition: general.h:53
static const struct @1 file_flags[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setone()

static void setone ( int  fd,
char *  v,
int  verbose 
)
static

Definition at line 191 of file fdflags.c.

References builtin_error(), errno, FD_CLOEXEC, getflags(), n, parseflags(), pos, and strerror().

Referenced by fdflags_builtin().

192 {
193  int f, n, pos, neg, cloexec;
194 
195  f = getflags(fd, 1);
196  if (f == -1)
197  return;
198 
199  parseflags(v, &pos, &neg);
200 
201  cloexec = -1;
202  if ((pos & O_CLOEXEC) && (f & O_CLOEXEC) == 0)
203  cloexec = FD_CLOEXEC;
204  if ((neg & O_CLOEXEC) && (f & O_CLOEXEC))
205  cloexec = 0;
206  if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1)
207  builtin_error("can't set status for fd %d: %s", fd, strerror(errno));
208 
209  pos &= ~O_CLOEXEC;
210  neg &= ~O_CLOEXEC;
211  f &= ~O_CLOEXEC;
212 
213  n = f;
214  n |= pos;
215  n &= ~neg;
216 
217  if (n != f && fcntl(fd, F_SETFL, n) == -1)
218  builtin_error("can't set flags for fd %d: %s", fd, strerror(errno));
219 }
static int parseflags(char *s, int *p, int *n)
Definition: fdflags.c:152
char * strerror()
#define FD_CLOEXEC
Definition: filecntl.h:28
unsigned long int n
Definition: eval-plural.h:35
void builtin_error(char *format, va_alist) const
Definition: common.c:106
static int getflags(int fd, int p)
Definition: fdflags.c:97
int errno
static int pos
Definition: test.c:108
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getmaxfd()

static int getmaxfd ( )
static

Definition at line 222 of file fdflags.c.

References getdtablesize(), and HIGH_FD_MAX.

Referenced by fdflags_builtin().

223 {
224  int maxfd, ignore;
225 
226 #ifdef F_MAXFD
227  maxfd = fcntl (0, F_MAXFD);
228  if (maxfd > 0)
229  return maxfd;
230 #endif
231 
232  maxfd = getdtablesize ();
233  if (maxfd <= 0)
234  maxfd = HIGH_FD_MAX;
235  for (maxfd--; maxfd > 0; maxfd--)
236  if (fcntl (maxfd, F_GETFD, &ignore) != -1)
237  break;
238 
239  return maxfd;
240 }
int getdtablesize()
Definition: oslib.c:146
#define HIGH_FD_MAX
Definition: general.h:260
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fdflags_builtin()

int fdflags_builtin ( WORD_LIST list)

Definition at line 243 of file fdflags.c.

References builtin_error(), builtin_usage(), CASE_HELPOPT, errno, EX_USAGE, EXECUTION_FAILURE, EXECUTION_SUCCESS, getmaxfd(), i, internal_getopt(), legal_number(), list_optarg, loptend, word_list::next, printone(), reset_internal_getopt(), setone(), strerror(), word_desc::word, and word_list::word.

244 {
245  int opt, maxfd, i, num, verbose, setflag;
246  char *setspec;
247  WORD_LIST *l;
248  intmax_t inum;
249 
250  setflag = verbose = 0;
252  while ((opt = internal_getopt (list, "s:v")) != -1)
253  {
254  switch (opt)
255  {
256  case 's':
257  setflag = 1;
258  setspec = list_optarg;
259  break;
260  case 'v':
261  verbose = 1;
262  break;
263  CASE_HELPOPT;
264  default:
265  builtin_usage ();
266  return (EX_USAGE);
267  }
268 
269  }
270  list = loptend;
271 
272  /* Maybe we could provide some default here, but we don't yet. */
273  if (list == 0 && setflag)
274  return (EXECUTION_SUCCESS);
275 
276  if (list == 0)
277  {
278  maxfd = getmaxfd ();
279  if (maxfd < 0)
280  {
281  builtin_error ("can't get max fd: %s", strerror (errno));
282  return (EXECUTION_FAILURE);
283  }
284  for (i = 0; i < maxfd; i++)
285  printone (i, 0, verbose);
286  return (EXECUTION_SUCCESS);
287  }
288 
289  opt = EXECUTION_SUCCESS;
290  for (l = list; l; l = l->next)
291  {
292  if (legal_number (l->word->word, &inum) == 0 || inum < 0)
293  {
294  builtin_error ("%s: invalid file descriptor", l->word->word);
295  opt = EXECUTION_FAILURE;
296  continue;
297  }
298  num = inum; /* truncate to int */
299  if (setflag)
300  setone (num, setspec, verbose);
301  else
302  printone (num, 1, verbose);
303  }
304 
305  return (opt);
306 }
char * list_optarg
Definition: bashgetopt.c:41
char * strerror()
static int getmaxfd()
Definition: fdflags.c:222
char * word
Definition: command.h:127
static nls_uint32 nls_uint32 i
Definition: gettextP.h:74
#define CASE_HELPOPT
Definition: common.h:38
void builtin_error(char *format, va_alist) const
Definition: common.c:106
void builtin_usage()
Definition: common.c:145
#define EXECUTION_SUCCESS
Definition: shell.h:53
WORD_DESC * word
Definition: command.h:134
static void setone(int fd, char *v, int verbose)
Definition: fdflags.c:191
#define EX_USAGE
Definition: shell.h:71
void reset_internal_getopt()
Definition: bashgetopt.c:177
WORD_LIST * loptend
Definition: bashgetopt.c:47
#define EXECUTION_FAILURE
Definition: shell.h:52
int errno
int internal_getopt(WORD_LIST *list, char *opts)
Definition: bashgetopt.c:50
static void printone(int fd, int p, int verbose)
Definition: fdflags.c:123
int legal_number(char *string, intmax_t *result) const
Definition: general.c:175
struct word_list * next
Definition: command.h:133
Here is the call graph for this function:

Variable Documentation

◆ file_flags

const { ... } file_flags[]
Initial value:
=
{
}

Referenced by getallflags(), parseflags(), and printone().

◆ errno

int errno

Referenced by fdflags_builtin(), getflags(), and setone().

◆ fdflags_doc

char* fdflags_doc[]
Initial value:
=
{
"Display and modify file descriptor flags.",
"",
"Display or, if the -s option is supplied, set flags for each file",
"descriptor supplied as an argument. If the -v option is supplied,",
"the display is verbose, including each settable option name in the",
"form of a string such as that accepted by the -s option.",
"",
"The -s option accepts a string with a list of flag names, each preceded",
"by a `+' (set) or `-' (unset). Those changes are applied to each file",
"descriptor supplied as an argument.",
"",
"If no file descriptor arguments are supplied, the displayed information",
"consists of the status of flags for each of the shell's open files.",
(char *)NULL
}
#define NULL
Definition: general.h:53

Definition at line 308 of file fdflags.c.

◆ fdflags_struct

struct builtin fdflags_struct
Initial value:
= {
"fdflags",
"fdflags [-v] [-s flags_string] [fd ...]",
0
}
#define BUILTIN_ENABLED
Definition: builtins.h:41
char * fdflags_doc[]
Definition: fdflags.c:308
int fdflags_builtin(WORD_LIST *list)
Definition: fdflags.c:243

Definition at line 329 of file fdflags.c.