[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

patches



Ok, here are patches for sysvinit and fakeroot. Fakeroot I've actually been
able to test, and it appears to work. sysvinit is waiting on me to finish
making my new (cleaner) chroot, and put on another box, so I can try booting.

	---Nathan
diff -urN fakeroot-0.4.5/communicate.h fakeroot-0.4.5-freebsd/communicate.h
--- fakeroot-0.4.5/communicate.h	Tue Sep  7 20:38:14 1999
+++ fakeroot-0.4.5-freebsd/communicate.h	Mon Jan 28 01:51:02 2002
@@ -14,7 +14,8 @@
    As I assume that pre-2.1 libc's will just ignore those _LARGEFILE64_SOURCE
    defines, I hope I can get away with this approach:
 */
-  
+
+#ifndef __FreeBSD__  
 /*First, unconditionally define these, so that glibc 2.1 features.h defines
   the needed 64 bits defines*/
 #ifndef _LARGEFILE64_SOURCE
@@ -59,6 +60,8 @@
 # define _LARGEFILE_SOURCE
 #endif
 #endif
+
+#endif /* __FreeBSD__ */
 
 
 #include <sys/types.h>
diff -urN fakeroot-0.4.5/fakerootconfig.h fakeroot-0.4.5-freebsd/fakerootconfig.h
--- fakeroot-0.4.5/fakerootconfig.h	Sat Oct  9 15:31:20 1999
+++ fakeroot-0.4.5-freebsd/fakerootconfig.h	Mon Jan 28 01:44:45 2002
@@ -1,14 +1,8 @@
-#define NEXT_STAT(a,b,c) next___xstat(a,b,c)
-#define STAT_ARG(a,b,c) (a,b,c)
-#define NEXT_FSTAT(a,b,c) next___fxstat(a,b,c)
-#define FSTAT_ARG(a,b,c) (a,b,c)
-#define NEXT_LSTAT(a,b,c) next___lxstat(a,b,c)
-#define LSTAT_ARG(a,b,c) (a,b,c)
-#define NEXT_STAT64(a,b,c) next___xstat64(a,b,c)
-#define STAT64_ARG(a,b,c) (a,b,c)
-#define NEXT_FSTAT64(a,b,c) next___fxstat64(a,b,c)
-#define FSTAT64_ARG(a,b,c) (a,b,c)
-#define NEXT_LSTAT64(a,b,c) next___lxstat64(a,b,c)
-#define LSTAT64_ARG(a,b,c) (a,b,c)
-#define NEXT_MKNOD(a,b,c,d) next___xmknod(a,b,c,d)
-#define MKNOD_ARG(a,b,c,d) (a,b,c,d)
+#define NEXT_STAT(a,b,c) next_stat(b,c)
+#define STAT_ARG(a,b,c) (b,c)
+#define NEXT_FSTAT(a,b,c) next_fstat(b,c)
+#define FSTAT_ARG(a,b,c) (b,c)
+#define NEXT_LSTAT(a,b,c) next_lstat(b,c)
+#define LSTAT_ARG(a,b,c) (b,c)
+#define NEXT_MKNOD(a,b,c,d) next_mknod(b,c,d)
+#define MKNOD_ARG(a,b,c,d) (b,c,d)
File fakeroot-0.4.5/test/hda3 is a regular empty file while file fakeroot-0.4.5-freebsd/test/hda3 is a block special file
diff -urN sysvinit-2.84/debian/control sysvinit-2.84-freebsd/debian/control
--- sysvinit-2.84/debian/control	Sun Feb  3 18:25:01 2002
+++ sysvinit-2.84-freebsd/debian/control	Sat Jan 26 00:54:18 2002
@@ -6,14 +6,11 @@
 
 Package: sysvinit
 Essential: yes
-Architecture: any
+Architecture: freebsd-i386
 Pre-depends: ${shlibs:Depends}
-Conflicts: last, file-rc (<= 0.5.7), kbd (<< 0.95-2), mdutils (<< 0.35-9)
-Depends: dpkg (>= 1.4.0.21), mount (>= 2.7i-1), util-linux (>= 2.9t-2), e2fsprogs (>= 1.15-1)
-Replaces: last, bsdutils (<=2.0-2), dpkg (<< 1.9.17)
+Depends: dpkg (>= 1.4.0.21)
 Description: System-V like init.
  Init is the first program to run after your system is booted, and
  continues to run as process number 1 until your system halts. Init's
  job is to start other programs that are essential to the operation of
  your system. All processes are descended from init. For more information,
- see the manual page init(8).
diff -urN sysvinit-2.84/debian/rules sysvinit-2.84-freebsd/debian/rules
--- sysvinit-2.84/debian/rules	Sun Feb  3 18:25:01 2002
+++ sysvinit-2.84-freebsd/debian/rules	Sat Jan 26 00:11:09 2002
@@ -18,7 +18,7 @@
 build:
 # Builds the binary package.
 	$(checkdir)
-	(cd src; make DEBIAN=cool)
+	(cd src; make DEBIAN=cool FREEBSD=1)
 	touch build
 
 # Architecture independant files.
@@ -57,7 +57,7 @@
 	install -d -g root -m 755 -o root $(tmp)/usr/share/man/fr/man8
 	install -d -g root -m 755 -o root $(tmp)/var/lib/urandom
 	install -g root -m 755 debian/fsck.nfs $(tmp)/sbin/fsck.nfs
-	(cd src; make ROOT=$(tmp) DEBIAN=cool install )
+	(cd src; make ROOT=$(tmp) DEBIAN=cool FREEBSD=1 install )
 	install -g root -m 644 debian/rc.boot.5 $(tmp)/usr/share/man/man5
 	install -g root -m 644 debian/rcS.5 $(tmp)/usr/share/man/man5
 	install -g root -m 644 debian/extra/*.8 \
diff -urN sysvinit-2.84/src/Makefile sysvinit-2.84-freebsd/src/Makefile
--- sysvinit-2.84/src/Makefile	Tue Nov  6 11:58:16 2001
+++ sysvinit-2.84-freebsd/src/Makefile	Sat Jan 26 00:12:31 2002
@@ -18,7 +18,11 @@
 PROGS	= init halt shutdown killall5 runlevel sulogin utmpdump \
 		last mesg wall
 else
-PROGS	= init halt shutdown killall5 runlevel sulogin last mesg
+  ifeq ($(FREEBSD),)
+    PROGS	= init halt shutdown killall5 runlevel sulogin last mesg
+  else
+    PROGS	= init halt shutdown killall5 sulogin 
+  endif
 endif
 
 BIN_OWNER	= root
@@ -34,11 +38,11 @@
 
 all:		$(PROGS)
 
-init:		init.o init_utmp.o
-		$(CC) $(LDFLAGS) $(STATIC) -o $@ init.o init_utmp.o
+init:		init.o init_utmp.o utent.o
+		$(CC) $(LDFLAGS) $(STATIC) -o $@ init.o init_utmp.o utent.o
 
-halt:		halt.o ifdown.o hddown.o utmp.o reboot.h
-		$(CC) $(LDFLAGS) -o $@ halt.o ifdown.o hddown.o utmp.o
+halt:		halt.o ifdown.o hddown.o utmp.o reboot.h utent.o getopt.o
+		$(CC) $(LDFLAGS) -o $@ halt.o ifdown.o hddown.o utmp.o utent.o
 
 last:		last.o oldutmp.h
 		$(CC) $(LDFLAGS) -o $@ last.o
@@ -58,8 +62,8 @@
 wall:		dowall.o wall.o
 		$(CC) $(LDFLAGS) -o $@ dowall.o wall.o
 
-shutdown:	dowall.o shutdown.o utmp.o reboot.h
-		$(CC) $(LDFLAGS) -o $@ dowall.o shutdown.o utmp.o
+shutdown:	dowall.o shutdown.o utmp.o reboot.h utent.h utent.c
+		$(CC) $(LDFLAGS) -o $@ dowall.o shutdown.o utmp.o utent.o
 
 bootlogd:	bootlogd.o
 		$(CC) $(LDFLAGS) -o $@ bootlogd.o
@@ -73,6 +77,12 @@
 init_utmp.o:	utmp.c init.h
 		$(CC) -c $(CFLAGS) -DINIT_MAIN utmp.c -o init_utmp.o
 
+utent.o:	utent.c
+		$(CC) -c $(CFLAGS) utent.c
+
+getopt.o:	getopt.c
+		$(CC) -c $(CFLAGS) getopt.c
+
 cleanobjs:
 		rm -f *.o *.bak
 
@@ -86,13 +96,15 @@
 
 install:
 		$(INSTALL) -m 755 halt init killall5 sulogin \
-			runlevel shutdown $(ROOT)/sbin
+			shutdown $(ROOT)/sbin
 		# These are not installed by default
 ifeq ($(DEBIAN),)
 		$(INSTALL) -m 555 utmpdump wall $(ROOT)/usr/bin
 endif
 		# $(INSTALL) -m 755 etc/initscript.sample $(ROOT)/etc
+ifeq ($FREEBSD,)
 		$(INSTALL) -m 755 mesg last $(ROOT)/usr/bin
+endif
 		cd $(ROOT)/sbin; ln -sf halt reboot; chown $(BIN_COMBO) reboot
 		cd $(ROOT)/sbin; ln -sf halt poweroff; chown $(BIN_COMBO) poweroff
 		cd $(ROOT)/sbin; ln -sf init telinit; chown $(BIN_COMBO) telinit
diff -urN sysvinit-2.84/src/dowall.c sysvinit-2.84-freebsd/src/dowall.c
--- sysvinit-2.84/src/dowall.c	Mon Sep 11 11:34:24 2000
+++ sysvinit-2.84-freebsd/src/dowall.c	Fri Jan 25 09:34:44 2002
@@ -25,6 +25,9 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <setjmp.h>
+#ifdef __FreeBSD__
+#include "utent.h"
+#endif
 
 static jmp_buf jbuf;
 
@@ -79,7 +82,7 @@
 	struct utmp *utmp;
 	uid_t uid;
 
-	setutent();
+	setutent(0);
 
 	if (init == 0) {
 		uid = getuid();
@@ -126,8 +129,13 @@
 	
 	siginterrupt (SIGALRM, 1);
 	while ((utmp = getutent()) != NULL) {
+#ifndef __FreeBSD__
 		if(utmp->ut_type != USER_PROCESS ||
 		   utmp->ut_user[0] == 0) continue;
+#else
+		if(utmp->ut_name[0] == 0) continue;
+#endif
+
 		term[sizeof(term) - 1] = 0;
 		if (strncmp(utmp->ut_line, "/dev/", 5) == 0)
 			strncpy(term, utmp->ut_line, sizeof(term)-1);
diff -urN sysvinit-2.84/src/getopt.c sysvinit-2.84-freebsd/src/getopt.c
--- sysvinit-2.84/src/getopt.c	Thu Jan  1 00:00:00 1970
+++ sysvinit-2.84-freebsd/src/getopt.c	Fri Jan 25 08:53:10 2002
@@ -0,0 +1,1065 @@
+/* Getopt for GNU.
+   NOTE: getopt is now part of the C library, so if you don't know what
+   "Keep this file name-space clean" means, talk to drepper@gnu.org
+   before changing it!
+
+   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
+   	Free Software Foundation, Inc.
+
+   NOTE: The canonical source of this file is maintained with the GNU C Library.
+   Bugs can be reported to bug-glibc@gnu.org.
+
+   This program is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
+   Ditto for AIX 3.2 and <stdlib.h>.  */
+#ifndef _NO_PROTO
+# define _NO_PROTO
+#endif
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if !defined __STDC__ || !__STDC__
+/* This is a separate conditional since some stdc systems
+   reject `defined (const)'.  */
+# ifndef const
+#  define const
+# endif
+#endif
+
+#include <stdio.h>
+
+/* Comment out all this code if we are using the GNU C Library, and are not
+   actually compiling the library itself.  This code is part of the GNU C
+   Library, but also included in many other GNU distributions.  Compiling
+   and linking in this code is a waste when using the GNU C library
+   (especially if it is a shared library).  Rather than having every GNU
+   program understand `configure --with-gnu-libc' and omit the object files,
+   it is simpler to just do this in the source for each such file.  */
+
+#define GETOPT_INTERFACE_VERSION 2
+#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
+# include <gnu-versions.h>
+# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
+#  define ELIDE_CODE
+# endif
+#endif
+
+#ifndef ELIDE_CODE
+
+
+/* This needs to come after some library #include
+   to get __GNU_LIBRARY__ defined.  */
+#ifdef	__GNU_LIBRARY__
+/* Don't include stdlib.h for non-GNU C libraries because some of them
+   contain conflicting prototypes for getopt.  */
+# include <stdlib.h>
+# include <unistd.h>
+#endif	/* GNU C library.  */
+
+#ifdef VMS
+# include <unixlib.h>
+# if HAVE_STRING_H - 0
+#  include <string.h>
+# endif
+#endif
+
+#ifndef _
+/* This is for other GNU distributions with internationalized messages.  */
+# if ENABLE_NLS || defined _LIBC
+#  include <libintl.h>
+#  ifndef _
+#   define _(msgid)	gettext (msgid)
+#  endif
+# else
+#  define _(msgid)	(msgid)
+# endif
+#endif
+
+/* This version of `getopt' appears to the caller like standard Unix `getopt'
+   but it behaves differently for the user, since it allows the user
+   to intersperse the options with the other arguments.
+
+   As `getopt' works, it permutes the elements of ARGV so that,
+   when it is done, all the options precede everything else.  Thus
+   all application programs are extended to handle flexible argument order.
+
+   Setting the environment variable POSIXLY_CORRECT disables permutation.
+   Then the behavior is completely standard.
+
+   GNU application programs can use a third alternative mode in which
+   they can distinguish the relative order of options and other arguments.  */
+
+#include "getopt.h"
+
+/* For communication from `getopt' to the caller.
+   When `getopt' finds an option that takes an argument,
+   the argument value is returned here.
+   Also, when `ordering' is RETURN_IN_ORDER,
+   each non-option ARGV-element is returned here.  */
+
+char *optarg = NULL;
+
+/* Index in ARGV of the next element to be scanned.
+   This is used for communication to and from the caller
+   and for communication between successive calls to `getopt'.
+
+   On entry to `getopt', zero means this is the first call; initialize.
+
+   When `getopt' returns -1, this is the index of the first of the
+   non-option elements that the caller should itself scan.
+
+   Otherwise, `optind' communicates from one call to the next
+   how much of ARGV has been scanned so far.  */
+
+/* 1003.2 says this must be 1 before any call.  */
+int optind = 1;
+
+/* Formerly, initialization of getopt depended on optind==0, which
+   causes problems with re-calling getopt as programs generally don't
+   know that. */
+
+int __getopt_initialized = 0;
+
+/* The next char to be scanned in the option-element
+   in which the last option character we returned was found.
+   This allows us to pick up the scan where we left off.
+
+   If this is zero, or a null string, it means resume the scan
+   by advancing to the next ARGV-element.  */
+
+static char *nextchar;
+
+/* Callers store zero here to inhibit the error message
+   for unrecognized options.  */
+
+int opterr = 1;
+
+/* Set to an option character which was unrecognized.
+   This must be initialized on some systems to avoid linking in the
+   system's own getopt implementation.  */
+
+int optopt = '?';
+
+/* Describe how to deal with options that follow non-option ARGV-elements.
+
+   If the caller did not specify anything,
+   the default is REQUIRE_ORDER if the environment variable
+   POSIXLY_CORRECT is defined, PERMUTE otherwise.
+
+   REQUIRE_ORDER means don't recognize them as options;
+   stop option processing when the first non-option is seen.
+   This is what Unix does.
+   This mode of operation is selected by either setting the environment
+   variable POSIXLY_CORRECT, or using `+' as the first character
+   of the list of option characters.
+
+   PERMUTE is the default.  We permute the contents of ARGV as we scan,
+   so that eventually all the non-options are at the end.  This allows options
+   to be given in any order, even with programs that were not written to
+   expect this.
+
+   RETURN_IN_ORDER is an option available to programs that were written
+   to expect options and other ARGV-elements in any order and that care about
+   the ordering of the two.  We describe each non-option ARGV-element
+   as if it were the argument of an option with character code 1.
+   Using `-' as the first character of the list of option characters
+   selects this mode of operation.
+
+   The special argument `--' forces an end of option-scanning regardless
+   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
+   `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
+
+static enum
+{
+  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
+} ordering;
+
+/* Value of POSIXLY_CORRECT environment variable.  */
+static char *posixly_correct;
+
+#ifdef	__GNU_LIBRARY__
+/* We want to avoid inclusion of string.h with non-GNU libraries
+   because there are many ways it can cause trouble.
+   On some systems, it contains special magic macros that don't work
+   in GCC.  */
+# include <string.h>
+# define my_index	strchr
+#else
+
+# if HAVE_STRING_H
+#  include <string.h>
+# else
+#  include <strings.h>
+# endif
+
+/* Avoid depending on library functions or files
+   whose names are inconsistent.  */
+
+#ifndef getenv
+extern char *getenv ();
+#endif
+
+static char *
+my_index (str, chr)
+     const char *str;
+     int chr;
+{
+  while (*str)
+    {
+      if (*str == chr)
+	return (char *) str;
+      str++;
+    }
+  return 0;
+}
+
+/* If using GCC, we can safely declare strlen this way.
+   If not using GCC, it is ok not to declare it.  */
+#ifdef __GNUC__
+/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
+   That was relevant to code that was here before.  */
+# if (!defined __STDC__ || !__STDC__) && !defined strlen
+/* gcc with -traditional declares the built-in strlen to return int,
+   and has done so at least since version 2.4.5. -- rms.  */
+extern int strlen (const char *);
+# endif /* not __STDC__ */
+#endif /* __GNUC__ */
+
+#endif /* not __GNU_LIBRARY__ */
+
+/* Handle permutation of arguments.  */
+
+/* Describe the part of ARGV that contains non-options that have
+   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
+   `last_nonopt' is the index after the last of them.  */
+
+static int first_nonopt;
+static int last_nonopt;
+
+#ifdef _LIBC
+/* Bash 2.0 gives us an environment variable containing flags
+   indicating ARGV elements that should not be considered arguments.  */
+
+/* Defined in getopt_init.c  */
+extern char *__getopt_nonoption_flags;
+
+static int nonoption_flags_max_len;
+static int nonoption_flags_len;
+
+static int original_argc;
+static char *const *original_argv;
+
+/* Make sure the environment variable bash 2.0 puts in the environment
+   is valid for the getopt call we must make sure that the ARGV passed
+   to getopt is that one passed to the process.  */
+static void
+__attribute__ ((unused))
+store_args_and_env (int argc, char *const *argv)
+{
+  /* XXX This is no good solution.  We should rather copy the args so
+     that we can compare them later.  But we must not use malloc(3).  */
+  original_argc = argc;
+  original_argv = argv;
+}
+# ifdef text_set_element
+text_set_element (__libc_subinit, store_args_and_env);
+# endif /* text_set_element */
+
+# define SWAP_FLAGS(ch1, ch2) \
+  if (nonoption_flags_len > 0)						      \
+    {									      \
+      char __tmp = __getopt_nonoption_flags[ch1];			      \
+      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
+      __getopt_nonoption_flags[ch2] = __tmp;				      \
+    }
+#else	/* !_LIBC */
+# define SWAP_FLAGS(ch1, ch2)
+#endif	/* _LIBC */
+
+/* Exchange two adjacent subsequences of ARGV.
+   One subsequence is elements [first_nonopt,last_nonopt)
+   which contains all the non-options that have been skipped so far.
+   The other is elements [last_nonopt,optind), which contains all
+   the options processed since those non-options were skipped.
+
+   `first_nonopt' and `last_nonopt' are relocated so that they describe
+   the new indices of the non-options in ARGV after they are moved.  */
+
+#if defined __STDC__ && __STDC__
+static void exchange (char **);
+#endif
+
+static void
+exchange (argv)
+     char **argv;
+{
+  int bottom = first_nonopt;
+  int middle = last_nonopt;
+  int top = optind;
+  char *tem;
+
+  /* Exchange the shorter segment with the far end of the longer segment.
+     That puts the shorter segment into the right place.
+     It leaves the longer segment in the right place overall,
+     but it consists of two parts that need to be swapped next.  */
+
+#ifdef _LIBC
+  /* First make sure the handling of the `__getopt_nonoption_flags'
+     string can work normally.  Our top argument must be in the range
+     of the string.  */
+  if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
+    {
+      /* We must extend the array.  The user plays games with us and
+	 presents new arguments.  */
+      char *new_str = malloc (top + 1);
+      if (new_str == NULL)
+	nonoption_flags_len = nonoption_flags_max_len = 0;
+      else
+	{
+	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
+			     nonoption_flags_max_len),
+		  '\0', top + 1 - nonoption_flags_max_len);
+	  nonoption_flags_max_len = top + 1;
+	  __getopt_nonoption_flags = new_str;
+	}
+    }
+#endif
+
+  while (top > middle && middle > bottom)
+    {
+      if (top - middle > middle - bottom)
+	{
+	  /* Bottom segment is the short one.  */
+	  int len = middle - bottom;
+	  register int i;
+
+	  /* Swap it with the top part of the top segment.  */
+	  for (i = 0; i < len; i++)
+	    {
+	      tem = argv[bottom + i];
+	      argv[bottom + i] = argv[top - (middle - bottom) + i];
+	      argv[top - (middle - bottom) + i] = tem;
+	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
+	    }
+	  /* Exclude the moved bottom segment from further swapping.  */
+	  top -= len;
+	}
+      else
+	{
+	  /* Top segment is the short one.  */
+	  int len = top - middle;
+	  register int i;
+
+	  /* Swap it with the bottom part of the bottom segment.  */
+	  for (i = 0; i < len; i++)
+	    {
+	      tem = argv[bottom + i];
+	      argv[bottom + i] = argv[middle + i];
+	      argv[middle + i] = tem;
+	      SWAP_FLAGS (bottom + i, middle + i);
+	    }
+	  /* Exclude the moved top segment from further swapping.  */
+	  bottom += len;
+	}
+    }
+
+  /* Update records for the slots the non-options now occupy.  */
+
+  first_nonopt += (optind - last_nonopt);
+  last_nonopt = optind;
+}
+
+/* Initialize the internal data when the first call is made.  */
+
+#if defined __STDC__ && __STDC__
+static const char *_getopt_initialize (int, char *const *, const char *);
+#endif
+static const char *
+_getopt_initialize (argc, argv, optstring)
+     int argc;
+     char *const *argv;
+     const char *optstring;
+{
+  /* Start processing options with ARGV-element 1 (since ARGV-element 0
+     is the program name); the sequence of previously skipped
+     non-option ARGV-elements is empty.  */
+
+  first_nonopt = last_nonopt = optind;
+
+  nextchar = NULL;
+
+  posixly_correct = getenv ("POSIXLY_CORRECT");
+
+  /* Determine how to handle the ordering of options and nonoptions.  */
+
+  if (optstring[0] == '-')
+    {
+      ordering = RETURN_IN_ORDER;
+      ++optstring;
+    }
+  else if (optstring[0] == '+')
+    {
+      ordering = REQUIRE_ORDER;
+      ++optstring;
+    }
+  else if (posixly_correct != NULL)
+    ordering = REQUIRE_ORDER;
+  else
+    ordering = PERMUTE;
+
+#ifdef _LIBC
+  if (posixly_correct == NULL
+      && argc == original_argc && argv == original_argv)
+    {
+      if (nonoption_flags_max_len == 0)
+	{
+	  if (__getopt_nonoption_flags == NULL
+	      || __getopt_nonoption_flags[0] == '\0')
+	    nonoption_flags_max_len = -1;
+	  else
+	    {
+	      const char *orig_str = __getopt_nonoption_flags;
+	      int len = nonoption_flags_max_len = strlen (orig_str);
+	      if (nonoption_flags_max_len < argc)
+		nonoption_flags_max_len = argc;
+	      __getopt_nonoption_flags =
+		(char *) malloc (nonoption_flags_max_len);
+	      if (__getopt_nonoption_flags == NULL)
+		nonoption_flags_max_len = -1;
+	      else
+		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
+			'\0', nonoption_flags_max_len - len);
+	    }
+	}
+      nonoption_flags_len = nonoption_flags_max_len;
+    }
+  else
+    nonoption_flags_len = 0;
+#endif
+
+  return optstring;
+}
+
+/* Scan elements of ARGV (whose length is ARGC) for option characters
+   given in OPTSTRING.
+
+   If an element of ARGV starts with '-', and is not exactly "-" or "--",
+   then it is an option element.  The characters of this element
+   (aside from the initial '-') are option characters.  If `getopt'
+   is called repeatedly, it returns successively each of the option characters
+   from each of the option elements.
+
+   If `getopt' finds another option character, it returns that character,
+   updating `optind' and `nextchar' so that the next call to `getopt' can
+   resume the scan with the following option character or ARGV-element.
+
+   If there are no more option characters, `getopt' returns -1.
+   Then `optind' is the index in ARGV of the first ARGV-element
+   that is not an option.  (The ARGV-elements have been permuted
+   so that those that are not options now come last.)
+
+   OPTSTRING is a string containing the legitimate option characters.
+   If an option character is seen that is not listed in OPTSTRING,
+   return '?' after printing an error message.  If you set `opterr' to
+   zero, the error message is suppressed but we still return '?'.
+
+   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
+   so the following text in the same ARGV-element, or the text of the following
+   ARGV-element, is returned in `optarg'.  Two colons mean an option that
+   wants an optional arg; if there is text in the current ARGV-element,
+   it is returned in `optarg', otherwise `optarg' is set to zero.
+
+   If OPTSTRING starts with `-' or `+', it requests different methods of
+   handling the non-option ARGV-elements.
+   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
+
+   Long-named options begin with `--' instead of `-'.
+   Their names may be abbreviated as long as the abbreviation is unique
+   or is an exact match for some defined option.  If they have an
+   argument, it follows the option name in the same ARGV-element, separated
+   from the option name by a `=', or else the in next ARGV-element.
+   When `getopt' finds a long-named option, it returns 0 if that option's
+   `flag' field is nonzero, the value of the option's `val' field
+   if the `flag' field is zero.
+
+   The elements of ARGV aren't really const, because we permute them.
+   But we pretend they're const in the prototype to be compatible
+   with other systems.
+
+   LONGOPTS is a vector of `struct option' terminated by an
+   element containing a name which is zero.
+
+   LONGIND returns the index in LONGOPT of the long-named option found.
+   It is only valid when a long-named option has been found by the most
+   recent call.
+
+   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
+   long-named options.  */
+
+int
+_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
+     int argc;
+     char *const *argv;
+     const char *optstring;
+     const struct option *longopts;
+     int *longind;
+     int long_only;
+{
+  int print_errors = opterr;
+  if (optstring[0] == ':')
+    print_errors = 0;
+
+  if (argc < 1)
+    return -1;
+
+  optarg = NULL;
+
+  if (optind == 0 || !__getopt_initialized)
+    {
+      if (optind == 0)
+	optind = 1;	/* Don't scan ARGV[0], the program name.  */
+      optstring = _getopt_initialize (argc, argv, optstring);
+      __getopt_initialized = 1;
+    }
+
+  /* Test whether ARGV[optind] points to a non-option argument.
+     Either it does not have option syntax, or there is an environment flag
+     from the shell indicating it is not an option.  The later information
+     is only used when the used in the GNU libc.  */
+#ifdef _LIBC
+# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
+		      || (optind < nonoption_flags_len			      \
+			  && __getopt_nonoption_flags[optind] == '1'))
+#else
+# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
+#endif
+
+  if (nextchar == NULL || *nextchar == '\0')
+    {
+      /* Advance to the next ARGV-element.  */
+
+      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
+	 moved back by the user (who may also have changed the arguments).  */
+      if (last_nonopt > optind)
+	last_nonopt = optind;
+      if (first_nonopt > optind)
+	first_nonopt = optind;
+
+      if (ordering == PERMUTE)
+	{
+	  /* If we have just processed some options following some non-options,
+	     exchange them so that the options come first.  */
+
+	  if (first_nonopt != last_nonopt && last_nonopt != optind)
+	    exchange ((char **) argv);
+	  else if (last_nonopt != optind)
+	    first_nonopt = optind;
+
+	  /* Skip any additional non-options
+	     and extend the range of non-options previously skipped.  */
+
+	  while (optind < argc && NONOPTION_P)
+	    optind++;
+	  last_nonopt = optind;
+	}
+
+      /* The special ARGV-element `--' means premature end of options.
+	 Skip it like a null option,
+	 then exchange with previous non-options as if it were an option,
+	 then skip everything else like a non-option.  */
+
+      if (optind != argc && !strcmp (argv[optind], "--"))
+	{
+	  optind++;
+
+	  if (first_nonopt != last_nonopt && last_nonopt != optind)
+	    exchange ((char **) argv);
+	  else if (first_nonopt == last_nonopt)
+	    first_nonopt = optind;
+	  last_nonopt = argc;
+
+	  optind = argc;
+	}
+
+      /* If we have done all the ARGV-elements, stop the scan
+	 and back over any non-options that we skipped and permuted.  */
+
+      if (optind == argc)
+	{
+	  /* Set the next-arg-index to point at the non-options
+	     that we previously skipped, so the caller will digest them.  */
+	  if (first_nonopt != last_nonopt)
+	    optind = first_nonopt;
+	  return -1;
+	}
+
+      /* If we have come to a non-option and did not permute it,
+	 either stop the scan or describe it to the caller and pass it by.  */
+
+      if (NONOPTION_P)
+	{
+	  if (ordering == REQUIRE_ORDER)
+	    return -1;
+	  optarg = argv[optind++];
+	  return 1;
+	}
+
+      /* We have found another option-ARGV-element.
+	 Skip the initial punctuation.  */
+
+      nextchar = (argv[optind] + 1
+		  + (longopts != NULL && argv[optind][1] == '-'));
+    }
+
+  /* Decode the current option-ARGV-element.  */
+
+  /* Check whether the ARGV-element is a long option.
+
+     If long_only and the ARGV-element has the form "-f", where f is
+     a valid short option, don't consider it an abbreviated form of
+     a long option that starts with f.  Otherwise there would be no
+     way to give the -f short option.
+
+     On the other hand, if there's a long option "fubar" and
+     the ARGV-element is "-fu", do consider that an abbreviation of
+     the long option, just like "--fu", and not "-f" with arg "u".
+
+     This distinction seems to be the most useful approach.  */
+
+  if (longopts != NULL
+      && (argv[optind][1] == '-'
+	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
+    {
+      char *nameend;
+      const struct option *p;
+      const struct option *pfound = NULL;
+      int exact = 0;
+      int ambig = 0;
+      int indfound = -1;
+      int option_index;
+
+      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
+	/* Do nothing.  */ ;
+
+      /* Test all long options for either exact match
+	 or abbreviated matches.  */
+      for (p = longopts, option_index = 0; p->name; p++, option_index++)
+	if (!strncmp (p->name, nextchar, nameend - nextchar))
+	  {
+	    if ((unsigned int) (nameend - nextchar)
+		== (unsigned int) strlen (p->name))
+	      {
+		/* Exact match found.  */
+		pfound = p;
+		indfound = option_index;
+		exact = 1;
+		break;
+	      }
+	    else if (pfound == NULL)
+	      {
+		/* First nonexact match found.  */
+		pfound = p;
+		indfound = option_index;
+	      }
+	    else if (long_only
+		     || pfound->has_arg != p->has_arg
+		     || pfound->flag != p->flag
+		     || pfound->val != p->val)
+	      /* Second or later nonexact match found.  */
+	      ambig = 1;
+	  }
+
+      if (ambig && !exact)
+	{
+	  if (print_errors)
+	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
+		     argv[0], argv[optind]);
+	  nextchar += strlen (nextchar);
+	  optind++;
+	  optopt = 0;
+	  return '?';
+	}
+
+      if (pfound != NULL)
+	{
+	  option_index = indfound;
+	  optind++;
+	  if (*nameend)
+	    {
+	      /* Don't test has_arg with >, because some C compilers don't
+		 allow it to be used on enums.  */
+	      if (pfound->has_arg)
+		optarg = nameend + 1;
+	      else
+		{
+		  if (print_errors)
+		    {
+		      if (argv[optind - 1][1] == '-')
+			/* --option */
+			fprintf (stderr,
+				 _("%s: option `--%s' doesn't allow an argument\n"),
+				 argv[0], pfound->name);
+		      else
+			/* +option or -option */
+			fprintf (stderr,
+				 _("%s: option `%c%s' doesn't allow an argument\n"),
+				 argv[0], argv[optind - 1][0], pfound->name);
+		    }
+
+		  nextchar += strlen (nextchar);
+
+		  optopt = pfound->val;
+		  return '?';
+		}
+	    }
+	  else if (pfound->has_arg == 1)
+	    {
+	      if (optind < argc)
+		optarg = argv[optind++];
+	      else
+		{
+		  if (print_errors)
+		    fprintf (stderr,
+			   _("%s: option `%s' requires an argument\n"),
+			   argv[0], argv[optind - 1]);
+		  nextchar += strlen (nextchar);
+		  optopt = pfound->val;
+		  return optstring[0] == ':' ? ':' : '?';
+		}
+	    }
+	  nextchar += strlen (nextchar);
+	  if (longind != NULL)
+	    *longind = option_index;
+	  if (pfound->flag)
+	    {
+	      *(pfound->flag) = pfound->val;
+	      return 0;
+	    }
+	  return pfound->val;
+	}
+
+      /* Can't find it as a long option.  If this is not getopt_long_only,
+	 or the option starts with '--' or is not a valid short
+	 option, then it's an error.
+	 Otherwise interpret it as a short option.  */
+      if (!long_only || argv[optind][1] == '-'
+	  || my_index (optstring, *nextchar) == NULL)
+	{
+	  if (print_errors)
+	    {
+	      if (argv[optind][1] == '-')
+		/* --option */
+		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
+			 argv[0], nextchar);
+	      else
+		/* +option or -option */
+		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
+			 argv[0], argv[optind][0], nextchar);
+	    }
+	  nextchar = (char *) "";
+	  optind++;
+	  optopt = 0;
+	  return '?';
+	}
+    }
+
+  /* Look at and handle the next short option-character.  */
+
+  {
+    char c = *nextchar++;
+    char *temp = my_index (optstring, c);
+
+    /* Increment `optind' when we start to process its last character.  */
+    if (*nextchar == '\0')
+      ++optind;
+
+    if (temp == NULL || c == ':')
+      {
+	if (print_errors)
+	  {
+	    if (posixly_correct)
+	      /* 1003.2 specifies the format of this message.  */
+	      fprintf (stderr, _("%s: illegal option -- %c\n"),
+		       argv[0], c);
+	    else
+	      fprintf (stderr, _("%s: invalid option -- %c\n"),
+		       argv[0], c);
+	  }
+	optopt = c;
+	return '?';
+      }
+    /* Convenience. Treat POSIX -W foo same as long option --foo */
+    if (temp[0] == 'W' && temp[1] == ';')
+      {
+	char *nameend;
+	const struct option *p;
+	const struct option *pfound = NULL;
+	int exact = 0;
+	int ambig = 0;
+	int indfound = 0;
+	int option_index;
+
+	/* This is an option that requires an argument.  */
+	if (*nextchar != '\0')
+	  {
+	    optarg = nextchar;
+	    /* If we end this ARGV-element by taking the rest as an arg,
+	       we must advance to the next element now.  */
+	    optind++;
+	  }
+	else if (optind == argc)
+	  {
+	    if (print_errors)
+	      {
+		/* 1003.2 specifies the format of this message.  */
+		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
+			 argv[0], c);
+	      }
+	    optopt = c;
+	    if (optstring[0] == ':')
+	      c = ':';
+	    else
+	      c = '?';
+	    return c;
+	  }
+	else
+	  /* We already incremented `optind' once;
+	     increment it again when taking next ARGV-elt as argument.  */
+	  optarg = argv[optind++];
+
+	/* optarg is now the argument, see if it's in the
+	   table of longopts.  */
+
+	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
+	  /* Do nothing.  */ ;
+
+	/* Test all long options for either exact match
+	   or abbreviated matches.  */
+	for (p = longopts, option_index = 0; p->name; p++, option_index++)
+	  if (!strncmp (p->name, nextchar, nameend - nextchar))
+	    {
+	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
+		{
+		  /* Exact match found.  */
+		  pfound = p;
+		  indfound = option_index;
+		  exact = 1;
+		  break;
+		}
+	      else if (pfound == NULL)
+		{
+		  /* First nonexact match found.  */
+		  pfound = p;
+		  indfound = option_index;
+		}
+	      else
+		/* Second or later nonexact match found.  */
+		ambig = 1;
+	    }
+	if (ambig && !exact)
+	  {
+	    if (print_errors)
+	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
+		       argv[0], argv[optind]);
+	    nextchar += strlen (nextchar);
+	    optind++;
+	    return '?';
+	  }
+	if (pfound != NULL)
+	  {
+	    option_index = indfound;
+	    if (*nameend)
+	      {
+		/* Don't test has_arg with >, because some C compilers don't
+		   allow it to be used on enums.  */
+		if (pfound->has_arg)
+		  optarg = nameend + 1;
+		else
+		  {
+		    if (print_errors)
+		      fprintf (stderr, _("\
+%s: option `-W %s' doesn't allow an argument\n"),
+			       argv[0], pfound->name);
+
+		    nextchar += strlen (nextchar);
+		    return '?';
+		  }
+	      }
+	    else if (pfound->has_arg == 1)
+	      {
+		if (optind < argc)
+		  optarg = argv[optind++];
+		else
+		  {
+		    if (print_errors)
+		      fprintf (stderr,
+			       _("%s: option `%s' requires an argument\n"),
+			       argv[0], argv[optind - 1]);
+		    nextchar += strlen (nextchar);
+		    return optstring[0] == ':' ? ':' : '?';
+		  }
+	      }
+	    nextchar += strlen (nextchar);
+	    if (longind != NULL)
+	      *longind = option_index;
+	    if (pfound->flag)
+	      {
+		*(pfound->flag) = pfound->val;
+		return 0;
+	      }
+	    return pfound->val;
+	  }
+	  nextchar = NULL;
+	  return 'W';	/* Let the application handle it.   */
+      }
+    if (temp[1] == ':')
+      {
+	if (temp[2] == ':')
+	  {
+	    /* This is an option that accepts an argument optionally.  */
+	    if (*nextchar != '\0')
+	      {
+		optarg = nextchar;
+		optind++;
+	      }
+	    else
+	      optarg = NULL;
+	    nextchar = NULL;
+	  }
+	else
+	  {
+	    /* This is an option that requires an argument.  */
+	    if (*nextchar != '\0')
+	      {
+		optarg = nextchar;
+		/* If we end this ARGV-element by taking the rest as an arg,
+		   we must advance to the next element now.  */
+		optind++;
+	      }
+	    else if (optind == argc)
+	      {
+		if (print_errors)
+		  {
+		    /* 1003.2 specifies the format of this message.  */
+		    fprintf (stderr,
+			     _("%s: option requires an argument -- %c\n"),
+			     argv[0], c);
+		  }
+		optopt = c;
+		if (optstring[0] == ':')
+		  c = ':';
+		else
+		  c = '?';
+	      }
+	    else
+	      /* We already incremented `optind' once;
+		 increment it again when taking next ARGV-elt as argument.  */
+	      optarg = argv[optind++];
+	    nextchar = NULL;
+	  }
+      }
+    return c;
+  }
+}
+
+int
+getopt (argc, argv, optstring)
+     int argc;
+     char *const *argv;
+     const char *optstring;
+{
+  return _getopt_internal (argc, argv, optstring,
+			   (const struct option *) 0,
+			   (int *) 0,
+			   0);
+}
+
+#endif	/* Not ELIDE_CODE.  */
+
+#ifdef TEST
+
+/* Compile with -DTEST to make an executable for use in testing
+   the above definition of `getopt'.  */
+
+int
+main (argc, argv)
+     int argc;
+     char **argv;
+{
+  int c;
+  int digit_optind = 0;
+
+  while (1)
+    {
+      int this_option_optind = optind ? optind : 1;
+
+      c = getopt (argc, argv, "abc:d:0123456789");
+      if (c == -1)
+	break;
+
+      switch (c)
+	{
+	case '0':
+	case '1':
+	case '2':
+	case '3':
+	case '4':
+	case '5':
+	case '6':
+	case '7':
+	case '8':
+	case '9':
+	  if (digit_optind != 0 && digit_optind != this_option_optind)
+	    printf ("digits occur in two different argv-elements.\n");
+	  digit_optind = this_option_optind;
+	  printf ("option %c\n", c);
+	  break;
+
+	case 'a':
+	  printf ("option a\n");
+	  break;
+
+	case 'b':
+	  printf ("option b\n");
+	  break;
+
+	case 'c':
+	  printf ("option c with value `%s'\n", optarg);
+	  break;
+
+	case '?':
+	  break;
+
+	default:
+	  printf ("?? getopt returned character code 0%o ??\n", c);
+	}
+    }
+
+  if (optind < argc)
+    {
+      printf ("non-option ARGV-elements: ");
+      while (optind < argc)
+	printf ("%s ", argv[optind++]);
+      printf ("\n");
+    }
+
+  exit (0);
+}
+
+#endif /* TEST */
diff -urN sysvinit-2.84/src/getopt.h sysvinit-2.84-freebsd/src/getopt.h
--- sysvinit-2.84/src/getopt.h	Thu Jan  1 00:00:00 1970
+++ sysvinit-2.84-freebsd/src/getopt.h	Fri Jan 25 08:53:10 2002
@@ -0,0 +1,182 @@
+/* Declarations for getopt.
+   Copyright (C) 1989,90,91,92,93,94,96,97,98,99 Free Software Foundation, Inc.
+
+   NOTE: The canonical source of this file is maintained with the GNU C Library.
+   Bugs can be reported to bug-glibc@gnu.org.
+
+   This program is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+#ifndef _GETOPT_H
+
+#ifndef __need_getopt
+# define _GETOPT_H 1
+#endif
+
+/* If __GNU_LIBRARY__ is not already defined, either we are being used
+   standalone, or this is the first header included in the source file.
+   If we are being used with glibc, we need to include <features.h>, but
+   that does not exist if we are standalone.  So: if __GNU_LIBRARY__ is
+   not defined, include <ctype.h>, which will pull in <features.h> for us
+   if it's from glibc.  (Why ctype.h?  It's guaranteed to exist and it
+   doesn't flood the namespace with stuff the way some other headers do.)  */
+#if !defined __GNU_LIBRARY__
+# include <ctype.h>
+#endif
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+/* For communication from `getopt' to the caller.
+   When `getopt' finds an option that takes an argument,
+   the argument value is returned here.
+   Also, when `ordering' is RETURN_IN_ORDER,
+   each non-option ARGV-element is returned here.  */
+
+extern char *optarg;
+
+/* Index in ARGV of the next element to be scanned.
+   This is used for communication to and from the caller
+   and for communication between successive calls to `getopt'.
+
+   On entry to `getopt', zero means this is the first call; initialize.
+
+   When `getopt' returns -1, this is the index of the first of the
+   non-option elements that the caller should itself scan.
+
+   Otherwise, `optind' communicates from one call to the next
+   how much of ARGV has been scanned so far.  */
+
+extern int optind;
+
+/* Callers store zero here to inhibit the error message `getopt' prints
+   for unrecognized options.  */
+
+extern int opterr;
+
+/* Set to an option character which was unrecognized.  */
+
+extern int optopt;
+
+#ifndef __need_getopt
+/* Describe the long-named options requested by the application.
+   The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+   of `struct option' terminated by an element containing a name which is
+   zero.
+
+   The field `has_arg' is:
+   no_argument		(or 0) if the option does not take an argument,
+   required_argument	(or 1) if the option requires an argument,
+   optional_argument 	(or 2) if the option takes an optional argument.
+
+   If the field `flag' is not NULL, it points to a variable that is set
+   to the value given in the field `val' when the option is found, but
+   left unchanged if the option is not found.
+
+   To have a long-named option do something other than set an `int' to
+   a compiled-in constant, such as set a value from `optarg', set the
+   option's `flag' field to zero and its `val' field to a nonzero
+   value (the equivalent single-letter option character, if there is
+   one).  For long options that have a zero `flag' field, `getopt'
+   returns the contents of the `val' field.  */
+
+struct option
+{
+# if defined __STDC__ && __STDC__
+  const char *name;
+# else
+  char *name;
+# endif
+  /* has_arg can't be an enum because some compilers complain about
+     type mismatches in all the code that assumes it is an int.  */
+  int has_arg;
+  int *flag;
+  int val;
+};
+
+/* Names for the values of the `has_arg' field of `struct option'.  */
+
+# define no_argument		0
+# define required_argument	1
+# define optional_argument	2
+#endif	/* need getopt */
+
+
+/* Get definitions and prototypes for functions to process the
+   arguments in ARGV (ARGC of them, minus the program name) for
+   options given in OPTS.
+
+   Return the option character from OPTS just read.  Return -1 when
+   there are no more options.  For unrecognized options, or options
+   missing arguments, `optopt' is set to the option letter, and '?' is
+   returned.
+
+   The OPTS string is a list of characters which are recognized option
+   letters, optionally followed by colons, specifying that that letter
+   takes an argument, to be placed in `optarg'.
+
+   If a letter in OPTS is followed by two colons, its argument is
+   optional.  This behavior is specific to the GNU `getopt'.
+
+   The argument `--' causes premature termination of argument
+   scanning, explicitly telling `getopt' that there are no more
+   options.
+
+   If OPTS begins with `--', then non-option arguments are treated as
+   arguments to the option '\0'.  This behavior is specific to the GNU
+   `getopt'.  */
+
+#if defined __STDC__ && __STDC__
+# ifdef __GNU_LIBRARY__
+/* Many other libraries have conflicting prototypes for getopt, with
+   differences in the consts, in stdlib.h.  To avoid compilation
+   errors, only prototype getopt for the GNU C library.  */
+extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
+# else /* not __GNU_LIBRARY__ */
+extern int getopt ();
+# endif /* __GNU_LIBRARY__ */
+
+# ifndef __need_getopt
+extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
+		        const struct option *__longopts, int *__longind);
+extern int getopt_long_only (int __argc, char *const *__argv,
+			     const char *__shortopts,
+		             const struct option *__longopts, int *__longind);
+
+/* Internal only.  Users should not call this directly.  */
+extern int _getopt_internal (int __argc, char *const *__argv,
+			     const char *__shortopts,
+		             const struct option *__longopts, int *__longind,
+			     int __long_only);
+# endif
+#else /* not __STDC__ */
+extern int getopt ();
+# ifndef __need_getopt
+extern int getopt_long ();
+extern int getopt_long_only ();
+
+extern int _getopt_internal ();
+# endif
+#endif /* __STDC__ */
+
+#ifdef	__cplusplus
+}
+#endif
+
+/* Make sure we later can get all the definitions and declarations.  */
+#undef __need_getopt
+
+#endif /* getopt.h */
diff -urN sysvinit-2.84/src/halt.c sysvinit-2.84-freebsd/src/halt.c
--- sysvinit-2.84/src/halt.c	Tue Nov 27 12:12:03 2001
+++ sysvinit-2.84-freebsd/src/halt.c	Fri Jan 25 09:21:52 2002
@@ -44,8 +44,9 @@
 #include <time.h>
 #include <signal.h>
 #include <stdio.h>
