summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/uustat/uustat.c
blob: 9ca16bbab68d173c8abe448a1dc9d0de2e048e0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
/* uustat.c
   UUCP status program

   Copyright (C) 1991, 1992, 1993, 1994, 1995 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#if USE_RCS_ID
const char uustat_rcsid[] = "$FreeBSD$";
#endif

#include <ctype.h>
#include <errno.h>

#if TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif

#include "getopt.h"

#include "uudefs.h"
#include "uuconf.h"
#include "system.h"

/* The uustat program permits various listings and manipulations of
   files in the spool directory.  This implementation supports the
   following switches:

   -a list all jobs
   -Blines number of lines of standard input to mail
   -ccommand list only executions of specified command
   -Ccommand list only jobs other than executions of specified command
   -e list execute jobs rather than command requests
   -i ask user whether to kill each listed job
   -Ifile set configuration file name
   -kjobid kill job with specified ID
   -K kill each listed job
   -m report status for all remote machines
   -M mail uucp about each job killed with -K
   -N mail requestor about each job killed with -K
   -ohour report jobs older than specified number of hours
   -p do "ps -flp" on all processes holding lock files (Unix specific)
   -q list number of jobs for all systems
   -Q don't list jobs, just do -K processing
   -rjobid rejuvenate job with specified ID
   -ssystem report on all jobs for specified system
   -Ssystem report on all jobs other than for specified system
   -uuser report on all jobs for specified user
   -Uuser report on all jobs other than for specified user
   -Wcomment comment to include in mail messages
   -xdebug set debugging level
   -yhour report jobs younger than specified number of hours  */

/* What to do with a job that matches the selection criteria; these
   values may be or'red together.  */
#define JOB_SHOW (01)
#define JOB_INQUIRE (02)
#define JOB_KILL (04)
#define JOB_REJUVENATE (010)
#define JOB_MAIL (020)
#define JOB_NOTIFY (040)

/* This structure is used to accumulate all the lines in a single
   command file, so that they can all be displayed at once and so that
   executions can be displayed reasonably.  */

struct scmdlist
{
  struct scmdlist *qnext;
  struct scmd s;
  long itime;
};

/* Local functions.  */

static void ususage P((void));
static void ushelp P((void));
static boolean fsxqt_file_read P((pointer puuconf, FILE *));
static void usxqt_file_free P((void));
static int isxqt_cmd P((pointer puuconf, int argc, char **argv, pointer pvar,
			pointer pinfo));
static int isxqt_file P((pointer puuconf, int argc, char **argv, pointer pvar,
			 pointer pinfo));
static int isxqt_user P((pointer puuconf, int argc, char **argv, pointer pvar,
			 pointer pinfo));
static boolean fsworkfiles P((pointer puuconf, int icmd, int csystems,
			      char **pazsystems, boolean fnotsystems,
			      int cusers, char **pazusers,
			      boolean fnotusers, long iold, long iyoung,
			      int ccommands, char **pazcommands,
			      boolean fnotcommands, const char *zcomment,
			      int cstdin));
static boolean fsworkfiles_system P((pointer puuconf,int icmd,
				     const struct uuconf_system *qsys,
				     int cusers,  char **pazusers,
				     boolean fnotusers, long iold,
				     long iyoung, int ccommands,
				     char **pazcommands,
				     boolean fnotcommands,
				     const char *zcomment, int cstdin));
static boolean fsworkfile_show P((pointer puuconf, int icmd,
				  const struct uuconf_system *qsys,
				  const struct scmd *qcmd,
				  long itime, int ccommands,
				  char **pazcommands, boolean fnotcommands,
				  const char *zcomment, int cstdin));
static void usworkfile_header P((const struct uuconf_system *qsys,
				 const struct scmd *qcmd,
				 const char *zjobid,
				 long itime, boolean ffirst));
static boolean fsexecutions P((pointer puuconf, int icmd, int csystems,
			       char **pazsystems, boolean fnotsystems,
			       int cusers, char **pazusers,
			       boolean fnotusers, long iold, long iyoung,
			       int ccommands, char **pazcommands,
			       boolean fnotcommands, const char *zcomment,
			       int cstdin));
static boolean fsnotify P((pointer puuconf, int icmd, const char *zcomment,
			   int cstdin, boolean fkilled, const char *zcmd,
			   struct scmdlist *qcmd, const char *zid,
			   long itime, const char *zuser,
			   const struct uuconf_system *qsys,
			   const char *zstdin, pointer pstdinseq,
			   const char *zrequestor));
static boolean fsquery P((pointer puuconf, int csystems,
			  char **pazsystems, boolean fnotsystems,
			  long iold, long iyoung));
static int csunits_show P((long idiff));
static boolean fsmachines P((void));

/* Long getopt options.  */
static const struct option asSlongopts[] =
{
  { "all", no_argument, NULL, 'a' },
  { "mail-lines", required_argument, NULL, 'B' },
  { "command", required_argument, NULL, 'c' },
  { "not-command", required_argument, NULL, 'C' },
  { "executions", no_argument, NULL, 'e' },
  { "prompt", no_argument, NULL, 'i' },
  { "kill", required_argument, NULL, 'k' },
  { "kill-all", no_argument, NULL, 'K' },
  { "status", no_argument, NULL, 'm' },
  { "mail", no_argument, NULL, 'M' },
  { "notify", no_argument, NULL, 'N' },
  { "older-than", required_argument, NULL, 'o' },
  { "ps", no_argument, NULL, 'p' },
  { "list", no_argument, NULL, 'q' },
  { "no-list", no_argument, NULL, 'Q' },
  { "rejuvenate", required_argument, NULL, 'r' },
  { "rejuvenate-all", no_argument, NULL, 'R' },
  { "system", required_argument, NULL, 's' },
  { "not-system", required_argument, NULL, 'S' },
  { "user", required_argument, NULL, 'u' },
  { "not-user", required_argument, NULL, 'U' },
  { "comment", required_argument, NULL, 'W' },
  { "younger-than", required_argument, NULL, 'y' },
  { "config", required_argument, NULL, 'I' },
  { "debug", required_argument, NULL, 'x' },
  { "version", no_argument, NULL, 'v' },
  { "help", no_argument, NULL, 1 },
  { NULL, 0, NULL, 0 }
};

