From: Andrew Poelstra on
On 2010-02-07, Rainer Weikusat <rweikusat(a)mssgmbh.com> wrote:
> pjb(a)informatimago.com (Pascal J. Bourguignon) writes:
>> Rainer Weikusat <rweikusat(a)mssgmbh.com> writes:
>>>
>>> There is no such thing as 'normal integer arithmetic'
>>
>> Of course. Ask anybody in the street how much is 4294967295 + 1.
>
> Since when is the belief of a majority of ignorants (ignorant on this
> particular topic) a proof?
>

I think that "the belief of a majority of ignorants" is the
definition of normal.

;)

From: frank on
Scott Lurndal wrote:

>> [lots of code, so big snips]

> Rather than a linked list, how about using the standard tsearch function?

Thanks, Scott, I have a mixed bag of success. Certainly there weren't
to be so many variables unused:


dan(a)dan-desktop:~/source42$ gcc -g -D_GNU_SOURCE -Wall -Wextra b1.c -o out
b1.c:35: warning: unused parameter �depth�
b1.c:66: warning: unused parameter �sp�
b1.c:67: warning: unused parameter �typeflag�
b1.c:67: warning: unused parameter �ftwbuf�
b1.c:92: warning: unused parameter �argc�
b1.c:92: warning: unused parameter �envp�
dan(a)dan-desktop:~/source42$ ./out
Segmentation fault
dan(a)dan-desktop:~/source42$ ./out /home/
^C
dan(a)dan-desktop:~/source42$ ./out /home/dan/source42/
/home/dan/source42
/home/dan/source42/b1.c
/home/dan/source42/b1.c~
/home/dan/source42/out
dan(a)dan-desktop:~/source42$ cat b1.c
#include <errno.h>
#include <ftw.h>
#include <memory.h>
#include <search.h>
#include <string.h>
#include <stdio.h>

void *root = NULL;
const char *myname;

/**
* tsearch(3) comparison function. The keys are nul-terminated
* arrays of type char.
*
* @param a The key being searched for
* @param b A key in the tree to compare against.
* @returns -1 if a collates before b
* 0 if a and b collate identically.
* +1 if a collates after b
*/
int
path_compare(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}

/**
* Print a tree node. The key for each node is a pathname.
*
* @param np A pointer to the node.
* @param which Point in tree traversal
* @param depth Depth of the tree.
*/
void
path_print(const void *np, const VISIT which, const int depth)
{
struct _node {
const char *key;
} *kp = (struct _node *)np;

switch (which) {
case preorder:
case endorder:
break;
case postorder:
case leaf:
fprintf(stdout, "%s\n", kp->key);
break;
}
}

/**
* nftw(3) callback function. Insert the provided path
* into the tree. Use the appropriate return value to
* avoid descending into subdirectories if desired.
*
* @param path The pathname for a directory entry.
* @param sp The corresponding struct stat
* @param typeflag Metadata
* @param ftwbuf Basename offset and path depth
* @returns FTW_STOP To stop walking the filesystem tree
* FTW_CONTINUE Continue walking the tree
* FTW_SKIP_SUBTREE Don't walk into this directory
*/
int
ftw_callback(const char *path, const struct stat *sp,
int typeflag, struct FTW* ftwbuf)
{
void *key = strdup(path);

if (key == NULL) {
fprintf(stderr, "%s: Unable to allocate memory for tree node: %m\n",
myname);
return FTW_STOP;
}

key = tsearch(key, &root, path_compare);
if (key == NULL) {
fprintf(stderr, "%s: Unable to allocate memory for tree node: %m\n",
myname);
return FTW_STOP;
}

return FTW_CONTINUE;
}

/**
* Walk a file system directory and insert the entries into a red-black
* tree.
*/
int
main(int argc, char **argv, char **envp)
{
int diag;

myname = argv[0];

diag = nftw(argv[1], ftw_callback, 20, 0);
if (diag == -1) {
fprintf(stderr, "%s: Unable to walk filesystem tree: %m\n",
argv[0]);
return 1;
}

twalk(root, path_print);

return 0;
}
/* vim: sw=4 sts=4 sta ts=8:
*/
// gcc -g -D_GNU_SOURCE -Wall -Wextra b1.c -o out
// -D_XOPEN_SOURCE=500
dan(a)dan-desktop:~/source42$

Where do I find out more about the -D* compiler switches? Apparently
I'm asking a question specifically about glibc.

I spent an hour reading about compiler switches in prel today, and I
think it perfectly reasonable for anyone in the contemporary open-source
movement to be almost constantly confused by them.

