File: wrapper.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (111 lines) | stat: -rw-r--r-- 3,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Description: wrapper for gs to correctly read the  papersize 
   in (/etc/papersize), for Debian systems.
   Copyright: GPL
   Author: Joost Witteveen <joost@rulcmc.leidenuniv.nl>
   Modified by: Marco Pistore <pistore@di.unipi.it>
*/

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <paper.h>

/* the real gs executable (in the case GSREAL is not defined 
   in the environment) */
char default_binary[] = "/usr/lib/ghostscript/3.33/gs.real";

/* the device for svgalibs */
#define SVGADEVICE "-sDEVICE=lvga256"

/* these options are used to set paper size */
#define GSPAPERSIZE   "-sPAPERSIZE="
#define GSPAPERWIDTH  "-dDEVICEWIDTHPOINTS="
#define GSPAPERHEIGHT "-dDEVICEHEIGHTPOINTS="
#define GSOTHERWIDTH  "-dDEVICEWIDTH="
#define GSOTHERHEIGHT "-dDEVICEHEIGHT="

void add_argument(char ***as,char *s){  
/* this adds aguments to the beginning of the
   argument list, so that the user is able to override them */
  int l;
  char **a=*as;
  char *t=malloc(strlen(s)+1);
  strcpy(t,s);
  
  for(l=0;a&&a[l];l++);
  if(!(*as=(char **) malloc(sizeof(char *)*(l+2)))){
    fprintf(stderr,"not enough memory, aborting.");
    exit(1);
  }
  (*as)[0]=a[0];
  (*as)[1]=t;
  (*as)[l+1]=NULL;
  while(--l)
    (*as)[l+1]=a[l];
  free(a);
}

int shortcmp(char *s, char *t){
  return strncmp(s,t,strlen(t));
}

void main (int argc, char ** argv){
  char b[100];
  char **arg;
  const struct paper *papinfo;
  char *binary;

  binary = getenv("GSREAL");
  if (! binary) 
    binary = default_binary;
  

  if(geteuid()!=getuid()){
    fprintf(stderr,
	    "Error (gs wrapper): the gs wrapper should _not_ be setuid.\n"
            "If you want to run gs setuid, make %s setuid.\n"
            "(See /usr/doc/gs/setuid for more info)\n", binary);
  }
  if(!(arg=(char**)malloc((argc+1)*sizeof(*argv)))){
     fprintf(stderr,"not enough memory, aborting.");
     exit(1);
  }
  memcpy(arg,argv,(argc+1)*sizeof(*argv));
  argv=arg;
  /*check value of /etc/papersize, but only use it if no
    other value is issued (need to check, as "-sPAPERSIZE" does
    not override an earlier issued "-dDEVICEPAPER" etc.)
    */
  for(arg=argv;
      *arg&&(shortcmp(*arg,GSPAPERWIDTH)&&
	     shortcmp(*arg,GSPAPERHEIGHT)&&
	     shortcmp(*arg,GSOTHERWIDTH)&&
	     shortcmp(*arg,GSOTHERHEIGHT)&&
	     shortcmp(*arg,GSPAPERSIZE)&&
	     shortcmp(*arg,"-g="));
      arg++);
  if(!*arg){
    paperinit();
    if((papinfo=paperinfo(systempapername()))){
      sprintf(b,GSPAPERWIDTH "%d",(int)paperpswidth(papinfo));
      add_argument(&argv,b);
      sprintf(b,GSPAPERHEIGHT "%d",(int)paperpsheight(papinfo));    
      add_argument(&argv,b);
      paperdone();
    }
  }
  /*if running on console, need add "-sDEVICE=lvga256" 
    (if user specified other -sDEVICE, this is OK, as user
     options always come later on the commandline)*/
  if(!getenv("DISPLAY") || !strcmp("",getenv("DISPLAY")))
    add_argument(&argv, SVGADEVICE);

  execv(binary,argv);

  fprintf(stderr,"Error (gs wrapper): could not exec %s.", binary);
}