int
main (argc, argv)
     int argc;
     char **argv;
{
  /* -a: list all jobs.  */
  boolean fall = FALSE;
  /* -B lines: number of lines of standard input to mail.  */
  int cstdin = 100;
  /* -c,-C command: list only specified command.  */
  int ccommands = 0;
  char **pazcommands = NULL;
  boolean fnotcommands = FALSE;
  /* -e: list execute jobs.  */
  boolean fexecute = FALSE;
  /* -k jobid: kill specified job.  */
  int ckills = 0;
  char **pazkills = NULL;
  /* -m: report machine status.  */
  boolean fmachine = FALSE;
  /* -o hour: report jobs older than given number of hours.  */
  int ioldhours = -1;
  /* -p: report status of jobs holding lock files.  */
  boolean fps = FALSE;
  /* -q: list number of jobs for each system.  */
  boolean fquery = FALSE;
  /* -r jobid: rejuvenate specified job.  */
  int crejuvs = 0;
  char **pazrejuvs = NULL;
  /* -s,-S system: list all jobs for specified system.  */
  int csystems = 0;
  char **pazsystems = NULL;
  boolean fnotsystems = FALSE;
  /* -u,-U user: list all jobs for specified user.  */
  int cusers = 0;
  char **pazusers = NULL;
  boolean fnotusers = FALSE;
  /* -W comment: comment to include in mail messages.  */
  const char *zcomment = NULL;
  /* -y hour: report jobs younger than given number of hours.  */
  int iyounghours = -1;
  /* -I file: set configuration file.  */
  const char *zconfig = NULL;
  /* -Q, -i, -K, -M, -N: what to do with each job.  */
  int icmd = JOB_SHOW;
  int ccmds;
  int iopt;
  pointer puuconf;
  int iuuconf;
  long iold;
  long iyoung;
  const char *azoneuser[1];
  boolean fret;

  zProgram = argv[0];

  while ((iopt = getopt_long (argc, argv,
			      "aB:c:C:eiI:k:KmMNo:pqQr:Rs:S:u:U:vW:x:y:",
			      asSlongopts, (int *) NULL)) != EOF)
    {
      switch (iopt)
	{
	case 'a':
	  /* List all jobs.  */
	  fall = TRUE;
	  break;

	case 'B':
	  /* Number of lines of standard input to mail.  */
	  cstdin = (int) strtol (optarg, (char **) NULL, 10);
	  break;

	case 'C':
	  /* List jobs for other than specified command.  */
	  fnotcommands = TRUE;
	  /* Fall through.  */
	case 'c':
	  /* List specified command.  */
	  ++ccommands;
	  pazcommands = (char **) xrealloc ((pointer) pazcommands,
					    ccommands * sizeof (char *));
	  pazcommands[ccommands - 1] = optarg;
	  break;

	case 'e':
	  /* List execute jobs.  */
	  fexecute = TRUE;
	  break;

	case 'i':
	  /* Prompt the user whether to kill each job.  */
	  icmd |= JOB_INQUIRE;
	  break;

	case 'I':
	  /* Set configuration file name.  */
	  if (fsysdep_other_config (optarg))
	    zconfig = optarg;
	  break;

	case 'k':
	  /* Kill specified job.  */
	  ++ckills;
	  pazkills = (char **) xrealloc ((pointer) pazkills,
					 ckills * sizeof (char *));
	  pazkills[ckills - 1] = optarg;
	  break;

	case 'K':
	  /* Kill each listed job.  */
	  icmd |= JOB_KILL;
	  break;

	case 'm':
	  /* Report machine status.  */
	  fmachine = TRUE;
	  break;

	case 'M':
	  /* Mail to uucp action taken on each job.  */
	  icmd |= JOB_MAIL;
	  break;

	case 'N':
	  /*  Mail to requestor action taken on each job.  */
	  icmd |= JOB_NOTIFY;
	  break;

	case 'o':
	  /* Report old jobs.  */
	  ioldhours = (int) strtol (optarg, (char **) NULL, 10);
	  break;

	case 'p':
	  /* Get status of processes holding locks.  */
	  fps = TRUE;
	  break;

	case 'q':
	  /* List number of jobs for each system.  */
	  fquery = TRUE;
	  break;

	case 'Q':
	  /* Don't list jobs, just do -K processing.  */
	  icmd &=~ JOB_SHOW;
	  break;

	case 'r':
	  /* Rejuvenate specified job.  */
	  ++crejuvs;
	  pazrejuvs = (char **) xrealloc ((pointer) pazrejuvs,
					  crejuvs * sizeof (char *));
	  pazrejuvs[crejuvs - 1] = optarg;
	  break;

	case 'R':
	  /* Rejuvenate each listed job.  */
	  icmd |= JOB_REJUVENATE;
	  break;

	case 'S':
	  /* List jobs for other than specified system.  */
	  fnotsystems = TRUE;
	  /* Fall through.  */
	case 's':
	  /* List jobs for specified system.  */
	  ++csystems;
	  pazsystems = (char **) xrealloc ((pointer) pazsystems,
					   csystems * sizeof (char *));
	  pazsystems[csystems - 1] = optarg;
	  break;

	case 'U':
	  /* List jobs for other than specified user.  */
	  fnotusers = TRUE;
	  /* Fall through.  */
	case 'u':
	  /* List jobs for specified user.  */
	  ++cusers;
	  pazusers = (char **) xrealloc ((pointer) pazusers,
					 cusers * sizeof (char *));
	  pazusers[cusers - 1] = optarg;
	  break;

	case 'W':
	  /* Comment to include in mail messages.  */
	  zcomment = optarg;
	  break;

	case 'x':
#if DEBUG > 1
	  /* Set debugging level.  */
	  iDebug |= idebug_parse (optarg);
#endif
	  break;

	case 'y':
	  /* List jobs younger than given number of hours.  */
	  iyounghours = (int) strtol (optarg, (char **) NULL, 10);
	  break;

	case 'v':
	  /* Print version and exit.  */
	  printf ("%s: Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
		  zProgram, VERSION);
	  exit (EXIT_SUCCESS);
	  /*NOTREACHED*/

	case 1:
	  /* --help.  */
	  ushelp ();
	  exit (EXIT_SUCCESS);
	  /*NOTREACHED*/

	case 0:
	  /* Long option found and flag set.  */
	  break;

	default:
	  ususage ();
	  /*NOTREACHED*/
	}
    }

  if (optind != argc)
    ususage ();

  /* To avoid confusion, most options are only permitted by
     themselves.  This restriction might be removed later, but it is
     imposed by most implementations.  We do permit any combination of
     -c, -s, -u, -o and -y, and any combination of -k and -r.  */
  ccmds = 0;
  if (fall)
    ++ccmds;
  if (ckills > 0 || crejuvs > 0)
    ++ccmds;
  if (fmachine)
    ++ccmds;
  if (fps)
    ++ccmds;
  if (fexecute || fquery || csystems > 0 || cusers > 0 || ioldhours != -1
      || iyounghours != -1 || ccommands > 0)
    ++ccmds;
  if (fexecute && fquery)
    ++ccmds;

  if (ccmds > 1)
    {
      fprintf (stderr, "%s: too many options\n", zProgram);
      ususage ();
    }

  if ((icmd & JOB_KILL) != 0
      && (icmd & JOB_REJUVENATE) != 0)
    {
      fprintf (stderr, "%s: can not both rejuvenate and kill jobs\n",
	       zProgram);
      ususage ();
    }

  iuuconf = uuconf_init (&puuconf, (const char *) NULL, zconfig);
  if (iuuconf != UUCONF_SUCCESS)
    ulog_uuconf (LOG_FATAL, puuconf, iuuconf);

#if DEBUG > 1
  {
    const char *zdebug;

    iuuconf = uuconf_debuglevel (puuconf, &zdebug);
    if (iuuconf != UUCONF_SUCCESS)
      ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
    if (zdebug != NULL)
      iDebug |= idebug_parse (zdebug);
  }
#endif

  usysdep_initialize (puuconf, INIT_SUID);

  /* If no commands were specified, we list all commands for the given
     user.  */
  if (ccmds == 0)
    {
      cusers = 1;
      azoneuser[0] = zsysdep_login_name ();
      pazusers = (char **) azoneuser;
    }

  /* Canonicalize the system names.  */
  if (csystems > 0)
    {
      int i;

      for (i = 0; i < csystems; i++)
	{
	  struct uuconf_system ssys;

	  iuuconf = uuconf_system_info (puuconf, pazsystems[i], &ssys);
	  if (iuuconf != UUCONF_SUCCESS)
	    {
	      if (iuuconf == UUCONF_NOT_FOUND)
		ulog (LOG_FATAL, "%s: System not found", pazsystems[i]);
	      else
		ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
	    }
	  if (strcmp (pazsystems[i], ssys.uuconf_zname) != 0)
	    pazsystems[i] = zbufcpy (ssys.uuconf_zname);
	  (void) uuconf_system_free (puuconf, &ssys);
	}
    }

  if (ioldhours == -1)
    iold = (long) -1;
  else
    {
      iold = (ixsysdep_time ((long *) NULL)
	      - (long) ioldhours * (long) 60 * (long) 60);
      if (iold < 0L)
	iold = 0L;
    }
  if (iyounghours == -1)
    iyoung = (long) -1;
  else
    {
      iyoung = (ixsysdep_time ((long *) NULL)
		- (long) iyounghours * (long) 60 * (long) 60);
      if (iyoung < 0L)
	iyoung = 0L;
    }

  if (! fexecute
      && ! fquery
      && (fall
	  || csystems > 0
	  || cusers > 0
	  || ioldhours != -1
	  || iyounghours != -1
	  || ccommands > 0))
    fret = fsworkfiles (puuconf, icmd, csystems, pazsystems, fnotsystems,
			cusers, pazusers, fnotusers, iold,  iyoung,
			ccommands, pazcommands, fnotcommands, zcomment,
			cstdin);
  else if (fexecute)
    fret = fsexecutions (puuconf, icmd, csystems, pazsystems, fnotsystems,
			 cusers, pazusers, fnotusers, iold, iyoung,
			 ccommands, pazcommands, fnotcommands, zcomment,
			 cstdin);
  else if (icmd != JOB_SHOW)
    {
      ulog (LOG_ERROR,
	    "-i, -K, -M, -N, -Q, -R not supported with -k, -m, -p, -q, -r");
      ususage ();
      fret = FALSE;
    }
  else if (fquery)
    {
      if (cusers > 0 || ccommands > 0)
	{
	  ulog (LOG_ERROR, "-u, -c not supported with -q");
	  ususage ();
	  fret = FALSE;
	}
      else
	fret = fsquery (puuconf, csystems, pazsystems, fnotsystems,
			iold, iyoung);
    }
  else if (fmachine)
    fret = fsmachines ();
  else if (ckills > 0 || crejuvs > 0)
    {
      int i;

      fret = TRUE;
      for (i = 0; i < ckills; i++)
	if (! fsysdep_kill_job (puuconf, pazkills[i]))
	  fret = FALSE;

      for (i = 0; i < crejuvs; i++)
	if (! fsysdep_rejuvenate_job (puuconf, pazrejuvs[i]))
	  fret = FALSE;
    }
  else if (fps)
    fret = fsysdep_lock_status ();
  else
    {
#if DEBUG > 0
      ulog (LOG_FATAL, "Can't happen");
#endif
      fret = FALSE;
    }

  ulog_close ();

  usysdep_exit (fret);

  /* Avoid errors about not returning a value.  */
  return 0;
}

