+ /*
+ * If the source was setuid, set the bits on the copy if the copy
+ * was created and is owned by the same uid. If the source was
+ * setgid, set the bits on the copy if the copy was created and is
+ * owned by the same gid and the user is a member of that group.
+ */
+ else if (fs->st_mode & (S_ISUID|S_ISGID)) {
+ if (fs->st_mode&S_ISUID && myuid != fs->st_uid)
+ fs->st_mode &= ~S_ISUID;
+ if (fs->st_mode & S_ISGID) {
+ if (fstat(to_fd, &to_stat)) {
+ error(to.p_path);
+ fs->st_mode &= ~S_ISGID;
+ }
+ else if (fs->st_gid != to_stat.st_gid ||
+ !ismember(fs->st_gid))
+ fs->st_mode &= ~S_ISGID;
+ }
+ if (fs->st_mode & (S_ISUID|S_ISGID) && fchmod(to_fd,
+ fs->st_mode & (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) &
+ ~myumask))
+ error(to.p_path);
+ }