Subversion Repositories Kolibri OS

Rev

Rev 5215 | Go to most recent revision | 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 <errno.h>
  18. #include <stdio.h>
  19. #include <alloca.h>
  20.  
  21. int _rename (char *from, char *to)
  22. {
  23.     void* buf;
  24.     int f_from;
  25.     int f_to;
  26.     int size;
  27.  
  28.     f_from = open(from,O_RDONLY);
  29.  
  30.     if (f_from < 0)
  31.     {
  32.         errno = ENOENT;
  33.         return -1;
  34.     };
  35.  
  36.     f_to = open(to,O_CREAT|O_WRONLY|O_EXCL);
  37.  
  38.     if (f_to < 0)
  39.     {
  40.         close(f_from);
  41.         errno = EACCES;
  42.         return -1;
  43.     };
  44.  
  45.     buf = alloca(32768);
  46.  
  47.     do
  48.     {
  49.         size = read(f_from, buf, 32768);
  50.  
  51.         if (size >= 0)
  52.             size = write(f_to, buf, size);
  53.  
  54.     }while (size == 32768);
  55.  
  56.     close(f_to);
  57.     close(f_from);
  58.  
  59.     if (size == -1)
  60.     {
  61.         errno = EACCES;
  62.         return -1;
  63.     };
  64.  
  65.     remove(from);
  66.  
  67.     return (0);
  68. };
  69.