'Rename' Library Function?
Chip Salzenberg
chip at ateng.com
Fri Sep 22 10:22:06 AEST 1989
According to dave at oldcolo.UUCP (Dave Hughes):
>WHen I try to compile an indexing program written on a Mac (AIX I think)
>system, which has a 'rename' library function, it bombs on that function,
>returning an 'unresolved external' error on _rename.
Here it is, folks -- a Xenix rename() function. It even renames directories
by calling the /usr/lib/mv_dir program. Thanks to Marc Frajola for the
original.
#! /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: rename.c
# Wrapped by chip at ateng on Thu Sep 21 20:21:27 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
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
echo shar: End of shell archive.
exit 0
More information about the Comp.unix.xenix
mailing list