blob: e07e8e99cd35b55f135603473b44b26d0ef96022 [file] [log] [blame]
Dan Pasanenc6e37862014-10-02 14:08:59 -05001/* shift.c, created from shift.def. */
2#line 22 "./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 49 "./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 if (get_numeric_arg (list, 0, &times) == 0)
36 return (EXECUTION_FAILURE);
37
38 if (times == 0)
39 return (EXECUTION_SUCCESS);
40 else if (times < 0)
41 {
42 sh_erange (list ? list->word->word : NULL, _("shift count"));
43 return (EXECUTION_FAILURE);
44 }
45 else if (times > number_of_args ())
46 {
47 if (print_shift_error)
48 sh_erange (list ? list->word->word : NULL, _("shift count"));
49 return (EXECUTION_FAILURE);
50 }
51
52 while (times-- > 0)
53 {
54 if (dollar_vars[1])
55 free (dollar_vars[1]);
56
57 for (count = 1; count < 9; count++)
58 dollar_vars[count] = dollar_vars[count + 1];
59
60 if (rest_of_args)
61 {
62 temp = rest_of_args;
63 dollar_vars[9] = savestring (temp->word->word);
64 rest_of_args = rest_of_args->next;
65 temp->next = (WORD_LIST *)NULL;
66 dispose_words (temp);
67 }
68 else
69 dollar_vars[9] = (char *)NULL;
70 }
71 return (EXECUTION_SUCCESS);
72}