Subject: FVWM: Perl script to convert all users .fvwmrc's

From: Austin Donnelly <[email-masked]>
Date: Fri, 19 Apr 96 15:51 BST

Hi,

I'm in the process of packaging up fvwm2 for Debian, so I'm doing
quite a bit of integration of fvwm with the rest of the system.

To help this, I have written a perl script which, when run as root,
will go through all the users who have .fvwmrcs and convert them
using the fvwmrc_convert AWK script provided with fvwm2.

I have also improved the AWK script somewhat: see my other message for
the diffs.

Chuck: could you put this script in the utils directory, please ?

Thanks,
Austin

------------------------- cut cut cut ---------------------------------
#!/usr/bin/perl
#  Copyright (C)1996 Austin Donnelly <[email-masked]>.
#  This is free software; you may modify and distribute it under the
#  terms of the GNU General Public Licence as published by the Free
#  Software Foundation, version 2 or at your option any later version.
#  There is NO WARRANTY.

$CONVPROG="fvwmrc_convert";

# Syscall number for setgroups(), from /usr/include/sys/syscall.h
#$SYS_SETGROUPS=81; # valid for linux, fill in for your flavour of unix
die "I need configuring" if (!defined($SYS_SETGROUPS));

print "Checking which users have untranslated ~/.fvwmrc files...\n";

# Read password file & build list of affected users
@todo=();
setpwent();
($name,$passwd,$uid,$gid,
 $quota,$comment,$gcos,$dir,$shell) = getpwent();
while(defined($uid))
{
    push(@todo, "$uid:$gid:$dir")
	if (-f "$dir/.fvwmrc" && ! -f "$dir/.fvwm2rc");
    ($name,$passwd,$uid,$gid,
     $quota,$comment,$gcos,$dir,$shell) = getpwent();
}
endpwent();

if ($#todo == -1)
{
    print "No user has an untranslated ~/.fvwmrc - no conversion necessary.\n";
    exit 0;
}

print ($#todo + 1);
print " users need conversion, shall I convert them all now?\n";
$resp="<invalid>";
while(! (($resp eq "") || ($resp =~ /^y/io) || ($resp =~ /^n/io)))
{
    print "Type Y to convert, N to skip [Y]: ";
    $resp=<STDIN>;
    chop($resp);
}

if ($resp =~ /^n/io)
{
    print "Ok, not converting\n";
    exit 0;
}

print <<EOT ;

Warnings during the conversion process will be written to
/home/<username>/.fvwm2rc.conversion
for each user.

EOT

# Do all the files
$unum=1;
printf(STDERR "Processing user: %3d/%3d", 0, $#todo+1);
foreach $line (@todo)
{
    printf(STDERR "\b\b\b\b\b\b\b"); # 7 backspaces
    printf(STDERR "%3d/%3d", $unum, $#todo+1);
    # XXX: assumes all directories in /home have corresponding uids.
    &translate($line);
    $unum++;
}
print STDERR "\n";

&mark_config_done;

print "All done!\n";
exit 0;

###########################

# run $CONVPROG
sub translate
{
    local ($line) = @_;
    local ($uid, $gid, $dir);

    $line =~ m/^([^:]+):([^:]+):(.+)$/o;
    $uid=$1;
    $gid=$2;
    $dir=$3;

    # fork, child drops privs & does work, while parent waits for child
    $pid=fork();
    die "fork() failed: $!\n" if ($pid==-1);
    if ($pid==0) # child
    {
	syscall($SYS_SETGROUPS, 0, 0); # SYS_setgroups(0, NULL); /* nasty! */
	$( = $) = $gid;  # real, eff gid
	$< = $> = $uid;  # real, eff uid
	# ok, now plain mortal, so run conversion
	open(STDOUT, ">$dir/.fvwm2rc.conversion") ||
	    die "cannot open $dir/.fvwm2rc.conversion for write: $!\n";
	open(STDERR, ">&STDOUT") || die "Can't dup stdout";
	exec($CONVPROG, "$dir/.fvwmrc", "$dir/.fvwm2rc",
	     ">$dir/.fvwm2rc.conversion 2>&1");
	die "exec() of $CONVPROG failed: $!\n";
    } else {
	# parent
	waitpid($pid, 0);
	$ret=$?>>8;
	die "Child returned with non-zero exit status($ret)\n" if ($ret);
    }
}
--
Visit the official FVWM web page at <URL:http://www.hpc.uh.edu/fvwm/>.
To unsubscribe from the list, send "unsubscribe fvwm" in the body of a
message to [email-masked].
To report problems, send mail to [email-masked].