int CEnvironment::setenv(const wchar_t* aName, const wchar_t* aValue, int aReplace, int& anErrno) { TPtrC16 name((TText16*)aName); TUint freeSlot=iCount; TEnvVar* ep=iVars; for (TUint i=0; i<iCount; ++i,++ep) { if (ep->NotEmpty()) { const TDesC16* existing=ep->Match(name); if (existing) { TInt err=KErrNone; if (aReplace) ep->SetValue(aValue); return MapError(err,anErrno); } } else freeSlot=i; } if (freeSlot==iCount) { // no free slots, time to grow the array ep=(TEnvVar*)User::ReAlloc(iVars,(iCount+1)*sizeof(TEnvVar)); if (ep==0) return KErrNoMemory; iVars=ep; ++iCount; } TRAPD(ret,iVars[freeSlot].ConstructL(name,aValue)); return MapError(ret,anErrno); }
rwlock_error_t RWTryLockWrite(rwlock_t *RWLock) { if (!RWLock) return RW_LOCK_ERROR_INVALID; #if RW_LOCK_USES_POSIX_LOCKS int err; if ((err = pthread_mutex_trylock(&RWLock->monitorLock))) { if (RWLock->destroyed) return RW_LOCK_ERROR_INVALID; return MapError(err); } if (!RWLock->destroyed) { pthread_t Self = pthread_self(); if (RWLock->writerThread != Self) { if ((err = pthread_mutex_trylock(&RWLock->lock))) { pthread_mutex_unlock(&RWLock->monitorLock); if (RWLock->destroyed) return RW_LOCK_ERROR_INVALID; return MapError(err); } RWLock->writerThread = Self; } RWLock->lockCount++; } pthread_mutex_unlock(&RWLock->monitorLock); #endif return RW_LOCK_SUCCESS; //lies when no locks are supported }
STDMETHODIMP CDisk::put_comment(BSTR newVal) { char *buf; dsk_err_t err; buf = get_string(newVal); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_set_comment(m_driver, buf); dsk_free(buf); return MapError(err); }
STDMETHODIMP CDisk::put_option(BSTR name, long newVal) { char *buf; dsk_err_t err; buf = get_string(name); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_set_option(m_driver, buf, newVal); dsk_free(buf); return MapError(err); }
/*---------------------------------------------------------------------- | NPT_Zip::Deflate +---------------------------------------------------------------------*/ NPT_Result NPT_Zip::Deflate(const NPT_DataBuffer& in, NPT_DataBuffer& out, int compression_level, Format format /* = ZLIB */) { // default return state out.SetDataSize(0); // check parameters if (compression_level < NPT_ZIP_COMPRESSION_LEVEL_DEFAULT || compression_level > NPT_ZIP_COMPRESSION_LEVEL_MAX) { return NPT_ERROR_INVALID_PARAMETERS; } // setup the stream z_stream stream; NPT_SetMemory(&stream, 0, sizeof(stream)); stream.next_in = (Bytef*)in.GetData(); stream.avail_in = (uInt)in.GetDataSize(); // setup the memory functions stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; // initialize the compressor int err = deflateInit2(&stream, compression_level, Z_DEFLATED, 15 + (format == GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY); if (err != Z_OK) return MapError(err); // reserve an output buffer known to be large enough out.Reserve(deflateBound(&stream, stream.avail_in) + (format==GZIP?10:0)); stream.next_out = out.UseData(); stream.avail_out = out.GetBufferSize(); // decompress err = deflate(&stream, Z_FINISH); if (err != Z_STREAM_END) { deflateEnd(&stream); return MapError(err); } // update the output size out.SetDataSize(stream.total_out); // cleanup err = deflateEnd(&stream); return MapError(err); }
STDMETHODIMP CDisk::get_option(BSTR name, long *pVal) { char *buf; dsk_err_t err; int value; buf = get_string(name); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_get_option(m_driver, buf, &value); dsk_free(buf); *pVal = value; return MapError(err); }
/*---------------------------------------------------------------------- | NPT_Zip::Inflate +---------------------------------------------------------------------*/ NPT_Result NPT_Zip::Inflate(const NPT_DataBuffer& in, NPT_DataBuffer& out, bool raw) { // assume an output buffer twice the size of the input plus a bit NPT_CHECK_WARNING(out.Reserve(32+2*in.GetDataSize())); // setup the stream z_stream stream; stream.next_in = (Bytef*)in.GetData(); stream.avail_in = (uInt)in.GetDataSize(); stream.next_out = out.UseData(); stream.avail_out = (uInt)out.GetBufferSize(); // setup the memory functions stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; // initialize the decompressor int err = inflateInit2(&stream, raw?-15:15+32); // 15 = default window bits, +32 = automatic header if (err != Z_OK) return MapError(err); // decompress until the end do { err = inflate(&stream, Z_SYNC_FLUSH); if (err == Z_STREAM_END || err == Z_OK || err == Z_BUF_ERROR) { out.SetDataSize((NPT_Size)stream.total_out); if ((err == Z_OK && stream.avail_out == 0) || err == Z_BUF_ERROR) { // grow the output buffer out.Reserve(out.GetBufferSize()*2); stream.next_out = out.UseData()+stream.total_out; stream.avail_out = out.GetBufferSize()-(NPT_Size)stream.total_out; } } } while (err == Z_OK); // check for errors if (err != Z_STREAM_END) { inflateEnd(&stream); return MapError(err); } // cleanup err = inflateEnd(&stream); return MapError(err); }
/** Get the internet name of this host. Actually this will always return a null string with TCPIP 030 and onwards because the "name" of a mobile host isn't really very meaningful - in practice the IP address is chosen dynamically once you start doing real networking, at which time the ISP can resolve the IP address into a name of some sort if you really want. @return @param name @param size */ EXPORT_C int gethostname (char *name, size_t size) { int* perrno=__errno(); RSocketServ ss; TInt err=ss.Connect(1); if (err==KErrNone) { RHostResolver r; err=r.Open(ss, AF_INET, KProtocolInetUdp); if (err==KErrNone) { TBuf<128> hostname; err=r.GetHostName(hostname); if (err==KErrNone) { if (size>(size_t)hostname.Length()) { TPtr8 retval((TText8*)name,size); retval.Copy(hostname); retval.PtrZ(); } else err=ENAMETOOLONG; } r.Close(); } ss.Close(); } return MapError(err,*perrno); }
rwlock_error_t RWLockDestroy(rwlock_t *RWLock) { if (!RWLock) return RW_LOCK_ERROR_INVALID; #if RW_LOCK_USES_POSIX_LOCKS //don't care about most fails int err; if ((err = pthread_mutex_lock(&RWLock->monitorLock))) { if (RWLock->destroyed) return RW_LOCK_ERROR_INVALID; return MapError(err); } RWLock->destroyed = TRUE; //wait and let any locks get finished up pthread_mutex_unlock(&RWLock->monitorLock); pthread_mutex_lock(&RWLock->lock); pthread_mutex_lock(&RWLock->monitorLock); pthread_mutex_unlock(&RWLock->lock); pthread_mutex_destroy(&RWLock->lock); pthread_mutex_unlock(&RWLock->monitorLock); pthread_mutex_destroy(&RWLock->monitorLock); #endif return RW_LOCK_SUCCESS; //lies when no locks are supported }
rwlock_error_t RWUnlockRead(rwlock_t *RWLock) { if (!RWLock) return RW_LOCK_ERROR_INVALID; #if RW_LOCK_USES_POSIX_LOCKS int err; if ((err = pthread_mutex_lock(&RWLock->monitorLock))) { if (RWLock->destroyed) return RW_LOCK_ERROR_INVALID; return MapError(err); } rwlock_error_t Error = RW_LOCK_SUCCESS; //if (!RWLock->destroyed) { if ((!RWLock->writerThread) || (RWLock->writerThread == pthread_self())) { if (!--RWLock->lockCount) { RWLock->writerThread = NULL; pthread_mutex_unlock(&RWLock->lock); } } else Error = RW_LOCK_ERROR_INVALID; } //else Error = RW_LOCK_ERROR_INVALID; pthread_mutex_unlock(&RWLock->monitorLock); return Error; #endif return RW_LOCK_SUCCESS; //lies when no locks are supported }
STDMETHODIMP CDisk::get_retries(short *pVal) { unsigned r; dsk_err_t err = dsk_get_retry(m_driver, &r); *pVal = r; return MapError(err); }
STDMETHODIMP CDisk::ltrkid(IGeometry *g, long track, short *count, VARIANT *buffer) { DSK_GEOMETRY geom; DSK_FORMAT *fmt; unsigned char *buf; dsk_err_t err; dsk_psect_t n, acount = 0; HRESULT hr; g_to_dg(g, &geom); err = dsk_ltrackids(m_driver, &geom, track, &acount, &fmt); if (acount) { if (count) *count = acount; buf = new unsigned char[4 * acount]; for (n = 0; n < acount; n++) { buf[n*4 ] = fmt[n].fmt_cylinder; buf[n*4+1] = fmt[n].fmt_head; buf[n*4+2] = fmt[n].fmt_sector; buf[n*4+3] = dsk_get_psh(fmt[n].fmt_secsize); } hr = PutBuffer(buf, buffer, 4 * acount, err); delete buf; return hr; } return MapError(err); }
STDMETHODIMP CGeometry::pt2lt(long cyl, short head, long *lsect) { dsk_ltrack_t lt; HRESULT hr = MapError(dg_pt2lt(&m_g, cyl, head, <)); *lsect = lt; return hr; }
STDMETHODIMP CGeometry::ps2ls(long cyl, short head, short sector, long *lsect) { dsk_lsect_t ls; HRESULT hr = MapError(dg_ps2ls(&m_g, cyl, head, sector, &ls)); *lsect = ls; return hr; }
/*---------------------------------------------------------------------- | NPT_File::ListDir +---------------------------------------------------------------------*/ NPT_Result NPT_File::ListDir(const char* path, NPT_List<NPT_String>& entries, NPT_Ordinal start /* = 0 */, NPT_Cardinal max /* = 0 */) { NPT_WIN32_USE_CHAR_CONVERSION; // default return value entries.Clear(); // check the arguments if (path == NULL || path[0] == '\0') return NPT_ERROR_INVALID_PARAMETERS; // construct a path name with a \* wildcard at the end NPT_String path_pattern = path; if (path_pattern.EndsWith("\\") || path_pattern.EndsWith("/")) { path_pattern += "*"; } else { path_pattern += "\\*"; } // list the entries WIN32_FIND_DATAW find_data; HANDLE find_handle = FindFirstFileW(NPT_WIN32_A2W(path_pattern.GetChars()), &find_data); if (find_handle == INVALID_HANDLE_VALUE) return MapError(GetLastError()); NPT_Cardinal count = 0; do { if (NPT_File_ProcessFindData(&find_data)) { // continue if we still have entries to skip if (start > 0) { --start; continue; } entries.Add(NPT_WIN32_W2A(find_data.cFileName)); // stop when we have reached the maximum requested if (max && ++count == max) return NPT_SUCCESS; } } while (FindNextFileW(find_handle, &find_data)); DWORD last_error = GetLastError(); FindClose(find_handle); if (last_error != ERROR_NO_MORE_FILES) return MapError(last_error); return NPT_SUCCESS; }
STDMETHODIMP CDisk::get_comment(BSTR *pVal) { char *cmt = NULL; dsk_err_t err = dsk_get_comment(m_driver, &cmt); if (cmt) *pVal = ret_string(cmt); return MapError(err); }
STDMETHODIMP CGeometry::lt2pt(long lsect, long *cyl, short *head) { HRESULT hr; dsk_pcyl_t c; dsk_phead_t h; hr = MapError(dg_lt2pt(&m_g, lsect, &c, &h)); *cyl = c; *head = h; return hr; }
/*---------------------------------------------------------------------- | NPT_File::RemoveDir +---------------------------------------------------------------------*/ NPT_Result NPT_File::RemoveDir(const char* path) { NPT_WIN32_USE_CHAR_CONVERSION; BOOL result = RemoveDirectoryW(NPT_WIN32_A2W(path)); if (result == 0) { return MapError(GetLastError()); } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_File::Rename +---------------------------------------------------------------------*/ NPT_Result NPT_File::Rename(const char* from_path, const char* to_path) { NPT_WIN32_USE_CHAR_CONVERSION; BOOL result = MoveFileW(NPT_WIN32_A2W(from_path), NPT_WIN32_A2W(to_path)); if (result == 0) { return MapError(GetLastError()); } return NPT_SUCCESS; }
STDMETHODIMP CDisk::alform(IGeometry *g, long track, short filler) { HRESULT hr; DSK_GEOMETRY geom; g_to_dg(g, &geom); hr = MapError(dsk_alform(m_driver, &geom, track, (unsigned char)filler)); dg_to_g(&geom, g); return hr; }
HRESULT CDisk::GetFormat(VARIANT *buf, DSK_FORMAT **bufout, unsigned length) { long lBound, uBound; unsigned char HUGEP* FAR pData; SAFEARRAY *psa; HRESULT hr; unsigned n; DSK_FORMAT *fmt; if (V_VT(buf) != (VT_ARRAY | VT_UI1)) { return Error("Passed parameter must be an array of unsigned characters", IID_IDisk, E_INVALIDARG); } psa = V_ARRAY(buf); // Check dimensions of the array. if (SafeArrayGetDim(psa) != 1) { return Error("Passed parameter must be a 1-dimensional array", IID_IDisk, E_INVALIDARG); } // Get array bounds. hr = SafeArrayGetLBound(psa, 1, &lBound); if (FAILED(hr)) { return Error("Could not get array bounds", IID_IDisk, hr); } hr = SafeArrayGetUBound(psa, 1, &uBound); if (FAILED(hr)) { return Error("Could not get array bounds", IID_IDisk, hr); } if (uBound + 1 - lBound < (long)length*4) { return Error("Passed array is too short", IID_IDisk, E_INVALIDARG); } // Get a pointer to the elements of the array. hr = SafeArrayAccessData(psa, (void HUGEP * FAR *)&pData); if (FAILED(hr)) { return Error("Could not access array data", IID_IDisk, E_INVALIDARG); } *bufout = (DSK_FORMAT *)dsk_malloc(length * sizeof(DSK_FORMAT)); if (!bufout) return MapError(DSK_ERR_NOMEM); fmt = bufout[0]; for (n = 0; n < length; n++) { fmt->fmt_cylinder = *pData++; fmt->fmt_head = *pData++; fmt->fmt_sector = *pData++; fmt->fmt_secsize = 128 << (*pData++); ++fmt; } SafeArrayUnaccessData(psa); return S_OK; }
//Makes small adjustments to the system time, //advancing it by the time specified by the timeval delta EXPORT_C int adjtime(const struct timeval *delta, struct timeval *olddelta) { //We can make only positive adjutments to the available clock //The synchronization to the global time server is provided to us //by the User::SetHomeTime api itself int retval = -1; TTime t; TInt err; TInt64 microtime; long secs = 0; suseconds_t usec = 0; //We expect the user of the library to give us a valid pointer if(delta == NULL) { errno = EFAULT; return retval; } if ((delta->tv_usec) > 1000000) { errno = ERANGE; return retval; } //Check for negative of seconds and make it positive if((delta->tv_sec) < 0) secs = -1*(delta->tv_sec); else secs = delta->tv_sec; //Check for negative of microseconds and make it positive if((delta->tv_usec) < 0) usec = -1*(delta->tv_usec); else usec = delta->tv_usec; t.HomeTime(); t+=(TTimeIntervalSeconds)secs; microtime = usec; t+=(TTimeIntervalMicroSeconds)microtime; err = User::SetHomeTime(t); if (!err) { retval = 0; //olddelta is always zero in case of success olddelta->tv_sec = 0; olddelta->tv_usec = 0; } else { MapError(err,errno); retval = -1; } return retval; }
STDMETHODIMP CLibrary::create(BSTR f, BSTR d, BSTR c, IDisk **ppDisk) { DSK_PDRIVER pd; dsk_err_t err; char *filename; char *driver; char *compress; if (!f || !ppDisk) { return MapError(DSK_ERR_BADPTR); } filename = get_string(f); driver = get_string(d); compress = get_string(c); if (driver[0] == 0) { dsk_free(driver); driver = NULL; } if (compress[0] == 0) { dsk_free(compress); compress = NULL; } err = dsk_creat(&pd, filename, driver, compress); if (filename) dsk_free(filename); if (driver) dsk_free(driver); if (compress) dsk_free(compress); if (err) { return MapError(err); } HRESULT hr = CDisk::CreateInstance(ppDisk); if (FAILED(hr)) return hr; ((CDisk *)ppDisk[0])->m_driver = pd; return hr; }
STDMETHODIMP CDisk::drive_status(IGeometry *g, short head, short *status) { dsk_err_t err; unsigned char st; DSK_GEOMETRY geom; g_to_dg(g, &geom); err = dsk_drive_status(m_driver, &geom, head, &st); *status = st; return MapError(err); }
STDMETHODIMP CGeometry::stdformat(int formatid, BSTR *name, BSTR *description) { dsk_cchar_t fname, fdesc; HRESULT hr = MapError(dg_stdformat(&m_g, (dsk_format_t)formatid, &fname, &fdesc)); if (FAILED(hr)) return hr; *name = CComBSTR(fname).Detach(); *description = CComBSTR(fdesc).Detach(); return S_OK; }
STDMETHODIMP CGeometry::ls2ps(long lsect, long *cyl, short *head, short *sector) { HRESULT hr; dsk_pcyl_t c; dsk_phead_t h; dsk_psect_t s; hr = MapError(dg_ls2ps(&m_g, lsect, &c, &h, &s)); *cyl = c; *head = h; *sector = s; return hr; }
rwlock_error_t RWLockInit(rwlock_t *RWLock) { if (!RWLock) return RW_LOCK_ERROR_INVALID; RWLock->destroyed = FALSE; #if RW_LOCK_USES_POSIX_LOCKS int err; if ((err = pthread_mutex_init(&RWLock->lock, NULL))) return MapError(err); if ((err = pthread_mutex_init(&RWLock->monitorLock, NULL))) { pthread_mutex_destroy(&RWLock->lock); return MapError(err); } RWLock->lockCount = 0; RWLock->writerThread = NULL; #endif return RW_LOCK_SUCCESS; //lies when no locks are supported }
STDMETHODIMP CDisk::pwrite(IGeometry *g, VARIANT *buf, long cylinder, short head, short sector) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, geom.dg_secsize); if (FAILED(hr)) return hr; hr = MapError(dsk_pwrite(m_driver, &geom, pData, cylinder, head, sector)); dsk_free(pData); return hr; }
STDMETHODIMP CDisk::xcheck(IGeometry *g, VARIANT *buf, long cylinder, short head, long cyl_expected, short head_expected, /*[in]*/ short sector,long sector_len) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, sector_len); if (FAILED(hr)) return hr; hr = MapError(dsk_xcheck(m_driver, &geom, pData, cylinder, head, cyl_expected, head_expected, sector, sector_len)); dsk_free(pData); return hr; }
STDMETHODIMP CDisk::lcheck(IGeometry *g, VARIANT *buf, long lsect) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, geom.dg_secsize); if (FAILED(hr)) return hr; hr = MapError(dsk_lcheck(m_driver, &geom, pData, lsect)); dsk_free(pData); return hr; }