I am having a little trouble in writing a sort function! ...I need to sort an array containing file / directory names for my personal ls command. At the moment, it sorts the array into alphabetical order, which is perfect, however, if i then add the switch -a the program ouputs all files beginning with . alphabetically and then all files with no leading . alphabetically, example:
Code:
[root@localhost shell]# ./myls -la /tmp
list of contents for: /tmp
-rwxrwxrwx 13 root root 4096 Sun Nov 27 21:53:55 2005 .
-rwxr-xr-x 20 root root 4096 Sun Nov 27 20:06:26 2005 ..
-rwx------ 1 root nobody 0 Sun Nov 27 20:07:26 2005 .fam_socket
-rwxrwxrwx 2 xfs xfs 4096 Sun Nov 27 20:06:59 2005 .font-unix
-rw-rw-rw- 1 root root 0 Sun Nov 27 20:07:01 2005 .gdm_socket
-rwxrwxrwx 2 root root 4096 Sun Nov 27 20:07:24 2005 .ICE-unix
-r--r--r-- 1 root root 11 Sun Nov 27 20:07:01 2005 .X0-lock
-rwxrwxrwx 2 root root 4096 Sun Nov 27 20:07:01 2005 .X11-unix
-rwx------ 2 root root 4096 Wed Nov 9 16:57:21 2005 mcop-root
-rwx------ 2 matt matt 4096 Tue Nov 8 21:12:41 2005 orbit-matt
-rwx------ 2 root root 12288 Sun Nov 27 20:08:50 2005 orbit-root
-rwx------ 2 root root 4096 Mon Nov 21 18:08:18 2005 plugtmp
-rwx------ 2 root root 4096 Fri Nov 25 12:27:53 2005 plugtmp-1
-rwx------ 2 root root 4096 Sun Nov 27 21:16:46 2005 plugtmp-2
-rwx------ 2 root root 4096 Mon Nov 7 13:35:34 2005 ssh-XX9iLygl
-rwx------ 2 root root 4096 Sun Nov 27 20:07:23 2005 ssh-XXNXMs4V
Basically, i need to add to my sort compare function an operation that ignores leading dots when sorting, but also a way to put the leading dots back onto the names once sorting is complete!!!
Does anyone know how to ignore the leading char when copying or comparing a string, because i cannot find it anywhere!
Code:
/**********************************************************************************************/
/** **/
/** FUNCTION : sortCompare **/
/** DESCRIPTION : function to compare 2 strings and returns an integer less than, **/
/** equal to, or greater than zero if str1 is found, respectively, to **/
/** be less than, to mathc, or be greater than str2. **/
/** NOTE: This function ignores leading zeros and case **/
/** PARAMETERS : a pointer to the first string to be compared **/
/** a pointer to the second string to be compared **/
/** AUTHOR : Matt Conway (03500546) **/
/** DATE : 21/11/2005 **/
/** **/
/**********************************************************************************************/
int sortCompare(const void *str1, const void *str2){
char tmpStr1[strlen(str1)];
char tmpStr2[strlen(str2)];
/* Check to see if str1 name is not . */
if (!strcmp(str1, ".") == 0){
/* so check for leading zero on str1 */
if (strncmp(str1, ".", 1) == 0){
/* copy str1 to tmpStr1 ignoring leading . */
}
}
/* DO THE SAME FOR str2 */
/* return the result of the copy */
return strcasecmp(str1,str2);
}
I know that the dynamic array assignment is bad programming, but i will change this to malloc space once it is in working order!!!!!!
Thanks for any ideas or hints!!!!