/* Print a usage message and die.  */

static void
ususage ()
{
  fprintf (stderr, "Usage: %s [options]\n", zProgram);
  fprintf (stderr, "Use %s --help for help\n", zProgram);
  exit (EXIT_FAILURE);
}

/* Print a help message.  */

static void
ushelp ()
{
  printf ("Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
	  VERSION);
  printf ("Usage: %s [options]\n", zProgram);
  printf (" -a,--all: list all UUCP jobs\n");
  printf (" -B,--mail-lines num: number of lines to return in -M or -N mail message\n");
  printf (" -c,--command command: list requests for named command\n");
  printf (" -C,--not-command command: list requests for other than named command\n");
  printf (" -e,--executions: list queued executions rather than job requests\n");
  printf (" -i,--prompt: prompt for whether to kill each listed job\n");
  printf (" -k,--kill job: kill specified UUCP job\n");
  printf (" -K,--kill-all: kill each listed job\n");
  printf (" -m,--status: report status for all remote machines\n");
  printf (" -M,--mail: mail report on each listed job to UUCP administrator\n");
  printf (" -N,--notify: mail report on each listed job to requestor\n");
  printf (" -o,--older-than hours: list all jobs older than given number of hours\n");
  printf (" -p,--ps: show status of all processes holding UUCP locks\n");
  printf (" -q,--list: list number of jobs for each system\n");
  printf (" -Q,--no-list: don't list jobs, just take actions (-i, -K, -M, -N)\n");
  printf (" -r,--rejuvenate job: rejuvenate specified UUCP job\n");
  printf (" -R,--rejuvenate-all: rejuvenate each listed job\n");
  printf (" -s,--system system: list all jobs for specified system\n");
  printf (" -S,--not-system system: list all jobs for other than specified system\n");
  printf (" -u,--user user: list all jobs for specified user\n");
  printf (" -U,--not-user user: list all jobs for other than specified user\n");
  printf (" -W,--comment comment: comment to include in mail messages\n");
  printf (" -y,--younger-than hours: list all jobs younger than given number of hours\n");
  printf (" -x,--debug debug: Set debugging level\n");
#if HAVE_TAYLOR_CONFIG
  printf (" -I,--config file: Set configuration file to use\n");
#endif /* HAVE_TAYLOR_CONFIG */
  printf (" -v,--version: Print version and exit\n");
  printf (" --help: Print help and exit\n");
}

/* We need to be able to read information from an execution file.  */

/* The user name extracted from an execution file.  */
static char *zSxqt_user;

/* The system name from an execution file.  */
static char *zSxqt_system;

/* Address of requesting user (who to send mail to).  */
static const char *zSxqt_requestor;

/* The command (no arguments) from an execution file.  */
static char *zSxqt_prog;

/* The full command line from an execution file.  */
static char *zSxqt_cmd;

/* Number of files associated with an execution file.  */
static int cSxqt_files;

/* Names of files associated with execution file.  */
static char **pazSxqt_files;

/* Standard input file name.  */
static const char *zSxqt_stdin;

/* A command table used to dispatch an execution file.  */
static const struct uuconf_cmdtab asSxqt_cmds[] =
{
  { "C", UUCONF_CMDTABTYPE_FN | 0, NULL, isxqt_cmd },
  { "I", UUCONF_CMDTABTYPE_STRING, (pointer) &zSxqt_stdin, NULL },
  { "F", UUCONF_CMDTABTYPE_FN | 0, NULL, isxqt_file },
  { "R", UUCONF_CMDTABTYPE_STRING, (pointer) &zSxqt_requestor, NULL },
  { "U", UUCONF_CMDTABTYPE_FN | 3, NULL, isxqt_user },
  { NULL, 0, NULL, NULL }
};

/* Read an execution file, setting the above variables.  */

static boolean
fsxqt_file_read (puuconf, e)
     pointer puuconf;
     FILE *e;
{
  int iuuconf;
  boolean fret;

  zSxqt_user = NULL;
  zSxqt_system = NULL;
  zSxqt_stdin = NULL;
  zSxqt_requestor = NULL;
  zSxqt_prog = NULL;
  zSxqt_cmd = NULL;
  cSxqt_files = 0;
  pazSxqt_files = NULL;

  iuuconf = uuconf_cmd_file (puuconf, e, asSxqt_cmds, (pointer) NULL,
			     (uuconf_cmdtabfn) NULL,
			     UUCONF_CMDTABFLAG_CASE, (pointer) NULL);
  if (iuuconf == UUCONF_SUCCESS)
    fret = TRUE;
  else
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      fret = FALSE;
    }

  if (zSxqt_user == NULL)
    zSxqt_user = zbufcpy ("*unknown*");
  if (zSxqt_system == NULL)
    zSxqt_system = zbufcpy ("*unknown*");
  if (zSxqt_prog == NULL)
    {
      zSxqt_prog = zbufcpy ("*none*");
      zSxqt_cmd = zbufcpy ("*none*");
    }

  return fret;
}

/* Free up the information read from an execution file.  */

static void
usxqt_file_free ()
{
  int i;

  ubuffree (zSxqt_user);
  zSxqt_user = NULL;
  ubuffree (zSxqt_system);
  zSxqt_system = NULL;
  ubuffree (zSxqt_prog);
  zSxqt_prog = NULL;
  ubuffree (zSxqt_cmd);
  zSxqt_cmd = NULL;
  for (i = 0; i < cSxqt_files; i++)
    ubuffree (pazSxqt_files[i]);
  cSxqt_files = 0;
  xfree ((pointer) pazSxqt_files);
  pazSxqt_files = NULL;
  zSxqt_stdin = NULL;
  zSxqt_requestor = NULL;
}

/* Get the command from an execution file.  */

/*ARGSUSED*/
static int
isxqt_cmd (puuconf, argc, argv, pvar, pinfo)
     pointer puuconf;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  size_t clen;
  int i;

  if (argc <= 1)
    return UUCONF_CMDTABRET_CONTINUE;

  zSxqt_prog = zbufcpy (argv[1]);

  clen = 0;
  for (i = 1; i < argc; i++)
    clen += strlen (argv[i]) + 1;

  zSxqt_cmd = zbufalc (clen);
  zSxqt_cmd[0] = '\0';
  for (i = 1; i < argc - 1; i++)
    {
      strcat (zSxqt_cmd, argv[i]);
      strcat (zSxqt_cmd, " ");
    }
  strcat (zSxqt_cmd, argv[i]);

  return UUCONF_CMDTABRET_CONTINUE;
}

/* Get the associated files from an execution file.  */

/*ARGSUSED*/
static int
isxqt_file (puuconf, argc, argv, pvar, pinfo)
     pointer puuconf;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  if (argc != 2 && argc != 3)
    return UUCONF_CMDTABRET_CONTINUE;

  /* If this file is not in the spool directory, just ignore it.  */
  if (! fspool_file (argv[1]))
    return UUCONF_CMDTABRET_CONTINUE;

  ++cSxqt_files;
  pazSxqt_files = (char **) xrealloc ((pointer) pazSxqt_files,
				      cSxqt_files * sizeof (char *));

  pazSxqt_files[cSxqt_files - 1] = zbufcpy (argv[1]);

  return UUCONF_CMDTABRET_CONTINUE;
}

/* Get the requesting user and system from an execution file.  */

/*ARGSUSED*/
static int
isxqt_user (puuconf, argc, argv, pvar, pinfo)
     pointer puuconf;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  zSxqt_user = zbufcpy (argv[1]);
  zSxqt_system = zbufcpy (argv[2]);
  return UUCONF_CMDTABRET_CONTINUE;
}

/* Handle various possible requests to look at work files.  */

