also sprach pcg:
> The question has come up recently (both here and in my contracting work)
> about alphabetizing the aliases and forwards as they show up in qmailadmin.
> There is a possible way to do this using scandir(3). Is this not portable?
> Or is there another reason why this isn't used? Or was it just not
> considered?

Sorry to follow up on my own post, but this was easy enough that I just did
it. Some general code cleanup should be done (as there might be unused
variables), but I wanted to get this out before I forgot.

To summarize, this patch lists the aliases and forwards in alphabetical
order, rather than pseudo-randomly. This hasn't been tested with the
MAX*PERPAGE settings, simply because I don't have that enabled.

It works for me; don't blame me if your hard drive catches on fire or you
lose copious amounts of data, &c.

/pg
-- 
Peter Green : Gospel Communications Network, SysAdmin : [EMAIL PROTECTED]
---
Audience: What will become of Linux when the Hurd is ready?
Eric Youngdale: Err... is Richard Stallman here?
(From the Linux conference in spring '95, Berlin.)

--- alias.c     Tue Aug  8 14:42:47 2000
+++ alias.c.new Mon Aug 14 22:51:02 2000
@@ -56,7 +56,9 @@
  char alias_user[MAX_FILE_NAME];
  char alias_name[MAX_FILE_NAME];
  char *alias_name_from_command;
- int i,j,stop,k,startnumber;
+ char *qmailfn;
+ struct dirent **dirlist;
+ int rc,i,j,stop,k,startnumber;
 
     if ( AdminType!=SYSTEM_ADMIN && AdminType!=DOMAIN_ADMIN ) {
         fprintf(actout,"<h2>No Authorization</h2>");
@@ -67,95 +69,99 @@
     }
        startnumber = MAXALIASESPERPAGE * (atoi(Pagenumber) - 1);
        k=0;
-       if ( (mydir = opendir(".")) == NULL ) {
-               fprintf(actout,"show dotqmail, directory permission error<BR>\n");
+
+       rc = scandir(".", &dirlist, 0, alphasort);
+       if (rc < 0) {
+               fprintf(actout,"show dotqmail, directory error<BR>\n");
                fprintf(actout,"</table>");
                return(0);
        }
+       else {
+               int cnt = 0;
+               while (cnt < rc) {
+                       strcpy (qmailfn,dirlist[cnt++]->d_name);
+                       if ( strncmp(".qmail-", qmailfn, 7) == 0 ) {
+                               if ( k < startnumber ) {
+                                       k++; 
+                                       continue;
+                               }
+                               if ( k >MAXALIASESPERPAGE + startnumber) {
+                                       break;
+                               }
 
-       while( (mydirent=readdir(mydir)) != NULL ) {
-               if ( strncmp(".qmail-", mydirent->d_name, 7) == 0 ) {
-                       if ( k < startnumber ) {
-                               k++; 
-                               continue;
-                       }
-                       if ( k >MAXALIASESPERPAGE + startnumber) {
-                               break;
-                       }
-
-                       if ( (fs=fopen(mydirent->d_name,"r"))==NULL) {
-                               fprintf(actout,"file permission error %s<BR>\n",
-                                       mydirent->d_name);
-                               fclose(fs);
-                               continue;
-                       }
-                       memset(TmpBuf2,0,MAX_BUFF);
-                       fgets( TmpBuf2, MAX_BUFF, fs);
-                       alias_name_from_command = dotqmail_alias_command(TmpBuf2);
-                       if ( alias_name_from_command != NULL ) {
-                               
-                               /* first case checks if the file beginnning is an email
-                                * and the function is not being called to display 
forwards,
-                                * the second case checks if the file beginning is an 
user, and
-                                * the type is not an alias 
-                                */
-                               
-                               if ((strstr(alias_name_from_command,"@")!=NULL) && 
-                                               (strcmp(dottype, "forward"))) {
-                                       fclose(fs);
-                                       continue; 
-                               } else if ((strstr(alias_name_from_command,"@")==NULL) 
&& 
-                                                       (strcmp(dottype, "alias"))) {
+                               if ( (fs=fopen(qmailfn,"r"))==NULL) {
+                                       fprintf(actout,"file permission error 
+%s<BR>\n",
+                                               qmailfn);
                                        fclose(fs);
                                        continue;
                                }
+                               memset(TmpBuf2,0,MAX_BUFF);
+                               fgets( TmpBuf2, MAX_BUFF, fs);
+                               alias_name_from_command = 
+dotqmail_alias_command(TmpBuf2);
+                               if ( alias_name_from_command != NULL ) {
+                               
+                                       /* first case checks if the file beginnning is 
+an email
+                                        * and the function is not being called to 
+display forwards,
+                                        * the second case checks if the file 
+beginning is an user, and
+                                        * the type is not an alias 
+                                        */
+                               
+                                       if 
+((strstr(alias_name_from_command,"@")!=NULL) && 
+                                                       (strcmp(dottype, "forward"))) {
+                                               fclose(fs);
+                                               continue; 
+                                       } else if 
+((strstr(alias_name_from_command,"@")==NULL) && 
+                                                               (strcmp(dottype, 
+"alias"))) {
+                                               fclose(fs);
+                                               continue;
+                                       }
                        
-                               
for(i=7,j=0;j<MAX_FILE_NAME-1&&mydirent->d_name[i]!=0;++i,++j) {
-                                       alias_name[j] = mydirent->d_name[i];
-                                       if ( alias_name[j] == ':' ) alias_name[j] = 
'.';
-                               }
-                               alias_name[j] = 0;
-                               stop=0;
+                                       
+for(i=7,j=0;j<MAX_FILE_NAME-1&&qmailfn[i]!=0;++i,++j) {
+                                               alias_name[j] = qmailfn[i];
+                                               if ( alias_name[j] == ':' ) 
+alias_name[j] = '.';
+                                       }
+                                       alias_name[j] = 0;
+                                       stop=0;
 
-                               fprintf(actout, "<TR><TD>%s</TD><TD align=center>", 
alias_name);
-                               while (stop == 0) {
-                                       alias_name_from_command = 
dotqmail_alias_command(TmpBuf2);
+                                       fprintf(actout, "<TR><TD>%s</TD><TD 
+align=center>", alias_name);
+                                       while (stop == 0) {
+                                               alias_name_from_command = 
+dotqmail_alias_command(TmpBuf2);
                                
-                                       /* check to see if it is an invalid line , 
-                                        * if so skip to next
-                                        */
-                                       if (alias_name_from_command == NULL ) {
-                                               if (fgets(TmpBuf2, 500, fs)==NULL) { 
-                                                       stop=1;
+                                               /* check to see if it is an invalid 
+line , 
+                                                * if so skip to next
+                                                */
+                                               if (alias_name_from_command == NULL ) {
+                                                       if (fgets(TmpBuf2, 500, 
+fs)==NULL) { 
+                                                               stop=1;
+                                                       }
+                                                       continue;
                                                }
-                                               continue;
-                                       }
                                        
-                                       strcpy(alias_user, alias_name_from_command);
+                                               strcpy(alias_user, 
+alias_name_from_command);
                                
-                                       if (fgets(TmpBuf2, 500, fs) == NULL) {
-                                               stop=1;
-                                               fprintf(actout, "%s ", alias_user);
-                                       } else {
-                                               fprintf(actout, "%s, ", alias_user);
+                                               if (fgets(TmpBuf2, 500, fs) == NULL) {
+                                                       stop=1;
+                                                       fprintf(actout, "%s ", 
+alias_user);
+                                               } else {
+                                                       fprintf(actout, "%s, ", 
+alias_user);
+                                               }
                                        }
-                               }
 
-                               fprintf(actout, "</TD>");
+                                       fprintf(actout, "</TD>");
 
-                       fprintf(actout,"<TD align=center><A 
href=%s/com/moddotqmail?atype=%s&user=%s&dom=%s&time=%d&modu=%s>\
+                               fprintf(actout,"<TD align=center><A 
+href=%s/com/moddotqmail?atype=%s&user=%s&dom=%s&time=%d&modu=%s>\
        <img src=\"/images/qmailadmin/delete.gif\" border=0></a></TD>",
-                CGIPATH,dottype, user, dom, mytime, alias_name);
-                       fprintf(actout,"<TD align=center><A 
href=%s/com/deldotqmail?atype=%s&user=%s&dom=%s&time=%d&modu=%s>\
+                       CGIPATH,dottype, user, dom, mytime, alias_name);
+                       fprintf(actout,"<TD align=center><A 
+href=%s/com/deldotqmail?atype=%s&user=%s&dom=%s&time=%d&modu=%s>\
        <img src=\"/images/qmailadmin/delete.gif\" border=0></a></TD>",
-                CGIPATH,dottype, user, dom, mytime, alias_name);
+                       CGIPATH,dottype, user, dom, mytime, alias_name);
        
+                               }
+                               fclose(fs);
+                               k++;
                        }
-                       fclose(fs);
-                       k++;
                }
        }
-       closedir(mydir);
 
        fprintf(actout,"<tr><td colspan=5><center>\n");
        if ( AdminType==SYSTEM_ADMIN || AdminType==DOMAIN_ADMIN ) {

Reply via email to