Path: utzoo!mnetor!uunet!ndsuvax!ncpascal From: ncpascal@ndsuvax.UUCP (Freeman Pascal) Newsgroups: comp.os.minix Subject: Rename(2) for MINIX Message-ID: <578@ndsuvax.UUCP> Date: 17 Dec 87 04:45:38 GMT Reply-To: ncpascal@ndsuvax.UUCP (Freeman Pascal) Organization: North Dakota State University Fargo, ND Lines: 267 Keywords: MINIX rename library Hello, While I was trying to port the BSD version of "arc" to MINIX I discovered I needed rename(2), so I wrote it. My version performs just as the BSD ver- sion does (I only have BSD 4.3 Manuals as references). Although there was no mention of what happens if the "from" file exist but is on another device I decided to make ever effort to maintain the orginal files and check if all conditions are correct before I did the actual rename. The "from" file is not removed if it exists but is on another device. If you find any bugs and such with rename please pass them on. I hope this is of some use to you. Just unshar it and follow the steps 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 INSTALL <<'END_OF_INSTALL' XManifest: X-------- X INSTALL This file X Makefile makefile for tst X makelibc shell script to make C library X rename.c library function (rename(3)) X tst.c test file for rename(3) X XInstallation instructions: X------------------------- X X1. Compile rename.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 rename.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 619 -ne `wc -c Makefile <<'END_OF_Makefile' X# X# Makefile for rename(3) test file X# XCFLAGS = -T. -i X Xtst: tst.c X cc $(CFLAGS) -o tst tst.c X @echo " done." X END_OF_Makefile if test 113 -ne `wc -c 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 rename.c <<'END_OF_rename.c' X/* X * rename() - rename a file X */ X#include X#include X#include X#include X X#define PUBLIC X X#ifndef NULL X#define NULL 0 X#endif X Xextern int errno; X X/*========================================================================*\ X** rename() ** X\*========================================================================*/ XPUBLIC int Xrename( from, to ) Xchar *from, *to; X{ X/* X * Attempts to link 'from' as 'to'. If 'to' exists it is unlinked. X * X * NOTE: 'rename()' will not rename across file systems or X * file types. Also, if an attempt is made to copy across X * file systems both files will be left intact and an X * error will be returned. X */ X struct stat s_to, s_from; X int (*s_int)(), (*s_hup)(), (*s_quit)(); X int ret = 0; X X /* X * get status if 'from' and 'to', if either attemp fails we know X * one of the files doesn't exist. if 'from' doesn't exist then X * return an error condition. if both files exist then test if X * their both on the same file system. if 'to' doesn't exist then X * don't worry about it, it soon will. X */ X if (stat( from, &s_from ) == 0) { X if (stat( to, &s_to ) == 0) { X if ( s_to.st_dev == s_from.st_dev ) { X errno = EXDEV; X return( -1 ); X } X } X /* X * Ignore SIGINT, SIGHUP, and SIGQUIT until X * we'er finished. X */ X s_int = signal( SIGINT, SIG_IGN ); X s_hup = signal( SIGHUP, SIG_IGN ); X s_quit = signal( SIGQUIT, SIG_IGN ); X /* X * does 'to' exist? if so remove it X */ X ret = unlink( to ); X if ((ret = link( from, to )) == 0) X ret = unlink( from ); X /* X * Restore signals X */ X signal( SIGINT, s_int ); X signal( SIGHUP, s_hup ); X signal( SIGQUIT, s_quit ); X return( ret ); X } else X return( -1 ); X} END_OF_rename.c if test 1764 -ne `wc -c tst.c <<'END_OF_tst.c' Xmain( argc, argv ) Xchar *argv[]; X{ X int ret = 0; X X if (argc != 3) { X std_err( "\nUsage: " ); X std_err( argv[0] ); X std_err( " from_file to_file\n" ); X exit( 1 ); X } X if ((ret = rename( argv[1], argv[2] )) != 0) { X printf( "\nreturn = %d\n", ret ); X perror( argv[0] ); X } X exit( 0 ); X} END_OF_tst.c if test 295 -ne `wc -c