Thread: Almost but not quite

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Almost but not quite

    I've written a function which I should be able to use with the scandir function. Basically it should sort a list of numbers, this almost works, i.e. it sorts them but not all of them eg. i get the following

    3
    4
    5
    1000
    1001
    1002
    1
    2
    400
    ..
    ..
    ..
    ..
    ..
    800

    Anyway here's the code that so nearly works.

    Code:
    int main(void){
      int result,count,index;   /*Set Conditions*/
      char path[] = "/proc";    /*Path to the proc directory*/
      struct dirent **direntp;  /*Directory entry pointer*/
      struct stat sbuf;         /*Holds information about files*/
    
      if (stat(path,&sbuf) != -1){
        if(S_ISDIR(sbuf.st_mode)){
           count = scandir(path,&direntp,NULL,intcompare);/*THE CALL*/
           if(count){
             char temp[FILENAME_MAX];
             printf("PID      TTY         TIME      CMD\n");
             for(index=0; index<count; ++index){
               strcpy(temp,path);
               result = matchDetails(direntp[index]->d_name,temp);
               if(result > 0)   /*If directory name was matched*/
                 printDetails(direntp[index]->d_name,temp);
               free(direntp[index]);
             }
           }else{
             fprintf(stderr,"Can't open %s\n",path);
             exit(EXIT_FAILURE);
           }
        }else
          return;
      }else{
        fprintf(stderr,"Can't stat %s\n",path);
        return;
      } 
      return EXIT_SUCCESS;
    }
    
    ....
    ....
    ....
    
    /*Used for sorting processes*/
    int intcompare(const struct dirent **x,const struct dirent **y){
      int *xp = (int*) x;
      int *yp = (int*) y;
      if(*xp < *yp)
        return -1;
      else if(*xp > *yp)
        return +1;
      else
        return 0;
    }
    PuterPaul.co.uk - Portfolio site

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Also tried this

    Code:
    int compare(const struct dirent **x, const struct dirent **y) {
      int result;
      result = strcmp((*x)->d_name, (*y)->d_name);
      if(result < 0)
        return -1;
      else if(result > 0)
        return +1;
      else
        return 0;
    }
    And this

    Code:
    int compare(const struct dirent **x, const struct dirent **y) {
      return strcmp((*x)->d_name, (*y)->d_name);
    }
    PuterPaul.co.uk - Portfolio site

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Nah using alphasort doesn't work, cos I'm sorting directorys with numerical names. I have the following which should work, but for some reason it causes a segmentation fault?? Any ideas??

    Code:
    int intcompare(const void* a,const void* b){
      struct dirent* const *x=a, *const *y=b;
      return atoi((*x)->d_name - atoi((*y)->d_name));
    }
    EDIT: I should get output like this, except pid column should be sorted.

    Code:
    PID      TTY         TIME      CMD
    1        /dev/pts/0  00:00:05  (init)
    11267    ?           00:00:00  (login)
    11268    ?           00:00:00  (bash)
    11291    ?           00:00:00  (startx)
    11298    ?           00:00:00  (xinit)
    11299    /dev/pts/0  00:00:02  (X)
    11301    ?           00:00:00  (gnome-session)
    11315    ?           00:00:00  (gnome-smproxy)
    11321    ?           00:00:00  (enlightenment)
    11323    ?           00:00:00  (magicdev)
    11336    /dev/pts/0  00:00:00  (gnome-name-serv)
    
    11340    ?           00:00:00  (panel)
    11342    ?           00:00:00  (gmc)
    11344    ?           00:00:00  (gnome-help-brow)
    11354    /dev/pts/0  00:00:00  (gnomepager_appl)
    11356    /dev/pts/0  00:00:00  (gen_util_applet)
    11380    ?           00:00:00  (gnome-terminal)
    11381    ?           00:00:00  (gnome-pty-helpe)
    11382    ?           00:00:00  (bash)
    11392    ?           00:00:00  (emacs)
    11499    ?           00:00:00  (a.out)
    2        /dev/pts/0  00:00:00  (kflushd)
    293      /dev/pts/0  00:00:00  (pump)
    3        /dev/pts/0  00:00:00  (kupdate)
    307      /dev/pts/0  00:00:00  (portmap)
    322      /dev/pts/0  00:00:00  (lockd)
    323      /dev/pts/0  00:00:00  (rpciod)
    332      /dev/pts/0  00:00:00  (rpc.statd)
    346      /dev/pts/0  00:00:00  (apmd)
    373      /dev/pts/0  00:00:00  (automount)
    3733     /dev/pts/0  00:00:00  (httpd)
    3734     /dev/pts/0  00:00:00  (httpd)
    3735     /dev/pts/0  00:00:00  (httpd)
    3736     /dev/pts/0  00:00:00  (httpd)
    3737     /dev/pts/0  00:00:00  (httpd)
    3738     /dev/pts/0  00:00:00  (httpd)
    3739     /dev/pts/0  00:00:00  (httpd)
    3740     /dev/pts/0  00:00:00  (httpd)
    4        /dev/pts/0  00:00:00  (kpiod)
    426      /dev/pts/0  00:00:00  (syslogd)
    435      /dev/pts/0  00:00:00  (klogd)
    449      /dev/pts/0  00:00:00  (identd)
    453      /dev/pts/0  00:00:00  (identd)
    454      /dev/pts/0  00:00:00  (identd)
    455      /dev/pts/0  00:00:00  (identd)
    456      /dev/pts/0  00:00:00  (identd)
    467      /dev/pts/0  00:00:00  (atd)
    481      /dev/pts/0  00:00:00  (crond)
    499      /dev/pts/0  00:00:00  (inetd)
    5        /dev/pts/0  00:00:00  (kswapd)
    513      /dev/pts/0  00:00:00  (lpd)
    561      /dev/pts/0  00:00:00  (sendmail)
    576      /dev/pts/0  00:00:00  (gpm)
    590      /dev/pts/0  00:00:00  (httpd)
    6        /dev/pts/0  00:00:00  (mdrecoveryd)
    669      /dev/pts/0  00:00:00  (xfs)
    710      ?           00:00:00  (mingetty)
    711      ?           00:00:00  (mingetty)
    712      ?           00:00:00  (mingetty)
    713      ?           00:00:00  (mingetty)
    714      ?           00:00:00  (mingetty)
    Last edited by pdstatha; 04-01-2002 at 08:19 AM.
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed