2001-04-28 18:49:31 +00:00
|
|
|
#!env perl
|
|
|
|
|
|
|
|
#
|
2004-04-18 14:21:17 +00:00
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-04-28 18:49:31 +00:00
|
|
|
#
|
2004-04-18 14:21:17 +00:00
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
2001-04-28 18:49:31 +00:00
|
|
|
#
|
|
|
|
# The Original Code is mozilla.org code.
|
|
|
|
#
|
2004-04-18 14:21:17 +00:00
|
|
|
# The Initial Developer of the Original Code is
|
|
|
|
# Netscape Communications Corporation.
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Christopher Seawood <cls@seawood.org>
|
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
2001-04-28 18:49:31 +00:00
|
|
|
#
|
2004-04-18 14:21:17 +00:00
|
|
|
# ***** END LICENSE BLOCK *****
|
2001-04-28 18:49:31 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# A generic script to add entries to a file
|
|
|
|
# if the entry does not already exist
|
|
|
|
#
|
2001-09-12 06:32:35 +00:00
|
|
|
# Usage: $0 [-l] <filename> <entry> [<entry> <entry>]
|
2001-06-20 20:21:49 +00:00
|
|
|
#
|
|
|
|
# -l do not attempt flock the file.
|
2001-04-28 18:49:31 +00:00
|
|
|
|
|
|
|
use Fcntl qw(:DEFAULT :flock);
|
2001-06-20 20:21:49 +00:00
|
|
|
use Getopt::Std;
|
2001-08-17 12:06:03 +00:00
|
|
|
use mozLock;
|
2001-04-28 18:49:31 +00:00
|
|
|
|
|
|
|
sub usage() {
|
2001-06-20 20:21:49 +00:00
|
|
|
print "$0 [-l] <filename> <entry>\n";
|
2001-04-28 18:49:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-06-20 20:21:49 +00:00
|
|
|
$nofilelocks = 0;
|
|
|
|
|
|
|
|
getopts("l");
|
|
|
|
|
|
|
|
$nofilelocks = 1 if defined($::opt_l);
|
2001-04-28 18:49:31 +00:00
|
|
|
|
|
|
|
$file = shift;
|
2001-09-12 06:32:35 +00:00
|
|
|
|
|
|
|
undef @entrylist;
|
2005-07-21 13:33:43 +00:00
|
|
|
while (defined($entry = shift)) {
|
2001-09-12 06:32:35 +00:00
|
|
|
push @entrylist, $entry;
|
|
|
|
}
|
2001-04-28 18:49:31 +00:00
|
|
|
|
2001-08-17 12:06:03 +00:00
|
|
|
$lockfile = $file . ".lck";
|
|
|
|
|
2001-04-28 18:49:31 +00:00
|
|
|
# touch the file if it doesn't exist
|
|
|
|
if ( ! -e "$file") {
|
|
|
|
$now = time;
|
|
|
|
utime $now, $now, $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
# This needs to be atomic
|
2001-08-17 12:06:03 +00:00
|
|
|
mozLock($lockfile) unless $nofilelocks;
|
2001-09-12 06:32:35 +00:00
|
|
|
|
|
|
|
# Read entire file into mem
|
|
|
|
undef @inbuf;
|
|
|
|
if ( -e "$file" ) {
|
|
|
|
open(IN, "$file") || die ("$file: $!\n");
|
2003-06-23 21:36:19 +00:00
|
|
|
binmode(IN);
|
2001-09-12 06:32:35 +00:00
|
|
|
while ($tmp = <IN>) {
|
|
|
|
chomp($tmp);
|
|
|
|
push @inbuf, $tmp;
|
|
|
|
}
|
|
|
|
close(IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
undef @outbuf;
|
|
|
|
# Add each entry to file if it's not already there
|
|
|
|
foreach $entry (@entrylist) {
|
|
|
|
push @outbuf, $entry if (!grep(/^$entry$/, @inbuf));
|
2001-06-20 20:21:49 +00:00
|
|
|
}
|
2001-09-12 06:32:35 +00:00
|
|
|
|
|
|
|
$count = $#outbuf + 1;
|
|
|
|
|
|
|
|
# Append new entry to file
|
|
|
|
if ($count) {
|
|
|
|
open(OUT, ">>$file") || die ("$file: $!\n");
|
2003-06-23 21:36:19 +00:00
|
|
|
binmode(OUT);
|
2001-09-12 06:32:35 +00:00
|
|
|
foreach $entry (@outbuf) {
|
|
|
|
print OUT "$entry\n";
|
|
|
|
}
|
|
|
|
close(OUT);
|
2001-04-28 18:49:31 +00:00
|
|
|
}
|
2001-09-12 06:32:35 +00:00
|
|
|
|
2001-08-17 12:06:03 +00:00
|
|
|
mozUnlock($lockfile) unless $nofilelocks;
|
2001-04-28 18:49:31 +00:00
|
|
|
|
|
|
|
exit(0);
|