2 * Copyright (c) 2000, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: Id: print.c,v 1.4 2001/01/28 07:35:01 bp Exp
36 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
37 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
38 * Use is subject to license terms.
41 #include <sys/types.h>
53 #include <netsmb/smb_lib.h>
57 static char titlebuf
[256];
58 static char databuf
[4096];
60 static int print_file(smb_ctx_t
*, char *, int);
65 printf(gettext("usage: smbutil print [connection options] //"
66 "[workgroup;][user[:password]@]"
67 "server/share {print_file|-}\n"));
72 cmd_print(int argc
, char *argv
[])
74 struct smb_ctx
*ctx
= NULL
;
79 /* last arg is the print file. */
83 error
= smb_ctx_alloc(&ctx
);
87 error
= smb_ctx_scan_argv(ctx
, argc
-1, argv
,
88 SMBL_SHARE
, SMBL_SHARE
, USE_SPOOLDEV
);
92 error
= smb_ctx_readrc(ctx
);
96 while ((opt
= getopt(argc
-1, argv
, STDPARAM_OPT
)) != EOF
) {
99 error
= smb_ctx_opt(ctx
, opt
, optarg
);
103 if (optind
!= argc
-2)
105 filename
= argv
[argc
-1];
107 if (strcmp(filename
, "-") == 0) {
108 file
= 0; /* stdin */
111 file
= open(filename
, O_RDONLY
, 0);
113 smb_error("could not open file %s\n", errno
, filename
);
119 * Resolve the server address,
120 * setup derived defaults.
122 error
= smb_ctx_resolve(ctx
);
127 * Have server + share names, options etc.
128 * Get the session and tree.
131 error
= smb_ctx_get_ssn(ctx
);
132 if (error
== EAUTH
) {
133 int err2
= smb_get_authentication(ctx
);
138 smb_error(gettext("//%s: login failed"),
139 error
, ctx
->ct_fullserver
);
143 error
= smb_ctx_get_tree(ctx
);
145 smb_error(gettext("//%s/%s: tree connect failed"),
146 error
, ctx
->ct_fullserver
, ctx
->ct_origshare
);
151 * Have the printer share connection.
154 snprintf(titlebuf
, sizeof (titlebuf
), "%s %s",
155 ctx
->ct_user
, filename
);
157 error
= print_file(ctx
, titlebuf
, file
);
161 /* don't close stdin (file=0) */
171 * Documentation for OPEN_PRINT_FILE is scarse.
172 * It's in a 1996 MS doc. entitled:
173 * SMB FILE SHARING PROTOCOL
175 * The extra parameters are:
176 * SetupLength: what part of the file is printer setup
177 * Mode: text or graphics (raw data)
178 * IdentifierString: job title
181 MODE_TEXT
= 0, /* TAB expansion, etc. */
182 MODE_GRAPHICS
/* raw data */
186 print_file(smb_ctx_t
*ctx
, char *title
, int file
)
190 int setup_len
= 0; /* No printer setup data */
191 int mode
= MODE_GRAPHICS
; /* treat as raw data */
195 pfd
= smb_open_printer(ctx
, title
, setup_len
, mode
);
198 smb_error("could not open print job", error
);
204 rcnt
= read(file
, databuf
, sizeof (databuf
));
207 smb_error("error reading input file\n", error
);
213 wcnt
= smb_fh_write(pfd
, offset
, rcnt
, databuf
);
216 smb_error("error writing spool file\n", error
);
220 smb_error("incomplete write to spool file\n", 0);
227 (void) smb_fh_close(pfd
);