Re: get_uid sub critic

From: John W. Krahn (krahnj_at_acm.org)
Date: 02/07/04


To: beginners@perl.org
Date: Sat, 07 Feb 2004 14:08:26 -0800

Kenton Brede wrote:
>
> I've written the following subroutine to snag the next available UID in
> the 700 range from /etc/passwd. I then use the return value with
> "useradd" to add a new user.
>
> The subroutine works fine. If no UID 700 is found it returns 700.
> It then returns the next available UID or adds 1 to the last UID in the 700
> block.
>
> Even though it is functional it seems a little clunky to me. If anyone
> has the time and inclination to point me to changes I could make I
> would appreciate it.

This will return the next UID or undef if one is not found:

sub get_uid {
    my ( $start, $end ) = @_;
    --$start;
    defined getpwuid $start or return $start while ++$start <= $end;
    return;
    }

print get_uid( 700, 799 ), "\n";

John

-- 
use Perl;
program
fulfillment


Relevant Pages

  • Re: get_uid sub critic
    ... > Kenton Brede wrote: ... >> I've written the following subroutine to snag the next available UID in ... > This will return the next UID or undef if one is not found: ...
    (perl.beginners)
  • get_uid sub critic
    ... I've written the following subroutine to snag the next available UID in ... "useradd" to add a new user. ... The subroutine works fine. ... If no UID 700 is found it returns 700. ...
    (perl.beginners)
  • Re: get_uid sub critic
    ... Kenton Brede wrote: ... > sub get_uid { ... > # iterate though list snagging the next available UID ...
    (perl.beginners)
  • RE: get_uid sub critic
    ... > I've written the following subroutine to snag the next available UID ... Note that this algorithm contains a race condition. ...
    (perl.beginners)