1d9cda0231b8698d489318c46547bd1305d46d52
[platform/upstream/bash.git] / general.c
1 /* general.c -- Stuff that is used by all files. */
2
3 /* Copyright (C) 1987-1999 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with Bash; see the file COPYING.  If not, write to the Free Software
19    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 #include "config.h"
22
23 #include "bashtypes.h"
24 #ifndef _MINIX
25 #  include <sys/param.h>
26 #endif
27 #include "posixstat.h"
28
29 #if defined (HAVE_UNISTD_H)
30 #  include <unistd.h>
31 #endif
32
33 #include "filecntl.h"
34 #include "bashansi.h"
35 #include <stdio.h>
36 #include "chartypes.h"
37 #include <errno.h>
38
39 #include "shell.h"
40 #include <tilde/tilde.h>
41
42 #if !defined (errno)
43 extern int errno;
44 #endif /* !errno */
45
46 extern int expand_aliases;
47 extern int interrupt_immediately;
48 extern int interactive_comments;
49 extern int check_hashed_filenames;
50 extern int source_uses_path;
51 extern int source_searches_cwd;
52
53 /* A standard error message to use when getcwd() returns NULL. */
54 char *bash_getcwd_errstr = "getcwd: cannot access parent directories";
55
56 /* Do whatever is necessary to initialize `Posix mode'. */
57 void
58 posix_initialize (on)
59      int on;
60 {
61   /* Things that should be turned on when posix mode is enabled. */
62   if (on != 0)
63     {
64       interactive_comments = source_uses_path = expand_aliases = 1;
65     }
66
67   /* Things that should be turned on when posix mode is disabled. */
68   if (on == 0)
69     {
70       source_searches_cwd = 1;
71       expand_aliases = interactive_shell;
72     }
73 }
74
75 /* **************************************************************** */
76 /*                                                                  */
77 /*  Functions to convert to and from and display non-standard types */
78 /*                                                                  */
79 /* **************************************************************** */
80
81 #if defined (RLIMTYPE)
82 RLIMTYPE
83 string_to_rlimtype (s)
84      char *s;
85 {
86   RLIMTYPE ret;
87   int neg;
88
89   ret = 0;
90   neg = 0;
91   while (s && *s && whitespace (*s))
92     s++;
93   if (*s == '-' || *s == '+')
94     {
95       neg = *s == '-';
96       s++;
97     }
98   for ( ; s && *s && DIGIT (*s); s++)
99     ret = (ret * 10) + TODIGIT (*s);
100   return (neg ? -ret : ret);
101 }
102
103 void
104 print_rlimtype (n, addnl)
105      RLIMTYPE n;
106      int addnl;
107 {
108   char s[INT_STRLEN_BOUND (RLIMTYPE) + 1], *p;
109
110   p = s + sizeof(s);
111   *--p = '\0';
112
113   if (n < 0)
114     {
115       do
116         *--p = '0' - n % 10;
117       while ((n /= 10) != 0);
118
119       *--p = '-';
120     }
121   else
122     {
123       do
124         *--p = '0' + n % 10;
125       while ((n /= 10) != 0);
126     }
127
128   printf ("%s%s", p, addnl ? "\n" : "");
129 }
130 #endif /* RLIMTYPE */
131
132 /* **************************************************************** */
133 /*                                                                  */
134 /*                     Input Validation Functions                   */
135 /*                                                                  */
136 /* **************************************************************** */
137
138 /* Return non-zero if all of the characters in STRING are digits. */
139 int
140 all_digits (string)
141      char *string;
142 {
143   register char *s;
144
145   for (s = string; *s; s++)
146     if (DIGIT (*s) == 0)
147       return (0);
148
149   return (1);
150 }
151
152 /* Return non-zero if the characters pointed to by STRING constitute a
153    valid number.  Stuff the converted number into RESULT if RESULT is
154    not null. */
155 int
156 legal_number (string, result)
157      char *string;
158      long *result;
159 {
160   long value;
161   char *ep;
162
163   if (result)
164     *result = 0;
165
166   errno = 0;
167   value = strtol (string, &ep, 10);
168   if (errno)
169     return 0;   /* errno is set on overflow or underflow */
170
171   /* Skip any trailing whitespace, since strtol does not. */
172   while (whitespace (*ep))
173     ep++;
174
175   /* If *string is not '\0' but *ep is '\0' on return, the entire string
176      is valid. */
177   if (string && *string && *ep == '\0')
178     {
179       if (result)
180         *result = value;
181       /* The SunOS4 implementation of strtol() will happily ignore
182          overflow conditions, so this cannot do overflow correctly
183          on those systems. */
184       return 1;
185     }
186     
187   return (0);
188 }
189
190 /* Return 1 if this token is a legal shell `identifier'; that is, it consists
191    solely of letters, digits, and underscores, and does not begin with a
192    digit. */
193 int
194 legal_identifier (name)
195      char *name;
196 {
197   register char *s;
198   unsigned char c;
199
200   if (!name || !(c = *name) || (legal_variable_starter (c) == 0))
201     return (0);
202
203   for (s = name + 1; (c = *s) != 0; s++)
204     {
205       if (legal_variable_char (c) == 0)
206         return (0);
207     }
208   return (1);
209 }
210
211 /* Make sure that WORD is a valid shell identifier, i.e.
212    does not contain a dollar sign, nor is quoted in any way.  Nor
213    does it consist of all digits.  If CHECK_WORD is non-zero,
214    the word is checked to ensure that it consists of only letters,
215    digits, and underscores. */
216 int
217 check_identifier (word, check_word)
218      WORD_DESC *word;
219      int check_word;
220 {
221   if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word))
222     {
223       internal_error ("`%s': not a valid identifier", word->word);
224       return (0);
225     }
226   else if (check_word && legal_identifier (word->word) == 0)
227     {
228       internal_error ("`%s': not a valid identifier", word->word);
229       return (0);
230     }
231   else
232     return (1);
233 }
234
235 /* **************************************************************** */
236 /*                                                                  */
237 /*           Functions to manage files and file descriptors         */
238 /*                                                                  */
239 /* **************************************************************** */
240
241 /* A function to unset no-delay mode on a file descriptor.  Used in shell.c
242    to unset it on the fd passed as stdin.  Should be called on stdin if
243    readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */
244
245 #if !defined (O_NDELAY)
246 #  if defined (FNDELAY)
247 #    define O_NDELAY FNDELAY
248 #  endif
249 #endif /* O_NDELAY */
250
251 /* Make sure no-delay mode is not set on file descriptor FD. */
252 int
253 sh_unset_nodelay_mode (fd)
254      int fd;
255 {
256   int flags, bflags;
257
258   if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
259     return -1;
260
261   bflags = 0;
262
263   /* This is defined to O_NDELAY in filecntl.h if O_NONBLOCK is not present
264      and O_NDELAY is defined. */
265 #ifdef O_NONBLOCK
266   bflags |= O_NONBLOCK;
267 #endif
268
269 #ifdef O_NDELAY
270   bflags |= O_NDELAY;
271 #endif
272
273   if (flags & bflags)
274     {
275       flags &= ~bflags;
276       return (fcntl (fd, F_SETFL, flags));
277     }
278
279   return 0;
280 }
281
282 /* There is a bug in the NeXT 2.1 rlogind that causes opens
283    of /dev/tty to fail. */
284
285 #if defined (__BEOS__)
286 /* On BeOS, opening in non-blocking mode exposes a bug in BeOS, so turn it
287    into a no-op.  This should probably go away in the future. */
288 #  undef O_NONBLOCK
289 #  define O_NONBLOCK 0
290 #endif /* __BEOS__ */
291
292 void
293 check_dev_tty ()
294 {
295   int tty_fd;
296   char *tty;
297
298   tty_fd = open ("/dev/tty", O_RDWR|O_NONBLOCK);
299
300   if (tty_fd < 0)
301     {
302       tty = (char *)ttyname (fileno (stdin));
303       if (tty == 0)
304         return;
305       tty_fd = open (tty, O_RDWR|O_NONBLOCK);
306     }
307   close (tty_fd);
308 }
309
310 /* Return 1 if PATH1 and PATH2 are the same file.  This is kind of
311    expensive.  If non-NULL STP1 and STP2 point to stat structures
312    corresponding to PATH1 and PATH2, respectively. */
313 int
314 same_file (path1, path2, stp1, stp2)
315      char *path1, *path2;
316      struct stat *stp1, *stp2;
317 {
318   struct stat st1, st2;
319
320   if (stp1 == NULL)
321     {
322       if (stat (path1, &st1) != 0)
323         return (0);
324       stp1 = &st1;
325     }
326
327   if (stp2 == NULL)
328     {
329       if (stat (path2, &st2) != 0)
330         return (0);
331       stp2 = &st2;
332     }
333
334   return ((stp1->st_dev == stp2->st_dev) && (stp1->st_ino == stp2->st_ino));
335 }
336
337 /* Move FD to a number close to the maximum number of file descriptors
338    allowed in the shell process, to avoid the user stepping on it with
339    redirection and causing us extra work.  If CHECK_NEW is non-zero,
340    we check whether or not the file descriptors are in use before
341    duplicating FD onto them.  MAXFD says where to start checking the
342    file descriptors.  If it's less than 20, we get the maximum value
343    available from getdtablesize(2). */
344 int
345 move_to_high_fd (fd, check_new, maxfd)
346      int fd, check_new, maxfd;
347 {
348   int script_fd, nfds, ignore;
349
350   if (maxfd < 20)
351     {
352       nfds = getdtablesize ();
353       if (nfds <= 0)
354         nfds = 20;
355       if (nfds > 256)
356         nfds = 256;
357     }
358   else
359     nfds = maxfd;
360
361   for (nfds--; check_new && nfds > 3; nfds--)
362     if (fcntl (nfds, F_GETFD, &ignore) == -1)
363       break;
364
365   if (nfds && fd != nfds && (script_fd = dup2 (fd, nfds)) != -1)
366     {
367       if (check_new == 0 || fd != fileno (stderr))      /* don't close stderr */
368         close (fd);
369       return (script_fd);
370     }
371
372   return (fd);
373 }
374  
375 /* Return non-zero if the characters from SAMPLE are not all valid
376    characters to be found in the first line of a shell script.  We
377    check up to the first newline, or SAMPLE_LEN, whichever comes first.
378    All of the characters must be printable or whitespace. */
379
380 int
381 check_binary_file (sample, sample_len)
382      char *sample;
383      int sample_len;
384 {
385   register int i;
386   unsigned char c;
387
388   for (i = 0; i < sample_len; i++)
389     {
390       c = sample[i];
391       if (c == '\n')
392         return (0);
393
394       if (ISSPACE (c) == 0 && ISPRINT (c) == 0)
395         return (1);
396     }
397
398   return (0);
399 }
400
401 /* **************************************************************** */
402 /*                                                                  */
403 /*                  Functions to manipulate pathnames               */
404 /*                                                                  */
405 /* **************************************************************** */
406
407 /* Turn STRING (a pathname) into an absolute pathname, assuming that
408    DOT_PATH contains the symbolic location of `.'.  This always
409    returns a new string, even if STRING was an absolute pathname to
410    begin with. */
411 char *
412 make_absolute (string, dot_path)
413      char *string, *dot_path;
414 {
415   char *result;
416
417   if (dot_path == 0 || ABSPATH(string))
418     result = savestring (string);
419   else
420     result = sh_makepath (dot_path, string, 0);
421
422   return (result);
423 }
424
425 /* Return 1 if STRING contains an absolute pathname, else 0.  Used by `cd'
426    to decide whether or not to look up a directory name in $CDPATH. */
427 int
428 absolute_pathname (string)
429      const char *string;
430 {
431   if (string == 0 || *string == '\0')
432     return (0);
433
434   if (ABSPATH(string))
435     return (1);
436
437   if (string[0] == '.' && PATHSEP(string[1]))   /* . and ./ */
438     return (1);
439
440   if (string[0] == '.' && string[1] == '.' && PATHSEP(string[2]))       /* .. and ../ */
441     return (1);
442
443   return (0);
444 }
445
446 /* Return 1 if STRING is an absolute program name; it is absolute if it
447    contains any slashes.  This is used to decide whether or not to look
448    up through $PATH. */
449 int
450 absolute_program (string)
451      const char *string;
452 {
453   return ((char *)strchr (string, '/') != (char *)NULL);
454 }
455
456 /* Return the `basename' of the pathname in STRING (the stuff after the
457    last '/').  If STRING is not a full pathname, simply return it. */
458 char *
459 base_pathname (string)
460      char *string;
461 {
462   char *p;
463
464   if (absolute_pathname (string) == 0)
465     return (string);
466
467   p = (char *)strrchr (string, '/');
468   return (p ? ++p : string);
469 }
470
471 /* Return the full pathname of FILE.  Easy.  Filenames that begin
472    with a '/' are returned as themselves.  Other filenames have
473    the current working directory prepended.  A new string is
474    returned in either case. */
475 char *
476 full_pathname (file)
477      char *file;
478 {
479   char *ret;
480
481   file = (*file == '~') ? bash_tilde_expand (file) : savestring (file);
482
483   if (ABSPATH(file))
484     return (file);
485
486   ret = sh_makepath ((char *)NULL, file, (MP_DOCWD|MP_RMDOT));
487   free (file);
488
489   return (ret);
490 }
491
492 /* A slightly related function.  Get the prettiest name of this
493    directory possible. */
494 static char tdir[PATH_MAX];
495
496 /* Return a pretty pathname.  If the first part of the pathname is
497    the same as $HOME, then replace that with `~'.  */
498 char *
499 polite_directory_format (name)
500      char *name;
501 {
502   char *home;
503   int l;
504
505   home = get_string_value ("HOME");
506   l = home ? strlen (home) : 0;
507   if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/'))
508     {
509       strncpy (tdir + 1, name + l, sizeof(tdir) - 2);
510       tdir[0] = '~';
511       tdir[sizeof(tdir) - 1] = '\0';
512       return (tdir);
513     }
514   else
515     return (name);
516 }
517
518 /* Given a string containing units of information separated by colons,
519    return the next one pointed to by (P_INDEX), or NULL if there are no more.
520    Advance (P_INDEX) to the character after the colon. */
521 char *
522 extract_colon_unit (string, p_index)
523      char *string;
524      int *p_index;
525 {
526   int i, start, len;
527   char *value;
528
529   if (string == 0)
530     return (string);
531
532   len = strlen (string);
533   if (*p_index >= len)
534     return ((char *)NULL);
535
536   i = *p_index;
537
538   /* Each call to this routine leaves the index pointing at a colon if
539      there is more to the path.  If I is > 0, then increment past the
540      `:'.  If I is 0, then the path has a leading colon.  Trailing colons
541      are handled OK by the `else' part of the if statement; an empty
542      string is returned in that case. */
543   if (i && string[i] == ':')
544     i++;
545
546   for (start = i; string[i] && string[i] != ':'; i++)
547     ;
548
549   *p_index = i;
550
551   if (i == start)
552     {
553       if (string[i])
554         (*p_index)++;
555       /* Return "" in the case of a trailing `:'. */
556       value = (char *)xmalloc (1);
557       value[0] = '\0';
558     }
559   else
560     value = substring (string, start, i);
561
562   return (value);
563 }
564
565 /* **************************************************************** */
566 /*                                                                  */
567 /*                  Tilde Initialization and Expansion              */
568 /*                                                                  */
569 /* **************************************************************** */
570
571 #if defined (PUSHD_AND_POPD)
572 extern char *get_dirstack_from_string __P((char *));
573 #endif
574
575 /* Reserved for post-bash-2.05a */
576 static char **bash_tilde_prefixes;
577 static char **bash_tilde_suffixes;
578
579 /* If tilde_expand hasn't been able to expand the text, perhaps it
580    is a special shell expansion.  This function is installed as the
581    tilde_expansion_preexpansion_hook.  It knows how to expand ~- and ~+.
582    If PUSHD_AND_POPD is defined, ~[+-]N expands to directories from the
583    directory stack. */
584 static char *
585 bash_special_tilde_expansions (text)
586      char *text;
587 {
588   char *result;
589
590   result = (char *)NULL;
591
592   if (text[0] == '+' && text[1] == '\0')
593     result = get_string_value ("PWD");
594   else if (text[0] == '-' && text[1] == '\0')
595     result = get_string_value ("OLDPWD");
596 #if defined (PUSHD_AND_POPD)
597   else if (DIGIT (*text) || ((*text == '+' || *text == '-') && DIGIT (text[1])))
598     result = get_dirstack_from_string (text);
599 #endif
600
601   return (result ? savestring (result) : (char *)NULL);
602 }
603
604 /* Initialize the tilde expander.  In Bash, we handle `~-' and `~+', as
605    well as handling special tilde prefixes; `:~" and `=~' are indications
606    that we should do tilde expansion. */
607 void
608 tilde_initialize ()
609 {
610   static int times_called = 0;
611
612   /* Tell the tilde expander that we want a crack first. */
613   tilde_expansion_preexpansion_hook = bash_special_tilde_expansions;
614
615   /* Tell the tilde expander about special strings which start a tilde
616      expansion, and the special strings that end one.  Only do this once.
617      tilde_initialize () is called from within bashline_reinitialize (). */
618   if (times_called++ == 0)
619     {
620       bash_tilde_prefixes = alloc_array (3);
621       bash_tilde_prefixes[0] = "=~";
622       bash_tilde_prefixes[1] = ":~";
623       bash_tilde_prefixes[2] = (char *)NULL;
624
625       tilde_additional_prefixes = bash_tilde_prefixes;
626
627       bash_tilde_suffixes = alloc_array (3);
628       bash_tilde_suffixes[0] = ":";
629       bash_tilde_suffixes[1] = "=~";    /* XXX - ?? */
630       bash_tilde_suffixes[2] = (char *)NULL;
631
632       tilde_additional_suffixes = bash_tilde_suffixes;
633     }
634 }
635
636 /* POSIX.2, 3.6.1:  A tilde-prefix consists of an unquoted tilde character
637    at the beginning of the word, followed by all of the characters preceding
638    the first unquoted slash in the word, or all the characters in the word
639    if there is no slash...If none of the characters in the tilde-prefix are
640    quoted, the characters in the tilde-prefix following the tilde shell be
641    treated as a possible login name. */
642
643 #define TILDE_END(c)    ((c) == '\0' || (c) == '/' || (c) == ':')
644
645 static int
646 unquoted_tilde_word (s)
647      const char *s;
648 {
649   const char *r;
650
651   for (r = s; TILDE_END(*r) == 0; r++)
652     {
653       switch (*r)
654         {
655         case '\\':
656         case '\'':
657         case '"':
658           return 0;
659         }
660     }
661   return 1;
662 }
663
664 /* Tilde-expand S by running it through the tilde expansion library. */
665 char *
666 bash_tilde_expand (s)
667      const char *s;
668 {
669   int old_immed, r;
670   char *ret;
671
672   old_immed = interrupt_immediately;
673   interrupt_immediately = 1;
674   r = (*s == '~') ? unquoted_tilde_word (s) : 1;
675   ret = r ? tilde_expand (s) : savestring (s);
676   interrupt_immediately = old_immed;
677   return (ret);
678 }
679
680 /* **************************************************************** */
681 /*                                                                  */
682 /*        Functions to manipulate and search the group list         */
683 /*                                                                  */
684 /* **************************************************************** */
685
686 static int ngroups, maxgroups;
687
688 /* The set of groups that this user is a member of. */
689 static GETGROUPS_T *group_array = (GETGROUPS_T *)NULL;
690
691 #if !defined (NOGROUP)
692 #  define NOGROUP (gid_t) -1
693 #endif
694
695 #if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
696 #  define getmaxgroups() sysconf(_SC_NGROUPS_MAX)
697 #else
698 #  if defined (NGROUPS_MAX)
699 #    define getmaxgroups() NGROUPS_MAX
700 #  else /* !NGROUPS_MAX */
701 #    if defined (NGROUPS)
702 #      define getmaxgroups() NGROUPS
703 #    else /* !NGROUPS */
704 #      define getmaxgroups() 64
705 #    endif /* !NGROUPS */
706 #  endif /* !NGROUPS_MAX */
707 #endif /* !HAVE_SYSCONF || !SC_NGROUPS_MAX */
708
709 static void
710 initialize_group_array ()
711 {
712   register int i;
713
714   if (maxgroups == 0)
715     maxgroups = getmaxgroups ();
716
717   ngroups = 0;
718   group_array = (GETGROUPS_T *)xrealloc (group_array, maxgroups * sizeof (GETGROUPS_T));
719
720 #if defined (HAVE_GETGROUPS)
721   ngroups = getgroups (maxgroups, group_array);
722 #endif
723
724   /* If getgroups returns nothing, or the OS does not support getgroups(),
725      make sure the groups array includes at least the current gid. */
726   if (ngroups == 0)
727     {
728       group_array[0] = current_user.gid;
729       ngroups = 1;
730     }
731
732   /* If the primary group is not in the groups array, add it as group_array[0]
733      and shuffle everything else up 1, if there's room. */
734   for (i = 0; i < ngroups; i++)
735     if (current_user.gid == (gid_t)group_array[i])
736       break;
737   if (i == ngroups && ngroups < maxgroups)
738     {
739       for (i = ngroups; i > 0; i--)
740         group_array[i] = group_array[i - 1];
741       group_array[0] = current_user.gid;
742       ngroups++;
743     }
744
745   /* If the primary group is not group_array[0], swap group_array[0] and
746      whatever the current group is.  The vast majority of systems should
747      not need this; a notable exception is Linux. */
748   if (group_array[0] != current_user.gid)
749     {
750       for (i = 0; i < ngroups; i++)
751         if (group_array[i] == current_user.gid)
752           break;
753       if (i < ngroups)
754         {
755           group_array[i] = group_array[0];
756           group_array[0] = current_user.gid;
757         }
758     }
759 }
760
761 /* Return non-zero if GID is one that we have in our groups list. */
762 int
763 #if defined (__STDC__) || defined ( _MINIX)
764 group_member (gid_t gid)
765 #else
766 group_member (gid)
767      gid_t gid;
768 #endif /* !__STDC__ && !_MINIX */
769 {
770 #if defined (HAVE_GETGROUPS)
771   register int i;
772 #endif
773
774   /* Short-circuit if possible, maybe saving a call to getgroups(). */
775   if (gid == current_user.gid || gid == current_user.egid)
776     return (1);
777
778 #if defined (HAVE_GETGROUPS)
779   if (ngroups == 0)
780     initialize_group_array ();
781
782   /* In case of error, the user loses. */
783   if (ngroups <= 0)
784     return (0);
785
786   /* Search through the list looking for GID. */
787   for (i = 0; i < ngroups; i++)
788     if (gid == (gid_t)group_array[i])
789       return (1);
790 #endif
791
792   return (0);
793 }
794
795 char **
796 get_group_list (ngp)
797      int *ngp;
798 {
799   static char **group_vector = (char **)NULL;
800   register int i;
801   char *nbuf;
802
803   if (group_vector)
804     {
805       if (ngp)
806         *ngp = ngroups;
807       return group_vector;
808     }
809
810   if (ngroups == 0)
811     initialize_group_array ();
812
813   if (ngroups <= 0)
814     {
815       if (ngp)
816         *ngp = 0;
817       return (char **)NULL;
818     }
819
820   group_vector = alloc_array (ngroups);
821   for (i = 0; i < ngroups; i++)
822     {
823       nbuf = itos (group_array[i]);
824       group_vector[i] = nbuf;
825     }
826
827   if (ngp)
828     *ngp = ngroups;
829   return group_vector;
830 }
831
832 int *
833 get_group_array (ngp)
834      int *ngp;
835 {
836   int i;
837   static int *group_iarray = (int *)NULL;
838
839   if (group_iarray)
840     {
841       if (ngp)
842         *ngp = ngroups;
843       return (group_iarray);
844     }
845
846   if (ngroups == 0)
847     initialize_group_array ();    
848
849   if (ngroups <= 0)
850     {
851       if (ngp)
852         *ngp = 0;
853       return (int *)NULL;
854     }
855
856   group_iarray = (int *)xmalloc (ngroups * sizeof (int));
857   for (i = 0; i < ngroups; i++)
858     group_iarray[i] = (int)group_array[i];
859
860   if (ngp)
861     *ngp = ngroups;
862   return group_iarray;
863 }