ref: 2385eae8d4de265c433778aa11f3fef391aaf62b
dir: /tools/listfiles.tool/
static char*
list_files(OToolcall tc, void*)
{
String *str;
int n, i, fd;
Dir *dirbuf;
char *s;
JSON *j, *jf;
if (!allowed(tc, 0))
return abortcall(tc);
j = jsonparse(tc.arguments);
jf = jsonbyname(j, "folder");
s = jsonstr(jf);
if (!(s && s[0])) {
fprint(2, "list_files: invalid folder!");
jsonfree(j);
return strdup("list_files: invalid folder");
}
dirbuf = dirstat(s);
if (!(dirbuf->mode&DMDIR)) {
s = smprint("%s is a file, not a folder", s);
free(dirbuf);
jsonfree(j);
return s;
}
fd = open(s, OREAD);
if (fd < 0)
return strdup("");
jsonfree(j);
n = dirreadall(fd, &dirbuf);
close(fd);
if (n < 0)
return strdup("");
str = s_new();
for (i = 0; i < n; i++) {
str = s_append(str, dirbuf[i].name);
if (dirbuf[i].mode&DMDIR)
str = s_append(str, "/");
str = s_append(str, "\n");
}
free(dirbuf);
s = strdup(s_to_c(str));
s_free(str);
return s;
}
static char* listfilesdesc = "list all files in the specified directory, similar to `ls` command. Paths are relative to the current working directory. Use `.` for the current directory.";
static char* listfilesargs = ""
%%json
{
"type": "object",
"properties": {
"folder": {
"type": "string",
"description": "relative or absolute path to the folder."
}
},
"required": [ "folder" ]
}
%/json
;