home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / lhaxenix.zoo / lharc.xenix / rename.c < prev   
C/C++ Source or Header  |  1990-04-02  |  2KB  |  83 lines

  1. /*
  2.  * $Author: chip $ $Date: 89/06/29 13:02:31 $
  3.  * $Header: rename.c,v 1.1 89/06/29 13:02:31 chip Exp $
  4.  * $Revision: 1.1 $
  5.  */
  6.  
  7. /*
  8.  * Rename system call -- Replacement for Berzerkeley 4.2 rename system
  9.  * call that is missing in Xenix.
  10.  *
  11.  * By Marc Frajola and Chris Paris.
  12.  * Directory hack by Chip Salzenberg.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <signal.h>
  19. #include <errno.h>
  20.  
  21. rename(src,dest)
  22.     char *src;            /* Source file to rename */
  23.     char *dest;            /* Name for renamed file */
  24. {
  25.     int status;            /* Status returned from link system call */
  26.     struct stat stbuf;        /* Buffer for statting destination file */
  27.  
  28.     /* Find out what the destination is: */
  29.     status = stat(dest,&stbuf);
  30.     if (status >= 0) {
  31.     /* See if the file is a regular file; if not, return error: */
  32.     if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
  33.         return(-1);
  34.     }
  35.     }
  36.  
  37.     /* Unlink destination since it is a file: */
  38.     unlink(dest);
  39.  
  40.     /* Find out what the source is: */
  41.     status = stat(src,&stbuf);
  42.     if (status < 0)
  43.     return -1;
  44.     if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
  45.     {
  46.     /* Directory hack for SCO Xenix */
  47.  
  48.     static char mvdir[] = "/usr/lib/mv_dir";
  49.     void (*oldsigcld)();
  50.     int pid;
  51.  
  52.     oldsigcld = signal(SIGCLD, SIG_DFL);
  53.     while ((pid = fork()) == -1)
  54.     {
  55.         if (errno != EAGAIN)
  56.         return -1;
  57.         sleep(5);
  58.     }
  59.     if (pid == 0)
  60.     {
  61.         execl(mvdir, mvdir, src, dest, (char *) 0);
  62.         perror(mvdir);
  63.         exit(1);
  64.     }
  65.     if (wait(&status) != pid)
  66.     {
  67.         fprintf(stderr, "rename: wait failure\n");
  68.         status = -1;
  69.     }
  70.     (void) signal(SIGCLD, oldsigcld);
  71.     }
  72.     else
  73.     {
  74.     /* Link source to destination file: */
  75.     status = link(src,dest);
  76.     if (status != 0) {
  77.         return(-1);
  78.     }
  79.     status = unlink(src);
  80.     }
  81.     return((status == 0) ? 0 : (-1));
  82. }
  83.