]> arthur.barton.de Git - netatalk.git/blob - bin/pap/papstatus.c
4536b6b7a2949c0a6ef6f6a5d46ecf3a78446a46
[netatalk.git] / bin / pap / papstatus.c
1 /*
2  * $Id: papstatus.c,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1990,1991 Regents of The University of Michigan.
5  * All Rights Reserved.
6  *
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.
16  *
17  *      Research Systems Unix Group
18  *      The University of Michigan
19  *      c/o Mike Clark
20  *      535 W. William Street
21  *      Ann Arbor, Michigan
22  *      +1-313-763-0525
23  *      netatalk@itd.umich.edu
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/uio.h>
33 #include <sys/file.h>
34 #include <netatalk/endian.h>
35 #include <netatalk/at.h>
36 #include <atalk/atp.h>
37 #include <atalk/pap.h>
38 #include <atalk/nbp.h>
39 #include <atalk/util.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #define _PATH_PAPRC     ".paprc"
46
47 /* Forward Declaration */
48 void getstatus(ATP atp, struct sockaddr_at *sat);
49
50 void usage( path )
51     char        *path;
52 {
53     char        *p;
54
55     if (( p = strrchr( path, '/' )) == NULL ) {
56         p = path;
57     } else {
58         p++;
59     }
60     fprintf( stderr,
61         "Usage:\t%s [ -A address ] [ -p printername ]\n", p );
62     exit( 1 );
63 }
64
65 char *
66 paprc()
67 {
68     static char s[ 32 + 1 + 32 + 1 + 32 ];
69     char        *name = NULL;
70     FILE        *f;
71
72     if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
73         return( NULL );
74     }
75     while ( fgets( s, sizeof( s ), f ) != NULL ) {
76         s[ strlen( s ) - 1 ] = '\0';    /* remove trailing newline */
77         if ( *s == '#' ) {
78             continue;
79         }
80         name = s;
81         break;
82     }
83     fclose( f );
84     return( name );
85 }
86
87 char                    *printer = NULL;
88
89 char                    cbuf[ 8 ];
90 struct nbpnve           nn;
91
92 int main( ac, av )
93     int         ac;
94     char        **av;
95 {
96     ATP                 atp;
97     int                 wait, c, err = 0;
98     char                *obj = NULL, *type = "LaserWriter", *zone = "*";
99     struct at_addr      addr;
100
101     extern char         *optarg;
102     extern int          optind;
103
104     memset(&addr, 0, sizeof(addr));
105     while (( c = getopt( ac, av, "p:s:A:" )) != EOF ) {
106         switch ( c ) {
107         case 'A':
108             if (!atalk_aton(optarg, &addr)) {
109               fprintf(stderr, "Bad address.\n");
110               exit(1);
111             }
112             break;
113         case 'p' :
114             printer = optarg;
115             break;
116
117         default :
118             fprintf( stderr, "Unknown option: '%c'\n", c );
119             err++;
120         }
121     }
122     if ( err ) {
123         usage( *av );
124     }
125     if ( printer == NULL && (( printer = paprc()) == NULL )) {
126         usage( *av );
127     }
128
129     /*
130      * Open connection.
131      */
132     if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
133         fprintf( stderr, "%s: Bad name\n", printer );
134         exit( 1 );
135     }
136     if ( obj == NULL ) {
137         fprintf( stderr, "%s: Bad name\n", printer );
138         exit( 1 );
139     }
140     if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
141         if ( errno != 0 ) {
142             perror( "nbp_lookup" );
143         } else {
144             fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
145         }
146         exit( 1 );
147     }
148
149     if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
150         perror( "atp_open" );
151         exit( 1 );
152     }
153
154     if ( optind == ac ) {
155         getstatus( atp, &nn.nn_sat );
156         exit( 0 );
157     }
158     if ( optind - ac > 1 ) {
159         usage( *av );
160     }
161     wait = atoi( av[ optind ] );
162     for (;;) {
163         getstatus( atp, &nn.nn_sat );
164         sleep( wait );
165     }
166
167     return 0;
168 }
169
170 void getstatus( atp, sat )
171     ATP                 atp;
172     struct sockaddr_at  *sat;
173 {
174     struct iovec        iov;
175     struct atp_block    atpb;
176     char                rbuf[ ATP_MAXDATA ];
177
178     cbuf[ 0 ] = 0;
179     cbuf[ 1 ] = PAP_SENDSTATUS;
180     cbuf[ 2 ] = cbuf[ 3 ] = 0;
181
182     atpb.atp_saddr = sat;
183     atpb.atp_sreqdata = cbuf;
184     atpb.atp_sreqdlen = 4;              /* bytes in SendStatus request */
185     atpb.atp_sreqto = 2;                /* retry timer */
186     atpb.atp_sreqtries = 5;             /* retry count */
187     if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
188         perror( "atp_sreq" );
189         exit( 1 );
190     }
191
192     iov.iov_base = rbuf;
193     iov.iov_len = sizeof( rbuf );
194     atpb.atp_rresiov = &iov;
195     atpb.atp_rresiovcnt = 1;
196     if ( atp_rresp( atp, &atpb ) < 0 ) {
197         perror( "atp_rresp" );
198         exit( 1 );
199     }
200
201     /* sanity */
202     if ( iov.iov_len < 8 ||
203             rbuf[ 1 ] != PAP_STATUS ) {
204         fprintf( stderr, "Bad response!\n" );
205         return; /* This is weird, since TIDs must match... */
206     }
207
208     printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
209 }