unsigned _tDosCreate(_TCHAR *pathP, unsigned attr, int *handlep, unsigned disp) { HANDLE handle; SECURITY_ATTRIBUTES sec; /* used only to set inheritance flag */ unsigned rc, oflag; /* Set the inheritance flag in the security attributes. */ sec.nLength = sizeof(sec); sec.lpSecurityDescriptor = NULL; sec.bInheritHandle = TRUE; /* Create the file. */ if ((handle = CreateFile(pathP, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, &sec, disp, attr, NULL)) == (HANDLE)-1) return (__DOSerror()); /* Save the open flags and find free file handle table slot. * Save the NT file handle in the table, return the table index. */ oflag = O_BINARY; if (__isatty_osfhandle((long)handle)) oflag |= O_DEVICE; if ((attr & FILE_ATTRIBUTE_READONLY) == 0) oflag |= _O_WRITABLE; /* fstat() uses this bit */ _lock_all_handles(); if ((*handlep = _get_handle((long)handle, oflag)) == -1) { __IOerror(rc = ERROR_TOO_MANY_OPEN_FILES); CloseHandle(handle); DeleteFile(pathP); } else rc = 0; _unlock_all_handles(); return rc; }
void _process_weak_node(bp_tree * tree, bp_node * p) { int i = _index_of_child(p); bp_node * parent = p->parent; if (i < parent->n - 1) { // has right sibling bp_node * rsib = parent->ptr[i+1]; if (rsib->n == 3) { // right sibling has enough children // move right sibling's left child to p p->key[1] = rsib->key[0]; p->ptr[1] = rsib->ptr[0]; if (p->ptr[1]) { p->ptr[1]->parent = p; } ++p->n; // update p's max key in parent int old_max = p->key[0], new_max = p->key[1]; _update_key(parent, old_max, new_max); // fill blank location in rsib rsib->key[0] = rsib->key[1]; rsib->key[1] = rsib->key[2]; rsib->ptr[0] = rsib->ptr[1]; rsib->ptr[1] = rsib->ptr[2]; --rsib->n; } else { // no enough children in right sibling // move data in p to right sibling for (int j = rsib->n; j > 0; --j) { rsib->key[j] = rsib->key[j-1]; rsib->ptr[j] = rsib->ptr[j-1]; } rsib->key[0] = p->key[0]; rsib->ptr[0] = p->ptr[0]; if (rsib->ptr[0]) { rsib->ptr[0]->parent = rsib; } ++rsib->n; // remove p then update key in parent, and don't forget leaf links if (_is_leaf(p)) { bp_node ** handle = _get_handle(tree, p); *handle = rsib; } delete p; for (int j = i + 1; j < parent->n; ++j) { parent->key[j-1] = parent->key[j]; parent->ptr[j-1] = parent->ptr[j]; } --parent->n; } } else { // p has no right sibling, just left bp_node * Lsib = parent->ptr[i-1]; if (Lsib->n == 3) { // left sibling has enouph children // move left sibling's right child to p p->key[1] = p->key[0]; p->ptr[1] = p->ptr[0]; p->key[0] = Lsib->key[Lsib->n-1]; p->ptr[0] = Lsib->ptr[Lsib->n-1]; if (p->ptr[0]) { p->ptr[0]->parent = p; } ++p->n; // update left sibling int old_max = Lsib->key[Lsib->n-1]; int new_max = Lsib->key[Lsib->n-2]; _update_key(parent, old_max, new_max); --Lsib->n; } else { // no enough children in left sibling // move data in p to left sibling Lsib->key[Lsib->n] = p->key[0]; Lsib->ptr[Lsib->n] = p->ptr[0]; if (p->ptr[0]) { p->ptr[0]->parent = Lsib; } ++Lsib->n; // update key of left sibling in parent int old_key = Lsib->key[Lsib->n-2]; int new_key = Lsib->key[Lsib->n-1]; _update_key(parent, old_key, new_key); // drop p then update key in parent, and don't forget leaf links if (_is_leaf(p)) { Lsib->next = p->next; } delete p; for (int j = i + 1; j < parent->n; ++j) { parent->key[j-1] = parent->key[j]; parent->ptr[j-1] = parent->ptr[j]; } --parent->n; } } }