[comp.protocols.appletalk] CAP 5.0 on Xenix part 1/3: BSD library

chip@ateng.com (Chip Salzenberg) (11/09/89)

#! /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:  Makefile fsync.c ftruncate.c groups.c rename.c scandir.c
# Wrapped by chip@border on Wed Jul 12 11:15:23 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(609 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# $Author: chip $ $Date: 89/07/12 11:15:08 $
X# $Header: Makefile,v 1.2 89/07/12 11:15:08 chip Exp $
X# $Revision: 1.2 $
X#
X
X#
X# Makefile for BSD library.
X#
X
XSHELL   = /bin/sh
XCC      = cc
XCFLAGS  = -O
XOBJS    = fsync.o ftruncate.o groups.o rename.o scandir.o
XSRCS    = fsync.c ftruncate.c groups.c rename.c scandir.c
XOTHERS  = Makefile
XSHAR    = libbsd.sh
XLIB     = Slibbsd.a
X
Xall:    $(LIB)
X
Xinstall: all
X	copy -m $(LIB) /usr/lib/386
X
Xclean:
X	rm -f *.o
Xclobber: clean
X	rm -f $(LIB) $(SHAR)
X
Xshar:   $(SHAR)
X$(SHAR): $(OTHERS) $(SRCS)
X	shar $(OTHERS) $(SRCS) >$@
X
X$(LIB): $(OBJS)
X	ar rc $@ $(OBJS)
X	ranlib $@
END_OF_FILE
if test 609 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'fsync.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'fsync.c'\"
else
echo shar: Extracting \"'fsync.c'\" \(304 characters\)
sed "s/^X//" >'fsync.c' <<'END_OF_FILE'
X/*
X * $Author: chip $ $Date: 89/06/29 13:02:28 $
X * $Header: fsync.c,v 1.1 89/06/29 13:02:28 chip Exp $
X * $Revision: 1.1 $
X */
X
X/*
X * Fsync() for non-BSD systems.
X *
X * For now, this is a no-op, since we can't force writes without
X * three system calls.
X */
X
Xint
Xfsync(fd)
X    int fd;
X{
X    return 0;
X}
END_OF_FILE
if test 304 -ne `wc -c <'fsync.c'`; then
    echo shar: \"'fsync.c'\" unpacked with wrong size!
fi
# end of 'fsync.c'
fi
if test -f 'ftruncate.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'ftruncate.c'\"
else
echo shar: Extracting \"'ftruncate.c'\" \(1349 characters\)
sed "s/^X//" >'ftruncate.c' <<'END_OF_FILE'
X/*
X * $Author: chip $ $Date: 89/06/29 13:02:29 $
X * $Header: ftruncate.c,v 1.1 89/06/29 13:02:29 chip Exp $
X * $Revision: 1.1 $
X */
X
X/*
X * Ftruncate() for non-BSD systems.
X *
X * This module gives the basic functionality for ftruncate() which
X * truncates the given file descriptor to the given length.
X * ftruncate() is a Berkeley system call, but does not exist in any
X * form on many other versions of UNIX such as SysV. Note that Xenix
X * has chsize() which changes the size of a given file descriptor,
X * so that is used if M_XENIX is defined.
X *
X * Since there is not a known way to support this under generic SysV,
X * there is no code generated for those systems.
X *
X * SPECIAL NOTE: On Xenix, using this call in the BSD library
X * will REQUIRE the use of -lx for the extended library since chsize()
X * is not in the standard C library.
X *
X * By Marc Frajola, 3/27/87
X */
X
X#ifdef M_XENIX
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <fcntl.h>
X
Xftruncate(fd,length)
X    int fd;			/* File descriptor to truncate */
X    off_t length;		/* Length to truncate file to */
X{
X    int status;			/* Status returned from truncate proc */
X
X#if defined(M_XENIX)
X    status = chsize(fd,length);
X#else
X    /* Stuff for Microport here */
X    status = -1;
X    NON-XENIX SYSTEMS CURRENTLY NOT SUPPORTED
X#endif
X
X    return(status);
X}
X
X#endif /* M_XENIX */
END_OF_FILE
if test 1349 -ne `wc -c <'ftruncate.c'`; then
    echo shar: \"'ftruncate.c'\" unpacked with wrong size!