static boolean
fsworkfiles (puuconf, icmd, csystems, pazsystems, fnotsystems, cusers,
	     pazusers, fnotusers, iold, iyoung, ccommands, pazcommands,
	     fnotcommands, zcomment, cstdin)
     pointer puuconf;
     int icmd;
     int csystems;
     char **pazsystems;
     boolean fnotsystems;
     int cusers;
     char **pazusers;
     boolean fnotusers;
     long iold;
     long iyoung;
     int ccommands;
     char **pazcommands;
     boolean fnotcommands;
     const char *zcomment;
     int cstdin;
{
  boolean fret;
  int i;
  int iuuconf;
  struct uuconf_system ssys;

  fret = TRUE;

  if (csystems > 0 && ! fnotsystems)
    {
      for (i = 0; i < csystems; i++)
	{
	  iuuconf = uuconf_system_info (puuconf, pazsystems[i], &ssys);
	  if (iuuconf != UUCONF_SUCCESS)
	    {
	      if (iuuconf == UUCONF_NOT_FOUND)
		ulog (LOG_ERROR, "%s: System not found", pazsystems[i]);
	      else
		ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	      fret = FALSE;
	      continue;
	    }

	  if (! fsworkfiles_system (puuconf, icmd, &ssys, cusers, pazusers,
				    fnotusers, iold, iyoung, ccommands,
				    pazcommands, fnotcommands, zcomment,
				    cstdin))
	    fret = FALSE;

	  (void) uuconf_system_free (puuconf, &ssys);
	}
    }
  else
    {
      char **pznames, **pz;

      iuuconf = uuconf_system_names (puuconf, &pznames, 0);
      if (iuuconf != UUCONF_SUCCESS)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  return FALSE;
	}
      
      for (pz = pznames; *pz != NULL; pz++)
	{
	  if (csystems > 0)
	    {
	      for (i = 0; i < csystems; i++)
		if (strcmp (*pz, pazsystems[i]) == 0)
		  break;
	      if (i < csystems)
		continue;
	    }

	  iuuconf = uuconf_system_info (puuconf, *pz, &ssys);
	  if (iuuconf != UUCONF_SUCCESS)
	    {
	      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	      fret = FALSE;
	      continue;
	    }

	  if (! fsworkfiles_system (puuconf, icmd, &ssys, cusers, pazusers,
				    fnotusers, iold, iyoung, ccommands,
				    pazcommands, fnotcommands, zcomment,
				    cstdin))
	    fret = FALSE;

	  (void) uuconf_system_free (puuconf, &ssys);
	  xfree ((pointer) *pz);
	}
      xfree ((pointer) pznames);
    }

  return fret;
}

/* Look at the work files for a particular system.  */

static boolean
fsworkfiles_system (puuconf, icmd, qsys, cusers, pazusers, fnotusers, iold,
		    iyoung, ccommands, pazcommands, fnotcommands, zcomment,
		    cstdin)
     pointer puuconf;
     int icmd;
     const struct uuconf_system *qsys;
     int cusers;
     char **pazusers;
     boolean fnotusers;
     long iold;
     long iyoung;
     int ccommands;
     char **pazcommands;
     boolean fnotcommands;
     const char *zcomment;
     int cstdin;
{
  boolean fret;

  if (! fsysdep_get_work_init (qsys, UUCONF_GRADE_LOW))
    return FALSE;

  while (TRUE)
    {
      struct scmd s;
      long itime;

      if (! fsysdep_get_work (qsys, UUCONF_GRADE_LOW, &s))
	{
	  usysdep_get_work_free (qsys);
	  return FALSE;
	}
      if (s.bcmd == 'H')
	break;

      if (cusers > 0)
	{
	  boolean fmatch;
	  int i;

	  fmatch = fnotusers;
	  for (i = 0; i < cusers; i++)
	    {
	      if (s.zuser != NULL
		  && strcmp (pazusers[i], s.zuser) == 0)
		{
		  fmatch = ! fmatch;
		  break;
		}
	    }
	  if (! fmatch)
	    continue;
	}

      itime = ixsysdep_work_time (qsys, s.pseq);

      if (iold != (long) -1 && itime > iold)
	continue;

      if (iyoung != (long) -1 && itime < iyoung)
	continue;

      if (! fsworkfile_show (puuconf, icmd, qsys, &s, itime, ccommands,
			     pazcommands, fnotcommands, zcomment, cstdin))
	{
	  usysdep_get_work_free (qsys);
	  return FALSE;
	}
    }

  fret = fsworkfile_show (puuconf, icmd, qsys, (const struct scmd *) NULL,
			  0L, ccommands, pazcommands, fnotcommands, zcomment,
			  cstdin);

  usysdep_get_work_free (qsys);

  return fret;
}

/* Show a single workfile.  This is actually called once for each line
   in the workfile, so we accumulate the lines and show them all at
   once.  This lets us show an execution in a useful fashion.  */

