Arnaud Taddei wrote:
>I support it too. Of course make it a function in a proper perl5 module
>Good luck
I'll let someone else put it into a proper perl5 module for version 2.0.
For now, we still only need perl4 to run majordomo, so I won't assume
any perl5 functionality exists.
I hacked the following perl4 clone of chown based on SunOS 4.1.x
semantics.
I have done only trivial testing on it (under SunOS 4.1.x and HP-UX
09.05), please feel free to enhance it if you so desire (please send
me any bug fixes or enhancments).
The script below assumes that an executable called 'perl' exists
somewhere in the users path. The script is its own man page. Use:
nroff -man chown.perl
to get the documentation for it.
Enjoy!
-- Mark
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: chown.perl
# Wrapped by mdb@cisco.com on Wed Oct 30 03:41:33 1996
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'chown.perl' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'chown.perl'\"
else
echo shar: Extracting \"'chown.perl'\" \(5468 characters\)
sed "s/^X//" >'chown.perl' <<'END_OF_FILE'
X: # use perl -*-Perl-*-
X eval 'exec perl -S $0 ${1+"$@"}'
X if 0;
X'di ';
X'ds 00 "';
X'ig 00 ';
X
X# Copyright (c) 1996 Mark D. Baushke
X# All rights reserved. You may distribute under the terms of either
X# the GNU General Public License or the Perl5 Artistic License, as
X# specified in the perl distribution.
X
X# Note: above incantations allow this script will be fed to a perl
X# executable somewhere in the users path as well as being run through
X# the 'nroff -man' command to print out the man page for this command.
X
Xrequire 'find.pl';
Xrequire 'getopts.pl';
X
X# Note: It might be desirable to modify the do_chown() function to
X# clear the set-user-ID and set-group-ID bits when some uid other than
X# the super-user runs the script. Doing so would allow this abort() to
X# be removed. However, some systems like SunOS 4.1.x only allows the
X# super-user to use the chown function. Given that the intent of this
X# script is to work uniformly on all systems, the "Must be root"
X# restriction seems almost reasonable.
X&abort("Must be root to use chown.perl") if ($< != 0);
X
X&Getopts('fR'); # process optional command-line args
X
X# Check to see if the remaining command-line args are enough to
X# process the command
X&usage() if (scalar(@ARGV) < 2);
X
X# set the $owner and optional $group fields.
X($owner, $group) = split(/\./, shift(@ARGV), 2);
X($uid, $gid) = ($owner, $group); # default the numeric values
X
X# if the owner contains non-numeric characters, it needs to be
X# looked up in the password file for the correct numeric user id.
Xif ($owner !~ /^[0-9]+$/) {
X ($name, $passwd, $uid) = getpwnam($owner); # look up numeric uid
X &abort("$0: unknown user id: $owner") if ($name ne $owner);
X}
X
X# if the optional group was set and was not a numeric value, then it
X# needs to be looked up in the group file for the correct numeric
X# group id.
Xif (($group ne '') && ($group !~ /^[0-9]+$/)) {
X ($name, $passwd, $gid) = getgrnam($group); # look up numeric gid
X &abort("$0: unknown group: $group") if ($name ne $group);
X}
X
X# Go through all of the command line args and assume they are files to
X# have their ownership changed. Skip symbolic links for possible
X# recursive processing. The default find.pl does not traverse symbolic
X# links either, so the wanted subroutine may call &do_chown directly
X# too.
Xforeach $entry (@ARGV) {
X &do_chown($uid, $gid, $entry);
X next if ( -l $entry ); # do not attempt to follow symlinks
X &find($entry) if ( $opt_R && -d $entry );
X}
X
Xexit 0;
X
X# Optionally print out an error message and exit with non-zero return
X# value. It looks like SunOS uses a value of 255 when chown fails, so
X# use that as the return value.
Xsub abort {
X print STDERR join("\n", @_),"\n" if (!$opt_f);
X exit 255;
X}
X
X# perform the chown operation. Retain the old group id if the command
X# line did not specify a new one to use.
Xsub do_chown {
X local($newuid, $newgid, $file) = @_;
X local($olduid, $oldgid);
X
X ($olduid, $oldgid) = (lstat($file))[4..5];
X $newgid = $oldgid if ($newgid eq '');
X if (($olduid != $newuid) || ($oldgid != $newgid)) {
X if (!chown($uid, $newgid, $file)) {
X &warn("$0: $file: $!");
X }
X }
X}
X
X# Give the user a summary of how to run this script.
Xsub usage {
X &abort("$0 [ -fR ] owner[.group] filename ...");
X}
X
X# when doing a -R recursion, try to change the ownership of any and
X# all files.
Xsub wanted {
X &do_chown($uid, $gid, $_);
X}
X
X# print just the warning, but continue execution.
Xsub warn {
X print STDERR join("\n", @_),"\n" if (!$opt_f);
X}
X
X##############################################################################
X
X # These next few lines are legal in both Perl and nroff.
X
X.00 ; # finish .ig
X
X'di \" finish diversion--previous line must be blank
X.nr nl 0-1 \" fake up transition to first page again
X.nr % 0 \" start at page 1
X'; __END__ ############# From here on it's a standard manual page ############
X.\" Copyright (c) 1996 Mark D. Baushke
X.\" All rights reserved. You may distribute under the terms of either
X.\" the GNU General Public License or the Perl5 Artistic License, as
X.\" specified in the perl distribution.
X.\"
X.\" @(#)chown.8 1.0 96/10/30 mdb; from UCB 6.2 5/22/86
X.TH CHOWN 8 "30 October 1996"
X.SH NAME
Xchown.perl \- change owner
X.SH SYNOPSIS
X.B chown.perl
X[
X.B \-fR
X]
X.IR owner [. group ] " filename " .\|.\|.
X.SH DESCRIPTION
X.IX "chown command" "" "\fLchown\fP \(em change owner of file"
X.IX file "change ownership" file "change ownership \(em \fLchown\fP"
X.IX "owner of file, change \(em \fLchown\fP"
X.IX change "owner of file \(em \fLchown\fR"
X.IX "user ID" "\fLchown\fR \(em change user ID of file"
X.LP
X.B chown
Xchanges the owner of the
X.IR filename s
Xto
X.IR owner .
XThe owner may be either a decimal user
X.SM ID
X(\s-1UID\s0)
Xor
Xa login name found in the password file.
XAn optional
X.I group
Xmay also be specified.
XThe group may be either a decimal group
X.SM ID
X(\s-1GID\s0)
Xor a group name found in the \s-1GID\s0 file.
X.LP
XOnly the super-user can change owner, in order to simplify accounting
Xprocedures.
X.SH OPTIONS
X.TP
X.B \-f
XDo not report errors.
X.TP
X.B \-R
XRecursively descend into directories setting the ownership of all
Xfiles in each directory encountered. When symbolic links are
Xencountered, their ownership is changed, but they are not traversed.
X.SH FILES
X.PD 0
X.TP 20
X.B /etc/passwd
Xpassword file
X.B /etc/group
Xgroup file
X.PD
X.SH "SEE ALSO"
X.BR chgrp (1),
X.BR chown (2V),
X.BR group (5),
X.BR passwd (5),
X.BR perl (1)
END_OF_FILE
if test 5468 -ne `wc -c <'chown.perl'`; then
echo shar: \"'chown.perl'\" unpacked with wrong size!
fi
chmod +x 'chown.perl'
# end of 'chown.perl'
fi
echo shar: End of shell archive.
exit 0
References:
|
|