mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 19:50:24 +02:00
Use gnulib for basename / dirname
* libguile/filesys.c (scm_dirname, scm_basename): Rewrite to use gnulib's dirname-lgpl.
This commit is contained in:
parent
8868c850e6
commit
1f14900a07
1 changed files with 32 additions and 54 deletions
|
@ -35,6 +35,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <alloca.h>
|
#include <alloca.h>
|
||||||
|
#include <dirname.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -1546,31 +1547,22 @@ SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
|
||||||
"component, @code{.} is returned.")
|
"component, @code{.} is returned.")
|
||||||
#define FUNC_NAME s_scm_dirname
|
#define FUNC_NAME s_scm_dirname
|
||||||
{
|
{
|
||||||
long int i;
|
char *c_filename, *c_dirname;
|
||||||
unsigned long int len;
|
SCM res;
|
||||||
|
|
||||||
SCM_VALIDATE_STRING (1, filename);
|
scm_dynwind_begin (0);
|
||||||
|
c_filename = scm_to_utf8_string (filename);
|
||||||
|
scm_dynwind_free (c_filename);
|
||||||
|
|
||||||
len = scm_i_string_length (filename);
|
c_dirname = mdir_name (c_filename);
|
||||||
|
if (!c_dirname)
|
||||||
|
SCM_SYSERROR;
|
||||||
|
scm_dynwind_free (c_dirname);
|
||||||
|
|
||||||
i = len - 1;
|
res = scm_from_utf8_string (c_dirname);
|
||||||
|
scm_dynwind_end ();
|
||||||
|
|
||||||
while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
|
return res;
|
||||||
--i;
|
|
||||||
while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
|
|
||||||
--i;
|
|
||||||
while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
|
|
||||||
--i;
|
|
||||||
|
|
||||||
if (i < 0)
|
|
||||||
{
|
|
||||||
if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
|
|
||||||
return scm_c_substring (filename, 0, 1);
|
|
||||||
else
|
|
||||||
return scm_dot_string;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return scm_c_substring (filename, 0, i + 1);
|
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
@ -1582,42 +1574,28 @@ SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
|
||||||
"@var{filename}, it is removed also.")
|
"@var{filename}, it is removed also.")
|
||||||
#define FUNC_NAME s_scm_basename
|
#define FUNC_NAME s_scm_basename
|
||||||
{
|
{
|
||||||
int i, j, len, end;
|
char *c_filename, *c_last_component;
|
||||||
|
SCM res;
|
||||||
|
|
||||||
SCM_VALIDATE_STRING (1, filename);
|
scm_dynwind_begin (0);
|
||||||
len = scm_i_string_length (filename);
|
c_filename = scm_to_utf8_string (filename);
|
||||||
|
scm_dynwind_free (c_filename);
|
||||||
|
|
||||||
if (SCM_UNBNDP (suffix))
|
c_last_component = last_component (c_filename);
|
||||||
j = -1;
|
if (!c_last_component)
|
||||||
|
res = filename;
|
||||||
else
|
else
|
||||||
{
|
res = scm_from_utf8_string (c_last_component);
|
||||||
SCM_VALIDATE_STRING (2, suffix);
|
scm_dynwind_end ();
|
||||||
j = scm_i_string_length (suffix) - 1;
|
|
||||||
}
|
if (!SCM_UNBNDP (suffix) &&
|
||||||
i = len - 1;
|
scm_is_true (scm_string_suffix_p (suffix, filename,
|
||||||
while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
|
SCM_UNDEFINED, SCM_UNDEFINED,
|
||||||
--i;
|
SCM_UNDEFINED, SCM_UNDEFINED)))
|
||||||
end = i;
|
res = scm_c_substring
|
||||||
while (i >= 0 && j >= 0
|
(res, 0, scm_c_string_length (res) - scm_c_string_length (suffix));
|
||||||
&& (scm_i_string_ref (filename, i)
|
|
||||||
== scm_i_string_ref (suffix, j)))
|
return res;
|
||||||
{
|
|
||||||
--i;
|
|
||||||
--j;
|
|
||||||
}
|
|
||||||
if (j == -1)
|
|
||||||
end = i;
|
|
||||||
while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
|
|
||||||
--i;
|
|
||||||
if (i == end)
|
|
||||||
{
|
|
||||||
if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
|
|
||||||
return scm_c_substring (filename, 0, 1);
|
|
||||||
else
|
|
||||||
return scm_dot_string;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return scm_c_substring (filename, i+1, end+1);
|
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue