File: rename.c

package info (click to toggle)
zlibc 0.9k-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 624 kB
  • ctags: 461
  • sloc: ansic: 3,073; sh: 3,023; csh: 128; makefile: 58; sed: 4
file content (50 lines) | stat: -rw-r--r-- 1,177 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * rename.c
 *
 * Copyright (C) 1993 Alain Knaff
 */
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE

#include "sysincludes.h"
#include "stat.h"

int rename(__const char *file_name1, __const char *file_name2)
{
  int st;
  char newname1[MAXPATHLEN + MAXEXTLEN + 1];
  char newname2[MAXPATHLEN + MAXEXTLEN + 1];

  _zlibc_init();
  st = zlib_real_rename(file_name1, file_name2);

  if ( st >= 0 || errno != ENOENT )
    return st;

  zlib_initialise();
  if ( zlib_mode & CM_DISAB )
    return st;
  if ( (zlib_getfiletype(file_name1,-1) & PM_READ_MASK) == PM_LEAVE_COMPR)
    return st;
  
  strncpy(newname1,file_name1,1024);
  strcat(newname1,zlib_ext);
  
  strncpy(newname2,file_name2,1024);
  strcat(newname2,zlib_ext);
  
  errno = 0;
  st = zlib_real_rename(newname1, newname2);
  if ( st >= 0 || errno != EINVAL )
    return st;

  /* we have EINVAL. Does this come from the source or the target ? */
  st = ___zlibc_testexistence(newname1);
  if ( st >= 0 ){
    /* old name exists. EINVAL is caused by target. keep it */
    errno = EINVAL ;
    return -1;
  } else
    return -1;  
  /* else the old name does not exist. return ENOENT (is already there) */    
}