Re: Order of files with 'cp'

From: Brian Candler (B.Candler_at_pobox.com)
Date: 11/18/05

  • Next message: Matthew Dillon: "7-CURRENT-SNAP009-amd64-bootonly.iso on Shuttle XPC w/ AMD X2 (was Re: Side note on Shuttle XPC)"
    Date: Fri, 18 Nov 2005 18:36:41 +0000
    To: Eric Anderson <anderson@centtech.com>
    
    

    On Fri, Nov 18, 2005 at 09:45:27AM -0600, Eric Anderson wrote:
    > Brian Candler wrote:
    > >>This just adds a -o flag to cp, which preserves order.
    > >
    > >
    > >Hmm, that's another solution that I hadn't thought of.
    > >
    > >Advantages: simple to implement. (Even simpler if you use the ?: operator).
    > >
    > >Disadvantages: it's still strange that the default behaviour is to copy the
    > >files in an arbitary shuffled order. The manpage will need updating to
    > >document the -o flag, and hence will have to explain the strangeness.
    > >Commands arguably have too many flags already.
    >
    > I didn't think cp (or any tool, like tar) did it 'arbitrarily', but in
    > order of mtime. Is that not true?

    No, it's not true, for cp anyway.

    As far as I can tell, cp indirectly calls qsort() on the source items, using
    its own mastercmp() function to compare them. The only comparison it does is
    whether each item is a file or a directory.

    qsort() is not a stable sort, so even if all items compare equal, it has a
    habit of shuffling them around.

    brian@mappit brian$ cat x.c
    #include <stdio.h>
    #include <stdlib.h>

    static int foo(const void *a, const void *b)
    {
        return 0;
    }

    #define NMEM 7

    int main(void)
    {
        int a[NMEM] = {1,2,3,4,5,6,7};
        int i;
        
        for (i=0; i<NMEM; i++) printf("%d ", a[i]);
        printf("\n");

        qsort(a, NMEM, sizeof(int), foo);
        for (i=0; i<NMEM; i++) printf("%d ", a[i]);
        printf("\n");
        
        return 0;
    }
    brian@mappit brian$ gcc -Wall -o x x.c
    brian@mappit brian$ ./x
    1 2 3 4 5 6 7
    4 2 3 1 5 6 7
    brian@mappit brian$
    _______________________________________________
    freebsd-current@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-current
    To unsubscribe, send any mail to "freebsd-current-unsubscribe@freebsd.org"


  • Next message: Matthew Dillon: "7-CURRENT-SNAP009-amd64-bootonly.iso on Shuttle XPC w/ AMD X2 (was Re: Side note on Shuttle XPC)"

    Relevant Pages

    • Re: msg box font
      ... Thanks a lot Brian ... the app I used (Reg Shot) had the change. ... > There's going to be word wrap on the keys here. ... > Note that each line shown in the compare alternates before/after. ...
      (microsoft.public.windowsxp.customize)
    • Re: Letter frequency of Chinese pinyin
      ... >Brian M. Scott wrote: ... >int compare ... >{register FREQ *p; ...
      (sci.lang)
    • Re: Letter frequency of Chinese pinyin
      ... Brian M. Scott wrote: ... int compare ... {register FREQ *p; ... register char *p; ...
      (sci.lang)
    • Re: Passing pointers?
      ... I have several introductory C books Brian. ... void x ... int z=99; ... have you bought yourself an introductory book on C as I have suggested? ...
      (microsoft.public.vc.language)
    • Re: Latest Usenet C Argument
      ... Many people find zero-based indexing difficult and non-intuitive. ... In the beginning Brian created the stdio.h and the main. ... upon the face of the kernel. ... Let there be int and there was int main. ...
      (comp.lang.c)