|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T m
Length: 2864 (0xb30)
Types: TextFile
Names: »mksort.c«
└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
└─⟦2fafebccf⟧ »EurOpenD3/mail/smail3.1.19.tar.Z«
└─⟦bcd2bc73f⟧
└─⟦this⟧ »util/mksort.c«
/* @(#)mksort.c 3.3 5/15/88 18:44:14 */
/*
* Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
*
* See the file COPYING, distributed with smail, for restriction
* and warranty information.
*/
/*
* mksort.c:
* Take a list of lines and sort them. If the flag -f is given,
* then ignore case in comparisons.
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "defs.h"
#include "smail.h"
#include "extern.h"
#include "field.h"
#include "addr.h"
#include "dys.h"
#include "exitcodes.h"
char *program; /* argv[0] from main */
int debug = 0;
FILE *errfile = stderr;
/*ARGSUSED*/
void
main(argc, argv)
int argc;
char **argv;
{
struct str strings;
int use_strcmp();
int use_strcmpic();
char **mkvectors();
int (*compare_fun)() = use_strcmp;
int ct = 0;
char **v;
int i;
program = *argv++;
/* if -f, then perform case-folded comparisons */
if (*argv && EQ(*argv, "-f")) {
compare_fun = use_strcmpic;
argv++;
}
STR_INIT(&strings);
if (*argv == NULL) {
ct += read_lines(&strings, stdin);
}
while (*argv) {
FILE *f;
if (EQ(*argv, "-")) {
f = stdin;
} else {
f = fopen(*argv, "r");
if (f == NULL) {
(void) fprintf(stderr, "%s: cannot open %s: ", program, *argv);
perror("");
exit(errno);
}
}
ct += read_lines(&strings, f);
argv++;
}
v = mkvectors(ct, strings.p);
qsort((char *)v, ct, sizeof(char *), compare_fun);
for (i = 0; i < ct; i++) {
printf("%s\n", v[i]);
}
exit(0);
}
int
read_lines(ssp, f)
register struct str *ssp;
register FILE *f;
{
register int c;
int ct = 0;
while ((c = getc(f)) != EOF) {
if (c == '\n') {
STR_NEXT(ssp, '\0');
ct++;
} else {
STR_NEXT(ssp, c);
}
}
return ct;
}
char **
mkvectors(ct, strings)
int ct; /* count of strings */
char *strings; /* null-terminated strings */
{
char **v;
register char **vp;
register char *sp = strings;
v = vp = (char **)xmalloc(ct * sizeof(char *));
for (sp = strings; ct; ct--, sp += strlen(sp) + 1) {
*vp++ = sp;
}
return v;
}
use_strcmp(a, b)
char **a;
char **b;
{
return strcmp(*a, *b);
}
use_strcmpic(a, b)
char **a;
char **b;
{
return strcmpic(*a, *b);
}
/*
* standalone versions of some referenced routines
*/
char *
xmalloc(len)
int len;
{
char *malloc();
register char *ret = malloc(len);
if (ret == NULL) {
(void) fprintf(stderr, "%s: out of memory!\n", program);
}
return ret;
}
char *
xrealloc(s, len)
char *s;
int len;
{
char *realloc();
register char *ret = realloc(s, len);
if (ret == NULL) {
(void) fprintf(stderr, "%s: out of memory!\n", program);
}
return ret;
}
void
xfree(s)
char *s;
{
free(s);
}