File: sorttest.c

package info (click to toggle)
funtools 1.4.4+dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,168 kB
  • ctags: 10,760
  • sloc: ansic: 87,238; sh: 9,727; lex: 4,595; asm: 3,281; ada: 1,681; makefile: 1,458; pascal: 1,089; cpp: 1,001; cs: 879; perl: 161; yacc: 64; sed: 32; csh: 10; tcl: 9
file content (152 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download | duplicates (4)
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
/* 
   export EVENTS="(dval:D,fval:E,ival:J,uival:V,sval:I,cval:B,cval2:B)"
   sorttest | _funsort -B24 +... | fundisp "stdin[EVENTS()]"
*/

#include <stdio.h>
#include <stdlib.h>
#include <salloc.h>

extern char *optarg;
extern int optind;

#define NREC 10
#define SZ_LINE 1024

#define MAX(a,b) (((a)>(b))?(a):(b))

typedef struct brec{
  double dval;
  float fval;
  int ival;
  unsigned int uival;
  short sval;
  char cval;
  char cval2;
} Binary;

void
fillb(Binary *b, int i, int dodouble)
{
  int idiv;
  b->dval = (double)rand();
  b->fval = (float)rand();
  if( dodouble ){
    if( (i%2) == 0 )
      b->ival = rand();
  }
  else{
    b->ival = rand();
  }
  b->uival = (unsigned)rand();
  idiv = MAX(1,(RAND_MAX/32768));
  b->sval = rand()/idiv;
  idiv = MAX(1,(RAND_MAX/128));
  b->cval = rand()/idiv;
  b->cval2 = rand()/idiv;
}

int main(int argc, char **argv)
{
  int i;
  int c;
  int ival;
  int ichan, ochan, pid, status;
  int rlen;
  int nrec=NREC;
  int dodouble=0;
  int doxeq=0;
  char *prog="_funsort";
  char *slist=NULL;
  char *s;
  char tbuf[SZ_LINE];
  char cmd[SZ_LINE];
  FILE *fp=stdout;
  Binary b;

  /* process switch arguments */
  while ((c = getopt(argc, argv, "dn:s:x")) != -1){
    switch(c){
    case 'd':
      dodouble = 1;
      break;
    case 'n':
      nrec = atoi(optarg);
      break;
    case 's':
      doxeq = 1;
      slist=(char *)NewString(optarg);
      break;
    case 'x':
      doxeq = 1;
      break;
    }
  }
  
  /* see rand */
  srand(time(NULL));

  /* generate sort parameters and start sort program */
  if( doxeq ){
    snprintf(cmd, SZ_LINE-1, "%s -B%d %s", prog, sizeof(Binary), tbuf);
    rlen = SZ_LINE - strlen(cmd);
    if( !slist ){
      slist = (char *)NewString("d");
    }
    for(s=slist; *s; s++){
      switch(*s){
      case 'd':
	sprintf(tbuf, "+d0");
	break;
      case 'f':
	sprintf(tbuf, "+f8");
	break;
      case 'i':
	sprintf(tbuf, "+i12");
	break;
      case 'I':
	sprintf(tbuf, "+I16");
	break;
      case 's':
	sprintf(tbuf, "+s20");
	break;
      case 'c':
	sprintf(tbuf, "+c22");
	break;
      default:
	fprintf(stderr, "ERROR: unknown sort type: %s\n", s);
	exit(1);
      }
      strncat(cmd, " ",  rlen);
      rlen--;
      strncat(cmd, tbuf, rlen);
      rlen -= strlen(tbuf);
      if( rlen <=0 ){
	fprintf(stderr, "ERROR: too many arguments\n");
	exit(1);
      }
    }
    if( !ProcessOpen(cmd, &ichan, &ochan, &pid) ){
      fprintf(stderr, "ERROR: can't start %s\n", prog);
      exit(1);
    }
    /* write unsorted records */
    for(i=0; i<nrec; i++){
      fillb(&b, i, dodouble);
      write(ochan, &b, sizeof(Binary));
    }
    /* signal EOF to sort program */
    close(ochan);
    while( read(ichan, &b, sizeof(Binary)) == sizeof(Binary) )
      write(1, &b, sizeof(Binary));
    if( slist ) free(slist);
    ProcessClose(pid, &status);
  }
  else{
    /* write unsorted records */
    for(i=0; i<nrec; i++){
      fillb(&b, i, dodouble);
      fwrite(&b, sizeof(Binary), 1, fp);
    }
  }
}