extern int access(const char* path, int op) { int r; int oerrno; char buf[PATH_MAX]; oerrno = errno; if ((r = sysaccess(path, op)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0)) { errno = oerrno; r = sysaccess(buf, op); } return r; }
static int wrstr(int fn, char *buf, int len, int echocheck) { int i; char dbuf[BUFSIZ], *dbptr = dbuf; buf[len] = 0; if (echocheck) return (wrchr(fn, buf, len)); if (Debug >= 5) { if (sysaccess(ACCESS_SYSTEMS) == 0) { /* Systems file access ok */ for (i = 0; i < len; i++) { *dbptr = buf[i]; if (*dbptr < 040) { *dbptr++ = '^'; *dbptr = buf[i] | 0100; } dbptr++; } *dbptr = 0; } else (void) strcpy(dbuf, "????????"); CDEBUG(5, "%s", dbuf); } if ((*Write)(fn, buf, len) != len) return (FAIL); return (SUCCESS); }
extern long pathconf(const char* path, int op) { if (sysaccess(path, F_OK)) return -1; return syspathconf(path, op); }
static int wrchr(int fn, char *buf, int len) { int i, saccess; char cin, cout; saccess = (sysaccess(ACCESS_SYSTEMS) == 0); /* protect Systems file */ if (setjmp(Sjbuf)) return (FAIL); (void) signal(SIGALRM, alarmtr); for (i = 0; i < len; i++) { cout = buf[i]; if (saccess) { /*EMPTY*/ CDEBUG(5, "%s", cout < 040 ? "^" : ""); CDEBUG(5, "%c", cout < 040 ? cout | 0100 : cout); } else { /*EMPTY*/ CDEBUG(5, "?%s", ""); } if (((*Write)(fn, &cout, 1)) != 1) return (FAIL); do { (void) alarm(expecttime); if ((*Read)(fn, &cin, 1) != 1) return (FAIL); (void) alarm(0); cin &= 0177; if (saccess) { /*EMPTY*/ CDEBUG(5, "%s", cin < 040 ? "^" : ""); CDEBUG(5, "%c", cin < 040 ? cin | 0100 : cin); } else { /*EMPTY*/ CDEBUG(5, "?%s", ""); } } while (cout != (cin & 0177)); } return (SUCCESS); }
extern int unlink(const char* path) { int r; int drive; int mask; int suffix; int stop; int oerrno; unsigned long base; char buf[PATH_MAX]; char tmp[MAX_PATH]; #define DELETED_DIR_1 7 #define DELETED_DIR_2 16 static char deleted[] = "%c:\\temp\\.deleted\\%08x.%03x"; static int count = 0; #if __CYGWIN__ DWORD fattr = FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE; DWORD share = FILE_SHARE_DELETE; HANDLE hp; struct stat st; char nat[MAX_PATH]; oerrno = errno; if (lstat(path, &st) || !S_ISREG(st.st_mode)) goto try_unlink; cygwin_conv_to_full_win32_path(path, nat); if (!strncasecmp(nat + 1, ":\\temp\\", 7)) goto try_unlink; drive = nat[0]; path = (const char*)nat; for (;;) { hp = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE, NULL); if (hp != INVALID_HANDLE_VALUE) { CloseHandle(hp); errno = oerrno; return 0; } if (GetLastError() != ERROR_FILE_NOT_FOUND) break; if (path == (const char*)buf || !execrate(path, buf, sizeof(buf), 1)) { errno = ENOENT; return -1; } path = (const char*)buf; } #else if (sysaccess(path, 0)) #if _win32_botch_access { if (errno != ENOENT || !execrate(path, buf, sizeof(buf), 1) || sysaccess(buf, 0)) return -1; path = (const char*)buf; } #else return -1; #endif drive = 'C': #endif /* * rename to a `deleted' path just in case the file is open * otherwise directory readers may choke on phantom entries */ base = ((getuid() & 0xffff) << 16) | (time(NiL) & 0xffff); suffix = (getpid() & 0xfff) + count++; snprintf(tmp, sizeof(tmp), deleted, drive, base, suffix); if (!sysrename(path, tmp)) { path = (const char*)tmp; goto try_delete; } if (errno != ENOTDIR && errno != ENOENT) goto try_unlink; tmp[DELETED_DIR_2] = 0; if (sysaccess(tmp, 0)) { mask = umask(0); tmp[DELETED_DIR_1] = 0; if (sysaccess(tmp, 0) && mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO)) { umask(mask); goto try_unlink; } tmp[DELETED_DIR_1] = '\\'; r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO); umask(mask); if (r) goto try_unlink; errno = 0; } tmp[DELETED_DIR_2] = '\\'; if (!errno && !sysrename(path, tmp)) { path = (const char*)tmp; goto try_delete; } #if !__CYGWIN__ if (errno == ENOENT) { #if !_win32_botch_access if (execrate(path, buf, sizeof(buf), 1) && !sysrename(buf, tmp)) path = (const char*)tmp; #endif goto try_unlink; } #endif stop = suffix; do { snprintf(tmp, sizeof(tmp), deleted, drive, base, suffix); if (!sysrename(path, tmp)) { path = (const char*)tmp; goto try_delete; } if (++suffix > 0xfff) suffix = 0; } while (suffix != stop); try_delete: #if __CYGWIN__ hp = CreateFile(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE, NULL); if (hp != INVALID_HANDLE_VALUE) { CloseHandle(hp); errno = oerrno; return 0; } #endif try_unlink: errno = oerrno; return sysunlink(path); }