xref: /illumos-gate/usr/src/cmd/lp/model/netpr/misc.c (revision 0ccfe5834c3b867c1b6c273cae02ec8922bc0fd2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1996-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include "netpr.h"
36 #include "netdebug.h"
37 
38 extern char *strtok_r(char *, const char *, char **);
39 
40 int
41 check_file(char * filename)
42 {
43 	struct stat status;
44 
45 	if (filename  == NULL)
46 		return (-1);
47 
48 	/* Checking read permission */
49 	if (access(filename, R_OK) < 0)
50 		return (-1);
51 
52 	if (stat(filename, &status) < 0)
53 		return (-1);
54 
55 	/* Checking for regular file */
56 	if (S_ISREG(status.st_mode) == 0) {
57 		errno = EISDIR;
58 		return (-1);
59 	}
60 
61 	/* Checking for empty file */
62 	if (status.st_size == 0) {
63 		errno = ESRCH;
64 		return (-1);
65 	}
66 	return (status.st_size);
67 }
68 
69 
70 /*
71  * allocate the space; fill with input
72  */
73 char *
74 alloc_str(char * instr)
75 {
76 	char * outstr;
77 
78 	outstr = (char *)malloc(strlen(instr) + 1);
79 	ASSERT(outstr, MALLOC_ERR);
80 	(void) memset(outstr, 0, strlen(instr) + 1);
81 	(void) strcpy(outstr, instr);
82 
83 	return (outstr);
84 }
85 
86 np_job_t *
87 init_job()
88 {
89 	np_job_t * job;
90 
91 	if ((job = calloc(1, sizeof (*job))) != NULL) {
92 		job->protocol = BSD;
93 		job->banner = BANNER;
94 	}
95 
96 	return (job);
97 }
98 
99 void
100 tell_lptell(int type, char *fmt, ...)
101 {
102 	char msg[BUFSIZ];
103 	va_list ap;
104 
105 	va_start(ap, fmt);
106 	(void) vsnprintf(msg, sizeof (msg), fmt, ap);
107 	va_end(ap);
108 
109 	if (msg == NULL)
110 		return;
111 
112 	switch (type) {
113 	case ERRORMSG:
114 		(void) fprintf(stderr, "%%%%[PrinterError: %s ]%%%%\n", msg);
115 		break;
116 	case OKMSG:
117 		/* In this case, the message is the job request-id */
118 		(void) fprintf(stderr,
119 		"%%%%[job: %s status: ok source: Netpr]%%%%\n", msg);
120 		break;
121 	default:
122 		/* unknown type, ignore */
123 		break;
124 	}
125 
126 
127 }
128 
129 
130 /*
131  * Parse destination
132  * bsd: <printer_host>[:<printer_vendor_defined_name]
133  * tcp: <printer_host>[:port_number]
134  */
135 
136 void
137 parse_dest(char * dest, char **str1, char **str2, char * sep)
138 {
139 	char * tmp;
140 	char * nexttok;
141 
142 	*str1 = NULL;
143 	*str2 = NULL;
144 
145 	if (dest != NULL) {
146 		tmp = (char *)strtok_r(dest, sep, &nexttok);
147 		if (tmp != NULL)
148 			*str1 = strdup(tmp);
149 		tmp = (char *)strtok_r(NULL, sep, &nexttok);
150 		if (tmp != NULL)
151 			*str2 = strdup(tmp);
152 	}
153 
154 }
155 
156 /*
157  * void panic call
158  * used with ASSERT macro; gives us a place to stop the debugger
159  */
160 void
161 panic()
162 {
163 }
164