[comp.os.minix] Getwd

ncpascal@ndsuvax.UUCP (Freeman Pascal) (12/17/87)

Hello,

    While I was porting the BSD version of "arc" to MINIX I discovered
I needed getwd(3) but libc lack one, so I wrote it.  This implementation
follows as outlined in the BSD 4.3 PRM.  I hope this is useful and makes
it's way into your libc.a.

    Just unshar and follow the instructions found in the "INSTALL" file.

				Freeman P. Pascal IV
				ncpascal@ndsuvax


#! /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 archive 1 (of 1)."
# Contents:  INSTALL Makefile getwd.c makelibc tst.c
# Wrapped by ncpascal@ndsuvax on Wed Dec 16 22:27:04 1987
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f INSTALL -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"INSTALL\"
else
echo shar: Extracting \"INSTALL\" \(615 characters\)
sed "s/^X//" >INSTALL <<'END_OF_INSTALL'
XManifest:
X--------
X	INSTALL		This file
X	Makefile	makefile for tst
X	makelibc	shell script to make C library
X	getwd.c		library function (getwd(3))
X	tst.c		test file for getwd(3)
X
XInstallation instructions:
X-------------------------
X
X1.  Compile getwd.c as a library routine and place in /usr/lib/libc.a.
X    If you are using the makelibc shell script that was posted quite 
X    awhile back just append "ar av libc.a getwd.s" to the beginning 
X    and run.  I am including my version if you don't have it.
X
X2.  Run makefile to compile tst.c.  
X
X3.  Run tst to varify results:
X
X	Usage:	tst file-from file-to
X
X3.  Enjoy
END_OF_INSTALL
if test 615 -ne `wc -c <INSTALL`; then
    echo shar: \"INSTALL\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f Makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(105 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
X#
X#	Makefile for getwd(3) test
X#
XCFLAGS = -T. -i
X
Xtst:		tst.c
X	cc $(CFLAGS) -o tst tst.c
X	@echo "done."
X
END_OF_Makefile
if test 105 -ne `wc -c <Makefile`; then
    echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f getwd.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"getwd.c\"
else
echo shar: Extracting \"getwd.c\" \(2553 characters\)
sed "s/^X//" >getwd.c <<'END_OF_getwd.c'
X/*
X *	getwd()	- get working directory
X *
X *	Modeled after Adri Koppes' pwd utility.
X */
X#include	<sys/types.h>
X#include	<sys/stat.h>
X#include	<sys/dir.h>
X#include	<sys/file.h>
X
X#ifndef NULL
X#define	NULL	0
X#endif
X
X#define PUBLIC
X#define PRIVATE	static
X
X#define	MAXPATHLEN	1024			/* just like BSD	*/
X
Xvoid	reverse_path();
X
X/*========================================================================*\
X**				getwd()					  **
X\*========================================================================*/
XPUBLIC char *
Xgetwd( pathname )
Xchar	*pathname;
X{
X  struct direct			dir;
X  register struct stat		*sp1, *sp2, *stmp;
X  struct stat			sbuf[ 2 ];
X  char				pbuf[ MAXPATHLEN ];
X  int				fd;
X
X  *pbuf = '\0';		/* clear path buffer */
X  sp1 = &sbuf[ 0 ];
X  sp2 = &sbuf[ 1 ];
X  stat( ".", sp1 );
X  do {
X	if (( fd = open( "..", O_RDONLY )) < 0) {
X		strcpy( pathname, "Cannot open \"..\"(1)" );
X		return( NULL );
X	}
X	stmp = sp1; 
X	sp1  = sp2;
X	sp2  = stmp;
X	stat( "..", sp1 );
X	chdir( ".." );
X	if ( sp1->st_dev == sp2->st_dev ) /* are we on the same device?	*/
X		do {
X			if (read( fd, &dir, sizeof( struct direct )) <
X				sizeof( struct direct )) {
X				strcpy( pathname, "Cannot open \"..\"(2)" );
X				return( NULL );	
X			}
X		} while( dir.d_ino != sp2->st_ino );
X	else
X		do {
X			if (read( fd, &dir, sizeof( struct direct )) <
X				sizeof( struct direct )) {
X				strcpy( pathname, "Cannot open \"..\"" );
X				return( NULL );
X			}
X			stat( dir.d_name, sp1 );
X		} while((sp1->st_dev != sp2->st_dev) || (sp1->st_ino != sp2->st_ino));
X	close( fd );
X	if (strcmp( ".", dir.d_name )) {
X		strcat( pbuf, "/" );
X		strcat( pbuf, dir.d_name );
X	}
X  } while (( sp1->st_ino != sp2->st_ino ) || ( sp1->st_dev != sp2->st_dev ));
X  if (!pathname)
X	if (( pathname = (char *) malloc( strlen( dir ))) == 0 ) {
X		strcpy( pathname, "Out of Memory" );
X		return( NULL );
X	}
X  if (!*pbuf)
X	strcpy( pbuf, "/" );
X  else
X	reverse_path( pbuf, pathname );
X  return( pathname );
X}
X
X/*========================================================================*\
X**				reverse_path()				  **
X\*========================================================================*/
XPRIVATE void
Xreverse_path( src, dest )
Xchar	*src;
Xchar	*dest;
X{
X/*
X *	reverse directory names in path sent by getwd().
X *
X *	contents of source string is destroyed
X */
X  char	*sp;
X
X  *dest = '\0';			/* clear destination string	*/
X  for( sp = src; *sp; sp++ )	/* find end if src string	*/
X	;
X  do {
X	while( *sp != '/' && sp-- != src )
X		;
X	strcat( dest, sp );
X	*sp = '\0';		/* mark new end of src string	*/
X  } while ( sp != src );
X}
END_OF_getwd.c
if test 2553 -ne `wc -c <getwd.c`; then
    echo shar: \"getwd.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f makelibc -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"makelibc\"