-#include <getopt.h>
+#include "getopt.h"
 #include "reboot.h"
+#include "init.h"
 
 char *Version = "@(#)halt  2.84  27-Nov-2001 miquels@cistron.nl";
 char *progname;
@@ -54,7 +55,9 @@
 #define RUNLVL_PICKY	0 /* Be picky about the runlevel */
 
 extern int ifdown(void);
+#ifndef __FreeBSD__
 extern int hddown(void);
+#endif
 extern void write_wtmp(char *user, char *id, int pid, int type, char *line);
 
 /*
@@ -95,6 +98,7 @@
 	boottime -= (times(NULL) / HZ);
 #endif
 
+#ifndef __FreeBSD__
 	/*
 	 *	Find runlevel in utmp.
 	 */
@@ -112,6 +116,7 @@
 #endif
 	}
 	endutent();
+#endif
 
 	/* This should not happen but warn the user! */
 	fprintf(stderr, "WARNING: could not determine runlevel"
@@ -246,8 +251,10 @@
 	if (do_ifdown)
 		(void)ifdown();
 
+#ifndef __FreeBSD__
 	if (do_hddown)
 		(void)hddown();
+#endif
 
 	if (do_reboot) {
 		init_reboot(BMAGIC_REBOOT);
@@ -255,7 +262,9 @@
 		/*
 		 *	Turn on hard reboot, CTRL-ALT-DEL will reboot now
 		 */
+#ifndef __FreeBSD__
 		init_reboot(BMAGIC_HARD);
+#endif
 
 		/*
 		 *	Stop init; it is insensitive to the signals sent
@@ -277,7 +286,9 @@
 	/*
 	 *	If we return, we (c)ontinued from the kernel monitor.
 	 */
+#ifndef __FreeBSD__
 	init_reboot(BMAGIC_SOFT);
+#endif
 	kill(1, SIGCONT);
 
 	exit(0);
diff -urN sysvinit-2.84/src/hddown.c sysvinit-2.84-freebsd/src/hddown.c
--- sysvinit-2.84/src/hddown.c	Wed Nov  7 15:11:21 2001
+++ sysvinit-2.84-freebsd/src/hddown.c	Fri Jan 25 09:03:27 2002
@@ -15,6 +15,7 @@
 
 #include <sys/ioctl.h>
 
+#ifndef __FreeBSD__
 #include <linux/hdreg.h>
 
 #define MAX_DISKS	64
@@ -111,3 +112,4 @@
 }
 #endif
 
+#endif
diff -urN sysvinit-2.84/src/init.c sysvinit-2.84-freebsd/src/init.c
--- sysvinit-2.84/src/init.c	Sun Feb  3 18:25:01 2002
+++ sysvinit-2.84-freebsd/src/init.c	Fri Jan 25 07:52:56 2002
@@ -32,7 +32,9 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#ifndef __FreeBSD__
 #include <sys/kd.h>
+#endif
 #include <sys/resource.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -43,6 +45,10 @@
 #include <string.h>
 #include <signal.h>
 #include <termios.h>
+#ifdef __FreeBSD__
+#define CBAUD	0010017
+#define CBAUDEX	0010000
+#endif
 #include <utmp.h>
 #include <ctype.h>
 #include <stdarg.h>
@@ -415,6 +421,7 @@
 	return oops_error;
 }
 
