/* ********************************************************************** */ /* * * */ /* * sorting.c * */ /* * * */ /* ********************************************************************** */ /* * * */ /* * This program is designed to show use of qsort. * */ /* * * */ /* ********************************************************************** */ /* * * */ /* * 06/06/2006 - Program Created * */ /* * * */ /* ********************************************************************** */ #include #include #include #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define MAX_FCOUNT 8192 /* Global array of structures to hold output to be sorted */ struct filename { char name[384]; /* Filename */ off_t size; /* File size in bytes */ ushort nlink; /* Number of links */ uid_t uid; /* UID for owner */ gid_t gid; /* GID for group owner */ ushort mode; /* File type and permissions */ dev_t dev; /* Device numbers */ time_t mtime; /* Last modification time */ long blocks; /* Number of blocks in file */ }; typedef struct filename FILENAME; /* Give us a shortcut for struct */ /* ********************************************************************** */ /* * Function: cmp_name * */ /* * * */ /* * Compare two filename entries based on name using same return * */ /* * values as strcmp to show less than, equal to, or greater than. * */ /* ********************************************************************** */ int cmp_name(a, b) FILENAME **a; FILENAME **b; { FILENAME *one, *two; /* Temp structures */ one = *a; two = *b; return(strcmp(one->name, two->name)); } /* ********************************************************************** */ /* * Function: main * */ /* * * */ /* * Allocate a quick array of structures and sort them. * */ /* ********************************************************************** */ int main() { FILENAME **files; /* filename array */ int i; /* loop variable */ /* Allocate the filename array */ files = (FILENAME **) malloc(sizeof(FILENAME *) * 4); files[0] = (FILENAME *) malloc(sizeof(FILENAME)); strcpy(files[0]->name, "zfile"); files[1] = (FILENAME *) malloc(sizeof(FILENAME)); strcpy(files[1]->name, "aafile"); files[2] = (FILENAME *) malloc(sizeof(FILENAME)); strcpy(files[2]->name, "cfile"); files[3] = (FILENAME *) malloc(sizeof(FILENAME)); strcpy(files[3]->name, "kfile"); qsort(files, 4, sizeof(FILENAME *), cmp_name); /* Print the output */ for (i = 0; i < 4; i++) { printf("%s\n", files[i]->name); } }