language: C99 strict (gcc-4.7.2)
date: 725 days 14 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
 
#define LGT 400
#define BUF 400
 
#define NUM  1
#define REV  2
#define FOLD 4
#define DIR  8
#define LINES 100
 
static char option=0;
int pos1=0;
int pos2=0;
static char buffer[BUF];
static char *bp=buffer;
 
char *alloc(int lg)
{
     if(buffer+LGT-bp<=lg)
          return 0;
     else {
          bp+=lg;
          return bp-lg;
     }
 
}
 
int getl(char *s, int max)
{
     int c;
     char *t=s;
     while(--max>0 && (c=getchar())!=EOF && c!='\n')
          *s++=c;
     if(c=='\n')
          *s++=c;
     *s='\0';
     return s-t;
}
 
int readl(char *lptr[], int maxl)
{
     int lenum,nl=0;
     char *p,k=0;
     char arnum[LGT];
     while((lenum=getl(arnum,LGT))>0)
          if(lenum>=maxl || (p=alloc(lenum))==NULL)
               return -1;
          else {
               arnum[lenum-1]='\0';
               strcpy(p,arnum);
               lptr[nl++]=p;
          }
          return nl;
 
}
 
void qsort(void *v[],int left, int right, int (*cmp) (void *, void *))
{
     void swap(void *v[],int l, int r);
     int last,i;
     if(left>=right)
          return;
 
     swap(v,left,(left+right)/2);
     last=left;
     for(i=left+1;i<=right;i++)
          if((*cmp)(v[i],v[left])<0)
               swap(v,++last,i);
     swap(v,left,last);
     qsort(v,left,last-1,cmp);
     qsort(v,last+1,right,cmp);
}
 
void swap(void *v[],int l, int r)
{
     void *temp;
     temp=v[l];
     v[l]=v[r];
     v[r]=temp;
 
}
 
void readargs(int argc, char *argv[])
{
     int c;
     void error(char *s);
 
         while(--argc>0 && (c = (*++argv)[0])=='-'||c=='+'){
            if(c=='-' && !isdigit(*(argv[0]+1)))
              while(c=*++argv[0])
                switch(c){
                    case 'd':
                         option |= DIR;
                         break;
                    case 'n':
                         option |= NUM;
                         break;
                    case 'r':
                         option |= REV;
                         break;
                    case 'f':
                         option |= FOLD;
                         break;
                    default:
                         printf("Option %s not found\n",c);
                         argc=1;
                         break;
                }
            else if(c=='-')
                 pos2=atoi(argv[0]+1);
            else if((pos1=atoi(argv[0]+1))<0)
                 error("Usage: ");
         }
         if(argc||pos1>pos2)
              error("Usage: ");
}
 
int numcmp(char *a, char *b, int maxs)
{
     void substr(char *s, char *str);
     double x, y;
     char str[LGT];
 
     substr(a,str);
     x=atof(str);
     substr(b,str);
     y=atof(str);
     if(x<y)
          return -1;
     else if(x>y)
          return 1;
     else 
          return 0;
}
 
void writel(char *lptr[],int lgt, int opt)
{
     int i;
     if(opt)
          for(i=lgt-1;i>=0;i--)
               printf("%s\n",lptr[i]);
     else
          for(i=0;i<lgt;i++)
               printf("%s\n",lptr[i]);
}
 
void error(char *s)
{
     printf("%s\n",s);
}
 
void substr(char *s, char *str)
{
     int i, j, len;
 
     len=strlen(s);
     if(pos2>0 && len>pos2)
          len=pos2;
     else if(pos2>0 && len<pos2)
          error("substr: string too short");
     for(j=0, i=pos1;i<len;i++,j++)
          str[j]=s[i];
     str[j]='\0';
}
 
int charcmp(char *s, char *t)
{
     char x, y;
     int i, j, endpos;
     int dir=(option & DIR) ? 1:0;
     int fold=(option & FOLD) ? 1:0;
 
     i=j=pos1;
 
     if(pos2>0)
          endpos=pos2;
     else if((unsigned)(endpos=strlen(s)) > strlen(t))
          endpos=strlen(t);
     do {
          if(dir){
               while(i<endpos && !isalnum(s[i]) && s[i]!=' ' && s[i]!='\0')
                    i++;
               while(j<endpos && !isalnum(t[j]) && t[j]!=' ' && t[j]!='\0')
                    j++;
          }
        if(i<endpos && j<endpos){
             x=fold?tolower(s[i]):s[i];
                i++;
             y=fold?tolower(t[j]):t[j];
                j++;
             if(x==y && x=='\0')
                return 0;
        }
     }while(x==y && i<endpos && j<endpos);
      return x-y;
}
 
int main2(int argc, char *argv[])
{
     int len,rc;
     char *lptrx[LGT];
 
         readargs(argc,argv);
               if((len=readl(lptrx,LGT))>0){
                    if(option & NUM)
                         qsort((void **)lptrx,0,len-1, 
                         (int (*)(void *, void *))numcmp);
                    else 
                         qsort((void **)lptrx,0,len-1, 
                         (int (*)(void *, void *))charcmp);
                    writel(lptrx,len,option & REV);     
 
               }
               else {
                    printf("Too long input\n");
                    rc=-1;
               }
}
 
int main(void) {
  char args[][20] = {"./a.out", "+5", NULL};
  return main2(2, args);  
}
cc1: warnings being treated as errors
prog.c: In function ‘readl’:
prog.c:48: error: unused variable ‘k’
prog.c: At top level:
prog.c:62: error: conflicting types for ‘qsort’
/usr/include/stdlib.h:689: error: previous declaration of ‘qsort’ was here
prog.c: In function ‘readargs’:
prog.c:93: error: suggest parentheses around && within ||
prog.c:95: error: suggest parentheses around assignment used as truth value
prog.c:110: error: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
prog.c: In function ‘main2’:
prog.c:223: error: control reaches end of non-void function
prog.c: In function ‘main’:
prog.c:226: error: missing braces around initializer
prog.c:226: error: (near initialization for ‘args[2]’)
prog.c:226: error: initialization makes integer from pointer without a cast
prog.c:227: error: passing argument 2 of ‘main2’ from incompatible pointer type