1/* shift.c, created from shift.def. */
2#line 23 "shift.def"
3
4#include <config.h>
5
6#if defined (HAVE_UNISTD_H)
7#  ifdef _MINIX
8#    include <sys/types.h>
9#  endif
10#  include <unistd.h>
11#endif
12
13#include "../bashansi.h"
14#include "../bashintl.h"
15
16#include "../shell.h"
17#include "common.h"
18
19#line 45 "shift.def"
20
21int print_shift_error;
22
23/* Shift the arguments ``left''.  Shift DOLLAR_VARS down then take one
24   off of REST_OF_ARGS and place it into DOLLAR_VARS[9].  If LIST has
25   anything in it, it is a number which says where to start the
26   shifting.  Return > 0 if `times' > $#, otherwise 0. */
27int
28shift_builtin (list)
29     WORD_LIST *list;
30{
31  intmax_t times;
32  register int count;
33  WORD_LIST *temp;
34
35  times = get_numeric_arg (list, 0);
36
37  if (times == 0)
38    return (EXECUTION_SUCCESS);
39  else if (times < 0)
40    {
41      sh_erange (list ? list->word->word : NULL, _("shift count"));
42      return (EXECUTION_FAILURE);
43    }
44  else if (times > number_of_args ())
45    {
46      if (print_shift_error)
47	sh_erange (list ? list->word->word : NULL, _("shift count"));
48      return (EXECUTION_FAILURE);
49    }
50
51  while (times-- > 0)
52    {
53      if (dollar_vars[1])
54	free (dollar_vars[1]);
55
56      for (count = 1; count < 9; count++)
57	dollar_vars[count] = dollar_vars[count + 1];
58
59      if (rest_of_args)
60	{
61	  temp = rest_of_args;
62	  dollar_vars[9] = savestring (temp->word->word);
63	  rest_of_args = rest_of_args->next;
64	  temp->next = (WORD_LIST *)NULL;
65	  dispose_words (temp);
66	}
67      else
68	dollar_vars[9] = (char *)NULL;
69    }
70  return (EXECUTION_SUCCESS);
71}
72