kwo pushed a commit to branch master.

commit e66c0e85021f89551f560a58e2f995881617a859
Author: Kim Woelders <[email protected]>
Date:   Sat Apr 6 10:22:19 2013 +0200

    Use qsort in stead of open coded sorting.
---
 src/file.c   | 23 ++++-------------------
 src/memory.c | 18 ++++++++++++++++--
 src/util.h   |  4 +++-
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/src/file.c b/src/file.c
index a5ec877..d1abb94 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2007-2010 Kim Woelders
+ * Copyright (C) 2007-2013 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -35,7 +35,6 @@ char              **
 E_ls(const char *dir, int *num)
 {
    int                 i, dirlen;
-   int                 done = 0;
    DIR                *dirp;
    char              **names;
    struct dirent      *dp;
@@ -80,23 +79,9 @@ E_ls(const char *dir, int *num)
    closedir(dirp);
    *num = dirlen;
 
-   /* do a simple bubble sort here to alphanumberic it */
-   while (!done)
-     {
-       done = 1;
-       for (i = 0; i < dirlen - 1; i++)
-         {
-            if (strcmp(names[i], names[i + 1]) > 0)
-              {
-                 char               *tmp;
-
-                 tmp = names[i];
-                 names[i] = names[i + 1];
-                 names[i + 1] = tmp;
-                 done = 0;
-              }
-         }
-     }
+   /* Sort names */
+   StrlistSort(names, dirlen);
+
    return names;
 }
 
diff --git a/src/memory.c b/src/memory.c
index c248eac..310bb4e 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2005-2008 Kim Woelders
+ * Copyright (C) 2005-2013 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -21,8 +21,10 @@
  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
-#include "E.h"
 #include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "util.h"
 
 char               *
 Estrtrim(char *s)
@@ -307,6 +309,18 @@ StrlistFromString(const char *str, int delim, int *num)
    return lst;
 }
 
+static int
+_qsort_strcmp(const void *s1, const void *s2)
+{
+   return strcmp(*(const char **)s1, *(const char **)s2);
+}
+
+void
+StrlistSort(char **lst, int len)
+{
+   qsort(lst, (unsigned int)len, sizeof(char *), _qsort_strcmp);
+}
+
 void
 Esetenv(const char *name, const char *value)
 {
diff --git a/src/util.h b/src/util.h
index 8ace7aa..a8f98a7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
- * Copyright (C) 2004-2012 Kim Woelders
+ * Copyright (C) 2004-2013 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -105,6 +105,8 @@ char               *StrlistEncodeEscaped(char *buf, int 
len, char **lst,
 char              **StrlistDecodeEscaped(const char *str, int *pnum);
 __EXPORT__ char   **StrlistFromString(const char *str, int delim, int *num);
 
+void                StrlistSort(char **lst, int num);
+
 void                Esetenv(const char *name, const char *value);
 
 /* misc.c */

-- 

------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire 
the most talented Cisco Certified professionals. Visit the 
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html

Reply via email to