static boolean
fsworkfile_show (puuconf, icmd, qsys, qcmd, itime, ccommands, pazcommands,
		 fnotcommands, zcomment, cstdin)
     pointer puuconf;
     int icmd;
     const struct uuconf_system *qsys;
     const struct scmd *qcmd;
     long itime;
     int ccommands;
     char **pazcommands;
     boolean fnotcommands;
     const char *zcomment;
     int cstdin;
{
  static struct scmdlist *qlist;
  static char *zlistid;
  char *zid;

  if (qcmd == NULL)
    zid = NULL;
  else
    {
      zid = zsysdep_jobid (qsys, qcmd->pseq);
      if (zid == NULL)
	return FALSE;
    }

  /* If this is the same jobid as the list, put it on the end.  */

  if (qcmd != NULL
      && qlist != NULL
      && strcmp (zlistid, zid) == 0)
    {
      struct scmdlist *qnew, **pq;

      ubuffree (zid);
      qnew = (struct scmdlist *) xmalloc (sizeof (struct scmdlist));
      qnew->qnext = NULL;
      qnew->s = *qcmd;
      qnew->itime = itime;
      for (pq = &qlist; *pq != NULL; pq = &(*pq)->qnext)
	;
      *pq = qnew;
      return TRUE;
    }

  /* Here we have found a different job ID, so we print the scmd
     structures that we have accumulated.  We look for the special
     case of an execution (an E command, or one of the destination
     files begins with X.).  We could be more clever about other
     situations as well.  */
  if (qlist != NULL)
    {
      boolean fmatch;
      const char *zprog, *zcmd, *zrequestor, *zstdin;
      char *zfree;
      struct scmdlist *qxqt;
      FILE *exqt = NULL;
      struct scmdlist *qfree;

      fmatch = FALSE;
      zprog = zcmd = zrequestor = zstdin = NULL;
      zfree = NULL;

      for (qxqt = qlist; qxqt != NULL; qxqt = qxqt->qnext)
	{
	  if (qxqt->s.bcmd == 'E')
	    break;
	  if (qxqt->s.bcmd == 'S'
	      && qxqt->s.zto[0] == 'X'
	      && qxqt->s.zto[1] == '.'
	      && fspool_file (qxqt->s.zfrom))
	    {
	      char *zxqt;

	      /* Open the file now, so that, if it does not exist, we
                 can still report sensibly (the qxqt == NULL case) on
                 any other files that may exist.  */

	      zxqt = zsysdep_spool_file_name (qsys, qxqt->s.zfrom,
					      qxqt->s.pseq);
	      if (zxqt == NULL)
		return FALSE;

	      exqt = fopen (zxqt, "r");

	      ubuffree (zxqt);

	      if (exqt != NULL)
		break;
	    }
	}

      if (qxqt == NULL)
	{
	  if (ccommands == 0
	      || (fnotcommands
		  && strcmp (pazcommands[0], "ALL") == 0))
	    {
	      /* Show all the lines in a regular work file.  */
	      fmatch = TRUE;

	      if ((icmd & JOB_SHOW) != 0)
		{
		  struct scmdlist *qshow;

		  for (qshow = qlist; qshow != NULL; qshow = qshow->qnext)
		    {
		      char *zfile;
		      long cbytes;

		      usworkfile_header (qsys, &qshow->s, zlistid,
					 qshow->itime, qshow == qlist);

		      switch (qshow->s.bcmd)
			{
			case 'S':
			  if (strchr (qshow->s.zoptions, 'C') != NULL
			      || fspool_file (qshow->s.zfrom))
			    zfile = zsysdep_spool_file_name (qsys,
							     qshow->s.ztemp,
							     qshow->s.pseq);
			  else
			    zfile = zbufcpy (qshow->s.zfrom);
			  if (zfile == NULL)
			    cbytes = -1;
			  else
			    cbytes = csysdep_size (zfile);
			  if (cbytes >= 0)
			    printf ("Sending %s (%ld bytes) to %s",
				    qshow->s.zfrom, cbytes, qshow->s.zto);
			  ubuffree (zfile);
			  break;
			case 'R':
			  printf ("Requesting %s to %s", qshow->s.zfrom,
				  qshow->s.zto);
			  break;
			case 'X':
			  printf ("Requesting %s to %s", qshow->s.zfrom,
				  qshow->s.zto);
			  break;
			case 'P':
			  printf ("(poll file)");
			  break;
#if DEBUG > 0
			default:
			  printf ("Bad line %d", qshow->s.bcmd);
			  break;
#endif
			}

		      printf ("\n");
		    }
		}
	    }
	}
      else
	{
	  long csize;
	  struct scmdlist *qsize;

	  /* Show the command for an execution file.  */
	  if (qxqt->s.bcmd == 'E')
	    {
	      zfree = zbufcpy (qxqt->s.zcmd);
	      zfree[strcspn (zfree, " \t")] = '\0';
	      zprog = zfree;
	      zcmd = qxqt->s.zcmd;
	      if (strchr (qxqt->s.zoptions, 'R') != NULL)
		zrequestor = qxqt->s.znotify;
	    }
	  else
	    {
	      if (! fsxqt_file_read (puuconf, exqt))
		{
		  (void) fclose (exqt);
		  return FALSE;
		}

	      (void) fclose (exqt);

	      zprog = zSxqt_prog;
	      zcmd = zSxqt_cmd;
	      zrequestor = zSxqt_requestor;
	    }

	  csize = 0L;
	  for (qsize = qlist; qsize != NULL; qsize = qsize->qnext)
	    {
	      if (qsize->s.bcmd == 'S' || qsize->s.bcmd == 'E')
		{
		  char *zfile;

		  if (strchr (qsize->s.zoptions, 'C') != NULL
		      || fspool_file (qsize->s.zfrom))
		    zfile = zsysdep_spool_file_name (qsys, qsize->s.ztemp,
						     qsize->s.pseq);
		  else
		    zfile = zbufcpy (qsize->s.zfrom);
		  if (zfile != NULL)
		    {
		      long cbytes;

		      cbytes = csysdep_size (zfile);
		      if (cbytes > 0)
			csize += cbytes;
		      ubuffree (zfile);
		    }
		}
	    }

	  if (ccommands == 0)
	    fmatch = TRUE;
	  else
	    {
	      int i;

	      fmatch = fnotcommands;
	      for (i = 0; i < ccommands; i++)
		{
		  if (strcmp (pazcommands[i], "ALL") == 0
		      || strcmp (pazcommands[i], zprog) == 0)
		    {
		      fmatch = ! fmatch;
		      break;
		    }
		}
	    }

	  /* To get the name of the standard input file on this system
	     we have to look through the list of file transfers to
	     find the right one on the remote system.  */
	  if (fmatch)
	    {
	      struct scmdlist *qstdin;

	      if (qxqt->s.bcmd == 'E')
		qstdin = qxqt;
	      else if (zSxqt_stdin != NULL)
		{
		  for (qstdin = qlist;
		       qstdin != NULL;
		       qstdin = qstdin->qnext)
		    if (qstdin->s.bcmd == 'S'
			&& strcmp (qstdin->s.zto, zSxqt_stdin) == 0)
		      break;
		}
	      else
		qstdin = NULL;

	      if (qstdin != NULL)
		{
		  if (strchr (qstdin->s.zoptions, 'C') != NULL
		      || fspool_file (qstdin->s.zfrom))
		    zstdin = qstdin->s.ztemp;
		  else
		    zstdin = qstdin->s.zfrom;
		}
	    }

	  if (fmatch && (icmd & JOB_SHOW) != 0)
	    {
	      usworkfile_header (qsys, &qxqt->s, zlistid, qxqt->itime,
				 TRUE);
	      printf ("Executing %s (sending %ld bytes)\n", zcmd, csize);
	    }
	}

      if (fmatch)
	{
	  boolean fkill_or_rejuv;

	  fkill_or_rejuv = FALSE;
	  if ((icmd & JOB_INQUIRE) != 0)
	    {
	      int b;

	      /* Ask stdin whether this job should be killed.  */
	      fprintf (stderr, "%s: %s %s? ",
		       zProgram,
		       (icmd & JOB_REJUVENATE) != 0 ? "Rejuvenate" : "Kill",
		       zlistid);
	      (void) fflush (stderr);
	      b = getchar ();
	      fkill_or_rejuv = b == 'y' || b == 'Y';
	      while (b != EOF && b != '\n')
		b = getchar ();
	    }
	  else if ((icmd & JOB_KILL) != 0
		   || (icmd & JOB_REJUVENATE) != 0)
	    fkill_or_rejuv = TRUE;
	      
	  if (fkill_or_rejuv
	      && (qlist->s.zuser == NULL
		  || strcmp (zsysdep_login_name (), qlist->s.zuser) != 0)
	      && ! fsysdep_privileged ())
	    ulog (LOG_ERROR, "%s: Not submitted by you", zlistid);
	  else
	    {
	      if ((icmd & (JOB_MAIL | JOB_NOTIFY)) != 0)
		{
		  if (! fsnotify (puuconf, icmd, zcomment, cstdin,
				  (fkill_or_rejuv &&
				   (icmd & JOB_REJUVENATE) == 0),
				  zcmd, qlist, zlistid, qlist->itime,
				  qlist->s.zuser, qsys, zstdin,
				  qlist->s.pseq, zrequestor))
		    return FALSE;
		}

	      if (fkill_or_rejuv)
		{
		  if ((icmd & JOB_REJUVENATE) == 0)
		    {
		      if (! fsysdep_kill_job (puuconf, zlistid))
			return FALSE;
		    }
		  else
		    {
		      if (! fsysdep_rejuvenate_job (puuconf, zlistid))
			return FALSE;
		    }
		}
	    }
	}

      if (qxqt != NULL)
	{
	  if (qxqt->s.bcmd == 'E')
	    ubuffree (zfree);
	  else
	    usxqt_file_free ();
	}

      /* Free up the list of entries.  */
      qfree = qlist;
      while (qfree != NULL)
	{
	  struct scmdlist *qnext;

	  qnext = qfree->qnext;
	  xfree ((pointer) qfree);
	  qfree = qnext;
	}

      ubuffree (zlistid);

      qlist = NULL;
      zlistid = NULL;
    }

  /* Start a new list with the entry we just got.  */
  if (qcmd != NULL)
    {
      qlist = (struct scmdlist *) xmalloc (sizeof (struct scmdlist));
      qlist->qnext = NULL;
      qlist->s = *qcmd;
      qlist->itime = itime;
      zlistid = zid;
    }

  return TRUE;
}

/* Show the header of the line describing a workfile.  */

static void
usworkfile_header (qsys, qcmd, zjobid, itime, ffirst)
     const struct uuconf_system *qsys;
     const struct scmd *qcmd;
     const char *zjobid;
     long itime;
     boolean ffirst;
{
  const char *zshowid;
  struct tm stime;

  if (ffirst)
    zshowid = zjobid;
  else
    zshowid = "-";

  printf ("%s %s %s ", zshowid, qsys->uuconf_zname,
	  qcmd->zuser != NULL ? qcmd->zuser : OWNER);

  usysdep_localtime (itime, &stime);
  printf ("%02d-%02d %02d:%02d ",
	  stime.tm_mon + 1, stime.tm_mday, stime.tm_hour, stime.tm_min);
}

/* List queued executions that have not been processed by uuxqt for
   one reason or another.  */

