1 #define _POSIX_C_SOURCE 2
24 int (*callback)(void*,int,char**,char**);
28 printf("usage: zpm foreach-path [-fncC] args ...\n");
31 static int run_program(void *f, int ncols, char **vals, char **cols) {
32 struct config *conf = f;
39 argv[0] = conf->program;
41 for (i=0;i<ncols;i++) {
42 argv[i+1] = vals[i] ? vals[i] : conf->nullstr;
49 perror("cannot fork");
55 rv = execvp(conf->program, argv);
57 perror("cannot exec");
64 perror("error waiting for child");
67 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
69 } else if (conf->errcontinue) {
76 static int run_shell(void *f, int ncols, char **vals, char **cols) {
77 struct config *conf = f;
89 for (i=0;i<ncols;i++) {
90 argv[i+4] = vals[i] ? vals[i] : conf->nullstr;
97 perror("cannot fork");
103 rv = execvp("sh", argv);
105 perror("cannot exec");
112 perror("error waiting for child");
114 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
116 } else if (conf->errcontinue) {
123 static int printpaths(void *f, int ncols, char **vals, char **cols) {
125 /* suppress unused warnings */
128 fprintf(stderr, "can't find path names\n");
131 // printf("path cols %d %p %p\n", ncols, vals, cols);
132 fprintf(stdout, "%s\n", vals[5]);
138 fprintf(stderr, "sqlite can't get column names\n");
140 for (i=0;i<ncols;i++) {
141 // if (i>3) fprintf(out, "\t");
142 fprintf(stdout, "%s\n", vals[i]);
149 #define PATHLEN PATH_MAX
154 int main(int ac, char **av){
158 struct config conf = { 0 };
160 conf.dbfile = getenv("ZPMDB");
161 conf.callback = printpaths;
165 conf.dbfile = "/var/lib/zpm/local.db";
170 * -f 'package database', otherwise regular default of env ZPMDB,
171 * or /var/lib/zpm/zpm.db, or die
173 * arg 1: pkgid triple, but will do a pkg find
174 * arg 2: program to run. equivalent of 'printf "%s\n" "$path"
178 while ((opt = getopt(ac, av, "f:c:n:Cp:F:")) != -1) {
180 case 'f': conf.dbfile = optarg;
182 case 'c': conf.cmd = optarg;
183 conf.callback = run_shell;
186 conf.errcontinue = 1;
189 conf.nullstr = optarg;
194 case 'F': conf.filter = optarg;
204 if (!zpm_open(&pkg, conf.dbfile)) {
205 fprintf(stderr, "can't open zpm db %s\n", conf.dbfile);
210 /* TODO lookup pkgid via zpm-findpkg equivalent */
213 conf.program = av[argn];
214 conf.callback = run_program;
216 /* TODO set conf.args to remaining arguments */
219 if (!zpm_foreach_path(&pkg, conf.pkgid, conf.filter, conf.callback, &conf, &errmsg)) {
221 fprintf(stderr, "database error: %s\n", errmsg);
224 if (pkg.error == 1) {
225 fprintf(stderr, "unable to allocate memory\n");
231 return conf.errors ? 1 : 0;