Path: utzoo!attcan!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!ateng!chip From: chip@ateng.com (Chip Salzenberg) Newsgroups: comp.unix.xenix Subject: Re: 'Rename' Library Function? Keywords: rename lib Message-ID: <25197B30.6012@ateng.com> Date: 22 Sep 89 00:22:06 GMT References: <[157]unix@oldcolo.UUCP> Organization: A T Engineering, Tampa, FL Lines: 114 According to dave@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 '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 X#include X#include X#include X#include 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