1f1b9d127SSheldon Hearn /*
2f1b9d127SSheldon Hearn * Copyright (c) 2000, Boris Popov
3f1b9d127SSheldon Hearn * All rights reserved.
4f1b9d127SSheldon Hearn *
5f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without
6f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions
7f1b9d127SSheldon Hearn * are met:
8f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright
9f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer.
10f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright
11f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the
12f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution.
13f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software
14f1b9d127SSheldon Hearn * must display the following acknowledgement:
15f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov.
16f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors
17f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software
18f1b9d127SSheldon Hearn * without specific prior written permission.
19f1b9d127SSheldon Hearn *
20f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f1b9d127SSheldon Hearn * SUCH DAMAGE.
31f1b9d127SSheldon Hearn *
32f1b9d127SSheldon Hearn * $Id: print.c,v 1.4 2001/01/28 07:35:01 bp Exp $
33f1b9d127SSheldon Hearn */
342f934d3eSGiorgos Keramidas
352f934d3eSGiorgos Keramidas #include <sys/cdefs.h>
362f934d3eSGiorgos Keramidas __FBSDID("$FreeBSD$");
372f934d3eSGiorgos Keramidas
38f1b9d127SSheldon Hearn #include <sys/param.h>
39f1b9d127SSheldon Hearn #include <sys/errno.h>
40f1b9d127SSheldon Hearn #include <sys/stat.h>
41f1b9d127SSheldon Hearn #include <err.h>
42f1b9d127SSheldon Hearn #include <fcntl.h>
43f1b9d127SSheldon Hearn #include <stdio.h>
44f1b9d127SSheldon Hearn #include <unistd.h>
45*833525a8SEnji Cooper #include <string.h>
46f1b9d127SSheldon Hearn #include <strings.h>
47f1b9d127SSheldon Hearn #include <stdlib.h>
48f1b9d127SSheldon Hearn #include <sysexits.h>
49f1b9d127SSheldon Hearn
50f1b9d127SSheldon Hearn #include <cflib.h>
51f1b9d127SSheldon Hearn
52f1b9d127SSheldon Hearn #include <netsmb/smb_lib.h>
53f1b9d127SSheldon Hearn #include <netsmb/smb_conn.h>
54f1b9d127SSheldon Hearn
55f1b9d127SSheldon Hearn #include "common.h"
56f1b9d127SSheldon Hearn
57f1b9d127SSheldon Hearn int
cmd_print(int argc,char * argv[])58f1b9d127SSheldon Hearn cmd_print(int argc, char *argv[])
59f1b9d127SSheldon Hearn {
60f1b9d127SSheldon Hearn struct smb_ctx sctx, *ctx = &sctx;
61f1b9d127SSheldon Hearn smbfh fh;
62f1b9d127SSheldon Hearn off_t offset;
63f1b9d127SSheldon Hearn char buf[8192];
64f1b9d127SSheldon Hearn char *filename;
65f1b9d127SSheldon Hearn char fnamebuf[256];
66f1b9d127SSheldon Hearn int error, opt, i, file, count;
67f1b9d127SSheldon Hearn
68f1b9d127SSheldon Hearn if (argc < 2)
69f1b9d127SSheldon Hearn view_usage();
70f1b9d127SSheldon Hearn if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_PRINTER) != 0)
71f1b9d127SSheldon Hearn exit(1);
72f1b9d127SSheldon Hearn if (smb_ctx_readrc(ctx) != 0)
73f1b9d127SSheldon Hearn exit(1);
74f1b9d127SSheldon Hearn if (smb_rc)
75f1b9d127SSheldon Hearn rc_close(smb_rc);
76f1b9d127SSheldon Hearn while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
77f1b9d127SSheldon Hearn switch(opt){
78f1b9d127SSheldon Hearn case STDPARAM_ARGS:
79f1b9d127SSheldon Hearn error = smb_ctx_opt(ctx, opt, optarg);
80f1b9d127SSheldon Hearn if (error)
81f1b9d127SSheldon Hearn exit(1);
82f1b9d127SSheldon Hearn break;
83f1b9d127SSheldon Hearn default:
84f1b9d127SSheldon Hearn view_usage();
85f1b9d127SSheldon Hearn /*NOTREACHED*/
86f1b9d127SSheldon Hearn }
87f1b9d127SSheldon Hearn }
88f1b9d127SSheldon Hearn if (optind + 1 >= argc)
89f1b9d127SSheldon Hearn print_usage();
90f1b9d127SSheldon Hearn filename = argv[optind + 1];
91f1b9d127SSheldon Hearn
92f1b9d127SSheldon Hearn if (strcmp(filename, "-") == 0) {
93f1b9d127SSheldon Hearn file = 0; /* stdin */
94f1b9d127SSheldon Hearn filename = "stdin";
95f1b9d127SSheldon Hearn } else {
96f1b9d127SSheldon Hearn file = open(filename, O_RDONLY, 0);
97f1b9d127SSheldon Hearn if (file < 0) {
98f1b9d127SSheldon Hearn smb_error("could not open file %s\n", errno, filename);
99f1b9d127SSheldon Hearn exit(1);
100f1b9d127SSheldon Hearn }
101f1b9d127SSheldon Hearn }
102f1b9d127SSheldon Hearn
103f1b9d127SSheldon Hearn if (smb_ctx_resolve(ctx) != 0)
104f1b9d127SSheldon Hearn exit(1);
105f1b9d127SSheldon Hearn error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
106f1b9d127SSheldon Hearn if (error) {
107f1b9d127SSheldon Hearn smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname);
108f1b9d127SSheldon Hearn exit(1);
109f1b9d127SSheldon Hearn }
110f1b9d127SSheldon Hearn snprintf(fnamebuf, sizeof(fnamebuf), "%s_%s_%s", ctx->ct_ssn.ioc_user,
111f1b9d127SSheldon Hearn ctx->ct_ssn.ioc_srvname, filename);
112f1b9d127SSheldon Hearn error = smb_smb_open_print_file(ctx, 0, 1, fnamebuf, &fh);
113f1b9d127SSheldon Hearn if (error) {
114f1b9d127SSheldon Hearn smb_error("could not open print job", error);
115f1b9d127SSheldon Hearn exit(1);
116f1b9d127SSheldon Hearn }
117f1b9d127SSheldon Hearn offset = 0;
118f1b9d127SSheldon Hearn error = 0;
119f1b9d127SSheldon Hearn for(;;) {
120f1b9d127SSheldon Hearn count = read(file, buf, sizeof(buf));
121f1b9d127SSheldon Hearn if (count == 0)
122f1b9d127SSheldon Hearn break;
123f1b9d127SSheldon Hearn if (count < 0) {
124f1b9d127SSheldon Hearn error = errno;
125f1b9d127SSheldon Hearn smb_error("error reading input file\n", error);
126f1b9d127SSheldon Hearn break;
127f1b9d127SSheldon Hearn }
128f1b9d127SSheldon Hearn i = smb_write(ctx, fh, offset, count, buf);
129f1b9d127SSheldon Hearn if (i < 0) {
130f1b9d127SSheldon Hearn error = errno;
131f1b9d127SSheldon Hearn smb_error("error writing spool file\n", error);
132f1b9d127SSheldon Hearn break;
133f1b9d127SSheldon Hearn }
134f1b9d127SSheldon Hearn if (i != count) {
135f1b9d127SSheldon Hearn smb_error("incomplete write to spool file\n", 0);
136f1b9d127SSheldon Hearn error = EIO;
137f1b9d127SSheldon Hearn break;
138f1b9d127SSheldon Hearn }
139f1b9d127SSheldon Hearn offset += count;
140f1b9d127SSheldon Hearn }
141f1b9d127SSheldon Hearn close(file);
142f1b9d127SSheldon Hearn error = smb_smb_close_print_file(ctx, fh);
143f1b9d127SSheldon Hearn if (error)
144f1b9d127SSheldon Hearn smb_error("an error while closing spool file\n", error);
145f1b9d127SSheldon Hearn return error ? 1 : 0;
146f1b9d127SSheldon Hearn }
147f1b9d127SSheldon Hearn
148f1b9d127SSheldon Hearn
149f1b9d127SSheldon Hearn void
print_usage(void)150f1b9d127SSheldon Hearn print_usage(void)
151f1b9d127SSheldon Hearn {
152f1b9d127SSheldon Hearn printf(
1532f934d3eSGiorgos Keramidas "usage: smbutil print [connection options] //user@server/share\n"
154f1b9d127SSheldon Hearn );
155f1b9d127SSheldon Hearn exit(1);
156f1b9d127SSheldon Hearn }
157f1b9d127SSheldon Hearn
158