]> arthur.barton.de Git - netatalk.git/blob - bin/pap/papstatus.c
Added checks for config.h in bin/ code
[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 <string.h>
40 #include <errno.h>
41
42 #define _PATH_PAPRC     ".paprc"
43
44 usage( path )
45     char        *path;
46 {
47     char        *p;
48
49     if (( p = strrchr( path, '/' )) == NULL ) {
50         p = path;
51     } else {
52         p++;
53     }
54     fprintf( stderr,
55         "Usage:\t%s [ -A address ] [ -p printername ]\n", p );
56     exit( 1 );
57 }
58
59 char *
60 paprc()
61 {
62     static char s[ 32 + 1 + 32 + 1 + 32 ];
63     char        *name = NULL;
64     FILE        *f;
65
66     if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
67         return( NULL );
68     }
69     while ( fgets( s, sizeof( s ), f ) != NULL ) {
70         s[ strlen( s ) - 1 ] = '\0';    /* remove trailing newline */
71         if ( *s == '#' ) {
72             continue;
73         }
74         name = s;
75         break;
76     }
77     fclose( f );
78     return( name );
79 }
80
81 char                    *printer = NULL;
82
83 char                    cbuf[ 8 ];
84 struct nbpnve           nn;
85
86 main( ac, av )
87     int         ac;
88     char        **av;
89 {
90     ATP                 atp;
91     int                 wait, c, err = 0;
92     char                *obj = NULL, *type = "LaserWriter", *zone = "*";
93     struct at_addr      addr;
94
95     extern char         *optarg;
96     extern int          optind;
97
98     memset(&addr, 0, sizeof(addr));
99     while (( c = getopt( ac, av, "p:s:A:" )) != EOF ) {
100         switch ( c ) {
101         case 'A':
102             if (!atalk_aton(optarg, &addr)) {
103               fprintf(stderr, "Bad address.\n");
104               exit(1);
105             }
106             break;
107         case 'p' :
108             printer = optarg;
109             break;
110
111         default :
112             fprintf( stderr, "Unknown option: '%c'\n", c );
113             err++;
114         }
115     }
116     if ( err ) {
117         usage( *av );
118     }
119     if ( printer == NULL && (( printer = paprc()) == NULL )) {
120         usage( *av );
121     }
122
123     /*
124      * Open connection.
125      */
126     if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
127         fprintf( stderr, "%s: Bad name\n", printer );
128         exit( 1 );
129     }
130     if ( obj == NULL ) {
131         fprintf( stderr, "%s: Bad name\n", printer );
132         exit( 1 );
133     }
134     if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
135         if ( errno != 0 ) {
136             perror( "nbp_lookup" );
137         } else {
138             fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
139         }
140         exit( 1 );
141     }
142
143     if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
144         perror( "atp_open" );
145         exit( 1 );
146     }
147
148     if ( optind == ac ) {
149         getstatus( atp, &nn.nn_sat );
150         exit( 0 );
151     }
152     if ( optind - ac > 1 ) {
153         usage( *av );
154     }
155     wait = atoi( av[ optind ] );
156     for (;;) {
157         getstatus( atp, &nn.nn_sat );
158         sleep( wait );
159     }
160 }
161
162 getstatus( atp, sat )
163     ATP                 atp;
164     struct sockaddr_at  *sat;
165 {
166     struct iovec        iov;
167     struct atp_block    atpb;
168     char                rbuf[ ATP_MAXDATA ];
169
170     cbuf[ 0 ] = 0;
171     cbuf[ 1 ] = PAP_SENDSTATUS;
172     cbuf[ 2 ] = cbuf[ 3 ] = 0;
173
174     atpb.atp_saddr = sat;
175     atpb.atp_sreqdata = cbuf;
176     atpb.atp_sreqdlen = 4;              /* bytes in SendStatus request */
177     atpb.atp_sreqto = 2;                /* retry timer */
178     atpb.atp_sreqtries = 5;             /* retry count */
179     if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
180         perror( "atp_sreq" );
181         exit( 1 );
182     }
183
184     iov.iov_base = rbuf;
185     iov.iov_len = sizeof( rbuf );
186     atpb.atp_rresiov = &iov;
187     atpb.atp_rresiovcnt = 1;
188     if ( atp_rresp( atp, &atpb ) < 0 ) {
189         perror( "atp_rresp" );
190         exit( 1 );
191     }
192
193     /* sanity */
194     if ( iov.iov_len < 8 ||
195             rbuf[ 1 ] != PAP_STATUS ) {
196         fprintf( stderr, "Bad response!\n" );
197         return; /* This is weird, since TIDs must match... */
198     }
199
200     printf( "%.*s\n", iov.iov_len - 9, (char *) iov.iov_base + 9 );
201 }