Added VG_(getcwd_alloc)(), which is much easier to use than VG_(getcwd)().

(getcwd() is really a pretty stupid syscall)


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1867
This commit is contained in:
Nicholas Nethercote 2003-09-30 13:51:23 +00:00
parent 7303425ddd
commit 4e147a4eaf
3 changed files with 27 additions and 8 deletions

View File

@ -2022,7 +2022,6 @@ void SK_(print_debug_usage)(void)
void SK_(pre_clo_init)(void)
{
UInt buf_size = 100;
Char* base_dir = NULL;
VG_(details_name) ("Cachegrind");
@ -2043,17 +2042,14 @@ void SK_(pre_clo_init)(void)
VG_(register_compact_helper)((Addr) & log_0I_2D_cache_access);
VG_(register_compact_helper)((Addr) & log_1I_2D_cache_access);
/* getcwd() fails if the buffer isn't big enough -- keep doubling size
until it succeeds. */
while (NULL == base_dir) {
base_dir = VG_(malloc)(buf_size);
if (NULL == VG_(getcwd)(base_dir, buf_size))
buf_size *= 2;
}
/* Get working directory */
sk_assert( VG_(getcwd_alloc)(&base_dir) );
/* Block is big enough for dir name + cachegrind.out.<pid> */
cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
base_dir, VG_(getpid)());
VG_(free)(base_dir);
}
void SK_(post_clo_init)(void)

View File

@ -1212,6 +1212,25 @@ Char* VG_(getcwd) ( Char* buf, Int size )
return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res;
}
/* Alternative version that does allocate the memory. Easier to use. */
Bool VG_(getcwd_alloc) ( Char** out )
{
UInt size = 4;
*out = NULL;
while (True) {
*out = VG_(malloc)(size);
if (NULL == VG_(getcwd)(*out, size)) {
VG_(free)(*out);
if (size > 65535)
return False;
size *= 2;
} else {
return True;
}
}
}
/* ---------------------------------------------------------------------
Misc functions looking for a proper home.

View File

@ -428,6 +428,10 @@ extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
extern Char* VG_(getcwd) ( Char* buf, Int size );
/* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself.
String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d.
Returns False if it fails. Will fail if the pathname is > 65535 bytes. */
extern Bool VG_(getcwd_alloc) ( Char** cwd );
/* ------------------------------------------------------------------ */
/* assert.h */