+#ifndef __FreeBSD__
 /*
  *	Set the process title.
  */
@@ -435,6 +442,7 @@
 
 	return len;
 }
+#endif
 
 /*
  *	Set console_dev to a working console.
@@ -1170,9 +1178,11 @@
 	if (!process)    strcpy(err, "missing process field");
 	if (!action || !*action)
 			strcpy(err, "missing action field");
+#ifndef __FreeBSD__
 	if (id && strlen(id) > sizeof(utproto.ut_id))
 		sprintf(err, "id field too long (max %d characters)",
 			(int)sizeof(utproto.ut_id));
+#endif
 	if (rlevel && strlen(rlevel) > 11)
 		strcpy(err, "rlevel field too long (max 11 characters)");
 	if (process && strlen(process) > 127)
@@ -1227,7 +1237,11 @@
 	 *	And fill it in.
 	 */
 	ch->action = actionNo;
+#ifndef __FreeBSD__
 	strncpy(ch->id, id, sizeof(utproto.ut_id) + 1); /* Hack for different libs. */
+#else
+	strncpy(ch->id, id, 8); /* Have no ut_id in utmp */
+#endif
 	strncpy(ch->process, process, sizeof(ch->process) - 1);
 	if (rlevel[0]) {
 		for(f = 0; f < sizeof(rlevel) - 1 && rlevel[f]; f++) {
@@ -1820,7 +1834,7 @@
 /*
  *	Attempt to re-exec.
  */
-void re_exec(void)
+void reexec(void)
 {
 	sigset_t mask, oldset;
 	pid_t	pid;
@@ -1915,7 +1929,7 @@
 		runlevel = read_level(level);
 		if (runlevel == 'U') {
 			runlevel = oldlevel;
-			re_exec();
+			reexec();
 		} else {
 			if (oldlevel != 'S' && runlevel == 'S') set_term(0);
 			if (runlevel == '6' || runlevel == '0' ||
@@ -2265,7 +2279,7 @@
 #endif
 		if (runlevel == 'U') {
 			runlevel = oldlevel;
-			re_exec();
+			reexec();
 		} else {
 			if (oldlevel != 'S' && runlevel == 'S') set_term(0);
 			if (runlevel == '6' || runlevel == '0' || runlevel == '1') set_term(0);
@@ -2319,13 +2333,14 @@
 	 *	Tell the kernel to send us SIGINT when CTRL-ALT-DEL
 	 *	is pressed, and that we want to handle keyboard signals.
 	 */
+#ifndef __FreeBSD__
 	init_reboot(BMAGIC_SOFT);
 	if ((f = open(VT_MASTER, O_RDWR | O_NOCTTY)) >= 0) {
 		(void) ioctl(f, KDSIGACCEPT, SIGWINCH);
 		close(f);
 	} else
 		(void) ioctl(0, KDSIGACCEPT, SIGWINCH);
-
+#endif
 	/*
 	 *	Ignore all signals.
 	 */
diff -urN sysvinit-2.84/src/init.h sysvinit-2.84-freebsd/src/init.h
--- sysvinit-2.84/src/init.h	Thu Jun  3 19:22:59 1999
+++ sysvinit-2.84-freebsd/src/init.h	Fri Jan 25 07:54:38 2002
@@ -113,3 +113,23 @@
 #define D_SLTIME       -8
 #define D_DIDBOOT      -9
 
+#ifdef __FreeBSD__
+/* Values for the `ut_type' field of a `struct utmp', which FreeBSD doesn't have  */
+#define EMPTY           0       /* No valid user accounting information.  */
+
+#define RUN_LVL         1       /* The system's runlevel.  */
+#define BOOT_TIME       2       /* Time of system boot.  */
+#define NEW_TIME        3       /* Time after system clock changed.  */
+#define OLD_TIME        4       /* Time when system clock changed.  */
+
+#define INIT_PROCESS    5       /* Process spawned by the init process.  */
+#define LOGIN_PROCESS   6       /* Session leader of a logged in user.  */
+#define USER_PROCESS    7       /* Normal process.  */
+#define DEAD_PROCESS    8       /* Terminated process.  */
+
+#define ACCOUNTING      9
+
+#define UTMP_FILE	_PATH_UTMP
+#define WTMP_FILE	_PATH_WTMP
+
+#endif
diff -urN sysvinit-2.84/src/killall5.c sysvinit-2.84-freebsd/src/killall5.c
--- sysvinit-2.84/src/killall5.c	Tue Jul 31 14:06:27 2001
+++ sysvinit-2.84-freebsd/src/killall5.c	Fri Jan 25 23:52:29 2002
@@ -37,7 +37,11 @@
 #include <signal.h>
 #include <dirent.h>
 #include <syslog.h>
+#ifndef __FreeBSD__
 #include <getopt.h>
+#else
+#include "getopt.h"
+#endif
 #include <stdarg.h>
 
 char *Version = "@(#)killall5 2.81 31-Jul-2001 miquels@cistron.nl";
diff -urN sysvinit-2.84/src/last.c sysvinit-2.84-freebsd/src/last.c
--- sysvinit-2.84/src/last.c	Wed Jun 13 12:13:21 2001
+++ sysvinit-2.84-freebsd/src/last.c	Sat Jan 26 00:02:13 2002
@@ -30,7 +30,11 @@
 #include <unistd.h>
 #include <string.h>
 #include <signal.h>
+#ifdef __FreeBSD__
+#include "getopt.h"
+#else
 #include <getopt.h>
+#endif
 #include <netinet/in.h>
 #include <netdb.h>
 #include <arpa/inet.h>
diff -urN sysvinit-2.84/src/reboot.h sysvinit-2.84-freebsd/src/reboot.h
--- sysvinit-2.84/src/reboot.h	Wed Sep 24 08:55:52 1997
+++ sysvinit-2.84-freebsd/src/reboot.h	Fri Jan 25 09:01:27 2002
@@ -10,14 +10,21 @@
 #  include <sys/reboot.h>
 #endif
 
+#ifdef __FreeBSD__
+#include <sys/reboot.h>
+#define BMAGIC_REBOOT	RB_AUTOBOOT
+#define BMAGIC_HALT	RB_HALT
+#define BMAGIC_POWEROFF	RB_POWEROFF
+#define init_reboot(magic) reboot(magic)
+#else
 #define BMAGIC_HARD	0x89ABCDEF
 #define BMAGIC_SOFT	0
 #define BMAGIC_REBOOT	0x01234567
 #define BMAGIC_HALT	0xCDEF0123
 #define BMAGIC_POWEROFF	0x4321FEDC
-
-#if defined(__GLIBC__)
-  #define init_reboot(magic) reboot(magic)
-#else
-  #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
+# if defined(__GLIBC__)
+   #define init_reboot(magic) reboot(magic)
+# else
+   #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
+# endif
 #endif
diff -urN sysvinit-2.84/src/runlevel.c sysvinit-2.84-freebsd/src/runlevel.c
--- sysvinit-2.84/src/runlevel.c	Wed Apr 16 18:29:51 1997
+++ sysvinit-2.84-freebsd/src/runlevel.c	Fri Jan 25 23:54:36 2002
@@ -13,8 +13,8 @@
  */
 
 #include <stdio.h>
-#include <utmp.h>
 #include <time.h>
+#include <utmp.h>
 #include <stdlib.h>
 
 int main(argc, argv)
@@ -24,6 +24,7 @@
   struct utmp *ut;
   char prev;
 
+#ifndef __FreeBSD__
   if (argc > 1) utmpname(argv[1]);
 
   setutent();
@@ -36,9 +37,10 @@
 		exit(0);
 	}
   }
+  endutent();
+#endif
   
   printf("unknown\n");
-  endutent();
   return(1);
 }
 
diff -urN sysvinit-2.84/src/shutdown.c sysvinit-2.84-freebsd/src/shutdown.c
--- sysvinit-2.84/src/shutdown.c	Wed Sep  5 10:46:30 2001
+++ sysvinit-2.84-freebsd/src/shutdown.c	Fri Jan 25 23:29:43 2002
@@ -38,6 +38,10 @@
 #include <utmp.h>
 #include "paths.h"
 #include "reboot.h"
+#ifdef __FreeBSD__
+#include "init.h"
+#include "utent.h"
+#endif
 
 char *Version = "@(#) shutdown 2.79 26-Feb-2001 miquels@cistron.nl";
 
@@ -190,9 +194,8 @@
 	va_end(ap);
 
 	chdir("/");
-	environ = clean_env;
 
-	execvp(argv[0], argv);
+	execve(argv[0], argv, clean_env);
 	perror(argv[0]);
 	exit(1);
 
@@ -435,6 +438,7 @@
   	}
   }
 
+#ifndef __FreeBSD__
   /* Do we need to use the shutdown.allow file ? */
   if (useacl && (fp = fopen(SDALLOW, "r")) != NULL) {
 
@@ -486,6 +490,7 @@
 		exit(1);
 	}
   }
