+static int is_in_group(gid_t gid)
+{
+#ifdef GETGROUPS_T
+ static gid_t last_in = GID_NONE, last_out;
+ static int ngroups = -2;
+ static GETGROUPS_T *gidset;
+ int n;
+
+ if (gid == last_in)
+ return last_out;
+ if (ngroups < -1) {
+ gid_t mygid = getgid();
+ ngroups = getgroups(0, 0);
+ /* If that didn't work, perhaps 0 isn't treated specially? */
+ if (ngroups <= 0)
+ ngroups = NGROUPS_MAX;
+ gidset = new_array(GETGROUPS_T, ngroups+1);
+ if (ngroups > 0)
+ ngroups = getgroups(ngroups, gidset);
+ /* The default gid might not be in the list on some systems. */
+ for (n = 0; n < ngroups; n++) {
+ if (gidset[n] == mygid)
+ break;
+ }
+ if (n == ngroups)
+ gidset[ngroups++] = mygid;
+ if (verbose > 3) {
+ for (n = 0; n < ngroups; n++) {
+ rprintf(FINFO, "process gid %d: %ld\n",
+ n, (long)gidset[n]);
+ }
+ }
+ }
+
+ last_in = gid;
+ for (n = 0; n < ngroups; n++) {
+ if (gidset[n] == gid)
+ return last_out = 1;
+ }
+ return last_out = 0;
+
+#else
+ static gid_t mygid = GID_NONE;
+ if (mygid == GID_NONE)
+ mygid = getgid();
+ return gid == mygid;
+#endif
+}
+