zpmod  b19981f
High-performance Zsh module for script optimization and filesystem helpers
fs_builtins.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
6 /* Canonical module header ordering */
7 #include "zpmod.mdh"
8 #include "zpmod.pro"
9 #include "zpmod_emoji.h"
10 #include "zpmod_fs.h"
11 #include "zpmod_vendor_shims.h"
12 #include <stddef.h>
13 
15 int bin_zppathstat(char *nam, char **argv, Options ops, int func) {
16  (void)func; /* unused */
17  int follow = OPT_ISSET(ops, 'L');
18  char *fields = NULL;
19  if (OPT_ISSET(ops, 'f')) {
20  fields = OPT_ARG(ops, 'f');
21  }
22  if (!argv || !argv[0] || !argv[1]) {
23  zwarnnam(nam, "%s %susage: %s [-L] [-f fields] out_array in_array",
24  zp_icon("⚠️ "), nam, nam);
25  return 1;
26  }
27  return zp_pathstat_core(nam, argv[0], argv[1], follow, fields);
28 }
29 
31 int bin_zpdirlist(char *nam, char **argv, Options ops, int func) {
32  (void)func; /* unused */
33  int inc_all = OPT_ISSET(ops, 'a');
34  int only_dirs = OPT_ISSET(ops, 'd');
35  int only_files = OPT_ISSET(ops, 'f');
36  if (!argv || !argv[0] || !argv[1]) {
37  zwarnnam(nam, "%s %susage: %s [-a] [-d] [-f] out_array dir", zp_icon("⚠️ "),
38  nam, nam);
39  return 1;
40  }
41  return zp_dirlist_core(nam, argv[0], argv[1], inc_all, only_dirs, only_files);
42 }
43 
45 int bin_zpreadfile(char *nam, char **argv, Options ops, int func) {
46  (void)func; /* unused */
47  int use_mmap = OPT_ISSET(ops, 'm');
48  int delim = '\n';
49  int split = 0;
50  if (OPT_ISSET(ops, '0')) {
51  split = 1;
52  delim = '\0';
53  }
54  if (OPT_ISSET(ops, 'd')) {
55  char *a = OPT_ARG(ops, 'd');
56  if (a && *a) {
57  split = 1;
58  if (a[0] == '\\') {
59  switch (a[1]) {
60  case 'n':
61  delim = '\n';
62  break;
63  case 't':
64  delim = '\t';
65  break;
66  case '0':
67  delim = '\0';
68  break;
69  case 'r':
70  delim = '\r';
71  break;
72  default:
73  delim = (unsigned char)a[1];
74  break;
75  }
76  } else {
77  delim = (unsigned char)a[0];
78  }
79  } else {
80  split = 1;
81  delim = '\n';
82  }
83  }
84  if (!argv || !argv[0] || !argv[1]) {
85  zwarnnam(nam, "%s %susage: %s [-m] [-d delim|-0] out file", zp_icon("⚠️ "),
86  nam, nam);
87  return 1;
88  }
89  return zp_readfile_core(nam, argv[0], argv[1], use_mmap, split, delim);
90 }
91 
92 /* Export table from this TU */
93 static struct builtin fs_builtins[] = {
94  BUILTIN("zppathstat", 0, bin_zppathstat, 2, 2, 0, "Lf:", NULL),
95  BUILTIN("zpdirlist", 0, bin_zpdirlist, 2, 2, 0, "adf", NULL),
96  BUILTIN("zpreadfile", 0, bin_zpreadfile, 2, 2, 0, "md:0", NULL),
97 };
98 
99 struct builtin *zp_get_fs_builtins(size_t *count) {
100  if (count) {
101  *count = sizeof(fs_builtins) / sizeof(*fs_builtins);
102  }
103  return fs_builtins;
104 }
const char * zp_icon(const char *s)
Return icon string if enabled, empty string otherwise.
Definition: emoji.c:58
int zp_readfile_core(char *nam, char *outname, char *path, int use_mmap, int split, int delim)
See zpmod_fs.h for contract.
Definition: fs.c:444
int zp_dirlist_core(char *nam, char *outname, char *dir, int inc_all, int only_dirs, int only_files)
See zpmod_fs.h for contract.
Definition: fs.c:382
int zp_pathstat_core(char *nam, char *outname, char *inname, int follow, char *fields)
See zpmod_fs.h for contract.
Definition: fs.c:36
int bin_zpdirlist(char *nam, char **argv, Options ops, int func)
zpdirlist builtin entrypoint
Definition: fs_builtins.c:31
int bin_zppathstat(char *nam, char **argv, Options ops, int func)
zppathstat builtin entrypoint
Definition: fs_builtins.c:15
static struct builtin fs_builtins[]
Definition: fs_builtins.c:93
struct builtin * zp_get_fs_builtins(size_t *count)
Definition: fs_builtins.c:99
int bin_zpreadfile(char *nam, char **argv, Options ops, int func)
zpreadfile builtin entrypoint
Definition: fs_builtins.c:45
Module declaration header (mdh) for zpmod.
Prototype stub for zpmod when building out-of-tree.
Optional terminal/locale detection for emoji support in messages.
Filesystem helpers used by builtins and zpmod subcommands.
void zwarnnam(const char *, const char *,...)
Local, non-invasive shims to suppress benign vendor header warnings.