2 * $Id: papstatus.c,v 1.7 2009-10-14 01:38:28 didg Exp $
4 * Copyright (c) 1990,1991 Regents of The University of Michigan.
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appears in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the name of The University
12 * of Michigan not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. This software is supplied as is without expressed or
15 * implied warranties of any kind.
17 * Research Systems Unix Group
18 * The University of Michigan
20 * 535 W. William Street
23 * netatalk@itd.umich.edu
28 #endif /* HAVE_CONFIG_H */
30 #include <sys/types.h>
34 #include <netatalk/endian.h>
35 #include <netatalk/at.h>
37 #include <atalk/atp.h>
38 #include <atalk/pap.h>
39 #include <atalk/nbp.h>
40 #include <atalk/util.h>
45 #define _PATH_PAPRC ".paprc"
47 /* Forward Declaration */
48 static void getstatus(ATP atp, struct sockaddr_at *sat);
50 static void usage(char *path)
54 if (( p = strrchr( path, '/' )) == NULL ) {
60 "Usage:\t%s [ -A address ] [ -p printername ]\n", p );
67 static char s[ 32 + 1 + 32 + 1 + 32 ];
71 if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
74 while ( fgets( s, sizeof( s ), f ) != NULL ) {
75 s[ strlen( s ) - 1 ] = '\0'; /* remove trailing newline */
86 static char *printer = NULL;
88 static char cbuf[ 8 ];
89 static struct nbpnve nn;
91 int main( int ac, char **av)
95 char *obj = NULL, *type = "LaserWriter", *zone = "*";
101 memset(&addr, 0, sizeof(addr));
102 while (( c = getopt( ac, av, "p:s:A:" )) != EOF ) {
105 if (!atalk_aton(optarg, &addr)) {
106 fprintf(stderr, "Bad address.\n");
115 fprintf( stderr, "Unknown option: '%c'\n", c );
122 if ( printer == NULL && (( printer = paprc()) == NULL )) {
129 if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
130 fprintf( stderr, "%s: Bad name\n", printer );
134 fprintf( stderr, "%s: Bad name\n", printer );
137 if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
139 perror( "nbp_lookup" );
141 fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
146 if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
147 perror( "atp_open" );
151 if ( optind == ac ) {
152 getstatus( atp, &nn.nn_sat );
155 if ( optind - ac > 1 ) {
158 wait = atoi( av[ optind ] );
160 getstatus( atp, &nn.nn_sat );
167 static void getstatus(ATP atp, struct sockaddr_at *sat)
170 struct atp_block atpb;
171 char rbuf[ ATP_MAXDATA ];
174 cbuf[ 1 ] = PAP_SENDSTATUS;
175 cbuf[ 2 ] = cbuf[ 3 ] = 0;
177 atpb.atp_saddr = sat;
178 atpb.atp_sreqdata = cbuf;
179 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
180 atpb.atp_sreqto = 2; /* retry timer */
181 atpb.atp_sreqtries = 5; /* retry count */
182 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
183 perror( "atp_sreq" );
188 iov.iov_len = sizeof( rbuf );
189 atpb.atp_rresiov = &iov;
190 atpb.atp_rresiovcnt = 1;
191 if ( atp_rresp( atp, &atpb ) < 0 ) {
192 perror( "atp_rresp" );
197 if ( iov.iov_len < 8 ||
198 rbuf[ 1 ] != PAP_STATUS ) {
199 fprintf( stderr, "Bad response!\n" );
200 return; /* This is weird, since TIDs must match... */
203 printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );