[comp.unix.xenix] 'Rename' Library Function?

dave@oldcolo.UUCP (Dave Hughes) (09/14/89)

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.

It is used - and needed - in the following type of use:

    char oldname[32], finalname[65];                                   
    sprintf (oldname, "x%dk0", generation_number);                     
    sprintf (finalname, "%s.k", doc_filename);                         
    if (rename (oldname, finalname))                                   
        printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
                finalname);                                            

I cannot find another 386 Xenix compiler library function which will
work in its place. Any suggestions?

dave@oldcolo.uuup

jbayer@ispi.UUCP (Jonathan Bayer) (09/19/89)

dave@oldcolo.UUCP (Dave Hughes) writes:

>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.

>It is used - and needed - in the following type of use:

>    char oldname[32], finalname[65];                                   
>    sprintf (oldname, "x%dk0", generation_number);                     
>    sprintf (finalname, "%s.k", doc_filename);                         
>    if (rename (oldname, finalname))                                   
>        printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
>                finalname);                                            

>I cannot find another 386 Xenix compiler library function which will
>work in its place. Any suggestions?



Use the following:

	if ( (z = link (oldname, newname)) == 0) {
		z = unlink (oldname);

	if (z) printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
                 finalname);
	printf ("error: %d\n",errno);


JB

-- 
Jonathan Bayer		Intelligent Software Products, Inc.
(201) 245-5922		500 Oakwood Ave.
jbayer@ispi.COM		Roselle Park, NJ   07204    

cpcahil@virtech.UUCP (Conor P. Cahill) (09/19/89)

In article <[157]unix@oldcolo.UUCP>, dave@oldcolo.UUCP (Dave Hughes) writes:
> WHen I try to compile an indexing program written on a Mac (AIX I think)
							      ^^^ AUX?
> system, which has a 'rename' library function, it bombs on that function,
> returning an 'unresolved external' error on _rename.

The problem is due to the fact that AUX is *probably* BSD based and has the
rename(2) system call.  This system call is not available in plain jane 
system V systems.  Although xenix is not plain jane system V, it is not 
based upon BSD and does not include the rename system call.

> It is used - and needed - in the following type of use:
> 
>     char oldname[32], finalname[65];                                   
>     sprintf (oldname, "x%dk0", generation_number);                     
>     sprintf (finalname, "%s.k", doc_filename);                         
>     if (rename (oldname, finalname))                                   
>         printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
>                 finalname);                                            

You neet to write a rename function that should do the following:

	rename(src,tgt)
		char		* src;
		char		* tgt;
	{
		if( (link(src,tgt) == -1) ||  (unlink(src) == -1) )
			return(-1);
		return(0);
	}


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

maart@cs.vu.nl (Maarten Litmaath) (09/20/89)

jbayer@ispi.UUCP (Jonathan Bayer) writes:
\...	if ( (z = link (oldname, newname)) == 0) {
\		z = unlink (oldname);
	}
\
\	if (z) printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
\                 finalname);
\	printf ("error: %d\n",errno);

...leading to `interesting' error messages like:

	error: 25	/* Not a typewriter */

Use something like the following instead:

	if (z) {
		int	saved_errno = errno;

		printf(...);
		printf("error: %d\n", saved_errno);
	}
-- 
   creat(2) shouldn't have been create(2): |Maarten Litmaath @ VU Amsterdam:
      it shouldn't have existed at all.    |maart@cs.vu.nl, mcvax!botter!maart

karl@ddsw1.MCS.COM (Karl Denninger) (09/20/89)

In article <[157]unix@oldcolo.UUCP> dave@oldcolo.UUCP (Dave Hughes) writes:
>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.
>
>It is used - and needed - in the following type of use:
>
>    char oldname[32], finalname[65];                                   
>    sprintf (oldname, "x%dk0", generation_number);                     
>    sprintf (finalname, "%s.k", doc_filename);                         
>    if (rename (oldname, finalname))                                   
>        printf ("Sorry -- error in renaming file %s to %s!\n", oldname,
>                finalname);                                            
>
>I cannot find another 386 Xenix compiler library function which will
>work in its place. Any suggestions?

Sure Dave!

Try this replacement:

int	rename(oname, fname)		/* Returns zero if ok, NZ otherwise */
char	*oname;
char	*nname;
{
	if (link(oname, nname)) {	/* Try to make a link */
		perror("failed");	/* Something is wrong, complain */
		return(1);		/* Failed to make a link */
	}
	return(unlink(oname));		/* Remove old name / return status */
}

Include that somewhere and it should replace your missing function.  Note
that this will not work if the new name (fname) is on a different file system.

(Ps: I noted that your message came from AKCS; glad to see you have the
     linkage working properly! :-))

--
Karl Denninger (karl@ddsw1.MCS.COM, <well-connected>!ddsw1!karl)
Public Access Data Line: [+1 312 566-8911], Voice: [+1 312 566-8910]
Macro Computer Solutions, Inc.		"Quality Solutions at a Fair Price"

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

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 <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@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

dave@oldcolo.UUCP (Dave Hughes) (09/29/89)

Thanks all, for the many solutions. 

yes Karl, its coming from linked AKCS - as a matter of fact I am trying
to see if I can harness this utility to your conferencing files, so
that using a 'find' command it will return either the sentence, paragraph
or response... Now of course if you would just incorporate that kind of
feature in your next release...:-)