fi
# end of 'ftruncate.c'
fi
if test -f 'groups.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'groups.c'\"
else
echo shar: Extracting \"'groups.c'\" \(587 characters\)
sed "s/^X//" >'groups.c' <<'END_OF_FILE'
X/*
X * $Author: chip $ $Date: 89/06/29 13:02:30 $
X * $Header: groups.c,v 1.1 89/06/29 13:02:30 chip Exp $
X * $Revision: 1.1 $
X */
X
X/*
X * BSD groups functions for non-BSD systems.
X *
X * For now, we're always part of one group, our egid.
X */
X
Xint
Xinitgroups(username, usergid)
X    char *username;
X    int usergid;
X{
X    /*
X     * When this function is called, setgid() has already been called.
X     * Therefore, we don't have to do anything.
X     */
X
X    return 0;
X}
X
Xint
Xgetgroups(gv, gmax)
X    int *gv;
X    int gmax;
X{
X    if (gmax < 1)
X	return 0;
X
X    gv[0] = getegid();
X    return 1;
X}
END_OF_FILE
if test 587 -ne `wc -c <'groups.c'`; then
    echo shar: \"'groups.c'\" unpacked with wrong size!
fi
# end of 'groups.c'
fi
if test -f 'rename.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'rename.c'\"
else
echo shar: Extracting \"'rename.c'\" \(1793 characters\)
sed "s/^X//" >'rename.c' <<'END_OF_FILE'
X/*
X * $Author: chip $ $Date: 89/06/29 13:02:31 $
X * $Header: rename.c,v 1.1 89/06/29 13:02:31 chip Exp $
X * $Revision: 1.1 $
X */
X
X/*
X * Rename system call -- Replacement for Berzerkeley 4.2 rename system
X * call that is missing in Xenix.
X *
X * By Marc Frajola and Chris Paris.
X * Directory hack by Chip Salzenberg.
X */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <signal.h>
X#include <errno.h>
X
Xrename(src,dest)
X    char *src;			/* Source file to rename */
X    char *dest;			/* Name for renamed file */
X{
X    int status;			/* Status returned from link system call */
X    struct stat stbuf;		/* Buffer for statting destination file */
X
X    /* Find out what the destination is: */
X    status = stat(dest,&stbuf);
X    if (status >= 0) {
X	/* See if the file is a regular file; if not, return error: */
X	if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
X	    return(-1);
X	}
X    }
X
X    /* Unlink destination since it is a file: */
X    unlink(dest);
X
X    /* Find out what the source is: */
X    status = stat(src,&stbuf);
X    if (status < 0)
X	return -1;
X    if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
X    {
X	/* Directory hack for SCO Xenix */
X
X	static char mvdir[] = "/usr/lib/mv_dir";
X	void (*oldsigcld)();
X	int pid;
X
X	oldsigcld = signal(SIGCLD, SIG_DFL);
X	while ((pid = fork()) == -1)
X	{
X	    if (errno != EAGAIN)
X		return -1;
X	    sleep(5);
X	}
X	if (pid == 0)
X	{
X	    execl(mvdir, mvdir, src, dest, (char *) 0);
X	    perror(mvdir);
X	    exit(1);
X	}
X	if (wait(&status) != pid)
X	{
X	    fprintf(stderr, "rename: wait failure\n");
X	    status = -1;
X	}
X	(void) signal(SIGCLD, oldsigcld);
X    }
X    else
X    {
X	/* Link source to destination file: */
X	status = link(src,dest);
X	if (status != 0) {
X	    return(-1);
X	}
X	status = unlink(src);
X    }
X    return((status == 0) ? 0 : (-1));
X}
END_OF_FILE
if test 1793 -ne `wc -c <'rename.c'`; then
    echo shar: \"'rename.c'\" unpacked with wrong size!
