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
|
/* sort - sort lines of text (with all kinds of options).
Copyright (C) 1988, 1991 Free Software Foundation
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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
Written December 1988 by Mike Haertel.
The author may be reached (Email) at the address mike@gnu.ai.mit.edu,
or (US mail) as Mike Haertel c/o Free Software Foundation. */
/* Get isblank from GNU libc. */
#define _GNU_SOURCE
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include "system.h"
#ifdef _POSIX_VERSION
#include <limits.h>
#else
#ifndef UCHAR_MAX
#define UCHAR_MAX 255
#endif
#endif
#ifndef STDC_HEADERS
char *malloc ();
char *realloc ();
void free ();
#endif
void error ();
static void usage ();
#define min(a, b) ((a) < (b) ? (a) : (b))
#define UCHAR_LIM (UCHAR_MAX + 1)
#define UCHAR(c) ((unsigned char) (c))
/* The kind of blanks for '-b' to skip in various options. */
enum blanktype { bl_start, bl_end, bl_both };
/* The name this program was run with. */
char *program_name;
/* Table of digits. */
static int digits[UCHAR_LIM];
/* Table of white space. */
static int blanks[UCHAR_LIM];
/* Table of non-printing characters. */
static int nonprinting[UCHAR_LIM];
/* Table of non-dictionary characters (not letters, digits, or blanks). */
static int nondictionary[UCHAR_LIM];
/* Translation table folding lower case to upper. */
static char fold_toupper[UCHAR_LIM];
/* Table mapping 3-letter month names to integers.
Alphabetic order allows binary search. */
static struct month
{
char *name;
int val;
} const monthtab[] =
{
{"APR", 4},
{"AUG", 8},
{"DEC", 12},
{"FEB", 2},
{"JAN", 1},
{"JUL", 7},
{"JUN", 6},
{"MAR", 3},
{"MAY", 5},
{"NOV", 11},
{"OCT", 10},
{"SEP", 9}
};
/* During the merge phase, the number of files to merge at once. */
#define NMERGE 16
/* Initial buffer size for in core sorting. Will not grow unless a
line longer than this is seen. */
static int sortalloc = 524288;
/* Initial buffer size for in core merge buffers. Bear in mind that
up to NMERGE * mergealloc bytes may be allocated for merge buffers. */
static int mergealloc = 16384;
/* Guess of average line length. */
static int linelength = 30;
/* Maximum number of elements for the array(s) of struct line's, in bytes. */
#define LINEALLOC 262144
/* Prefix for temporary file names. */
static char *prefix;
/* Flag to reverse the order of all comparisons. */
static int reverse;
/* Flag for stable sort. This turns off the last ditch bytewise
comparison of lines, and instead leaves lines in the same order
they were read if all keys compare equal. */
static int stable;
/* Tab character separating fields. If NUL, then fields are separated
by the empty string between a non-whitespace character and a whitespace
character. */
static char tab;
/* Flag to remove consecutive duplicate lines from the output.
Only the last of a sequence of equal lines will be output. */
static int unique;
/* Nonzero if any of the input files are the standard input. */
static int have_read_stdin;
/* Lines are held in core as counted strings. */
struct line
{
char *text; /* Text of the line. */
int length; /* Length not including final newline. */
char *keybeg; /* Start of first key. */
char *keylim; /* Limit of first key. */
};
/* Arrays of lines. */
struct lines
{
struct line *lines; /* Dynamically allocated array of lines. */
int used; /* Number of slots used. */
int alloc; /* Number of slots allocated. */
int limit; /* Max number of slots to allocate. */
};
/* Input buffers. */
struct buffer
{
char *buf; /* Dynamically allocated buffer. */
int used; /* Number of bytes used. */
int alloc; /* Number of bytes allocated. */
int left; /* Number of bytes left after line parsing. */
};
/* Lists of key field comparisons to be tried. */
static struct keyfield
{
int sword; /* Zero-origin 'word' to start at. */
int schar; /* Additional characters to skip. */
int skipsblanks; /* Skip leading white space at start. */
int eword; /* Zero-origin first word after field. */
int echar; /* Additional characters in field. */
int skipeblanks; /* Skip trailing white space at finish. */
int *ignore; /* Boolean array of characters to ignore. */
char *translate; /* Translation applied to characters. */
int numeric; /* Flag for numeric comparison. */
int month; /* Flag for comparison by month name. */
int reverse; /* Reverse the sense of comparison. */
struct keyfield *next; /* Next keyfield to try. */
} keyhead;
/* The list of temporary files. */
static struct tempnode
{
char *name;
struct tempnode *next;
} temphead;
/* Clean up any remaining temporary files. */
static void
cleanup ()
{
struct tempnode *node;
for (node = temphead.next; node; node = node->next)
unlink (node->name);
}
/* Allocate N bytes of memory dynamically, with error checking. */
char *
xmalloc (n)
unsigned n;
{
char *p;
p = malloc (n);
if (p == 0)
{
error (0, 0, "virtual memory exhausted");
cleanup ();
exit (2);
}
return p;
}
/* Change the size of an allocated block of memory P to N bytes,
with error checking.
If P is NULL, run xmalloc.
If N is 0, run free and return NULL. */
char *
xrealloc (p, n)
char *p;
unsigned n;
{
if (p == 0)
return xmalloc (n);
if (n == 0)
{
free (p);
return 0;
}
p = realloc (p, n);
if (p == 0)
{
error (0, 0, "virtual memory exhausted");
cleanup ();
exit (2);
}
return p;
}
static FILE *
xfopen (file, how)
char *file, *how;
{
FILE *fp = strcmp (file, "-") ? fopen (file, how) : stdin;
if (fp == 0)
{
error (0, errno, "%s", file);
cleanup ();
exit (2);
}
if (fp == stdin)
have_read_stdin = 1;
return fp;
}
static void
xfclose (fp)
FILE *fp;
{
fflush (fp);
if (fp != stdin && fp != stdout)
{
if (fclose (fp) != 0)
{
error (0, errno, "error closing file");
cleanup ();
exit (2);
}
}
else
/* Allow reading stdin from tty more than once. */
clearerr (fp);
}
static void
xfwrite (buf, size, nelem, fp)
char *buf;
int size, nelem;
FILE *fp;
{
if (fwrite (buf, size, nelem, fp) != nelem)
{
error (0, errno, "write error");
cleanup ();
exit (2);
}
}
/* Return a name for a temporary file. */
static char *
tempname ()
{
static int seq;
int len = strlen (prefix);
char *name = xmalloc (len + 16);
struct tempnode *node =
(struct tempnode *) xmalloc (sizeof (struct tempnode));
if (len && prefix[len - 1] != '/')
sprintf (name, "%s/sort%5.5d%5.5d", prefix, getpid (), ++seq);
else
sprintf (name, "%ssort%5.5d%5.5d", prefix, getpid (), ++seq);
node->name = name;
node->next = temphead.next;
temphead.next = node;
return name;
}
/* Search through the list of temporary files for NAME;
remove it if it is found on the list. */
static void
zaptemp (name)
char *name;
{
struct tempnode *node, *temp;
for (node = &temphead; node->next; node = node->next)
if (!strcmp (name, node->next->name))
break;
if (node->next)
{
temp = node->next;
unlink (temp->name);
free (temp->name);
node->next = temp->next;
free ((char *) temp);
}
}
/* Initialize the character class tables. */
static void
inittables ()
{
int i;
for (i = 0; i < UCHAR_LIM; ++i)
{
if (ISBLANK (i))
blanks[i] = 1;
if (ISDIGIT (i))
digits[i] = 1;
if (!ISPRINT (i))
nonprinting[i] = 1;
if (!ISALNUM (i) && !ISBLANK (i))
nondictionary[i] = 1;
if (ISLOWER (i))
fold_toupper[i] = toupper (i);
else
fold_toupper[i] = i;
}
}
/* Initialize BUF, allocating ALLOC bytes initially. */
static void
initbuf (buf, alloc)
struct buffer *buf;
int alloc;
{
buf->alloc = alloc;
buf->buf = xmalloc (buf->alloc);
buf->used = buf->left = 0;
}
/* Fill BUF reading from FP, moving buf->left bytes from the end
of buf->buf to the beginning first. If EOF is reached and the
file wasn't terminated by a newline, supply one. Return a count
of bytes buffered. */
static int
fillbuf (buf, fp)
struct buffer *buf;
FILE *fp;
{
int cc;
bcopy (buf->buf + buf->used - buf->left, buf->buf, buf->left);
buf->used = buf->left;
while (!feof (fp) && (buf->used == 0 || !memchr (buf->buf, '\n', buf->used)))
{
if (buf->used == buf->alloc)
{
buf->alloc *= 2;
buf->buf = xrealloc (buf->buf, buf->alloc);
}
cc = fread (buf->buf + buf->used, 1, buf->alloc - buf->used, fp);
if (ferror (fp))
{
error (0, errno, "read error");
cleanup ();
exit (2);
}
buf->used += cc;
}
if (feof (fp) && buf->used && buf->buf[buf->used - 1] != '\n')
{
if (buf->used == buf->alloc)
{
buf->alloc *= 2;
buf->buf = xrealloc (buf->buf, buf->alloc);
}
buf->buf[buf->used++] = '\n';
}
return buf->used;
}
/* Initialize LINES, allocating space for ALLOC lines initially.
LIMIT is the maximum possible number of lines to allocate space
for, ever. */
static void
initlines (lines, alloc, limit)
struct lines *lines;
int alloc;
int limit;
{
lines->alloc = alloc;
lines->lines = (struct line *) xmalloc (lines->alloc * sizeof (struct line));
lines->used = 0;
lines->limit = limit;
}
/* Return a pointer to the first character of the field specified
by KEY in LINE. */
static char *
begfield (line, key)
struct line *line;
struct keyfield *key;
{
register char *ptr = line->text, *lim = ptr + line->length;
register int sword = key->sword, schar = key->schar;
if (tab)
while (ptr < lim && sword--)
{
while (ptr < lim && *ptr != tab)
++ptr;
if (ptr < lim)
++ptr;
}
else
while (ptr < lim && sword--)
{
while (ptr < lim && blanks[UCHAR (*ptr)])
++ptr;
while (ptr < lim && !blanks[UCHAR (*ptr)])
++ptr;
}
if (key->skipsblanks)
while (ptr < lim && blanks[UCHAR (*ptr)])
++ptr;
while (ptr < lim && schar--)
++ptr;
return ptr;
}
/* Return the limit of (a pointer to the first character after) the field
in LINE specified by KEY. */
static char *
limfield (line, key)
struct line *line;
struct keyfield *key;
{
register char *ptr = line->text, *lim = ptr + line->length;
register int eword = key->eword, echar = key->echar;
if (tab)
while (ptr < lim && eword--)
{
while (ptr < lim && *ptr != tab)
++ptr;
if (ptr < lim && (eword || key->skipeblanks))
++ptr;
}
else
while (ptr < lim && eword--)
{
while (ptr < lim && blanks[UCHAR (*ptr)])
++ptr;
while (ptr < lim && !blanks[UCHAR (*ptr)])
++ptr;
}
if (key->skipeblanks)
while (ptr < lim && blanks[UCHAR (*ptr)])
++ptr;
while (ptr < lim && echar--)
++ptr;
return ptr;
}
/* Find the lines in BUF, storing pointers and lengths in LINES.
Also replace newlines with NULs. */
static void
findlines (buf, lines)
struct buffer *buf;
struct lines *lines;
{
register char *beg = buf->buf, *lim = buf->buf + buf->used, *ptr;
struct keyfield *key = keyhead.next;
lines->used = 0;
while (beg < lim && (ptr = memchr (beg, '\n', lim - beg))
&& lines->used < lines->limit)
{
/* There are various places in the code that rely on a NUL
being at the end of in-core lines; NULs inside the lines
will not cause trouble, though. */
*ptr = '\0';
if (lines->used == lines->alloc)
{
lines->alloc *= 2;
lines->lines = (struct line *)
xrealloc ((char *) lines->lines,
lines->alloc * sizeof (struct line));
}
lines->lines[lines->used].text = beg;
lines->lines[lines->used].length = ptr - beg;
/* Precompute the position of the first key for efficiency. */
if (key)
{
if (key->eword >= 0)
lines->lines[lines->used].keylim =
limfield (&lines->lines[lines->used], key);
else
lines->lines[lines->used].keylim = ptr;
if (key->sword >= 0)
lines->lines[lines->used].keybeg =
begfield (&lines->lines[lines->used], key);
else
{
if (key->skipsblanks)
while (blanks[UCHAR (*beg)])
++beg;
lines->lines[lines->used].keybeg = beg;
}
}
++lines->used;
beg = ptr + 1;
}
buf->left = lim - beg;
}
/* Compare strings A and B containing decimal fractions < 1. Each string
should begin with a decimal point followed immediately by the digits
of the fraction. Strings not of this form are considered to be zero. */
static int
fraccompare (a, b)
register char *a, *b;
{
register tmpa = UCHAR (*a), tmpb = UCHAR (*b);
if (tmpa == '.' && tmpb == '.')
{
do
tmpa = UCHAR (*++a), tmpb = UCHAR (*++b);
while (tmpa == tmpb && digits[tmpa]);
if (digits[tmpa] && digits[tmpb])
return tmpa - tmpb;
if (digits[tmpa])
{
while (tmpa == '0')
tmpa = UCHAR (*++a);
if (digits[tmpa])
return 1;
return 0;
}
if (digits[tmpb])
{
while (tmpb == '0')
tmpb = UCHAR (*++b);
if (digits[tmpb])
return -1;
return 0;
}
return 0;
}
else if (tmpa == '.')
{
do
tmpa = UCHAR (*++a);
while (tmpa == '0');
if (digits[tmpa])
return 1;
return 0;
}
else if (tmpb == '.')
{
do
tmpb = UCHAR (*++b);
while (tmpb == '0');
if (digits[tmpb])
return -1;
return 0;
}
return 0;
}
/* Compare strings A and B as numbers without explicitly converting them to
machine numbers. Comparatively slow for short strings, but asymptotically
hideously fast. */
static int
numcompare (a, b)
register char *a, *b;
{
register int tmpa, tmpb, loga, logb, tmp;
tmpa = UCHAR (*a), tmpb = UCHAR (*b);
if (tmpa == '-')
{
tmpa = UCHAR (*++a);
if (tmpb != '-')
{
if (digits[tmpa] && digits[tmpb])
return -1;
return 0;
}
tmpb = UCHAR (*++b);
while (tmpa == '0')
tmpa = UCHAR (*++a);
while (tmpb == '0')
tmpb = UCHAR (*++b);
while (tmpa == tmpb && digits[tmpa])
tmpa = UCHAR (*++a), tmpb = UCHAR (*++b);
if ((tmpa == '.' && !digits[tmpb]) || (tmpb == '.' && !digits[tmpa]))
return -fraccompare (a, b);
if (digits[tmpa])
for (loga = 1; digits[UCHAR (*++a)]; ++loga)
;
else
loga = 0;
if (digits[tmpb])
for (logb = 1; digits[UCHAR (*++b)]; ++logb)
;
else
logb = 0;
if ((tmp = logb - loga) != 0)
return tmp;
if (!loga)
return 0;
return tmpb - tmpa;
}
else if (tmpb == '-')
{
if (digits[UCHAR (tmpa)] && digits[UCHAR (*++b)])
return 1;
return 0;
}
else
{
while (tmpa == '0')
tmpa = UCHAR (*++a);
while (tmpb == '0')
tmpb = UCHAR (*++b);
while (tmpa == tmpb && digits[tmpa])
tmpa = UCHAR (*++a), tmpb = UCHAR (*++b);
if ((tmpa == '.' && !digits[tmpb]) || (tmpb == '.' && !digits[tmpa]))
return fraccompare (a, b);
if (digits[tmpa])
for (loga = 1; digits[UCHAR (*++a)]; ++loga)
;
else
loga = 0;
if (digits[tmpb])
for (logb = 1; digits[UCHAR (*++b)]; ++logb)
;
else
logb = 0;
if ((tmp = loga - logb) != 0)
return tmp;
if (!loga)
return 0;
return tmpa - tmpb;
}
}
/* Return an integer <= 12 associated with month name S with length LEN,
0 if the name in S is not recognized. */
static int
getmonth (s, len)
char *s;
int len;
{
char month[4];
register int i, lo = 0, hi = 12;
if (len < 3)
return 0;
for (i = 0; i < 3; ++i)
month[i] = fold_toupper[UCHAR (s[i])];
month[3] = '\0';
while (hi - lo > 1)
if (strcmp (month, monthtab[(lo + hi) / 2].name) < 0)
hi = (lo + hi) / 2;
else
lo = (lo + hi) / 2;
if (!strcmp (month, monthtab[lo].name))
return monthtab[lo].val;
return 0;
}
/* Compare two lines A and B trying every key in sequence until there
are no more keys or a difference is found. */
static int
keycompare (a, b)
struct line *a, *b;
{
register char *texta, *textb, *lima, *limb, *translate;
register int *ignore;
struct keyfield *key;
int diff = 0, iter = 0, lena, lenb;
for (key = keyhead.next; key; key = key->next, ++iter)
{
ignore = key->ignore;
translate = key->translate;
/* Find the beginning and limit of each field. */
if (iter || a->keybeg == NULL || b->keybeg == NULL)
{
if (key->eword >= 0)
lima = limfield (a, key), limb = limfield (b, key);
else
lima = a->text + a->length, limb = b->text + b->length;
if (key->sword >= 0)
texta = begfield (a, key), textb = begfield (b, key);
else
{
texta = a->text, textb = b->text;
if (key->skipsblanks)
{
while (texta < lima && blanks[UCHAR (*texta)])
++texta;
while (textb < limb && blanks[UCHAR (*textb)])
++textb;
}
}
}
else
{
/* For the first iteration only, the key positions have
been precomputed for us. */
texta = a->keybeg, lima = a->keylim;
textb = b->keybeg, limb = b->keylim;
}
/* Find the lengths. */
lena = lima - texta, lenb = limb - textb;
if (lena < 0)
lena = 0;
if (lenb < 0)
lenb = 0;
/* Actually compare the fields. */
if (key->numeric)
{
if (*lima || *limb)
{
char savea = *lima, saveb = *limb;
*lima = *limb = '\0';
diff = numcompare (texta, textb);
*lima = savea, *limb = saveb;
}
else
diff = numcompare (texta, textb);
if (diff)
return key->reverse ? -diff : diff;
continue;
}
else if (key->month)
{
diff = getmonth (texta, lena) - getmonth (textb, lenb);
if (diff)
return key->reverse ? -diff : diff;
continue;
}
else if (ignore && translate)
while (texta < lima && textb < limb)
{
while (texta < lima && ignore[UCHAR (*texta)])
++texta;
while (textb < limb && ignore[UCHAR (*textb)])
++textb;
if (texta < lima && textb < limb &&
translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
{
diff = translate[UCHAR (*--texta)] - translate[UCHAR (*--textb)];
break;
}
}
else if (ignore)
while (texta < lima && textb < limb)
{
while (texta < lima && ignore[UCHAR (*texta)])
++texta;
while (textb < limb && ignore[UCHAR (*textb)])
++textb;
if (texta < lima && textb < limb && *texta++ != *textb++)
{
diff = *--texta - *--textb;
break;
}
}
else if (translate)
while (texta < lima && textb < limb)
{
if (translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
{
diff = translate[UCHAR (*--texta)] - translate[UCHAR (*--textb)];
break;
}
}
else
diff = memcmp (texta, textb, min (lena, lenb));
if (diff)
return key->reverse ? -diff : diff;
if ((diff = lena - lenb) != 0)
return key->reverse ? -diff : diff;
}
return 0;
}
/* Compare two lines A and B, returning negative, zero, or positive
depending on whether A compares less than, equal to, or greater than B. */
static int
compare (a, b)
register struct line *a, *b;
{
int diff, tmpa, tmpb, mini;
if (keyhead.next)
{
diff = keycompare (a, b);
if (diff)
return diff;
if (!unique && !stable)
{
tmpa = a->length, tmpb = b->length;
diff = memcmp (a->text, b->text, min (tmpa, tmpb));
if (!diff)
diff = tmpa - tmpb;
}
}
else
{
tmpa = a->length, tmpb = b->length;
mini = min (tmpa, tmpb);
if (mini == 0)
diff = tmpa - tmpb;
else
{
char *ap = a->text, *bp = b->text;
diff = *ap - *bp;
if (diff == 0)
{
diff = memcmp (ap, bp, mini);
if (diff == 0)
diff = tmpa - tmpb;
}
}
}
return reverse ? -diff : diff;
}
/* Check that the lines read from the given FP come in order. Return
1 if they do and 0 if there is a disorder. */
static int
checkfp (fp)
FILE *fp;
{
struct buffer buf; /* Input buffer. */
struct lines lines; /* Lines scanned from the buffer. */
struct line temp; /* Copy of previous line. */
int cc; /* Character count. */
int cmp; /* Result of calling compare. */
int alloc, i, success = 1;
initbuf (&buf, mergealloc);
initlines (&lines, mergealloc / linelength + 1,
LINEALLOC / ((NMERGE + NMERGE) * sizeof (struct line)));
alloc = linelength;
temp.text = xmalloc (alloc);
cc = fillbuf (&buf, fp);
findlines (&buf, &lines);
if (cc)
do
{
/* Compare each line in the buffer with its successor. */
for (i = 0; i < lines.used - 1; ++i)
{
cmp = compare (&lines.lines[i], &lines.lines[i + 1]);
if ((unique && cmp >= 0) || (cmp > 0))
{
success = 0;
goto finish;
}
}
/* Save the last line of the buffer and refill the buffer. */
if (lines.lines[lines.used - 1].length > alloc)
{
while (lines.lines[lines.used - 1].length + 1 > alloc)
alloc *= 2;
temp.text = xrealloc (temp.text, alloc);
}
bcopy (lines.lines[lines.used - 1].text, temp.text,
lines.lines[lines.used - 1].length + 1);
temp.length = lines.lines[lines.used - 1].length;
cc = fillbuf (&buf, fp);
if (cc)
{
findlines (&buf, &lines);
/* Make sure the line saved from the old buffer contents is
less than or equal to the first line of the new buffer. */
cmp = compare (&temp, &lines.lines[0]);
if ((unique && cmp >= 0) || (cmp > 0))
{
success = 0;
break;
}
}
}
while (cc);
finish:
xfclose (fp);
free (buf.buf);
free ((char *) lines.lines);
free (temp.text);
return success;
}
/* Merge lines from FPS onto OFP. NFPS cannot be greater than NMERGE.
Close FPS before returning. */
static void
mergefps (fps, nfps, ofp)
FILE *fps[], *ofp;
register int nfps;
{
struct buffer buffer[NMERGE]; /* Input buffers for each file. */
struct lines lines[NMERGE]; /* Line tables for each buffer. */
struct line saved; /* Saved line for unique check. */
int savedflag = 0; /* True if there is a saved line. */
int savealloc; /* Size allocated for the saved line. */
int cur[NMERGE]; /* Current line in each line table. */
int ord[NMERGE]; /* Table representing a permutation of fps,
such that lines[ord[0]].lines[cur[ord[0]]]
is the smallest line and will be next
output. */
register int i, j, t;
/* Allocate space for a saved line if necessary. */
if (unique)
{
savealloc = linelength;
saved.text = xmalloc (savealloc);
}
/* Read initial lines from each input file. */
for (i = 0; i < nfps; ++i)
{
initbuf (&buffer[i], mergealloc);
/* If a file is empty, eliminate it from future consideration. */
while (i < nfps && !fillbuf (&buffer[i], fps[i]))
{
xfclose (fps[i]);
--nfps;
for (j = i; j < nfps; ++j)
fps[j] = fps[j + 1];
}
if (i == nfps)
free (buffer[i].buf);
else
{
initlines (&lines[i], mergealloc / linelength + 1,
LINEALLOC / ((NMERGE + NMERGE) * sizeof (struct line)));
findlines (&buffer[i], &lines[i]);
cur[i] = 0;
}
}
/* Set up the ord table according to comparisons among input lines.
Since this only reorders two items if one is strictly greater than
the other, it is stable. */
for (i = 0; i < nfps; ++i)
ord[i] = i;
for (i = 1; i < nfps; ++i)
if (compare (&lines[ord[i - 1]].lines[cur[ord[i - 1]]],
&lines[ord[i]].lines[cur[ord[i]]]) > 0)
t = ord[i - 1], ord[i - 1] = ord[i], ord[i] = t, i = 0;
/* Repeatedly output the smallest line until no input remains. */
while (nfps)
{
/* If uniqified output is turned out, output only the first of
an identical series of lines. */
if (unique)
{
if (savedflag && compare (&saved, &lines[ord[0]].lines[cur[ord[0]]]))
{
xfwrite (saved.text, 1, saved.length, ofp);
putc ('\n', ofp);
savedflag = 0;
}
if (!savedflag)
{
if (savealloc < lines[ord[0]].lines[cur[ord[0]]].length + 1)
{
while (savealloc < lines[ord[0]].lines[cur[ord[0]]].length + 1)
savealloc *= 2;
saved.text = xrealloc (saved.text, savealloc);
}
saved.length = lines[ord[0]].lines[cur[ord[0]]].length;
bcopy (lines[ord[0]].lines[cur[ord[0]]].text, saved.text,
saved.length + 1);
savedflag = 1;
}
}
else
{
xfwrite (lines[ord[0]].lines[cur[ord[0]]].text, 1,
lines[ord[0]].lines[cur[ord[0]]].length, ofp);
putc ('\n', ofp);
}
/* Check if we need to read more lines into core. */
if (++cur[ord[0]] == lines[ord[0]].used)
if (fillbuf (&buffer[ord[0]], fps[ord[0]]))
{
findlines (&buffer[ord[0]], &lines[ord[0]]);
cur[ord[0]] = 0;
}
else
{
/* We reached EOF on fps[ord[0]]. */
for (i = 1; i < nfps; ++i)
if (ord[i] > ord[0])
--ord[i];
--nfps;
xfclose (fps[ord[0]]);
free (buffer[ord[0]].buf);
free ((char *) lines[ord[0]].lines);
for (i = ord[0]; i < nfps; ++i)
{
fps[i] = fps[i + 1];
buffer[i] = buffer[i + 1];
lines[i] = lines[i + 1];
cur[i] = cur[i + 1];
}
for (i = 0; i < nfps; ++i)
ord[i] = ord[i + 1];
continue;
}
/* The new line just read in may be larger than other lines
already in core; push it back in the queue until we encounter
a line larger than it. */
for (i = 1; i < nfps; ++i)
{
t = compare (&lines[ord[0]].lines[cur[ord[0]]],
&lines[ord[i]].lines[cur[ord[i]]]);
if (!t)
t = ord[0] - ord[i];
if (t < 0)
break;
}
t = ord[0];
for (j = 1; j < i; ++j)
ord[j - 1] = ord[j];
ord[i - 1] = t;
}
if (unique && savedflag)
{
xfwrite (saved.text, 1, saved.length, ofp);
putc ('\n', ofp);
free (saved.text);
}
}
/* Sort the array LINES with NLINES members, using TEMP for temporary space. */
static void
sortlines (lines, nlines, temp)
struct line *lines, *temp;
int nlines;
{
register struct line *lo, *hi, *t;
register int nlo, nhi;
if (nlines == 2)
{
if (compare (&lines[0], &lines[1]) > 0)
*temp = lines[0], lines[0] = lines[1], lines[1] = *temp;
return;
}
nlo = nlines / 2;
lo = lines;
nhi = nlines - nlo;
hi = lines + nlo;
if (nlo > 1)
sortlines (lo, nlo, temp);
if (nhi > 1)
sortlines (hi, nhi, temp);
t = temp;
while (nlo && nhi)
if (compare (lo, hi) <= 0)
*t++ = *lo++, --nlo;
else
*t++ = *hi++, --nhi;
while (nlo--)
*t++ = *lo++;
for (lo = lines, nlo = nlines - nhi, t = temp; nlo; --nlo)
*lo++ = *t++;
}
/* Check that each of the NFILES FILES is ordered.
Return a count of disordered files. */
static int
check (files, nfiles)
char *files[];
int nfiles;
{
int i, disorders = 0;
FILE *fp;
for (i = 0; i < nfiles; ++i)
{
fp = xfopen (files[i], "r");
if (!checkfp (fp))
{
printf ("%s: disorder on %s\n", program_name, files[i]);
++disorders;
}
}
return disorders;
}
/* Merge NFILES FILES onto OFP. */
static void
merge (files, nfiles, ofp)
char *files[];
int nfiles;
FILE *ofp;
{
int i, j, t;
char *temp;
FILE *fps[NMERGE], *tfp;
while (nfiles > NMERGE)
{
t = 0;
for (i = 0; i < nfiles / NMERGE; ++i)
{
for (j = 0; j < NMERGE; ++j)
fps[j] = xfopen (files[i * NMERGE + j], "r");
tfp = xfopen (temp = tempname (), "w");
mergefps (fps, NMERGE, tfp);
xfclose (tfp);
for (j = 0; j < NMERGE; ++j)
zaptemp (files[i * NMERGE + j]);
files[t++] = temp;
}
for (j = 0; j < nfiles % NMERGE; ++j)
fps[j] = xfopen (files[i * NMERGE + j], "r");
tfp = xfopen (temp = tempname (), "w");
mergefps (fps, nfiles % NMERGE, tfp);
xfclose (tfp);
for (j = 0; j < nfiles % NMERGE; ++j)
zaptemp (files[i * NMERGE + j]);
files[t++] = temp;
nfiles = t;
}
for (i = 0; i < nfiles; ++i)
fps[i] = xfopen (files[i], "r");
mergefps (fps, i, ofp);
for (i = 0; i < nfiles; ++i)
zaptemp (files[i]);
}
/* Sort NFILES FILES onto OFP. */
static void
sort (files, nfiles, ofp)
char **files;
int nfiles;
FILE *ofp;
{
struct buffer buf;
struct lines lines;
struct line *tmp;
int i, ntmp;
FILE *fp, *tfp;
struct tempnode *node;
int ntemp = 0;
char **tempfiles;
initbuf (&buf, sortalloc);
initlines (&lines, sortalloc / linelength + 1,
LINEALLOC / sizeof (struct line));
ntmp = lines.alloc;
tmp = (struct line *) xmalloc (ntmp * sizeof (struct line));
while (nfiles--)
{
fp = xfopen (*files++, "r");
while (fillbuf (&buf, fp))
{
findlines (&buf, &lines);
if (lines.used > ntmp)
{
while (lines.used > ntmp)
ntmp *= 2;
tmp = (struct line *)
xrealloc ((char *) tmp, ntmp * sizeof (struct line));
}
sortlines (lines.lines, lines.used, tmp);
if (feof (fp) && !nfiles && !ntemp && !buf.left)
tfp = ofp;
else
{
++ntemp;
tfp = xfopen (tempname (), "w");
}
for (i = 0; i < lines.used; ++i)
if (!unique || i == 0
|| compare (&lines.lines[i], &lines.lines[i - 1]))
{
xfwrite (lines.lines[i].text, 1, lines.lines[i].length, tfp);
putc ('\n', tfp);
}
if (tfp != ofp)
xfclose (tfp);
}
xfclose (fp);
}
free (buf.buf);
free ((char *) lines.lines);
free ((char *) tmp);
if (ntemp)
{
tempfiles = (char **) xmalloc (ntemp * sizeof (char *));
i = ntemp;
for (node = temphead.next; node; node = node->next)
tempfiles[--i] = node->name;
merge (tempfiles, ntemp, ofp);
free ((char *) tempfiles);
}
}
/* Insert key KEY at the end of the list (`keyhead'). */
static void
insertkey (key)
struct keyfield *key;
{
struct keyfield *k = &keyhead;
while (k->next)
k = k->next;
k->next = key;
key->next = NULL;
}
static void
badfieldspec (s)
char *s;
{
error (2, 0, "invalid field specification `%s'", s);
}
/* Handle interrupts and hangups. */
static void
sighandler (sig)
int sig;
{
#ifdef _POSIX_VERSION
struct sigaction sigact;
sigact.sa_handler = SIG_DFL;
sigemptyset (&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction (sig, &sigact, NULL);
#else /* !_POSIX_VERSION */
signal (sig, SIG_DFL);
#endif /* _POSIX_VERSION */
cleanup ();
kill (getpid (), sig);
}
/* Set the ordering options for KEY specified in S.
Return the address of the first character in S that
is not a valid ordering option.
BLANKTYPE is the kind of blanks that 'b' should skip. */
static char *
set_ordering (s, key, blanktype)
register char *s;
struct keyfield *key;
enum blanktype blanktype;
{
while (*s)
{
switch (*s)
{
case 'b':
if (blanktype == bl_start || blanktype == bl_both)
key->skipsblanks = 1;
if (blanktype == bl_end || blanktype == bl_both)
key->skipeblanks = 1;
break;
case 'd':
key->ignore = nondictionary;
break;
case 'f':
key->translate = fold_toupper;
break;
#if 0
case 'g':
/* Reserved for comparing floating-point numbers. */
break;
#endif
case 'i':
key->ignore = nonprinting;
break;
case 'M':
key->skipsblanks = key->skipeblanks = key->month = 1;
break;
case 'n':
key->skipsblanks = key->skipeblanks = key->numeric = 1;
break;
case 'r':
key->reverse = 1;
break;
default:
return s;
}
++s;
}
return s;
}
void
main (argc, argv)
int argc;
char *argv[];
{
struct keyfield *key = NULL, gkey;
char *s;
int i, t, t2;
int checkonly = 0, mergeonly = 0, nfiles = 0;
char *minus = "-", *outfile = minus, **files, *tmp;
FILE *ofp;
#ifdef _POSIX_VERSION
struct sigaction oldact, newact;
#endif /* _POSIX_VERSION */
program_name = argv[0];
have_read_stdin = 0;
inittables ();
prefix = getenv ("TMPDIR");
if (prefix == NULL)
prefix = "/tmp";
#ifdef _POSIX_VERSION
newact.sa_handler = sighandler;
sigemptyset (&newact.sa_mask);
newact.sa_flags = 0;
sigaction (SIGINT, NULL, &oldact);
if (oldact.sa_handler != SIG_IGN)
sigaction (SIGINT, &newact, NULL);
sigaction (SIGHUP, NULL, &oldact);
if (oldact.sa_handler != SIG_IGN)
sigaction (SIGHUP, &newact, NULL);
sigaction (SIGPIPE, NULL, &oldact);
if (oldact.sa_handler != SIG_IGN)
sigaction (SIGPIPE, &newact, NULL);
sigaction (SIGTERM, NULL, &oldact);
if (oldact.sa_handler != SIG_IGN)
sigaction (SIGTERM, &newact, NULL);
#else /* !_POSIX_VERSION */
if (signal (SIGINT, SIG_IGN) != SIG_IGN)
signal (SIGINT, sighandler);
if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
signal (SIGHUP, sighandler);
if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
signal (SIGPIPE, sighandler);
if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
signal (SIGTERM, sighandler);
#endif /* !_POSIX_VERSION */
gkey.sword = gkey.eword = -1;
gkey.ignore = NULL;
gkey.translate = NULL;
gkey.numeric = gkey.month = gkey.reverse = 0;
gkey.skipsblanks = gkey.skipeblanks = 0;
files = (char **) xmalloc (sizeof (char *) * argc);
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '+')
{
if (key)
insertkey (key);
key = (struct keyfield *) xmalloc (sizeof (struct keyfield));
key->eword = -1;
key->ignore = NULL;
key->translate = NULL;
key->skipsblanks = key->skipeblanks = 0;
key->numeric = key->month = key->reverse = 0;
s = argv[i] + 1;
if (!digits[UCHAR (*s)])
badfieldspec (argv[i]);
for (t = 0; digits[UCHAR (*s)]; ++s)
t = 10 * t + *s - '0';
t2 = 0;
if (*s == '.')
for (++s; digits[UCHAR (*s)]; ++s)
t2 = 10 * t2 + *s - '0';
if (t2 || t)
{
key->sword = t;
key->schar = t2;
}
else
key->sword = -1;
s = set_ordering (s, key, bl_start);
if (*s)
badfieldspec (argv[i]);
}
else if (argv[i][0] == '-' && argv[i][1])
{
s = argv[i] + 1;
if (digits[UCHAR (*s)])
{
if (!key)
usage ();
for (t = 0; digits[UCHAR (*s)]; ++s)
t = t * 10 + *s - '0';
t2 = 0;
if (*s == '.')
for (++s; digits[UCHAR (*s)]; ++s)
t2 = t2 * 10 + *s - '0';
key->eword = t;
key->echar = t2;
s = set_ordering (s, key, bl_end);
if (*s)
badfieldspec (argv[i]);
insertkey (key);
key = NULL;
}
else
while (*s)
{
s = set_ordering (s, &gkey, bl_both);
switch (*s)
{
case '\0':
break;
case 'c':
checkonly = 1;
break;
case 'k':
if (s[1])
++s;
else
{
if (i == argc - 1)
error (2, 0, "option `-k' requires an argument");
else
s = argv[++i];
}
if (key)
insertkey (key);
key = (struct keyfield *)
xmalloc (sizeof (struct keyfield));
key->eword = -1;
key->ignore = NULL;
key->translate = NULL;
key->skipsblanks = key->skipeblanks = 0;
key->numeric = key->month = key->reverse = 0;
/* Get POS1. */
if (!digits[UCHAR (*s)])
badfieldspec (argv[i]);
for (t = 0; digits[UCHAR (*s)]; ++s)
t = 10 * t + *s - '0';
if (t)
t--;
t2 = 0;
if (*s == '.')
{
for (++s; digits[UCHAR (*s)]; ++s)
t2 = 10 * t2 + *s - '0';
if (t2)
t2--;
}
if (t2 || t)
{
key->sword = t;
key->schar = t2;
}
else
key->sword = -1;
s = set_ordering (s, key, bl_start);
if (*s && *s != ',')
badfieldspec (argv[i]);
else if (*s++)
{
/* Get POS2. */
for (t = 0; digits[UCHAR (*s)]; ++s)
t = t * 10 + *s - '0';
t2 = 0;
if (*s == '.')
{
for (++s; digits[UCHAR (*s)]; ++s)
t2 = t2 * 10 + *s - '0';
if (t2)
t--;
}
key->eword = t;
key->echar = t2;
s = set_ordering (s, key, bl_end);
if (*s)
badfieldspec (argv[i]);
}
insertkey (key);
key = NULL;
goto outer;
case 'm':
mergeonly = 1;
break;
case 'o':
if (s[1])
outfile = s + 1;
else
{
if (i == argc - 1)
error (2, 0, "option `-o' requires an argument");
else
outfile = argv[++i];
}
goto outer;
case 's':
stable = 1;
break;
case 't':
if (s[1])
tab = *++s;
else if (i < argc - 1)
{
tab = *argv[++i];
goto outer;
}
else
error (2, 0, "option `-t' requires an argument");
break;
case 'u':
unique = 1;
break;
default:
fprintf (stderr, "%s: unrecognized option `-%c'\n",
argv[0], *s);
usage ();
}
if (*s)
++s;
}
}
else /* Not an option. */
{
files[nfiles++] = argv[i];
}
outer:;
}
if (key)
insertkey (key);
/* Inheritance of global options to individual keys. */
for (key = keyhead.next; key; key = key->next)
if (!key->ignore && !key->translate && !key->skipsblanks && !key->reverse
&& !key->skipeblanks && !key->month && !key->numeric)
{
key->ignore = gkey.ignore;
key->translate = gkey.translate;
key->skipsblanks = gkey.skipsblanks;
key->skipeblanks = gkey.skipeblanks;
key->month = gkey.month;
key->numeric = gkey.numeric;
key->reverse = gkey.reverse;
}
if (!keyhead.next && (gkey.ignore || gkey.translate || gkey.skipsblanks
|| gkey.reverse || gkey.skipeblanks
|| gkey.month || gkey.numeric))
insertkey (&gkey);
if (nfiles == 0)
{
nfiles = 1;
files = −
}
if (checkonly)
exit (check (files, nfiles) != 0);
if (strcmp (outfile, "-"))
{
for (i = 0; i < nfiles; ++i)
if (!strcmp (outfile, files[i]))
break;
if (i == nfiles)
ofp = xfopen (outfile, "w");
else
{
char buf[8192];
FILE *fp = xfopen (outfile, "r");
int cc;
tmp = tempname ();
ofp = xfopen (tmp, "w");
while ((cc = fread (buf, 1, sizeof buf, fp)) > 0)
xfwrite (buf, 1, cc, ofp);
if (ferror (fp))
{
error (0, errno, "%s", outfile);
cleanup ();
exit (2);
}
xfclose (ofp);
xfclose (fp);
files[i] = tmp;
ofp = xfopen (outfile, "w");
}
}
else
ofp = stdout;
if (mergeonly)
merge (files, nfiles, ofp);
else
sort (files, nfiles, ofp);
cleanup ();
if (have_read_stdin && fclose (stdin) == EOF)
error (1, errno, "-");
if (ferror (stdout) || fclose (stdout) == EOF)
error (1, 0, "write error");
exit (0);
}
static void
usage ()
{
fprintf (stderr, "\
Usage: %s [-cmus] [-t separator] [-o output-file] [-bdfiMnr] [+POS1 [-POS2]]\n\
[-k POS1[,POS2]] [file...]\n",
program_name);
exit (2);
}
|