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
|
$NetBSD: patch-aa,v 1.2 1998/11/11 11:42:57 agc Exp $
Read default papersize from a configuration file at run-time.
If it's not in the correct format, or it's not found, default to
the papersize that was compiled in.
--- psutil.c 1998/10/30 16:48:18 1.1
+++ psutil.c 1998/11/09 16:18:30
@@ -19,6 +19,7 @@
#include <string.h>
#include <sys/types.h>
+#include <sys/param.h>
#include <sys/stat.h>
#define iscomment(x,y) (strncmp(x,y,strlen(y)) == 0)
@@ -64,10 +65,62 @@
{ NULL, 0, 0 }
};
+/* bounds-checking strncpy */
+char *
+strnncpy(char *to, size_t tosize, char *from, size_t cc)
+{
+ size_t len;
+
+ if ((len = cc) >= tosize - 1) {
+ len = tosize - 1;
+ }
+ (void) strncpy(to, from, len);
+ to[len] = 0;
+ return to;
+}
+
+#if (defined(BSD) && BSD >= 199306)
+#include <ctype.h>
+
+/* read PAPERSIZE from file */
+int
+readconfig(char *name, size_t namesize)
+{
+ FILE *fp;
+ char buf[BUFSIZ];
+ char *cp;
+ int found;
+
+ found = 0;
+ if ((fp = fopen("@prefix@/etc/psutils.cfg", "r")) != (FILE *) NULL) {
+ while (fgets(buf, sizeof(buf), fp) != (char *) NULL) {
+ buf[strlen(buf) - 1] = 0;
+ for (cp = buf ; *cp && *cp != '\n' && isspace(*cp) ; cp++) {
+ }
+ if (*cp == '#') {
+ continue;
+ }
+ if (strncmp(cp, "PAPERSIZE=", 10) == 0) {
+ for (cp += 10; *cp && isspace(*cp) ; cp++) {
+ }
+ if (*cp != 0) {
+ (void) strnncpy(name, namesize, cp, strlen(cp));
+ found = 1;
+ break;
+ }
+ }
+ }
+ (void) fclose(fp);
+ }
+ return found;
+}
+#endif
+
/* return pointer to paper size struct or NULL */
Paper* findpaper(char *name)
{
Paper *pp;
+
for (pp = papersizes; PaperName(pp); pp++) {
if (strcmp(PaperName(pp), name) == 0) {
return pp;
|