static boolean
fsexecutions (puuconf, icmd, csystems, pazsystems, fnotsystems, cusers,
	      pazusers, fnotusers, iold, iyoung, ccommands, pazcommands,
	      fnotcommands, zcomment, cstdin)
     pointer puuconf;
     int icmd;
     int csystems;
     char **pazsystems;
     boolean fnotsystems;
     int cusers;
     char **pazusers;
     boolean fnotusers;
     long iold;
     long iyoung;
     int ccommands;
     char **pazcommands;
     boolean fnotcommands;
     const char *zcomment;
     int cstdin;
{
  const char *zlocalname;
  int iuuconf;
  char *zfile;
  char *zsystem;
  boolean ferr;

  iuuconf = uuconf_localname (puuconf, &zlocalname);
  if (iuuconf == UUCONF_NOT_FOUND)
    {
      zlocalname = zsysdep_localname ();
      if (zlocalname == NULL)
	return FALSE;
    }
  else if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      return FALSE;
    }

  if (! fsysdep_get_xqt_init ((const char *) NULL))
    return FALSE;

  while ((zfile = zsysdep_get_xqt ((const char *) NULL, &zsystem, &ferr))
	 != NULL)
    {
      boolean fmatch;
      int i;
      long itime;
      FILE *e;

      if (csystems > 0)
	{
	  fmatch = fnotsystems;
	  for (i = 0; i < csystems; i++)
	    {
	      if (strcmp (pazsystems[i], zsystem) == 0)
		{
		  fmatch = ! fmatch;
		  break;
		}
	    }
	  if (! fmatch)
	    {
	      ubuffree (zfile);
	      ubuffree (zsystem);
	      continue;
	    }
	}

      itime = ixsysdep_file_time (zfile);

      if ((iold != (long) -1 && itime > iold)
	  || (iyoung != (long) -1 && itime < iyoung))
	{
	  ubuffree (zfile);
	  ubuffree (zsystem);
	  continue;
	}

      /* We need to read the execution file before we can check the
	 user name.  */
      e = fopen (zfile, "r");
      if (e == NULL)
	{
	  /* Probably uucico just deleted the file.  */
	  continue;
	}
      if (! fsxqt_file_read (puuconf, e))
	{
	  (void) fclose (e);
	  ubuffree (zfile);
	  ubuffree (zsystem);
	  continue;      
	}
      (void) fclose (e);

      if (cusers == 0)
	fmatch = TRUE;
      else
	{
	  fmatch = fnotusers;
	  for (i = 0; i < cusers; i++)
	    {
	      if (strcmp (zSxqt_user, pazusers[i]) == 0
		  || (zSxqt_requestor != NULL
		      && strcmp (zSxqt_requestor, pazusers[i]) == 0))
		{
		  fmatch = ! fmatch;
		  break;
		}
	    }
	}

      if (fmatch && ccommands > 0)
	{
	  fmatch = fnotcommands;
	  for (i = 0; i < ccommands; i++)
	    {
	      if (strcmp (pazcommands[i], "ALL") == 0
		  || strcmp (pazcommands[i], zSxqt_prog) == 0)
		{
		  fmatch = ! fmatch;
		  break;
		}
	    }
	}

      if (fmatch)
	{
	  boolean fbad, fkill_or_rejuv;
	  struct uuconf_system ssys;

	  fbad = FALSE;

	  if ((icmd & JOB_SHOW) != 0)
	    {
	      struct tm stime;

	      printf ("%s %s!", zsystem, zSxqt_system);
	      if (zSxqt_requestor != NULL)
		printf ("%s", zSxqt_requestor);
	      else
		printf ("%s", zSxqt_user);

	      usysdep_localtime (itime, &stime);
	      printf (" %02d-%02d %02d:%02d ",
		      stime.tm_mon + 1, stime.tm_mday, stime.tm_hour,
		      stime.tm_min);

	      printf ("%s\n", zSxqt_cmd);
	    }

	  fkill_or_rejuv = FALSE;
	  if ((icmd & JOB_INQUIRE) != 0)
	    {
	      int b;

	      /* Ask stdin whether this job should be killed.  */
	      fprintf (stderr, "%s: %s %s? ",
		       zProgram,
		       (icmd & JOB_REJUVENATE) != 0 ? "Rejuvenate" : "Kill",
		       zSxqt_cmd);
	      (void) fflush (stderr);
	      b = getchar ();
	      fkill_or_rejuv = b == 'y' || b == 'Y';
	      while (b != EOF && b != '\n')
		b = getchar ();
	    }
	  else if ((icmd & JOB_KILL) != 0
		   || (icmd & JOB_REJUVENATE) != 0)
	    fkill_or_rejuv = TRUE;

	  if (fkill_or_rejuv)
	    {
	      if ((strcmp (zSxqt_user, zsysdep_login_name ()) != 0
		   || strcmp (zsystem, zlocalname) != 0)
		  && ! fsysdep_privileged ())
		{
		  ulog (LOG_ERROR, "Job not submitted by you\n");
		  fbad = TRUE;
		}
	    }

	  if (! fbad)
	    {
	      iuuconf = uuconf_system_info (puuconf, zsystem, &ssys);
	      if (iuuconf != UUCONF_SUCCESS)
		{
		  if (iuuconf != UUCONF_NOT_FOUND)
		    {
		      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
		      fbad = TRUE;
		    }
		  else if (strcmp (zsystem, zlocalname) == 0)
		    {
		      iuuconf = uuconf_system_local (puuconf, &ssys);
		      if (iuuconf != UUCONF_SUCCESS)
			{
			  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
			  fbad = TRUE;
			}
		      ssys.uuconf_zname = (char *) zlocalname;
		    }
		  else if (! funknown_system (puuconf, zsystem, &ssys))
		    {
		      ulog (LOG_ERROR, "Job for unknown system %s",
			    zsystem);
		      fbad = TRUE;
		    }
		}
	    }

	  if (! fbad && (icmd & (JOB_MAIL | JOB_NOTIFY)) != 0)
	    {
	      if (! fsnotify (puuconf, icmd, zcomment, cstdin,
			      fkill_or_rejuv && (icmd & JOB_REJUVENATE) == 0,
			      zSxqt_cmd, (struct scmdlist *) NULL,
			      (const char *) NULL, itime, zSxqt_user, &ssys,
			      zSxqt_stdin, (pointer) NULL, zSxqt_requestor))
		{
		  ferr = TRUE;
		  usxqt_file_free ();
		  ubuffree (zfile);
		  ubuffree (zsystem);
		  break;
		}
	    }

	  if (! fbad && fkill_or_rejuv)
	    {
	      for (i = 0; i < cSxqt_files; i++)
		{
		  char *z;

		  z = zsysdep_spool_file_name (&ssys, pazSxqt_files[i],
					       (pointer) NULL);
		  if (z != NULL)
		    {
		      if ((icmd & JOB_REJUVENATE) != 0)
			(void) fsysdep_touch_file (z);
		      else
			(void) remove (z);
		      ubuffree (z);
		    }
		}
	      if ((icmd & JOB_REJUVENATE) != 0)
		(void) fsysdep_touch_file (zfile);
	      else
		{
		  if (remove (zfile) != 0)
		    ulog (LOG_ERROR, "remove (%s): %s", zfile,
			  strerror (errno));
		}
	    }

	  if (! fbad)
	    (void) uuconf_system_free (puuconf, &ssys);
	}

      usxqt_file_free ();
      ubuffree (zfile);
      ubuffree (zsystem);
    }

  usysdep_get_xqt_free ((const char *) NULL);

  return ferr;
}

/* When a job is killed, send mail to the appropriate people.  */