Cheers,
--
frank
From: Phred Phungus on
Moi wrote:
> On Wed, 27 Jan 2010 00:21:30 -0700, frank wrote:
>
>> Hello again comp.unix.programer,gnu.gcc.help
>>
>
>> What follows is the readdir() I understand best and then working code
>> from unleashed that has a 2-d resizable array.
>>
>> Jens Thoerring has given me the best design idea that I have right now,
>> but if you think you have a better one given the source below, then I'm
>> all ears.
>
> Ok, I cleaned it up a bit, removing the need for multidimentional arrays
> and ***pointers. (it does use the struct hack, but that is still allowed in c89/c90 ;-)
>
> Sorry for the mixed indentation ...
>
> *****************************/
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <errno.h>
>
> #include <unistd.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <dirent.h>
>
> char * strdup ( const char *org ); /* sorry! */
> ssize_t readlink(const char *path, char *buf, size_t bufsiz); /* sorry */
>
> #define MOI AvK
> #define SHOW_BAGS 0
> #define SHOW_PROGRESS 0
> #define PATH_SIZE 4096
>
> struct bag {
> unsigned used;
> unsigned size;
> char *data[1]; /* struct hack here ... */
> };
>
> struct bag * bag_add( struct bag * bag, char *str );
> char * bag_del( struct bag * bag );
> struct bag * bag_resize( struct bag * old, unsigned size );
> struct bag * get_files( struct bag *dirs );
>
> /* NB: this program does about the same as "find arg[1]... arg[n] -type f"
> * (except for some details, such as ordering and the handling of symlinks)
> */
> int main( int argc, char *argv[] )
> {
> struct bag *todo=NULL, *result=NULL;
> unsigned iter;
>
> while( argc-- > 1 ) {
> #if SHOW_PROGRESS
> fprintf( stderr, "Add to todo list[%d]: %s\n", argc, argv[argc] );
> #endif
> todo = bag_add ( todo, strdup( argv[argc] ) );
> }
>
> result = get_files ( todo );
>
> if ( result ) for ( iter = 0; iter < result->used; iter++ ) {
> fprintf( stdout, "[%u]: %s\n", iter, result->data[iter] );
> }
> return 0;
> }
>
> struct bag * get_files( struct bag *dirs )
> {
> DIR *dir = NULL;
> struct dirent *dp = NULL;
> char *dirname, *fullpath ;
> size_t dirlen, namelen;
> struct bag *files = NULL;
> int rc;
> for ( ; dirname = bag_del(dirs) ; free(dirname) ) {
> /* Open the given directory, if we can. */
> #if SHOW_PROGRESS
> fprintf ( stderr, "Processing %s\n", dirname );
> #endif
> dir = opendir ( dirname );
> if ( !dir ) {
> fprintf ( stderr, "Error opening %s: %d(%s)\n", dirname, errno, strerror (errno));
> continue;
> }
>
> dirlen = strlen( dirname );
> while ( dp = readdir(dir )) {
> struct stat sst;
>
> if ( !strcmp ( dp->d_name, "." ) || !strcmp ( dp->d_name, ".." ) ) continue;
> namelen = strlen( dp->d_name );
>
> fullpath = malloc( dirlen + 1 + namelen +1 );
> if ( !fullpath ) {
> fprintf( stderr,"Name too long: %u+1+%u+1 %s/%s\n"
> , (unsigned) dirlen, (unsigned) namelen, dirname, dp->d_name );
> continue; }
> memcpy ( fullpath, dirname, dirlen );
> fullpath[dirlen] = '/';
> memcpy ( fullpath+dirlen+1, dp->d_name, namelen +1 );
>
> if ( lstat ( fullpath, &sst ) == -1 ) { /* stat() failed, let's party */
> fprintf ( stderr, "Error statting %s: %s\n", fullpath, strerror (errno)); continue; }
>
> if ( S_ISDIR ( sst.st_mode )) { /* directory */
> #if SHOW_PROGRESS
> fprintf ( stderr, "\tAdding Dir %s:/\n", fullpath );
> #endif
> dirs = bag_add( dirs, fullpath );
> continue; }
>
> if ( S_ISREG ( sst.st_mode )) { /* regular file */
> #if SHOW_PROGRESS
> fprintf ( stderr, "\t%s has %lld bytes\n"
> , fullpath, (long long) sst.st_size );
> #endif
> files = bag_add( files, fullpath );
> continue; }
>
> if ( S_ISLNK ( sst.st_mode )) { /* symbolic link */
> char targetName[PATH_SIZE + 1];
> if ( readlink ( fullpath, targetName, PATH_SIZE ) == -1) {
> fprintf ( stderr, "\t%s -> (invalid symbolic link!)\n", fullpath );
> goto skip; }
>
> #if SHOW_PROGRESS
> strcpy( targetName+PATH_SIZE-3, "/.." );
> fprintf ( stderr, "\t%s -> %s\n", fullpath, targetName);
> #endif
> goto skip; }
>
> fprintf ( stderr, "\t%s :Strange Mode: 0x%x\n", fullpath, sst.st_mode);
> skip:
> free( fullpath);
> }
>
> rc = closedir ( dir );
> if (rc == -1) fprintf ( stderr, "Error closdir(%s): %s", dirname, strerror (errno));
> }
> return files;
> }
>
> /***************/
> struct bag * bag_add( struct bag * bag, char *str )
> {
>
> #if SHOW_BAGS
> fprintf( stderr, "Bag_add(%u+%s)\n", bag?bag->used:0, str );
> #endif
>
> if ( !bag || !bag->size ) bag = bag_resize( bag, 2 );
> else if ( bag->used > bag->size ) bag = bag_resize( bag, 2 * bag->size );
>
> if ( !bag || bag->used > bag->size ) {
> fprintf( stderr, "The bag got robbed.\n" );
> return bag;
> }
> bag->data[bag->used++] = str;
> return bag;
> }
>
> char * bag_del( struct bag * bag )
> {
>
> #if SHOW_BAGS
> fprintf( stderr, "Bag_del(%u --> %s )\n"
> , bag?bag->used:0
> , bag && bag->used ? bag->data[bag->used-1] : "NoNe" );
> #endif
>
> if ( !bag || !bag->used ) return NULL;
>
> return bag->data[--bag->used] ;
> }
>
> struct bag * bag_resize( struct bag * old, unsigned size )
> {
> struct bag * new;
> unsigned used;
>
> #if SHOW_BAGS
> fprintf( stderr, "Bag_resize(%u -->> %u)\n", old?old->size:0, size );
> #endif
> if ( !size ) { free ( old ); return NULL; }new = malloc ( sizeof *new + size * sizeof new->data[0] );
> if ( !new ) {
> fprintf( stderr, "Bag_resize: Malloc(%u) went wrong\n"
> , (unsigned) sizeof *new + size * sizeof new->data[0] );
> return old;
> }
> used = old ? size < old->used ? size : old->used : 0;
> if ( used ) memcpy ( new->data, old->data, used * sizeof new->data[0] );
> new->used = used;
> new->size = size;
> for ( ; used < size; used++ ) new->data[used] = NULL;
> free ( old );
> return new;
> }
> /*****
>
> HTH,
> AvK
>

Thanks so much for your diligence, Moi, I won't put a struct hack on my
compiler on a sunday, in particular on my Super Bowl Holy Day.

Roger Daltrey should stop singing. I'm sure there's a willing and able
pterydactl out there to handle the screaming burden for him. The best
vocalist in that band has always been Pete, in particular, when he got
serious about music. Roger did that producing, which I hope he does
more of.

45 minutes cued. I'm ready to observe the NFL champs.

Cheers,
--

From: Phred Phungus on
Pascal J. Bourguignon wrote:

[adding alt.os.linux.ubuntu to x-post]
> Phred Phungus <Phred(a)example.invalid> writes:
>
>> Pascal J. Bourguignon wrote:
>>> frank <frank(a)example.invalid> writes:
>>>
>>>> Pascal J. Bourguignon wrote:
>>>>> frank <frank(a)example.invalid> writes:
>>>>>> So, are there any more than than these possibilities for entry.d_name:
>>>>>> 1) .
>>>>>> 2) ..
>>>>>> 3) directory
>>>>>> 4) regular file
>>>>>> 5) link
>>>>>> ?
>>>>> NAME_MAX
>>>>> Yes. There are about Σ 254^i - 5 other possibilities for entry.d_name.
>>>>> 1
>>>>> (all the byte values are possible in d_name, but 0 and 47, and
>>>>> d_name
>>>>> may be any length from 1 to NAME_MAX).
>>>>> On my system, where NAME_MAX is 255, that makes:
>>>>> 17151848561775
>>>>> 524542260230899909345527623287683809220880277204575135673770
>>>>> 350790616073358738754853669771246761598037707765815012818953
>>>>> 935481259985629589001894465990514560016269745315761470342905
>>>>> 916427583379787162454198607651001112150892505697618658680722
>>>>> 340651617534533646250823154025408246347525049252624268535973
>>>>> 202331185548752849740059467645750723512718885190900684145962
>>>>> 332212188080108626733451568195673134215363122237241696148086
>>>>> 390170981092766227408226801240032059729007827039773745754612
>>>>> 965254102893844749238702970926320276892692441496131912435884
>>>>> 815683131626478581446738498427741257604636279796430784947109
>>>> Thx, Pascal, I think I've got enough fingers and toes to count out the
>>>> possibilities there. :)
>>>>
>>>> I wouldn't mind seeing the source for this calculation.
>>> NAME_MAX
>>> The source is: Σ 254^i - 5
>>> i=1
>>>
>>> The compiled form is: (- (loop for i from 1 to 255 sum (expt 254 i)) 5)
>> But the number is larger than long long max and outputted
>> appropriately for usenet. If you don't want to post your source,
>> that's fine, but why hide a buschel?
>
> I didn't hide anything. Some programming language are actually high
> level programming language, and use normal integer arithmetic, not
> modulo 2^w arithmetic. Specifically, I use Common Lisp (Scheme and
> other programming languages would do too).
>
> Here is the copy-and-paste of a real interaction with the Common Lisp
> environment:
>
>
> C/USER[1]> (expt 2 100)
> 1267650600228229401496703205376
> C/USER[2]> (defun factorial (x) (if (< x 1) 1 (* x (factorial (- x 1)))))
> FACTORIAL
> C/USER[3]> (factorial 40)
> 815915283247897734345611269596115894272000000000
> C/USER[4]> (/ 2 3)
> 2/3
>
>
> Notice also that 2/3 is not 0, but 2/3.
>
> Also, notice that lisp is technology FIFTY years old, nothing from the
> other world. Where have you been in the last 50 years?

I'm not on the hook for all this, so I was hoping for partial credit.
>
>
>>>> How would '...' be interpreted by stat?
>>> stat(2) doesn't interpret anything, and "..." is a perfectly valid
>>> name for any directory entry.
>>>
>>> [pjb(a)galatea :0.0 tmp]$ touch ...
>>> [pjb(a)galatea :0.0 tmp]$ ls -l ...
>>> -rw-r--r-- 1 pjb wheel 0 Jan 31 01:21 ...
>>> [pjb(a)galatea :0.0 tmp]$ rm ...
>>> [pjb(a)galatea :0.0 tmp]$ sudo mknod ... b 1 2
>>> Password:
>>> [pjb(a)galatea :0.0 tmp]$ ls -l ...
>>> brw-r--r-- 1 root wheel 1, 2 Jan 31 01:22 ...
>>> [pjb(a)galatea :0.0 tmp]$ sudo rm ...
>>> [pjb(a)galatea :0.0 tmp]$ # etc
>>>
>> I have close to no idea what you mean show there, but are you possibly
>> a Galatian?
>
> I've shown that a directory entry named '...' can be either a regular
> file (created with touch), a block device (created by mknod b) a
> character device (created by mknod c), or whatever else it can be.
>
> This is called unix, not Galatian, a simple operating system FOURTY
> years old (not precisely high tech). Where have you been in the last 40
> years?

I'm looking for an opinion how I might get a lisp capability in
contemporary unix. What might be the best version number for 9.04
ubuntu? Thx for your comment.

As for where I've been for the first half of my life, I've been up
against the wall.
--
fred
From: Phred Phungus on
Huge wrote:
> On 2010-02-08, Phred Phungus <Phred(a)example.invalid> wrote:
>
>> I'm looking for an opinion how I might get a lisp capability in
>> contemporary unix.
>
> http://www.osix.net/modules/article/?id=912
>

dan(a)dan-desktop:~/source42$ sudo apt-get install clisp
[sudo] password for dan:
Reading package lists... Done
Building dependency tree
Reading state information... Done
....
created /usr/lib/clisp-2.44.1/full/lispinit.mem as expected.
-rw-r--r-- 1 root root 3255620 Feb 8 02:08
/usr/lib/clisp-2.44.1/full/lispinit.mem

Processing triggers for libc6 ...
ldconfig deferred processing now taking place
dan(a)dan-desktop:~/source42$ clisp
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8

Welcome to GNU CLISP 2.44.1 (2008-02-23) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2008

Type :h and hit Enter for context help.

[1]>

You're huge, huge. Thanks for the tip.
--
fred
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7
Prev: Reading /proc/<pid>/maps
Next: Code and Creation 73169