- if (is_dir (output_name_global))
- {
- char cwd[PATH_MAX];
- getcwd (cwd, PATH_MAX);
-
- if (output_name_global != cwd)
- {
- global_path.prepend (cwd);
- message (_f ("Changing working directory to `%s'",
- output_name_global.to_str0 ()));
- chdir (output_name_global.to_str0 ());
-
- }
- output_name_global = "";
- }
- else
- out_file_name = File_name (output_name_global);
+ string dir;
+ if (is_dir (output_name))
+ {
+ dir = output_name;
+ output_name = "";
+ }
+ else
+ {
+ File_name out (output_name);
+ dir = out.dir_part ();
+ out_file_name = out.file_part ();
+ }
+
+ if (dir != "" && dir != "." && dir != get_working_directory ())
+ {
+ global_path.prepend (get_working_directory ());
+ message (_f ("Changing working directory to: `%s'", dir));
+ // If we can't change to the output dir (not existing, wrong
+ // permissions), exit lilypond
+ if (chdir (dir.c_str ()) != 0)
+ error (_f ("unable to change directory to: `%s'", dir));
+ }
+ else
+ out_file_name = File_name (output_name);