Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
list_dir.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
21
23 /* pops path string; pushes array of strings */
24 Value pathv = pop_value(vm);
27
29 if (path) {
30 /*
31 * Using 'ls -1' as a fallback to avoid dirent.h conflicts on some systems.
32 * We escape the path minimally for the shell.
33 */
34 size_t slen = strlen(path) + 16;
35 char *cmd = (char *)malloc(slen);
36 if (cmd) {
37 snprintf(cmd, slen, "ls -1 \"%s\"", path);
38 FILE *fp = popen(cmd, "r");
39 if (fp) {
40 char line[1024];
41 while (fgets(line, sizeof(line), fp)) {
42 /* Strip trailing newline */
43 size_t l = strlen(line);
44 if (l > 0 && line[l - 1] == '\n') line[l - 1] = '\0';
45 if (l > 1 && line[l - 2] == '\r') line[l - 2] = '\0';
46
47 if (line[0] != '\0') {
49 }
50 }
51 pclose(fp);
52 }
53 free(cmd);
54 }
55 free(path);
56 }
58 break;
59}
@ OP_OS_LIST_DIR
Definition bytecode.h:238
array_clear & arr
Definition clear.c:38
int64_t l
Definition lcm.c:61
push_value(vm, arr)
free_value(pathv)
free(vals)
char * cmd
Definition proc_run.c:33
Value pathv
Definition serial_open.c:91
Tagged union representing a Fun value.
Definition value.h:68
vm fp
Definition throw.c:57
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
int array_push(Value *v, Value newElem)
Append a Value to an array.
Definition value.c:257
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
Value path
Definition write_file.c:33