summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2018-09-18 13:42:13 -0700
committerH. Peter Anvin <hpa@zytor.com>2018-09-18 13:42:13 -0700
commit61ba4fa24c18cbf476b230c374659f66644a5f6b (patch)
treea3a62a5f93c05cdf325f2839f50f158445707dd1
parentf2cf87be8deb869b06ecc4be41d1b5752c0da00a (diff)
downloadabc80sim-61ba4fa24c18cbf476b230c374659f66644a5f6b.tar.gz
abc80sim-61ba4fa24c18cbf476b230c374659f66644a5f6b.tar.xz
abc80sim-61ba4fa24c18cbf476b230c374659f66644a5f6b.zip
print: drop stdin redirection & environment variables
On "some other operating systems" it is just too painful. Simply substitute a * character in the command with the path of the temporary file.
-rw-r--r--print.c87
-rw-r--r--tempfile.c11
-rw-r--r--tempfile.h1
3 files changed, 34 insertions, 65 deletions
diff --git a/print.c b/print.c
index 679a2dc..b07a6e0 100644
--- a/print.c
+++ b/print.c
@@ -22,78 +22,45 @@
#include "tempfile.h"
#ifdef __WIN32__
-const char lpr_command[] = "powershell -command \"GetContent -raw -path $Env:PRINT_FILE | OutPrinter\"";
+const char lpr_command[] = "powershell -command \"GetContent -raw -path '*' | OutPrinter\"";
#else
-const char lpr_command[] = "lpr";
+const char lpr_command[] = "lpr '*'";
#endif
static struct temp_file *temp;
-static char **push_env(const char *var, const char *val)
-{
- char **oldenviron = environ;
- char **newenviron, **oep, **nep;
- size_t nenv;
- size_t varlen, vallen;
- char *newvar;
-
- nenv = 0;
- while (oldenviron[nenv])
- nenv++;
-
- varlen = strlen(var);
- vallen = strlen(val);
-
- newenviron = malloc((nenv+2)*sizeof(char *) + varlen + vallen + 2);
- if (!newenviron)
- return NULL;
-
- newvar = (char *)&newenviron[nenv+2];
-
- memcpy(newvar, var, varlen);
- newvar[varlen] = '=';
- memcpy(newvar + varlen + 1, val, vallen+1);
-
- nep = newenviron;
- *nep++ = newvar;
- oep = oldenviron;
- while (*oep) {
- /* Don't copy a duplicate variable */
- if (strncmp(*oep, newvar, varlen+1))
- *nep++ = *oep;
- oep++;
- }
- *nep = NULL;
-
- environ = newenviron;
- return oldenviron;
-}
-
-static void pop_env(char **oldenviron)
-{
- char **newenviron = environ;
-
- environ = oldenviron;
- free(newenviron);
-}
-
static void print_finish(void)
{
- int oldstdin;
- char **oldenviron;
+ const char *p;
+ char *cmd, *q;
+ size_t cmdlen, namelen;
if (!temp)
return;
fflush(temp->f);
- rewind(temp->f);
-
- oldstdin = dup(STDIN_FILENO);
- oldenviron = push_env("PRINT_FILE", temp->filename);
- dup2(temp->fd, STDIN_FILENO);
- system(lpr_command);
- dup2(oldstdin, STDIN_FILENO);
- pop_env(oldenviron);
+
+ namelen = temp->namelen;
+ cmdlen = 0;
+ for (p = lpr_command; *p; p++) {
+ cmdlen += (*p == '*') ? namelen : 1;
+ }
+ cmd = malloc(cmdlen+1);
+
+ if (cmd) {
+ for (p = lpr_command, q = cmd; *p; p++) {
+ if (*p == '*') {
+ memcpy(q, temp->filename, namelen);
+ q += namelen;
+ } else {
+ *q++ = *p;
+ }
+ }
+ *q = '\0';
+
+ system(cmd);
+ free(cmd);
+ }
close_temp(&temp);
}
diff --git a/tempfile.c b/tempfile.c
index 25a8cd5..8d5e96d 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -47,7 +47,7 @@ struct temp_file *temp_file(enum temp_file_mode mode)
return NULL;
temp->mode = mode;
-
+ temp->namelen = sizeof template - 1;
memcpy(temp->filename, template, sizeof template);
temp->fd = mkstemp(temp->filename);
@@ -80,7 +80,7 @@ struct temp_file *temp_file(enum temp_file_mode mode)
char *filename = NULL;
int fd;
int attempts = TMP_MAX;
- size_t fnlen;
+ size_t namelen;
const int openflags = O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_SHORT_LIVED| \
mode_openflags(mode);
@@ -102,9 +102,10 @@ struct temp_file *temp_file(enum temp_file_mode mode)
return NULL;
}
- fnlen = strlen(filename);
- temp = malloc(sizeof *temp + fnlen);
- memcpy(temp->filename, filename, fnlen+1);
+ namelen = strlen(filename);
+ temp = malloc(sizeof *temp + namelen);
+ memcpy(temp->filename, filename, namelen + 1);
+ temp->namelen = namelen;
temp->fd = fd;
temp->mode = mode;
free(filename);
diff --git a/tempfile.h b/tempfile.h
index bfccf47..cf0a054 100644
--- a/tempfile.h
+++ b/tempfile.h
@@ -11,6 +11,7 @@ enum temp_file_mode {
struct temp_file {
FILE *f;
+ size_t namelen;
int fd;
enum temp_file_mode mode;
char filename[1];