#include
#include
#include
#define MAXLNES 2000
static int renumber = 0;
static int renum = 0;
static int renum_f = 0;
static int renum_d = 0;
int read_argv(char **,int);
int readline(char **,int);
void writeline(char**,int);
void qsorts(void **,int,int,int (*)(void *,void *));
void swap(void **,int,int);
int numcmp(char *,char *);
int getlines(char *,int);
int restrcmp(char *,char *);
int renum_ds(char *,char *);
int main(int argc,char **argv)
{
int nline,numeric;
char *line[MAXLNES];
numeric = read_argv(argv,argc);
if((nline = readline(line,MAXLNES)) > 0){
qsorts((void **)line,0,nline - 1,(int (*)(void *,void*))(numeric ? numcmp : restrcmp));
writeline(line,nline);
return EXIT_SUCCESS;
}else{
printf("input too big on store.\n");
return EXIT_FAILURE;
}
}
int read_argv(char **argv,int argc)
{
int numeric,c;
numeric = 0;
if(argc < 2)
return numeric;
else{
while(--argc > 0 && *(++argv)[0] == '-')
while((c = *++argv[0])){
switch(c){
case 'r':
renumber = 1;
break;
case 'n':
numeric = 1;
renum = 1;
break;
case 'f':
renum_f = 1;
break;
case 'd':
renum_d = 1;
break;
default:
printf("error:[%s] illegar option!\n",*argv);
printf("Usage: [-r] [-n] [-f] [-d] or [-rn] [-fd] [-fdrn]\n");
exit(EXIT_FAILURE);
}
}
if(renum_f || renum_d)
renum = 0;
}
return numeric;
}
#define MAXLINE 1000
char *alloc(int n);
int readline(char **line,int nline)
{
int number,i;
char *p;
char s[MAXLINE];
i = 0;
while((number = getlines(s,MAXLINE)) > 0){
if(i >= nline || (p = alloc(number + 1)) == NULL)
return -1;
else{
strcpy(p,s);
line[i++] = p;
}
}
return i;
}
int getlines(char *s,int max)
{
int i,c;
for(i = 0; i < max - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
s[i] = '\0';
return i;
}
void writeline(char **line,int n)
{
int i;
for(i = 0; i < n; i++)
printf((renum ? "%s " : "%s\n"),line[i]);
}
void qsorts(void **v,int left,int right,int (*comp)(void *,void *))
{
int i,last;
void swap(void **,int,int);
if(left >= right)
return;
swap(v,left,(left + right) / 2);
last = left;
for(i = left + 1; i <= right; i++)
if((*comp)(v[i],v[left]) < 0)
swap(v,++last,i);
swap(v,left,last);
qsorts(v,left,last - 1,comp);
qsorts(v,last + 1,right,comp);
}
void swap(void **v,int i,int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
#include
int restrcmp(char *s1,char *s2)
{
int c,c1;
char *p = (renumber ? s2 : s1);
char *p2 = (renumber ? s1 : s2);
if(renum_f){
if(renum_d)
return renum_ds(p,p2);
else{
while(*p == *p2){
if(*p == '\0')
return 0;
p++;
p2++;
}
c = toupper(*p);
c1 = toupper(*p2);
if(c - c1 < 0)
return -1;
else if(c - c1 > 0)
return 1;
else
return 0;
}
}
if(renum_d)
return renum_ds(p,p2);
return strcmp(p,p2);
}
int renum_ds(char *s1,char *s2)
{
int c,c1;
while(!isalnum(*s1) && *s1 != ' ')
s1++;
while(!isalnum(*s2) && *s2 != ' ')
s2++;
if(*s1 == '\0' && *s2 == '\0')
return 0;
while(*s1 == *s2){
if(*s1 == '\0')
return 0;
s1++;
s2++;
}
c = (renum_f ? toupper(*s1) : *s1);
c1 = (renum_f ? toupper(*s2) : *s2);
if(c == c1)
return 0;
else if(*s1 - *s2 < 0)
return -1;
else
return 1;
}
int numcmp(char *s1,char *s2)
{
int t1,t2;
char *const p = (renumber ? s2 : s1);
char *const p1 = (renumber ? s1 : s2);
t1 = atoi(p);
t2 = atoi(p1);
if(t1 - t2 < 0)
return -1;
else if(t1 - t2 > 0)
return 1;
else
return 0;
}
#define BUFMAX 10000
static char buff[BUFMAX];
static char *ps = buff;
char *alloc(int n)
{
if(buff + BUFMAX - ps > n)
{
ps += n;
return ps-n;
}
else
return 0;
}