Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
- Subject: Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
- From: Vernon Williams <email@hidden>
- Date: Mon, 19 Nov 2007 04:03:40 -0600
Monday, November 19, 2007, 3:16am
For whatever it is worth, attached below is a little C program I just
hacked out to list its command arguments to standard output,
in order, but with any duplicates (except for the first) removed.
It could readily be converted into a filter as well. It is a fast,
general
purpose program for removing duplicates, using two qsorts,
which might be useful in the current problem, at least for the
algorithm. I have not tested it extensively, but it seems to work
for the cases I have tried. If anybody tries it and finds any problems,
I would be glad to hear about it.
Vernon Williams
On Nov 18, 2007, at 10:33 PM, Andrew J. Hesford wrote:
On Nov 18, 2007, at 8:24 PM, Mark J. Reed wrote:
IMESHO, this particular program might be best written in a different
language. C, for instance. Or Perl/Python/Ruby/Tcl - they all ship
standard with OSX, right? Any of them would probably do this more
efficiently than bash; it's not the sort of thing the shell is
optimized for.
I re-implemented path_helper in Perl, and it's attached. I'd
appreciate if people would give it a try. I'm no Perl hacker, so maybe
it's horrendously stupid, but it seems okay to me. It seems to
duplicate the exact functionality of the original path_helper. Any
existing PATH or MANPATH contents are preserved, in order. Next, the
/etc/{man,}paths files are read for defaults, and finally, the files
in /etc/{man,}paths.d are processed.
For efficient checking for duplicates, I use hashes. Perl is supposed
to be good at this, so it should be reasonably efficient. Time tests
reveal no noticeable difference in user time (0.01 sec) or system time
(0.00 sec), but the wall-clock time of my script is consistently half
of the system path_helper run time (0.011 versus 0.022 sec). This even
includes the simplified glob posted earlier by Nate.
Oh, and it works properly with LaTeXiT, too. (Does anybody care about
this but me?)
<path_helper.pl>
--
Andrew J. Hesford <email@hidden>
Department of Electrical and Computer Engineering
University of Illinois at Urbana-Champaign
/* C Program remove_dups Level 0 Date 11/19/07
/*
/* Purpose: to remove duplicated arguments and write the remaining
/* arguments to standard output, one per line; if an
/* argument is duplicated, then only the first occurrence
/* is kept.
/*
/* Usage: remove_dups { arg }
/*
/* Written by Vernon Williams, November 19, 2007.
/*
/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* types: */
struct sort_item {
char * item;
int index;
};
int main (int num_args, char * * args)
{
/* local variables and arrays: */
int num_items;
int nitems;
int it;
int status;
struct sort_item * items;
/* external functions: */
int item_comparison (const void *, const void *);
int int_comparison (const void *, const void *);
status = 1;
if (num_args > 1) {
num_items = num_args - 1;
/* allocate the items array (arguments and argument numbers): */
items = (struct sort_item *) malloc (num_items *
sizeof (struct sort_item));
if (items != NULL) {
/* allocation succeeded, so set up the items array: */
for (it = 0; it <= num_items - 1; it++) {
items [it].item = args [it + 1];
items [it].index = it + 1;
}
/* sort the items array by argument and then argument number: */
qsort (& (items [0]), num_items, sizeof (items [0]),
& item_comparison);
/* remove each of a sequence of items with */
/* the same argument, except for the first: */
nitems = 1;
for (it = 0; it <= num_items - 2; it++) {
if (strcmp (items [it].item, items [it + 1].item) != 0) {
if (nitems < it + 1) {
items [nitems] = items [it + 1];
}
nitems++;
}
}
num_items = nitems;
/* sort the remaining items by argument number: */
qsort (& (items [0]), num_items, sizeof (items [0]), int_comparison);
/* write the remaining arguments to standard output */
/* in the proper order, one per line: */
for (it = 0; it <= num_items - 1; it++) {
printf ("%s\n", items [it].item);
}
status = 0;
}
}
return status;
}
/*--------------------------------------------------------------------------*/
/* compare the strings in two items, and the indices in the two items */
/* if the two strings are the same: */
int item_comparison (const void * item1_ptr, const void * item2_ptr)
{
/* local variables: */
struct sort_item * itm1_ptr;
struct sort_item * itm2_ptr;
char * str1;
char * str2;
int ind1;
int ind2;
int result;
itm1_ptr = (struct sort_item *) item1_ptr;
itm2_ptr = (struct sort_item *) item2_ptr;
str1 = (* itm1_ptr).item;
str2 = (* itm2_ptr).item;
result = strcmp (str1, str2);
if (result == 0) {
ind1 = (* itm1_ptr).index;
ind2 = (* itm2_ptr).index;
if (ind1 < ind2) {
result = -1;
} else if (ind1 > ind2) {
result = 1;
} else {
result = 0;
}
}
return result;
}
/*--------------------------------------------------------------------------*/
/* compare the indices in two items: */
int int_comparison (const void * item1_ptr, const void * item2_ptr)
{
/* local variables: */
struct sort_item * itm1_ptr;
struct sort_item * itm2_ptr;
int ind1, ind2;
int result;
itm1_ptr = (struct sort_item *) item1_ptr;
itm2_ptr = (struct sort_item *) item2_ptr;
ind1 = (* itm1_ptr).index;
ind2 = (* itm2_ptr).index;
if (ind1 < ind2) {
result = -1;
} else if (ind1 > ind2) {
result = 1;
} else {
result = 0;
}
return result;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
X11-users mailing list (email@hidden)
This email sent to email@hidden