fi
# end of 'rename.c'
fi
if test -f 'scandir.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'scandir.c'\"
else
echo shar: Extracting \"'scandir.c'\" \(3198 characters\)
sed "s/^X//" >'scandir.c' <<'END_OF_FILE'
X/*
X * $Author: chip $ $Date: 89/06/29 13:02:32 $
X * $Header: scandir.c,v 1.1 89/06/29 13:02:32 chip Exp $
X * $Revision: 1.1 $
X */
X
X/*
X * Copyright (c) 1983 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that the above copyright notice and this paragraph are
X * duplicated in all such forms and that any documentation,
X * advertising materials, and other materials related to such
X * distribution and use acknowledge that the software was developed
X * by the University of California, Berkeley.  The name of the
X * University may not be used to endorse or promote products derived
X * from this software without specific prior written permission.
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
X */
X
X#if defined(LIBC_SCCS) && !defined(lint)
Xstatic char sccsid[] = "@(#)scandir.c	5.3 (Berkeley) 6/18/88";
X#endif /* LIBC_SCCS and not lint */
X
X/*
X * Scan the directory dirname calling select to make a list of selected
X * directory entries then sort using qsort and compare routine dcomp.
X * Returns the number of entries and a pointer to a list of pointers to
X * struct direct (through namelist). Returns -1 if there were any errors.
X */
X
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/ndir.h>
X
Xscandir(dirname, namelist, select, dcomp)
X	char *dirname;
X	struct direct *(*namelist[]);
X	int (*select)(), (*dcomp)();
X{
X	register struct direct *d, *p, **names;
X	register int nitems;
X	register char *cp1, *cp2;
X	struct stat stb;
X	long arraysz;
X	DIR *dirp;
X
X	if ((dirp = opendir(dirname)) == NULL)
X		return(-1);
X	if (fstat(dirp->dd_fd, &stb) < 0)
X		return(-1);
X
X	/*
X	 * estimate the array size by taking the size of the directory file
X	 * and dividing it by a multiple of the minimum size entry. 
X	 */
X	arraysz = (stb.st_size / 24);
X	names = (struct direct **)malloc(arraysz * sizeof(struct direct *));
X	if (names == NULL)
X		return(-1);
X
X	nitems = 0;
X	while ((d = readdir(dirp)) != NULL) {
X		if (select != NULL && !(*select)(d))
X			continue;	/* just selected names */
X		/*
X		 * Make a minimum size copy of the data
X		 */
X		p = (struct direct *)malloc(DIRSIZ(d));
X		if (p == NULL)
X			return(-1);
X		p->d_ino = d->d_ino;
X		p->d_reclen = d->d_reclen;
X		p->d_namlen = d->d_namlen;
X		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
X		/*
X		 * Check to make sure the array has space left and
X		 * realloc the maximum size.
X		 */
X		if (++nitems >= arraysz) {
X			if (fstat(dirp->dd_fd, &stb) < 0)
X				return(-1);	/* just might have grown */
X			arraysz = stb.st_size / 12;
X			names = (struct direct **)realloc((char *)names,
X				arraysz * sizeof(struct direct *));
X			if (names == NULL)
X				return(-1);
X		}
X		names[nitems-1] = p;
X	}
X	closedir(dirp);
X	if (nitems && dcomp != NULL)
X		qsort(names, nitems, sizeof(struct direct *), dcomp);
X	*namelist = names;
X	return(nitems);
X}
X
X/*
X * Alphabetic order comparison routine for those who want it.
X */
Xalphasort(d1, d2)
X	struct direct **d1, **d2;
X{
X	return(strcmp((*d1)->d_name, (*d2)->d_name));
X}
END_OF_FILE
if test 3198 -ne `wc -c <'scandir.c'`; then
    echo shar: \"'scandir.c'\" unpacked with wrong size!
fi
# end of 'scandir.c'
fi
echo shar: End of shell archive.
exit 0
-- 
You may redistribute this article only to those who may freely do likewise.
Chip Salzenberg at A T Engineering;  <chip@ateng.com> or <uunet!ateng!chip>