modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / natsort / natsort.c
blob9e8b069bf29bbe9978f6f612d8d58d42aad3a450
1 /* -*- mode: c; c-file-style: "k&r" -*-
3 natsort.c -- Example strnatcmp application.
5 Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
24 /* Partial change history:
26 * 2003-03-18: Add --reverse option, from Alessandro Pisani.
29 #define _GNU_SOURCE
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <getopt.h>
36 #include "strnatcmp.h"
38 #if defined(__GNUC__)
39 # define UNUSED __attribute__((__unused__))
40 #endif
42 static int fold_case = 0, verbose = 0, reverse = 0;
44 static void trace_result(char const *a, char const *b, int ret)
46 char const *op;
48 if (ret < 0)
49 op = "<";
50 else if (ret > 0)
51 op = ">";
52 else
53 op = "==";
55 fprintf(stderr, "\tstrncatcmp: \"%s\" %s \"%s\"\n",
56 a, op, b);
61 static int compare_strings(const void *a, const void *b)
63 char const *pa = *(char const **)a, *pb = *(char const **)b;
64 int ret;
66 if (fold_case)
67 ret = strnatcasecmp(pa, pb);
68 else
69 ret = strnatcmp(pa, pb);
71 if (reverse)
72 ret *= -1;
74 if (verbose)
75 trace_result(pa, pb, ret);
77 return ret;
81 static void usage(void)
83 fprintf(stderr, "Usage: natsort [OPTIONS]\n"
84 "Performs a natural sort on standard input, and writes to \n"
85 "standard output.\n"
86 "\n"
87 " --help, -h show help text\n"
88 " --verbose, -v show comparisons\n"
89 " --fold-case, -f ignore case differences for letters\n"
90 " --reverse, -r reverse the result of comparisons\n");
94 int main(int argc, char **argv)
96 int nlines = 0;
97 char *line;
98 char **list = 0;
99 int linelen = 0, i;
100 int c, opt_ind;
101 size_t bufsize;
103 static struct option long_options[] = {
104 { "verbose", 0, NULL, 'v'},
105 { "reverse", 0, NULL, 'r'},
106 { "fold-case", 0, NULL, 'f'},
107 { "help", 0, 0, 'h' },
108 { 0, 0, 0, 0 }
111 /* process arguments */
112 while ((c = getopt_long(argc, argv, "frvh", long_options, &opt_ind)) != -1) {
113 switch (c) {
114 case 'f':
115 fold_case = 1;
116 break;
117 case 'h':
118 usage();
119 return 0;
120 case 'r':
121 reverse = 1;
122 break;
123 case 'v':
124 verbose = 1;
125 break;
126 case '?':
127 return 1;
128 default:
129 abort();
133 /* read lines into an array */
134 while (1) {
135 line = NULL;
136 bufsize = 0;
137 if ((linelen = getline(&line, &bufsize, stdin)) <= 0)
138 break;
139 if (line[linelen-1] == '\n')
140 line[--linelen] = 0;
141 nlines++;
142 list = (char **) realloc(list, nlines * sizeof list[0]);
143 if (!list) {
144 perror("allocate list");
145 return 1;
147 list[nlines-1] = line;
150 if (ferror(stdin)) {
151 perror("input");
152 return 1;
154 fclose(stdin);
156 /* quicksort */
157 qsort(list, nlines, sizeof list[0], compare_strings);
159 /* and output */
160 for (i = 0; i < nlines; i++) {
161 puts(list[i]);
163 if (ferror(stdout)) {
164 perror("output");
165 return 1;
168 return 0;