else
echo shar: Extracting \"makelibc\" \(2138 characters\)
sed "s/^X//" >makelibc <<'END_OF_makelibc'
X#
X#  makelibc - Make C library
X#
X#				- NOTE -
X#
X#  This shell script will -REMOVE- the old version of libc.a if it
X#  exists in the current directory.  It will also -REPLACE- the old
X#  /usr/lib/libc.a with version just packaged.
X#
Xif (test -f ./libc.a)		# remove old libc.a if it exists
X	then rm ./libc.a
Xfi
Xar av libc.a getwd.s rename.s			# getwd(3) rename(2)
Xar av libc.a dir.s scandir.s			# directory(3), scandir(3)
Xar av libc.a getgrp.s				# process groups
Xar av libc.a termcap.s gtty.s stty.s		# v1.2 update
Xar av libc.a popen.s ctime.s system.s qsort.s	# v1.2 upgrade
Xar av libc.a regexp.s regsub.s
Xar av libc.a getopt.s getgrent.s getpwent.s crypt.s
Xar av libc.a fdopen.s
Xar av libc.a fgets.s fprintf.s fputs.s fread.s freopen.s fclose.s
Xar av libc.a fopen.s fseek.s ftell.s fwrite.s gets.s scanf.s getc.s printdat.s
Xar av libc.a fflush.s setbuf.s sprintf.s doprintf.s putc.s ungetc.s strcmp.s
Xar av libc.a access.s chdir.s chmod.s chown.s chroot.s creat.s dup.s dup2.s
Xar av libc.a exec.s exit.s cleanup.s fork.s isatty.s fstat.s getegid.s getenv.s
Xar av libc.a geteuid.s getgid.s getpass.s close.s getuid.s ioctl.s kill.s
Xar av libc.a link.s lseek.s malloc.s brk.s brk2.s brksize.s mknod.s mktemp.s
Xar av libc.a getpid.s mount.s open.s perror.s pipe.s prints.s read.s setgid.s
Xar av libc.a setuid.s sleep.s alarm.s pause.s signal.s catchsig.s stat.s
Xar av libc.a stime.s strcat.s strcpy.s strlen.s strncat.s strncmp.s strncpy.s
Xar av libc.a ftime.s
Xar av libc.a sync.s time.s times.s umask.s umount.s unlink.s utime.s wait.s
Xar av libc.a stderr.s write.s syslib.s call.s atoi.s message.s sendrec.s
Xar av libc.a printk.s abort.s itoa.s stb.s abs.s atol.s ctype.s index.s bcopy.s
Xar av libc.a getutil.s rand.s rindex.s adi.s and.s cii.s cms.s cmu4.s com.s
Xar av libc.a csa2.s csb2.s cuu.s .dup.s dvi.s dvi4.s dvu.s dvu4.s exg.s fakfp.s
Xar av libc.a gto.s iaar.s ilar.s inn.s ior.s isar.s lar2.s loi.s mli.s mli4.s
Xar av libc.a ngi.s nop.s rck.s rmi.s rmi4.s rmu.s rmu4.s rol.s ror.s sar2.s
Xar av libc.a sbi.s set.s sli.s sri.s sti.s xor.s error.s unknown.s trp.s
Xar av libc.a setjmp.s
X
Xcp libc.a /usr/lib
Xecho
Xecho "Done."
Xecho
X
END_OF_makelibc
if test 2138 -ne `wc -c <makelibc`; then
    echo shar: \"makelibc\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f tst.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"tst.c\"
else
echo shar: Extracting \"tst.c\" \(83 characters\)
sed "s/^X//" >tst.c <<'END_OF_tst.c'
X#include <stdio.h>
X
Xmain()
X{
X	char	*pn = NULL;
X
X	prints( "%s\n\n", getwd( pn ));
X}
END_OF_tst.c
if test 83 -ne `wc -c <tst.c`; then
    echo shar: \"tst.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 1 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0