static boolean
fsnotify (puuconf, icmd, zcomment, cstdin, fkilled, zcmd, qcmd, zid, itime,
	  zuser, qsys, zstdin, pstdinseq, zrequestor)
     pointer puuconf;
     int icmd;
     const char *zcomment;
     int cstdin;
     boolean fkilled;
     const char *zcmd;
     struct scmdlist *qcmd;
     const char *zid;
     long itime;
     const char *zuser;
     const struct uuconf_system *qsys;
     const char *zstdin;
     pointer pstdinseq;
     const char *zrequestor;
{
  const char **pz;
  int cgot;
  int i, istdin;
  struct tm stime;
  char ab[sizeof "1991-12-31 12:00:00"];
  const char *zsubject;
  boolean fret;

  pz = (const char **) xmalloc (20 * sizeof (const char *));
  cgot = 20;

  i = 0;
  if (zid == NULL)
    pz[i++] = "A UUCP execution request";
  else
    {
      pz[i++] = "UUCP job\n\t";
      pz[i++] = zid;
      pz[i++] = "\nfor system\n\t";
      pz[i++] = qsys->uuconf_zname;
    }
  pz[i++] = "\nrequested by\n\t";
  pz[i++] = zuser != NULL ? zuser : OWNER;
  if (zid == NULL)
    {
      pz[i++] = "\non system\n\t";
      pz[i++] = qsys->uuconf_zname;
    }
  pz[i++] = "\n";

  if (fkilled)
    pz[i++] = "has been killed.\n";

  if (zcomment != NULL)
    {
      pz[i++] = zcomment;
      pz[i++] = "\n";
    }

  pz[i++] = "The job was queued at ";
  usysdep_localtime (itime, &stime);
  sprintf (ab, "%04d-%02d-%02d %02d:%02d:%02d",
	   stime.tm_year + 1900, stime.tm_mon + 1, stime.tm_mday,
	   stime.tm_hour, stime.tm_min, stime.tm_sec);
  pz[i++] = ab;
  pz[i++] = ".\nIt ";

  if (fkilled)
    pz[i++] = "was\n";
  else
    pz[i++] = "is\n";

  if (zcmd != NULL)
    {
      pz[i++] = "\t";
      pz[i++] = zcmd;
    }
  else
    {
      struct scmdlist *qshow;

      for (qshow = qcmd; qshow != NULL; qshow = qshow->qnext)
	{
	  if (i + 10 > cgot)
	    {
	      cgot += 20;
	      pz = (const char **) xrealloc ((pointer) pz,
					     cgot * sizeof (const char *));
	    }

	  switch (qshow->s.bcmd)
	    {
	    case 'S':
	      pz[i++] = "\tsend ";
	      break;
	    default:
	    case 'R':
	    case 'X':
	      pz[i++] = "\trequest ";
	      break;
	    case 'P':
	      pz[i++] = "\tpoll ";
	      break;
#if DEBUG > 0
	    case 'E':
	      ulog (LOG_FATAL, "fsnotify: Can't happen");
	      break;
#endif
	    }
	  if (qshow->s.zfrom != NULL && qshow->s.zto != NULL)
	    {
	      pz[i++] = qshow->s.zfrom;
	      pz[i++] = " to ";
	      pz[i++] = qshow->s.zto;
	    }
	}
    }

  istdin = i;
  if (cstdin > 0 && zstdin != NULL)
    {
      boolean fspool;
      char *zfile;
      FILE *e;

      fspool = fspool_file (zstdin);
      if (fspool)
	zfile = zsysdep_spool_file_name (qsys, zstdin, pstdinseq);
      else
	zfile = zsysdep_local_file (zstdin, qsys->uuconf_zpubdir,
				    (boolean *) NULL);

      if (zfile != NULL
	  && (fspool
	      || fin_directory_list (zfile, qsys->uuconf_pzremote_send,
				     qsys->uuconf_zpubdir, TRUE, TRUE,
				     (const char *) NULL)))
	{
	  e = fopen (zfile, "r");
	  if (e != NULL)
	    {
	      int clines, clen;
	      char *zline;
	      size_t cline;

	      pz[i++] = "\n";
	      istdin = i;

	      clines = 0;

	      zline = NULL;
	      cline = 0;
	      while ((clen = getline (&zline, &cline, e)) > 0)
		{
		  if (memchr (zline, '\0', (size_t) clen) != NULL)
		    {
		      int ifree;

		      /* A null character means this is probably a
			 binary file.  */
		      for (ifree = istdin; ifree < i; ifree++)
			ubuffree ((char *) pz[ifree]);
		      i = istdin - 1;
		      break;
		    }
		  ++clines;
		  if (clines > cstdin)
		    break;
		  if (i >= cgot)
		    {
		      cgot += 20;
		      pz = (const char **) xrealloc ((pointer) pz,
						     (cgot
						      * sizeof (char *)));
		    }
		  if (strncmp (zline, "From ", sizeof "From " - 1) != 0)
		    pz[i++] = zbufcpy (zline);
		  else
		    {
		      char *zalc;

		      /* Escape "From " at the start of a line.  This
			 should really be the responsibility of the
			 mail transfer agent.  On some systems,
			 though, the mail transfer agent does not do
			 it, but user mail programs expect it.  We
			 help them out here, since it doesn't matter
			 much--we're already truncating the message
			 anyhow.  */
		      zalc = zbufalc (strlen (zline) + 2);
		      zalc[0] = '>';
		      strcpy (zalc + 1, zline);
		      pz[i++] = zalc;
		    }
		}
	      xfree ((pointer) zline);
	      (void) fclose (e);
	    }
	}

      ubuffree (zfile);
    }

  if (fkilled)
    zsubject = "UUCP job killed";
  else
    zsubject = "UUCP notification";

  fret = TRUE;

  if ((icmd & JOB_MAIL) != 0)
    {
      if (! fsysdep_mail (OWNER, zsubject, i, pz))
	fret = FALSE;
    }

  if ((icmd & JOB_NOTIFY) != 0
      && (zrequestor != NULL || zuser != NULL))
    {
      const char *zmail;
      char *zfree;

      if (zrequestor != NULL)
	zmail = zrequestor;
      else
	zmail = zuser;

      zfree = NULL;

      if (zid == NULL)
	{
	  int iuuconf;
	  const char *zloc;

	  /* This is an execution request, which may be from another
	     system.  If it is, we must prepend that system name to
	     the user name extracted from the X. file.  */
	  iuuconf = uuconf_localname (puuconf, &zloc);
	  if (iuuconf == UUCONF_NOT_FOUND)
	    {
	      zloc = zsysdep_localname ();
	      if (zloc == NULL)
		return FALSE;
	    }
	  else if (iuuconf != UUCONF_SUCCESS)
	    ulog_uuconf (LOG_FATAL, puuconf, iuuconf);

	  if (strcmp (qsys->uuconf_zname, zloc) != 0
#if HAVE_INTERNET_MAIL
	      && strchr (zmail, '@') == NULL
#endif
	      )
	    {
	      zfree = zbufalc (strlen (qsys->uuconf_zname)
			       + strlen (zmail)
			       + sizeof "!");
	      sprintf (zfree, "%s!%s", qsys->uuconf_zname, zmail);
	      zmail = zfree;
	    }
	}

      if (! fsysdep_mail (zmail, zsubject, i, pz))
	fret = FALSE;

      ubuffree (zfree);
    }

  while (istdin < i)
    {
      ubuffree ((char *) pz[istdin]);
      istdin++;
    }

  xfree ((pointer) pz);

  return fret;
}

/* Handle the -q option.  For each remote system this lists the number
   of jobs queued, the number of executions queued, and the current
   call status.  We get the executions all at once, because they are
   not accessed by system.  They could be, but it is possible to have
   executions pending for an unknown system, so special handling would
   still be required.  */

struct sxqtlist
{
  struct sxqtlist *qnext;
  char *zsystem;
  int cxqts;
  long ifirst;
};

/* These local functions need the definition of sxqtlist for the
   prototype.  */

static boolean fsquery_system P((const struct uuconf_system *qsys,
				 struct sxqtlist **pq,
				 long inow, const char *zlocalname,
				 int csystems, char **pazsystems,
				 boolean fnotsystems, long iold, long iyoung));
static boolean fsquery_show P((const struct uuconf_system *qsys, int cwork,
			       long ifirstwork, struct sxqtlist *qxqt,
			       long inow, const char *zlocalname,
			       int csystems, char **pazsystems,
			       boolean fnotsystems, long iold, long iyoung));

static boolean
fsquery (puuconf, csystems, pazsystems, fnotsystems, iold, iyoung)
     pointer puuconf;
     int csystems;
     char **pazsystems;
     boolean fnotsystems;
     long iold;
     long iyoung;
{
  int iuuconf;
  const char *zlocalname;
  struct sxqtlist *qlist;
  char *zfile, *zsystem;
  boolean ferr;
  long inow;
  char **pznames, **pz;
  boolean fret;

  iuuconf = uuconf_localname (puuconf, &zlocalname);
  if (iuuconf == UUCONF_NOT_FOUND)
    {
      zlocalname = zsysdep_localname ();
      if (zlocalname == NULL)
	return FALSE;
    }
  else if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      return FALSE;
    }

  /* Get a count of all the execution files.  */
  if (! fsysdep_get_xqt_init ((const char *) NULL))
    return FALSE;

  qlist = NULL;
  while ((zfile = zsysdep_get_xqt ((const char *) NULL, &zsystem, &ferr))
	 != NULL)
    {
      struct sxqtlist *qlook;

      for (qlook = qlist; qlook != NULL; qlook = qlook->qnext)
	if (strcmp (zsystem, qlook->zsystem) == 0)
	  break;

      if (qlook != NULL)
	{
	  long itime;

	  ubuffree (zsystem);
	  ++qlook->cxqts;
	  itime = ixsysdep_file_time (zfile);
	  if (itime < qlook->ifirst)
	    qlook->ifirst = itime;
	}
      else
	{
	  struct sxqtlist *qnew;

	  qnew = (struct sxqtlist *) xmalloc (sizeof (struct sxqtlist));
	  qnew->qnext = qlist;
	  qnew->zsystem = zsystem;
	  qnew->cxqts = 1;
	  qnew->ifirst = ixsysdep_file_time (zfile);
	  qlist = qnew;
	}

      ubuffree (zfile);
    }

  usysdep_get_xqt_free ((const char *) NULL);

  if (ferr)
    return FALSE;

  inow = ixsysdep_time ((long *) NULL);

  /* Show the information for each system.  */
  iuuconf = uuconf_system_names (puuconf, &pznames, 0);
  if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      return FALSE;
    }

  fret = TRUE;

  for (pz = pznames; *pz != NULL; pz++)
    {
      struct uuconf_system ssys;

      iuuconf = uuconf_system_info (puuconf, *pz, &ssys);
      if (iuuconf != UUCONF_SUCCESS)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  fret = FALSE;
	  continue;
	}

      if (! fsquery_system (&ssys, &qlist, inow, zlocalname, csystems,
			    pazsystems, fnotsystems, iold, iyoung))
	fret = FALSE;

      (void) uuconf_system_free (puuconf, &ssys);
      xfree ((pointer) *pz);
    }

  /* Check for the local system in the list of execution files.  */
  if (qlist != NULL)
    {
      struct sxqtlist **pq;

      for (pq = &qlist; *pq != NULL; pq = &(*pq)->qnext)
	{
	  if (strcmp ((*pq)->zsystem, zlocalname) == 0)
	    {
	      struct uuconf_system ssys;
	      struct sxqtlist *qfree;

	      iuuconf = uuconf_system_info (puuconf, zlocalname, &ssys);
	      if (iuuconf != UUCONF_SUCCESS)
		{
		  if (iuuconf != UUCONF_NOT_FOUND)
		    {
		      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
		      fret = FALSE;
		      break;
		    }

		  iuuconf = uuconf_system_local (puuconf, &ssys);
		  if (iuuconf != UUCONF_SUCCESS)
		    {
		      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
		      fret = FALSE;
		      break;
		    }
		  ssys.uuconf_zname = (char *) zlocalname;
		}

	      if (! fsquery_show (&ssys, 0, 0L, *pq, inow, zlocalname,
				  csystems, pazsystems, fnotsystems,
				  iold, iyoung))
		fret = FALSE;
	      (void) uuconf_system_free (puuconf, &ssys);
	      qfree = *pq;
	      *pq = qfree->qnext;
	      ubuffree (qfree->zsystem);
	      xfree ((pointer) qfree);
	      break;
	    }
	}
    }

  /* Print out information for any unknown systems for which we have
     execution files.  */
  while (qlist != NULL)
    {
      struct uuconf_system ssys;
      struct sxqtlist *qnext;

      if (! funknown_system (puuconf, qlist->zsystem, &ssys))
	{
	  ulog (LOG_ERROR, "Executions queued up for unknown systems");
	  fret = FALSE;
	  break;
	}

      if (! fsquery_show (&ssys, 0, 0L, qlist, inow, zlocalname,
			  csystems, pazsystems, fnotsystems, iold, iyoung))
	fret = FALSE;
      (void) uuconf_system_free (puuconf, &ssys);
      qnext = qlist->qnext;
      ubuffree (qlist->zsystem);
      xfree ((pointer) qlist);
      qlist = qnext;
    }

  return fret;
}

