print.c revision 87867
11638Srgrimes/* 21638Srgrimes * Copyright (c) 2000, Boris Popov 31638Srgrimes * All rights reserved. 41638Srgrimes * 51638Srgrimes * Redistribution and use in source and binary forms, with or without 61638Srgrimes * modification, are permitted provided that the following conditions 71638Srgrimes * are met: 81638Srgrimes * 1. Redistributions of source code must retain the above copyright 91638Srgrimes * notice, this list of conditions and the following disclaimer. 101638Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111638Srgrimes * notice, this list of conditions and the following disclaimer in the 121638Srgrimes * documentation and/or other materials provided with the distribution. 131638Srgrimes * 3. All advertising materials mentioning features or use of this software 141638Srgrimes * must display the following acknowledgement: 151638Srgrimes * This product includes software developed by Boris Popov. 161638Srgrimes * 4. Neither the name of the author nor the names of any co-contributors 171638Srgrimes * may be used to endorse or promote products derived from this software 181638Srgrimes * without specific prior written permission. 191638Srgrimes * 201638Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 211638Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221638Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231638Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 241638Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251638Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261638Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271638Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281638Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291638Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301638Srgrimes * SUCH DAMAGE. 311638Srgrimes * 321638Srgrimes * $Id: print.c,v 1.4 2001/01/28 07:35:01 bp Exp $ 3350476Speter */ 341638Srgrimes#include <sys/param.h> 35281830Sbrooks#include <sys/errno.h> 361638Srgrimes#include <sys/stat.h> 3779538Sru#include <err.h> 381638Srgrimes#include <fcntl.h> 3921030Sphk#include <stdio.h> 4014941Sgibbs#include <unistd.h> 4121030Sphk#include <strings.h> 4254833Sphantom#include <stdlib.h> 43251887Slstewart#include <sysexits.h> 44118904Skan 45251887Slstewart#include <cflib.h> 4614941Sgibbs 4761499Sjake#include <netsmb/smb_lib.h> 4814941Sgibbs#include <netsmb/smb_conn.h> 4914941Sgibbs 5014941Sgibbs#include "common.h" 5121030Sphk 52192926Sedint 5314941Sgibbscmd_print(int argc, char *argv[]) 5414941Sgibbs{ 55221843Smdf struct smb_ctx sctx, *ctx = &sctx; 5694939Stmm smbfh fh; 5754833Sphantom off_t offset; 5814941Sgibbs char buf[8192]; 5954833Sphantom char *filename; 6054833Sphantom char fnamebuf[256]; 61251887Slstewart int error, opt, i, file, count; 62118904Skan 63251887Slstewart if (argc < 2) 6414941Sgibbs view_usage(); 6561499Sjake if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_PRINTER) != 0) 6614941Sgibbs exit(1); 6714941Sgibbs if (smb_ctx_readrc(ctx) != 0) 6814941Sgibbs exit(1); 6914941Sgibbs if (smb_rc) 7054833Sphantom rc_close(smb_rc); 7154833Sphantom while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { 72192926Sed switch(opt){ 7314941Sgibbs case STDPARAM_ARGS: 7414941Sgibbs error = smb_ctx_opt(ctx, opt, optarg); 75221843Smdf if (error) 7654833Sphantom exit(1); 771638Srgrimes break; 7854833Sphantom default: 7954833Sphantom view_usage(); 80251887Slstewart /*NOTREACHED*/ 81118879Sbmilekic } 82251887Slstewart } 831638Srgrimes if (optind + 1 >= argc) 8461499Sjake print_usage(); 851638Srgrimes filename = argv[optind + 1]; 861638Srgrimes 8713698Sgibbs if (strcmp(filename, "-") == 0) { 881638Srgrimes file = 0; /* stdin */ 8954833Sphantom filename = "stdin"; 90240422Sed } else { 911638Srgrimes file = open(filename, O_RDONLY, 0); 92221843Smdf if (file < 0) { 9394939Stmm smb_error("could not open file %s\n", errno, filename); 9415139Sphk exit(1); 951638Srgrimes } 9615139Sphk } 9754833Sphantom 98251887Slstewart if (smb_ctx_resolve(ctx) != 0) 99118904Skan exit(1); 100251887Slstewart error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE); 10159862Sarchie if (error) { 102251887Slstewart smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); 103118904Skan exit(1); 104251887Slstewart } 1051638Srgrimes snprintf(fnamebuf, sizeof(fnamebuf), "%s_%s_%s", ctx->ct_ssn.ioc_user, 10661499Sjake ctx->ct_ssn.ioc_srvname, filename); 1071638Srgrimes error = smb_smb_open_print_file(ctx, 0, 1, fnamebuf, &fh); 1081638Srgrimes if (error) { 10913698Sgibbs smb_error("could not open print job", error); 1101638Srgrimes exit(1); 1111638Srgrimes } 11215139Sphk offset = 0; 11315139Sphk error = 0; 11454833Sphantom for(;;) { 115221843Smdf count = read(file, buf, sizeof(buf)); 116221843Smdf if (count == 0) 11714941Sgibbs break; 11870469Sphk if (count < 0) { 1191638Srgrimes error = errno; 12084306Sru smb_error("error reading input file\n", error); 12122148Smpp break; 12221030Sphk } 12314941Sgibbs i = smb_write(ctx, fh, offset, count, buf); 12421030Sphk if (i < 0) { 12554833Sphantom error = errno; 126251887Slstewart smb_error("error writing spool file\n", error); 127118904Skan break; 128251887Slstewart } 12914941Sgibbs if (i != count) { 13061499Sjake smb_error("incomplete write to spool file\n", 0); 13114941Sgibbs error = EIO; 13214941Sgibbs break; 13314941Sgibbs } 13421030Sphk offset += count; 135192926Sed } 13614941Sgibbs close(file); 13714941Sgibbs error = smb_smb_close_print_file(ctx, fh); 138221843Smdf if (error) 13922148Smpp smb_error("an error while closing spool file\n", error); 14094939Stmm return error ? 1 : 0; 14154833Sphantom} 14214941Sgibbs 14354833Sphantom 14454833Sphantomvoid 145251887Slstewartprint_usage(void) 146118904Skan{ 147251887Slstewart printf( 14814941Sgibbs "usage: smbutil print [connection optinons] //user@server/share\n" 14961499Sjake ); 15014941Sgibbs exit(1); 15114941Sgibbs} 15214941Sgibbs 15314941Sgibbs