mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-19 18:20:22 +02:00
* filesys.c (scm_readlink): Make local vars rv and size ints, to
avoid signed/unsigned comparison warnings, and because the return value of readlink may be -1. Don't bother casting the third argument to readlink. * filesys.c (scm_dirname, scm_basename): Move these to their own page, at the end of the file. * filesys.h (scm_dirname, scm_basename): Add prototypes for these.
This commit is contained in:
parent
8ecf1f1359
commit
6a738a2516
2 changed files with 86 additions and 78 deletions
|
@ -461,81 +461,6 @@ scm_stat (object)
|
|||
return scm_stat2scm (&stat_temp);
|
||||
}
|
||||
|
||||
SCM scm_dot_string;
|
||||
|
||||
SCM_PROC (s_dirname, "dirname", 1, 0, 0, scm_dirname);
|
||||
|
||||
SCM
|
||||
scm_dirname (SCM filename)
|
||||
{
|
||||
char *s;
|
||||
int i, len;
|
||||
SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
|
||||
filename,
|
||||
SCM_ARG1,
|
||||
s_dirname);
|
||||
s = SCM_ROCHARS (filename);
|
||||
len = SCM_LENGTH (filename);
|
||||
i = len - 1;
|
||||
while (i >= 0 && s[i] == '/') --i;
|
||||
while (i >= 0 && s[i] != '/') --i;
|
||||
while (i >= 0 && s[i] == '/') --i;
|
||||
if (i < 0)
|
||||
{
|
||||
if (len > 0 && s[0] == '/')
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
|
||||
else
|
||||
return scm_dot_string;
|
||||
}
|
||||
else
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (i + 1));
|
||||
}
|
||||
|
||||
SCM_PROC (s_basename, "basename", 1, 1, 0, scm_basename);
|
||||
|
||||
SCM
|
||||
scm_basename (SCM filename, SCM suffix)
|
||||
{
|
||||
char *f, *s = 0;
|
||||
int i, j, len, end;
|
||||
SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
|
||||
filename,
|
||||
SCM_ARG1,
|
||||
s_basename);
|
||||
SCM_ASSERT (SCM_UNBNDP (suffix)
|
||||
|| (SCM_NIMP (suffix) && SCM_ROSTRINGP (suffix)),
|
||||
suffix,
|
||||
SCM_ARG2,
|
||||
s_basename);
|
||||
f = SCM_ROCHARS (filename);
|
||||
if (SCM_UNBNDP (suffix))
|
||||
j = -1;
|
||||
else
|
||||
{
|
||||
s = SCM_ROCHARS (suffix);
|
||||
j = SCM_LENGTH (suffix) - 1;
|
||||
}
|
||||
len = SCM_LENGTH (filename);
|
||||
i = len - 1;
|
||||
while (i >= 0 && f[i] == '/') --i;
|
||||
end = i;
|
||||
while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
|
||||
if (j == -1)
|
||||
end = i;
|
||||
while (i >= 0 && f[i] != '/') --i;
|
||||
if (i == end)
|
||||
{
|
||||
if (len > 0 && f[0] == '/')
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
|
||||
else
|
||||
return scm_dot_string;
|
||||
}
|
||||
else
|
||||
return scm_make_shared_substring (filename,
|
||||
SCM_MAKINUM (i + 1),
|
||||
SCM_MAKINUM (end + 1));
|
||||
}
|
||||
|
||||
|
||||
/* {Modifying Directories}
|
||||
*/
|
||||
|
@ -1296,8 +1221,8 @@ scm_readlink(path)
|
|||
SCM path;
|
||||
{
|
||||
#ifdef HAVE_READLINK
|
||||
scm_sizet rv;
|
||||
scm_sizet size = 100;
|
||||
int rv;
|
||||
int size = 100;
|
||||
char *buf;
|
||||
SCM result;
|
||||
SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, (char *) SCM_ARG1,
|
||||
|
@ -1305,7 +1230,7 @@ scm_readlink(path)
|
|||
SCM_COERCE_SUBSTR (path);
|
||||
SCM_DEFER_INTS;
|
||||
buf = scm_must_malloc (size, s_readlink);
|
||||
while ((rv = readlink (SCM_ROCHARS (path), buf, (scm_sizet) size)) == size)
|
||||
while ((rv = readlink (SCM_ROCHARS (path), buf, size)) == size)
|
||||
{
|
||||
scm_must_free (buf);
|
||||
size *= 2;
|
||||
|
@ -1405,6 +1330,86 @@ scm_copy_file (oldfile, newfile)
|
|||
return SCM_UNSPECIFIED;
|
||||
}
|
||||
|
||||
|
||||
/* Filename manipulation */
|
||||
|
||||
SCM scm_dot_string;
|
||||
|
||||
SCM_PROC (s_dirname, "dirname", 1, 0, 0, scm_dirname);
|
||||
|
||||
SCM
|
||||
scm_dirname (SCM filename)
|
||||
{
|
||||
char *s;
|
||||
int i, len;
|
||||
SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
|
||||
filename,
|
||||
SCM_ARG1,
|
||||
s_dirname);
|
||||
s = SCM_ROCHARS (filename);
|
||||
len = SCM_LENGTH (filename);
|
||||
i = len - 1;
|
||||
while (i >= 0 && s[i] == '/') --i;
|
||||
while (i >= 0 && s[i] != '/') --i;
|
||||
while (i >= 0 && s[i] == '/') --i;
|
||||
if (i < 0)
|
||||
{
|
||||
if (len > 0 && s[0] == '/')
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
|
||||
else
|
||||
return scm_dot_string;
|
||||
}
|
||||
else
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (i + 1));
|
||||
}
|
||||
|
||||
SCM_PROC (s_basename, "basename", 1, 1, 0, scm_basename);
|
||||
|
||||
SCM
|
||||
scm_basename (SCM filename, SCM suffix)
|
||||
{
|
||||
char *f, *s = 0;
|
||||
int i, j, len, end;
|
||||
SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
|
||||
filename,
|
||||
SCM_ARG1,
|
||||
s_basename);
|
||||
SCM_ASSERT (SCM_UNBNDP (suffix)
|
||||
|| (SCM_NIMP (suffix) && SCM_ROSTRINGP (suffix)),
|
||||
suffix,
|
||||
SCM_ARG2,
|
||||
s_basename);
|
||||
f = SCM_ROCHARS (filename);
|
||||
if (SCM_UNBNDP (suffix))
|
||||
j = -1;
|
||||
else
|
||||
{
|
||||
s = SCM_ROCHARS (suffix);
|
||||
j = SCM_LENGTH (suffix) - 1;
|
||||
}
|
||||
len = SCM_LENGTH (filename);
|
||||
i = len - 1;
|
||||
while (i >= 0 && f[i] == '/') --i;
|
||||
end = i;
|
||||
while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
|
||||
if (j == -1)
|
||||
end = i;
|
||||
while (i >= 0 && f[i] != '/') --i;
|
||||
if (i == end)
|
||||
{
|
||||
if (len > 0 && f[0] == '/')
|
||||
return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
|
||||
else
|
||||
return scm_dot_string;
|
||||
}
|
||||
else
|
||||
return scm_make_shared_substring (filename,
|
||||
SCM_MAKINUM (i + 1),
|
||||
SCM_MAKINUM (end + 1));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
|
|
|
@ -81,6 +81,9 @@ extern SCM scm_symlink SCM_P ((SCM oldpath, SCM newpath));
|
|||
extern SCM scm_readlink SCM_P ((SCM path));
|
||||
extern SCM scm_lstat SCM_P ((SCM str));
|
||||
extern SCM scm_copy_file SCM_P ((SCM oldfile, SCM newfile));
|
||||
extern SCM scm_dirname SCM_P ((SCM filename));
|
||||
extern SCM scm_basename SCM_P ((SCM filename, SCM suffix));
|
||||
|
||||
extern void scm_init_filesys SCM_P ((void));
|
||||
|
||||
#endif /* FILESYSH */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue