// This routine returns a malloc'd string through its path_out pointer. // The caller is responsible for freeing that memory. err_t sys_getcwd(const char** path_out) { int sz = 128; char* buf; char* got; err_t err = 0; buf = (char*) qio_malloc(sz); if( !buf ) return ENOMEM; while( 1 ) { got = getcwd(buf, sz); if( got != NULL ) break; else if( errno == ERANGE ) { // keep looping but with bigger buffer. sz = 2*sz; got = (char*) qio_realloc(buf, sz); if( ! got ) { qio_free(buf); return ENOMEM; } } else { // Other error, stop. err = errno; } } *path_out = buf; return err; }
// This routine returns a malloc'd string (through the working_dir pointer) // that must be deallocated by the caller. qioerr chpl_fs_cwd(const char** working_dir) { qioerr err = 0; size_t bufsize = MAXPATHLEN*sizeof(char); char* bufptr; char* pathbuf = (char *)qio_malloc(bufsize); bufptr = getcwd(pathbuf, bufsize); if (bufptr == NULL) err = qio_mkerror_errno(); else *working_dir = pathbuf; return err; }
static char* get_locale_name(char *loc) { char* ret = NULL; const char *keys = "."; // We lop off the .us.cray... int len = strcspn (loc, keys); ret = (char*) qio_malloc(len+1); if (ret) { qio_memcpy(ret, loc, len); ret[len] = '\0'; } return ret; }
qioerr chpl_fs_realpath(const char* path, const char **shortened) { qioerr err = 0; size_t bufsize = MAXPATHLEN*sizeof(char); char* bufptr; char* pathbuf = (char *)qio_malloc(bufsize); bufptr = realpath(path, pathbuf); if (bufptr == NULL) { // If an error occurred, bufptr will be NULL. Otherwise, it will // point to pathbuf anyways err = qio_mkerror_errno(); qio_free(pathbuf); } else { *shortened = pathbuf; } return err; }
err_t qbuffer_create(qbuffer_t** out) { qbuffer_t* ret = NULL; err_t err; ret = (qbuffer_t*) qio_malloc(sizeof(qbuffer_t)); if( ! ret ) return ENOMEM; err = qbuffer_init(ret); if( err ) { qio_free(ret); return err; } *out = ret; return 0; }
qioerr qbuffer_create(qbuffer_t** out) { qbuffer_t* ret = NULL; qioerr err; ret = (qbuffer_t*) qio_malloc(sizeof(qbuffer_t)); if( ! ret ) { *out = NULL; return QIO_ENOMEM; } err = qbuffer_init(ret); if( err ) { qio_free(ret); *out = NULL; return err; } *out = ret; return 0; }
qioerr hdfs_getcwd(void* file, const char** path_out, void* fs) { int sz = 128; char* buf = (char*) qio_malloc(sz); qioerr err = 0; if ( !buf ) QIO_GET_CONSTANT_ERROR(err, ENOMEM, "Out of memory in hdfs_getcwd"); // hdfsGetWorkingDirectory will return 0 if buf[] is not large enough // If this happens, grow the buffer and try again while (err == 0 && hdfsGetWorkingDirectory(to_hdfs_fs(fs)->hfs, buf, sz) == 0) { if (errno == ERANGE) { int newSz = 2 * sz; char* newBuf = (char*) qio_realloc(buf, newSz); if (newBuf == 0) { QIO_GET_CONSTANT_ERROR(err, ENOMEM, "Out of memory in hdfs_getcwd"); } else { sz = newSz; buf = newBuf; } } else { // Other error, stop. QIO_GET_CONSTANT_ERROR(err, EREMOTEIO, "Unable to get path to file in HDFS"); } } if (err != 0) { qio_free(buf); buf = 0; } *path_out = buf; return err; }
// This routine returns a malloc'd string through its path_out pointer. // The caller is responsible for freeing that memory. err_t sys_getcwd(const char** path_out) { int sz = 128; char* buf = (char*) qio_malloc(sz); err_t err = (buf == 0) ? ENOMEM : 0; // getcwd() returns 0 if the provided buffer is too small // If this happens, grow the buffer and try again while (err == 0 && getcwd(buf, sz) == 0) { if (errno == ERANGE) { int newSz = 2 * sz; char* newBuf = (char*) qio_realloc(buf, newSz); if (newBuf == 0) { qio_free(buf); err = ENOMEM; } else { sz = newSz; buf = newBuf; } } else { err = errno; } } if (err != 0) { qio_free(buf); sz = 0; buf = 0; } *path_out = buf; return err; }
qioerr hdfs_getpath(void* file, const char** string_out, void* fs) { // Speculatively allocate 128 bytes for the string int sz = 128; int left = 0; char* buf; char* got; qioerr err = 0; const char* host = to_hdfs_fs(fs)->fs_name; int port = to_hdfs_fs(fs)->fs_port; const char* path = to_hdfs_file(file)->pathnm; buf = (char*) qio_malloc(sz); if( !buf ) QIO_GET_CONSTANT_ERROR(err, ENOMEM, "Out of memory in hdfs_getpath"); while (1) { left = snprintf(buf, sz, "hdfs://%s:%d/%s", host, port, path); if (left > -1 && left < sz) { break; } else { // keep looping but with bigger buffer. // We know the size that we need now if n > -1 sz = left > -1 ? left + 1 : 2*sz; got = (char*) qio_realloc(buf, sz); if( ! got ) { qio_free(buf); QIO_GET_CONSTANT_ERROR(err, ENOMEM, "Out of memory in hdfs_getpath"); } } } *string_out = buf; return err; }
// allocates and returns an error string in *string_out // which must be freed. static err_t sys_strerror_internal(err_t error, char** string_out, size_t extra_space) { // normal errors are in normal places. // EAI_AGAIN... etc are at 10000 + num. int buf_sz = 248 + extra_space; char* buf = NULL; char* newbuf; const char* errmsg; int got; err_t err_out; err_out = 0; if( error == 0 || (EXTEND_ERROR_OFFSET <= error && error < EXTEND_ERROR_OFFSET+EXTEND_ERROR_NUM) ) { if( error == 0 ) errmsg = error_string_no_error; else errmsg = extended_errors[error - EXTEND_ERROR_OFFSET]; buf_sz = strlen(errmsg) + 1; buf = (char*) qio_malloc(buf_sz + extra_space); if( ! buf ) return ENOMEM; strcpy(buf, errmsg); *string_out = buf; return 0; } while( 1 ) { newbuf = (char*) qio_realloc(buf, buf_sz + extra_space); if( ! newbuf ) { qio_free(buf); return ENOMEM; } buf = newbuf; got = sys_xsi_strerror_r(error, buf, buf_sz); if( got == 0 ) break; if( got == -1 && errno != ERANGE ) { err_out = errno; break; } buf_sz *= 2; // try again with a bigger buffer. } // maybe it's a EAI/gai error, which we add GAI_ERROR_OFFSET to. #ifdef HAS_GETADDRINFO if( got == -1 && err_out == EINVAL ) { const char* gai_str; int len; gai_str = gai_strerror(error - GAI_ERROR_OFFSET); if( ! gai_str ) { err_out = errno; } else { len = strlen(gai_str); if( len + 1 > buf_sz ) { newbuf = (char*) qio_realloc(buf, len + 1 + extra_space); if( ! newbuf ) { qio_free(buf); return ENOMEM; } buf = newbuf; } strcpy(buf, gai_str); } } #endif *string_out = buf; return err_out; }