+ * Build a command line that causes the user's shell to list the file name
+ * arguments. This handles and wildcard expansion, tilde expansion, etc.
+ */
+static void
+writelscmd(char *buf, int bufsz, char **argp)
+{
+ writesomecmd(buf, bufsz, "ls", "", argp);
+}
+
+/* Like system(), but run the command in directory dir.
+ * This assumes the program is single-threaded!
+ */
+static int
+system_in_dir(const char *dir, const char *cmd)
+{
+ char olddir[BUFSIZ];
+ int r;
+
+ /* ensure that $SHELL exists, as the cmd was written relying on
+ a non-blank $SHELL... */
+ setenv("SHELL","/bin/sh",0); /* don't overwrite */
+
+ if (getcwd(olddir, sizeof(olddir)) == 0)
+ adios("getcwd", "could not get working directory");
+ if (chdir(dir) != 0)
+ adios("chdir", "could not change working directory");
+ r = system(cmd);
+ if (chdir(olddir) != 0)
+ adios("chdir", "could not change working directory");
+ return r;
+}
+
+/* ditto for popen() */
+static FILE*
+popen_in_dir(const char *dir, const char *cmd, const char *type)
+{
+ char olddir[BUFSIZ];
+ FILE *f;
+
+ /* ensure that $SHELL exists, as the cmd was written relying on
+ a non-blank $SHELL... */
+ setenv("SHELL","/bin/sh",0); /* don't overwrite */
+
+ if (getcwd(olddir, sizeof(olddir)) == 0)
+ adios("getcwd", "could not get working directory");
+ if (chdir(dir) != 0)
+ adios("chdir", "could not change working directory");
+ f = popen(cmd, type);
+ if (chdir(olddir) != 0)
+ adios("chdir", "could not change working directory");
+ return f;
+}
+
+
+/*