+#endif
 
   /* Read pid of running shutdown from a file */
   if ((fp = fopen(SDPID, "r")) != NULL) {
diff -urN sysvinit-2.84/src/sulogin.c sysvinit-2.84-freebsd/src/sulogin.c
--- sysvinit-2.84/src/sulogin.c	Tue Jul 31 14:22:07 2001
+++ sysvinit-2.84-freebsd/src/sulogin.c	Sat Jan 26 00:01:32 2002
@@ -21,7 +21,9 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <pwd.h>
+#ifndef __FreeBSD__
 #include <shadow.h>
+#endif
 #include <termios.h>
 #include <sys/ioctl.h>
 #if defined(__GLIBC__)
@@ -152,10 +154,14 @@
 	 *	First, we try to get the password the standard
 	 *	way using normal library calls.
 	 */
+#ifndef __FreeBSD__
 	if ((pw = getpwnam("root")) &&
 	    !strcmp(pw->pw_passwd, "x") &&
 	    (spw = getspnam("root")))
 		pw->pw_passwd = spw->sp_pwdp;
+#else
+	pw = getpwnam("root");
+#endif
 	if (pw || !try_manually) return pw;
 
 	/*
@@ -254,7 +260,11 @@
 
 	tcgetattr(0, &old);
 	tcgetattr(0, &tty);
+#ifdef __FreeBSD__
+	tty.c_iflag &= ~(IXON|IXOFF|IXANY);
+#else
 	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
+#endif
 	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
 	tcsetattr(0, TCSANOW, &tty);
 
diff -urN sysvinit-2.84/src/utent.c sysvinit-2.84-freebsd/src/utent.c
--- sysvinit-2.84/src/utent.c	Thu Jan  1 00:00:00 1970
+++ sysvinit-2.84-freebsd/src/utent.c	Sat Jan 26 00:09:49 2002
@@ -0,0 +1,412 @@
+/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@cygnus.com>
+   and Paul Janzen <pcj@primenet.com>, 1996.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <utmp.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include "init.h"
+
+/* This is the default name.  */
+static const char default_file_name[] = _PATH_UTMP;
+
+/* Current file name.  */
+static const char *file_name = (const char *) default_file_name;
+
+/* Descriptor for the file and position.  */
+static int file_fd = INT_MIN;
+static off_t file_offset;
+
+static struct utmp last_entry;
+static struct utmp _ut;
+
+/* Functions defined here.  */
+ int setutent (int reset);
+ int getutent_r_file (struct utmp *buffer, struct utmp **result);
+ int getutid_r_file (const struct utmp *key, struct utmp *buffer,
+			   struct utmp **result);
+ int getutline_r_file (const struct utmp *key, struct utmp *buffer,
+			     struct utmp **result);
+ struct utmp *pututline (const struct utmp *data);
+ void endutent (void);
+ int utmpname_file (const char *name);
+
+ int
+setutent (int reset)
+{
+  if (file_fd == INT_MIN)
+    {
+      file_fd = open (file_name, O_RDWR);
+      if (file_fd == -1)
+	{
+	  /* Hhm, read-write access did not work.  Try read-only.  */
+	  file_fd = open (file_name, O_RDONLY);
+	  if (file_fd == -1)
+	    {
+	      perror ("while opening UTMP file");
+	      return 0;
+	    }
+	}
+      file_offset = 0;
+
+#if _HAVE_UT_TYPE - 0
+      /* Make sure the entry won't match.  */
+      last_entry.ut_type = -1;
+#endif
+    }
+  else if (reset)
+    {
+      lseek (file_fd, 0, SEEK_SET);
+
+      /* Remember we are at beginning of file.  */
+      file_offset = 0;
+
+#if _HAVE_UT_TYPE - 0
+      /* Make sure the entry won't match.  */
+      last_entry.ut_type = -1;
+#endif
+    }
+
+  return 1;
+}
+
+
+ void
+endutent (void)
+{
+  if (file_fd >= 0)
+    close (file_fd);
+
+  file_fd = INT_MIN;
+}
+
+
+ int
+getutent_r_file (struct utmp *buffer, struct utmp **result)
+{
+  int nbytes;
+  struct flock fl;			/* Information struct for locking.  */
+
+  /* Open utmp file if not already done.  */
+  if (file_fd == INT_MIN)
+    setutent (1);
+
+  if (file_fd == -1 || file_offset == -1l)
+    {
+      /* Not available.  */
+      *result = NULL;
+      return -1;
+    }
+
+  /* XXX The following is not perfect.  Instead of locking the file itself
+     Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl> suggests to
+     use an extra locking file.  */
+
+  /* Try to get the lock.  */
+  memset (&fl, '\0', sizeof (struct flock));
+  fl.l_type = F_WRLCK;
+  fl.l_whence = SEEK_SET;
+  fcntl (file_fd, F_SETLKW, &fl);
+
+  /* Read the next entry.  */
+  nbytes = read (file_fd, &last_entry, sizeof (struct utmp));
+
+  /* And unlock the file.  */
+  fl.l_type = F_UNLCK;
+  fcntl (file_fd, F_SETLKW, &fl);
+
+  if (nbytes != sizeof (struct utmp))
+    {
+      file_offset = -1l;
+      *result = NULL;
+      return -1;
+    }
+
+  /* Update position pointer.  */
+  file_offset += sizeof (struct utmp);
+
+  memcpy (buffer, &last_entry, sizeof (struct utmp));
+  *result = buffer;
+
+  return 0;
+}
+
+
+/* For implementing this function we don't use the getutent_r function
+   because we can avoid the reposition on every new entry this way.  */
+ int
+getutline_r_file (const struct utmp *line, struct utmp *buffer,
+		  struct utmp **result)
+{
+  if (file_fd < 0 || file_offset == -1l)
+    {
+      *result = NULL;
+      return -1;
+    }
+
+  while (1)
+    {
+      /* Read the next entry.  */
+      if (read (file_fd, &last_entry, sizeof (struct utmp))
+	  != sizeof (struct utmp))
+	{
+	  errno= ESRCH;
+	  file_offset = -1l;
+	  *result = NULL;
+	  return -1;
+	}
+      file_offset += sizeof (struct utmp);
+
+      /* Stop if we found a user or login entry.  */
+      if (
+#if _HAVE_UT_TYPE - 0
+	  (last_entry.ut_type == USER_PROCESS
+	   || last_entry.ut_type == LOGIN_PROCESS)
+	  &&
+#endif
+	  !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
+	break;
+    }
+
+  memcpy (buffer, &last_entry, sizeof (struct utmp));
+  *result = buffer;
+
+  return 0;
+}
+
+
+ int
+proc_utmp_eq (const struct utmp *entry, const struct utmp *match)
+{
+  return
+    (
+#if _HAVE_UT_TYPE - 0
+     (entry->ut_type == INIT_PROCESS
+      || entry->ut_type == LOGIN_PROCESS
+      || entry->ut_type == USER_PROCESS
+      || entry->ut_type == DEAD_PROCESS)
+     &&
+     (match->ut_type == INIT_PROCESS
+      || match->ut_type == LOGIN_PROCESS
+      || match->ut_type == USER_PROCESS
+      || match->ut_type == DEAD_PROCESS)
+     &&
+#endif
+#if _HAVE_UT_ID - 0
+     (entry->ut_id[0] && match->ut_id[0]
+      ? strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
+      : strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0)
+#else
+     strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0
+#endif
+     );
+}
+
+ int
+internal_getut_r (const struct utmp *id, struct utmp *buffer)
+{
+#if _HAVE_UT_TYPE - 0
+  if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
+      || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
+    {
+      /* Search for next entry with type RUN_LVL, BOOT_TIME,
+	 OLD_TIME, or NEW_TIME.  */
+
+      while (1)
+	{
+	  /* Read the next entry.  */
+	  if (read (file_fd, buffer, sizeof (struct utmp))
+	      != sizeof (struct utmp))
+	    {
+	      errno=ESRCH;
+	      file_offset = -1l;
+	      return -1;
+	    }
+	  file_offset += sizeof (struct utmp);
+
+	  if (id->ut_type == buffer->ut_type)
+	    break;
+	}
+    }
+  else
+#endif /* _HAVE_UT_TYPE */
+    {
+      /* Search for the next entry with the specified ID and with type
+	 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS.  */
+
+      while (1)
+	{
+	  /* Read the next entry.  */
+	  if (read (file_fd, buffer, sizeof (struct utmp))
+	      != sizeof (struct utmp))
+	    {
+	      errno=ESRCH;
+	      file_offset = -1l;
+	      return -1;
+	    }
+	  file_offset += sizeof (struct utmp);
+
+	  if (proc_utmp_eq (buffer, id))
+	    break;
+	}
+    }
+
+  return 0;
+}
+
+
+ struct utmp *
+pututline (const struct utmp *data)
+{
+  struct flock fl;			/* Information struct for locking.  */
+  struct utmp buffer;
+  struct utmp *pbuf;
+  int found;
+
+  if (file_fd < 0)
+    /* Something went wrong.  */
+    return NULL;
+
+  if (file_fd == INT_MIN)
+    /* The file is closed.  Open it again.  */
+    setutent (0);
+
+  /* Find the correct place to insert the data.  */
+  if (file_offset > 0
+      && (
+#if _HAVE_UT_TYPE - 0
+	  (last_entry.ut_type == data->ut_type
+	   && (last_entry.ut_type == RUN_LVL
+	       || last_entry.ut_type == BOOT_TIME
+	       || last_entry.ut_type == OLD_TIME
+	       || last_entry.ut_type == NEW_TIME))
+	  ||
+#endif
+	  proc_utmp_eq (&last_entry, data)))
+    found = 1;
+  else
+    found = internal_getut_r (data, &buffer);
+
+  /* Try to lock the file.  */
+  memset (&fl, '\0', sizeof (struct flock));
+  fl.l_type = F_WRLCK;
+  fl.l_whence = SEEK_SET;
+  fcntl (file_fd, F_SETLKW, &fl);
+
+  if (found < 0)
+    {
+      /* We append the next entry.  */
+      file_offset = lseek (file_fd, 0, SEEK_END);
+      if (file_offset % sizeof (struct utmp) != 0)
+	{
+	  file_offset -= file_offset % sizeof (struct utmp);
+	  ftruncate (file_fd, file_offset);
+
+	  if (lseek (file_fd, 0, SEEK_END) < 0)
+	    {
+	      pbuf = NULL;
+	      goto unlock_return;
+	    }
+	}
+    }
+  else
+    {
+      /* We replace the just read entry.  */
+      file_offset -= sizeof (struct utmp);
+      lseek (file_fd, file_offset, SEEK_SET);
+    }
+
+  /* Write the new data.  */
+  if (write (file_fd, data, sizeof (struct utmp)) != sizeof (struct utmp)
+      /* If we appended a new record this is only partially written.
+	 Remove it.  */
+      && found < 0)
+    {
+      (void) ftruncate (file_fd, file_offset);
+      pbuf = NULL;
+    }
+  else
+    {
+      file_offset += sizeof (struct utmp);
+      pbuf = (struct utmp *) data;
+    }
+
+ unlock_return:
+   /* And unlock the file.  */
+  fl.l_type = F_UNLCK;
+  fcntl (file_fd, F_SETLKW, &fl);
+
+  return pbuf;
+}
+
+
+ int
+utmpname_file (const char *name)
+{
+  if (strcmp (name, file_name) != 0)
+    {
+      if (strcmp (name, default_file_name) == 0)
+	{
+	  if (file_name != default_file_name)
+	    free ((char *) file_name);
+
+	  file_name = default_file_name;
+	}
+      else
+	{
+	  char *new_name = strdup (name);
+	  if (new_name == NULL)
+	    /* Out of memory.  */
+	    return -1;
+
+	  if (file_name != default_file_name)
+	    free ((char *) file_name);
+
+	  file_name = new_name;
+	}
+    }
+  return 0;
+}
+
+struct utmp *getutent (void)
+{
+  struct utmp *result;
+
+  if (getutent_r_file (&_ut, &result) < 0)
+    return NULL;
+
+  return (struct utmp *)&_ut;
+}
+
+struct utmp *getutline ( struct utmp *line)
+{
+  struct utmp *result;
+ 
+  if (getutline_r_file ((const struct utmp *)&line, &_ut, &result) < 0)
+    return NULL;
+ 
+  return (struct utmp *)&_ut;
+}
diff -urN sysvinit-2.84/src/utent.h sysvinit-2.84-freebsd/src/utent.h
--- sysvinit-2.84/src/utent.h	Thu Jan  1 00:00:00 1970
+++ sysvinit-2.84-freebsd/src/utent.h	Fri Jan 25 09:33:54 2002
@@ -0,0 +1,14 @@
+#include <utmp.h>
+
+/* Functions defined here.  */
+ int setutent (int reset);
+ int getutent_r_file (struct utmp *buffer, struct utmp **result);
+ int getutid_r_file (const struct utmp *key, struct utmp *buffer,
+			   struct utmp **result);
+ int getutline_r_file (const struct utmp *key, struct utmp *buffer,
+			     struct utmp **result);
+ struct utmp *pututline (const struct utmp *data);
+ void endutent (void);
+ int utmpname_file (const char *name);
+ struct utmp *getutent (void);
+ struct utmp *getutline ( struct utmp *line);
diff -urN sysvinit-2.84/src/utmp.c sysvinit-2.84-freebsd/src/utmp.c
--- sysvinit-2.84/src/utmp.c	Wed Jun  9 11:11:33 1999
+++ sysvinit-2.84-freebsd/src/utmp.c	Sat Jan 26 00:09:01 2002
@@ -21,7 +21,9 @@
 #include "init.h"
 #include "initreq.h"
 #include "paths.h"
-
+#ifdef __FreeBSD__
+#include "utent.h"
+#endif
 
 #if defined(__GLIBC__)
 #  if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) && defined(__powerpc__)
@@ -80,10 +82,12 @@
 #else
 	time(&utmp.ut_time);
 #endif
+#ifndef __FreeBSD__
 	utmp.ut_pid  = pid;
 	utmp.ut_type = type;
-	strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
 	strncpy(utmp.ut_id  , id  , sizeof(utmp.ut_id  ));
+#endif
+	strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
 	strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
         
         /* Put the OS version in place of the hostname */
@@ -140,6 +144,7 @@
 	 *	Fill out an utmp struct.
 	 */
 	memset(&utmp, 0, sizeof(utmp));
+#ifndef __FreeBSD__
 	utmp.ut_type = type;
 	utmp.ut_pid = pid;
 	strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
@@ -149,8 +154,10 @@
 	time(&utmp.ut_time);
 #endif
 	strncpy(utmp.ut_user, user, UT_NAMESIZE);
+#endif
 	if (line) strncpy(utmp.ut_line, line, UT_LINESIZE);
 	
+#ifndef __FreeBSD__
 	/*
 	 *	We might need to find the existing entry first, to
 	 *	find the tty of the process (for wtmp accounting).
@@ -159,7 +166,7 @@
 		/*
 		 *	Find existing entry for the tty line.
 		 */
-		setutent();
+		setutent(0);
 		tmp = utmp;
 		if ((utmptr = getutid(&tmp)) != NULL) {
 			strncpy(utmp.ut_line, utmptr->ut_line, UT_LINESIZE);
@@ -167,11 +174,12 @@
 				strncpy(oldline, utmptr->ut_line, UT_LINESIZE);
 		}
 	}
+#endif
 
 	/*
 	 *	Update existing utmp file.
 	 */
-	setutent();
+	setutent(0);
 	pututline(&utmp);
 	endutent();
 }

Attachment: pgpY97N_NdsXA.pgp
Description: PGP signature


Reply to: