_rename.c 2.1 KB

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