Debian Codegraph @

theos/_rename.c

raw
1 /*
2 Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
3
4 See the accompanying file LICENSE, version 1999-Oct-05 or later
5 (the contents of which are also included in zip.h) for terms of use.
6 If, for some reason, both of these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #define EXDEV 590
15
16 #define _sys_rename() _sc_140()
17 extern unsigned short _sys_rename(const char _far *oldfn, char *newfn);
18
19 /* rename a file. Report an error on cross disk renames */
20
21 static void _n_(const char* fn, char* bfn)
22 {
23 if (*fn != '.' && *fn != '/')
24 strcpy(bfn, "./");
25 else
26 *bfn = '\0';
27 strcat(bfn, fn);
28 }
29
30 int _rename(const char* old, const char* new)
31 {
32 char* p;
33 char* q;
34 char* r;
35 char olddrv, newdrv;
36 char dir[FILENAME_MAX];
37 short status;
38 char bold[FILENAME_MAX], bnew[FILENAME_MAX];
39
40 p = strrchr(old, ':');
41 q = strrchr(new, ':');
42
43 /* if at least one path includes a disk name, check for equality */
44 if (p != NULL || q != NULL) {
45 /* getcwd return a NULL pointer for /:S */
46 getcwd(dir, FILENAME_MAX);
47 r = strrchr(dir, ':');
48
49 if (p == NULL)
50 p = r;
51 olddrv = p ? p[1] : 'S';
52
53 if (q == NULL)
54 q = r;
55 newdrv = q ? q[1] : 'S';
56
57 /* return an error if uppercased disk names are not the same */
58 if ((old & ~0x20) != (new & ~0x20)) {
59 _errarg = NULL;
60 return errno = _errnum = EXDEV;
61 }
62 }
63 /* prepend ./ if there is no path to force rename to work on files
64 * in the current directory instead of default library
65 */
66 _n_(old, bold);
67 _n_(new, bnew);
68
69 status = _sys_rename(bold, bnew);
70 /* can be :
71 * 0 no error
72 * 19 "old" file not found
73 * 44 "new" file already exist
74 * 45 "new" filename missing
75 * 46 "old" file name missing
76 */
77 if (status) {
78 errno = _errnum = status;
79 _errarg = (status == 44 || status == 45) ? new : old;
80 }
81
82 return status;
83 }