Skip to content

Commit

Permalink
Utility: chdir() to path containing file.
Browse files Browse the repository at this point in the history
  • Loading branch information
joakim-hove authored and Pål Grønås Drange committed Oct 11, 2017
1 parent c20d0f7 commit 3a700d5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ foreach (name ert_util_alloc_file_components
ert_util_binary_split
ert_util_buffer
ert_util_clamp
ert_util_chdir
ert_util_filename
ert_util_hash_test
ert_util_logh
Expand Down
2 changes: 1 addition & 1 deletion lib/include/ert/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ typedef enum {left_pad = 0,
char * util_alloc_envvar( const char * value );
bool util_is_link(const char * ); // Will always return false on windows
int util_chdir(const char * path);

bool util_chdir_file( const char * filename );

#define UTIL_FWRITE_SCALAR(s,stream) { if (fwrite(&s , sizeof s , 1 , stream) != 1) util_abort("%s: write failed: %s\n",__func__ , strerror(errno)); }

Expand Down
45 changes: 45 additions & 0 deletions lib/util/tests/ert_util_chdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright (C) 2017 Statoil ASA, Norway.
The file 'ert_util_chdir.c' is part of ERT - Ensemble based Reservoir Tool.
ERT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ERT is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
for more details.
*/
#include <stdlib.h>
#include <stdbool.h>

#include <ert/util/test_util.h>
#include <ert/util/util.h>
#include <ert/util/test_work_area.h>


void test_chdir() {
test_work_area_type * work_area = test_work_area_alloc("test-area");
const char * cwd = test_work_area_get_cwd( work_area );

test_assert_false( util_chdir_file( "/file/does/not/exist"));
test_assert_false( util_chdir_file( cwd ));
{
FILE * stream = util_mkdir_fopen("path/FILE","w");
fclose( stream );
}
test_assert_true( util_chdir_file( "path/FILE" ));
test_assert_string_equal( util_alloc_cwd() , util_alloc_filename( cwd, "path", NULL));
test_work_area_free( work_area );
}


int main(int argc , char ** argv) {
test_chdir( );
exit(0);
}
19 changes: 19 additions & 0 deletions lib/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -5500,3 +5500,22 @@ int util_chdir(const char * path) {
}

#endif



bool util_chdir_file( const char * filename ) {
if (!util_is_file( filename ))
return false;

bool chdir_OK = false;
char * path;
char * abs_path;
util_alloc_file_components( filename , &path, NULL , NULL );
abs_path = util_alloc_abs_path( path );
if (util_is_directory( abs_path ))
chdir_OK = (0 == util_chdir( abs_path ));

free( abs_path );
free( path );
return chdir_OK;
}

0 comments on commit 3a700d5

Please sign in to comment.