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