Subversion Repositories Kolibri OS

Rev

Rev 5220 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* _rename.c -- Implementation of the low-level rename() routine
  2.  *
  3.  * Copyright (c) 2004 National Semiconductor Corporation
  4.  *
  5.  * The authors hereby grant permission to use, copy, modify, distribute,
  6.  * and license this software and its documentation for any purpose, provided
  7.  * that existing copyright notices are retained in all copies and that this
  8.  * notice is included verbatim in any distributions. No written agreement,
  9.  * license, or royalty fee is required for any of the authorized uses.
  10.  * Modifications to this software may be copyrighted by their authors
  11.  * and need not follow the licensing terms described here, provided that
  12.  * the new terms are clearly indicated on the first page of each file where
  13.  * they apply.
  14.  */
  15.  
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <alloca.h>
  20. #include <errno.h>
  21.  
  22. int _rename (char *from, char *to)
  23. {
  24.     void* buf;
  25.     int f_from;
  26.     int f_to;
  27.     int size;
  28.  
  29.     f_from = open(from,O_RDONLY);
  30.  
  31.     if (f_from < 0)
  32.     {
  33.         errno = ENOENT;
  34.         return -1;
  35.     };
  36.  
  37.     f_to = open(to,O_CREAT|O_WRONLY|O_EXCL);
  38.  
  39.     if (f_to < 0)
  40.     {
  41.         close(f_from);
  42.         errno = EACCES;
  43.         return -1;
  44.     };
  45.  
  46.     buf = alloca(32768);
  47.  
  48.     do
  49.     {
  50.         size = read(f_from, buf, 32768);
  51.  
  52.         if (size >= 0)
  53.             size = write(f_to, buf, size);
  54.  
  55.     }while (size == 32768);
  56.  
  57.     close(f_to);
  58.     close(f_from);
  59.  
  60.     if (size == -1)
  61.     {
  62.         errno = EACCES;
  63.         return -1;
  64.     };
  65.  
  66.     remove(from);
  67.  
  68.     return (0);
  69. };
  70.