File: rename.c

package info (click to toggle)
libjodycode 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,676 kB
  • sloc: ansic: 2,820; makefile: 372; sh: 160; xml: 37
file content (45 lines) | stat: -rw-r--r-- 993 bytes parent folder | download
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
/* libjodycode: rename() stdio call
 *
 * Copyright (C) 2014-2026 by Jody Bruchon <jody@jodybruchon.com>
 * Released under The MIT License
 */

#include <errno.h>
#include <stdio.h>
#include "likely_unlikely.h"
#include "libjodycode.h"

#ifdef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#endif


/* Rename a file, converting for Windows if necessary */
int jc_rename(const char *oldpath, const char *newpath)
{
	int retval;
#ifdef UNICODE
	JC_WCHAR_T *wideold, *widenew;
#endif

	if (unlikely(oldpath == NULL || newpath == NULL)) {
		jc_errno = EFAULT;
		return -1;
	}

#ifdef UNICODE
	if (unlikely(jc_string_to_wstring(oldpath, &wideold) != 0 || jc_string_to_wstring(newpath, &widenew) != 0)) {
		jc_errno = ENOMEM;
		return -1;
	}
	retval = MoveFileW(wideold, widenew) ? 0 : -1;
	free(wideold); free(widenew);
	if (retval != 0) jc_errno = jc_GetLastError();
#else
	retval = rename(oldpath, newpath);
	if (retval != 0) jc_errno = errno;
#endif
	return retval;
}