/* Query a single known system.  */

static boolean
fsquery_system (qsys, pq, inow, zlocalname, csystems, pazsystems,
		fnotsystems, iold, iyoung)
     const struct uuconf_system *qsys;
     struct sxqtlist **pq;
     long inow;
     const char *zlocalname;
     int csystems;
     char **pazsystems;
     boolean fnotsystems;
     long iold;
     long iyoung;
{
  int cwork;
  long ifirstwork;
  char *zid;
  boolean fret;

  if (! fsysdep_get_work_init (qsys, UUCONF_GRADE_LOW))
    return FALSE;

  cwork = 0;
  ifirstwork = 0L;
  zid = NULL;
  while (TRUE)
    {
      struct scmd s;
      long itime;
      char *zthisid;

      if (! fsysdep_get_work (qsys, UUCONF_GRADE_LOW, &s))
	return FALSE;
      if (s.bcmd == 'H')
	break;

      zthisid = zsysdep_jobid (qsys, s.pseq);
      if (zid != NULL && strcmp (zid, zthisid) == 0)
	ubuffree (zthisid);
      else
	{
	  ++cwork;
	  ubuffree (zid);
	  zid = zthisid;
	}

      itime = ixsysdep_work_time (qsys, s.pseq);
      if (ifirstwork == 0L || ifirstwork > itime)
	ifirstwork = itime;
    }

  usysdep_get_work_free (qsys);
  ubuffree (zid);

  /* Find the execution information, if any.  */
  while (*pq != NULL)
    {
      if (strcmp ((*pq)->zsystem, qsys->uuconf_zname) == 0)
	break;
      pq = &(*pq)->qnext;
    }

  /* If there are no commands and no executions, don't print any
     information for this system.  */
  if (cwork == 0 && *pq == NULL)
    return TRUE;

  fret = fsquery_show (qsys, cwork, ifirstwork, *pq, inow,
		       zlocalname, csystems, pazsystems, fnotsystems,
		       iold, iyoung);

  if (*pq != NULL)
    {
      struct sxqtlist *qfree;

      qfree = *pq;
      *pq = qfree->qnext;
      ubuffree (qfree->zsystem);
      xfree ((pointer) qfree);
    }

  return fret;
}

/* Print out the query information for a single system.  We handle the
   local system specially.  */

static boolean
fsquery_show (qsys, cwork, ifirstwork, qxqt, inow, zlocalname,
	      csystems, pazsystems, fnotsystems, iold, iyoung)
     const struct uuconf_system *qsys;
     int cwork;
     long ifirstwork;
     struct sxqtlist *qxqt;
     long inow;
     const char *zlocalname;
     int csystems;
     char **pazsystems;
     boolean fnotsystems;
     long iold;
     long iyoung;
{
  boolean flocal;
  struct sstatus sstat;
  boolean fnostatus;
  struct tm stime;
  int cpad;

  /* Make sure this is one of the systems we are printing.  */
  if (csystems > 0)
    {
      boolean fmatch;
      int i;

      fmatch = fnotsystems;
      for (i = 0; i < csystems; i++)
	{
	  if (strcmp (pazsystems[i], qsys->uuconf_zname) == 0)
	    {
	      fmatch = ! fmatch;
	      break;
	    }
	}
      if (! fmatch)
	return TRUE;
    }

  /* Make sure the commands are within the time bounds.  */
  if ((iold != (long) -1
       && (cwork == 0 || ifirstwork > iold)
       && (qxqt == NULL || qxqt->ifirst > iold))
      || (iyoung != (long) -1
	  && (cwork == 0 || ifirstwork < iyoung)
	  && (qxqt == NULL || qxqt->ifirst < iyoung)))
    return TRUE;

  flocal = strcmp (qsys->uuconf_zname, zlocalname) == 0;

  if (! flocal)
    {
      if (! fsysdep_get_status (qsys, &sstat, &fnostatus))
	return FALSE;
    }

  printf ("%-10s %3dC (", qsys->uuconf_zname, cwork);

  if (cwork == 0)
    {
      printf ("0 secs");
      cpad = 3;
    }
  else
    cpad = csunits_show (inow - ifirstwork);

  printf (") ");
  while (cpad-- != 0)
    printf (" ");

  if (qxqt == NULL)
    printf ("  0X (0 secs)  ");
  else
    {
      printf ("%3dX (", qxqt->cxqts);
      cpad = csunits_show (inow - qxqt->ifirst);
      printf (")");
      while (cpad-- != 0)
	printf (" ");
    }

  if (flocal || fnostatus)
    {
      printf ("\n");
      if (! flocal)
	ubuffree (sstat.zstring);
      return TRUE;
    }

  usysdep_localtime (sstat.ilast, &stime);

  printf (" %02d-%02d %02d:%02d ", 
	  stime.tm_mon + 1,stime.tm_mday, stime.tm_hour, stime.tm_min);

  if (sstat.zstring == NULL)
    printf ("%s\n", azStatus[(int) sstat.ttype]);
  else
    {
      printf ("%s\n", sstat.zstring);
      ubuffree (sstat.zstring);
    }

  return TRUE;
}

/* Print a time difference in the largest applicable units.  */

static int
csunits_show (idiff)
     long idiff;
{
  const char *zunit;
  long iunits;
  int cpad;

  if (idiff > (long) 24 * (long) 60 * (long) 60)
    {
      iunits = idiff / ((long) 24 * (long) 60 * (long) 60);
      zunit = "day";
      cpad = 4;
    }
  else if (idiff > (long) 60 * 60)
    {
      iunits = idiff / (long) (60 * 60);
      zunit = "hour";
      cpad = 3;
    }
  else if (idiff > (long) 60)
    {
      iunits = idiff / (long) 60;
      zunit = "min";
      cpad = 4;
    }
  else
    {
      iunits = idiff;
      zunit = "sec";
      cpad = 4;
    }

  printf ("%ld %s%s", iunits, zunit, iunits == 1 ? "" : "s");

  if (iunits != 1)
    --cpad;
  if (iunits > 99)
    --cpad;
  if (iunits > 9)
    --cpad;
  return cpad;
}

/* Give a list of all status entries for all machines that we have
   status entries for.  We need to get a list of status entries in a
   system dependent fashion, since we may have status for unknown
   systems.  */

static boolean
fsmachines ()
{
  pointer phold;
  char *zsystem;
  boolean ferr;
  struct sstatus sstat;

  if (! fsysdep_all_status_init (&phold))
    return FALSE;

  while ((zsystem = zsysdep_all_status (phold, &ferr, &sstat)) != NULL)
    {
      struct tm stime;

      usysdep_localtime (sstat.ilast, &stime);
      printf ("%-14s %02d-%02d %02d:%02d ", zsystem,
	      stime.tm_mon + 1, stime.tm_mday, stime.tm_hour,
	      stime.tm_min);
      if (sstat.zstring == NULL)
	printf ("%s", azStatus[(int) sstat.ttype]);
      else
	{
	  printf ("%s", sstat.zstring);
	  ubuffree (sstat.zstring);
	}
      ubuffree (zsystem);
      if (sstat.ttype != STATUS_TALKING
	  && sstat.cwait > 0)
	{
	  printf (" (%d %s", sstat.cretries,
		  sstat.cretries == 1 ? "try" : "tries");
	  if (sstat.ilast + sstat.cwait > ixsysdep_time ((long *) NULL))
	    {
	      usysdep_localtime (sstat.ilast + sstat.cwait, &stime);
	      printf (", next after %02d-%02d %02d:%02d",
		      stime.tm_mon + 1, stime.tm_mday, stime.tm_hour,
		      stime.tm_min);
	    }
	  printf (")");
	}
      printf ("\n");
    }

  usysdep_all_status_free (phold);

  return ! ferr;
}
OpenPOWER on IntegriCloud