xref: /illumos-gate/usr/src/cmd/lp/model/netpr/misc.c (revision 948f2876ce2a3010558f4f6937e16086ebcd36f2)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include "netpr.h"
38 #include "netdebug.h"
39 
40 extern char *strtok_r(char *, const char *, char **);
41 
42 int
43 check_file(char * filename)
44 {
45 	struct stat status;
46 
47 	if (filename  == NULL)
48 		return (-1);
49 
50 	/* Checking read permission */
51 	if (access(filename, R_OK) < 0)
52 		return (-1);
53 
54 	if (stat(filename, &status) < 0)
55 		return (-1);
56 
57 	/* Checking for regular file */
58 	if (S_ISREG(status.st_mode) == 0) {
59 		errno = EISDIR;
60 		return (-1);
61 	}
62 
63 	/* Checking for empty file */
64 	if (status.st_size == 0) {
65 		errno = ESRCH;
66 		return (-1);
67 	}
68 	return (status.st_size);
69 }
70 
71 
72 /*
73  * allocate the space; fill with input
74  */
75 char *
76 alloc_str(char * instr)
77 {
78 	char * outstr;
79 
80 	outstr = (char *)malloc(strlen(instr) + 1);
81 	ASSERT(outstr, MALLOC_ERR);
82 	(void) memset(outstr, 0, strlen(instr) + 1);
83 	(void) strcpy(outstr, instr);
84 
85 	return (outstr);
86 }
87 
88 np_job_t *
89 init_job()
90 {
91 	np_job_t * job;
92 
93 	if ((job = calloc(1, sizeof (*job))) != NULL) {
94 		job->protocol = BSD;
95 		job->banner = BANNER;
96 	}
97 
98 	return (job);
99 }
100 
101 void
102 tell_lptell(int type, char *fmt, ...)
103 {
104 	char msg[BUFSIZ];
105 	va_list ap;
106 
107 	va_start(ap, fmt);
108 	(void) vsnprintf(msg, sizeof (msg), fmt, ap);
109 	va_end(ap);
110 
111 	if (msg == NULL)
112 		return;
113 
114 	switch (type) {
115 	case ERRORMSG:
116 		(void) fprintf(stderr, "%%%%[PrinterError: %s ]%%%%\n", msg);
117 		break;
118 	case OKMSG:
119 		/* In this case, the message is the job request-id */
120 		(void) fprintf(stderr,
121 		"%%%%[job: %s status: ok source: Netpr]%%%%\n", msg);
122 		break;
123 	default:
124 		/* unknown type, ignore */
125 		break;
126 	}
127 
128 
129 }
130 
131 
132 /*
133  * Parse destination
134  * bsd: <printer_host>[:<printer_vendor_defined_name]
135  * tcp: <printer_host>[:port_number]
136  */
137 
138 void
139 parse_dest(char * dest, char **str1, char **str2, char * sep)
140 {
141 	char * tmp;
142 	char * nexttok;
143 
144 	*str1 = NULL;
145 	*str2 = NULL;
146 
147 	if (dest != NULL) {
148 		tmp = (char *)strtok_r(dest, sep, &nexttok);
149 		if (tmp != NULL)
150 			*str1 = strdup(tmp);
151 		tmp = (char *)strtok_r(NULL, sep, &nexttok);
152 		if (tmp != NULL)
153 			*str2 = strdup(tmp);
154 	}
155 
156 }
157 
158 /*
159  * void panic call
160  * used with ASSERT macro; gives us a place to stop the debugger
161  */
162 void
163 panic()
164 {
165 }
166