sort.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. --- END COPYRIGHT BLOCK --- */
  38. /* DON'T SHIP THIS PROGRAM. It's terribly un-secure, as it
  39. enables an HTTP client to read the contents of any file.
  40. */
  41. /* This is a Gateway CGI program, for testing collation.
  42. It reads the text file named by $PATH_INFO and outputs its lines, sorted,
  43. in a table with the script and collation key computed by dsgw_strkeygen.
  44. The locale is controlled by the Accept-Language header in the HTTP request,
  45. like any Gateway CGI.
  46. */
  47. #include <errno.h>
  48. #include <stdio.h> /* fopen, fgets, perror */
  49. #include <stdlib.h> /* getenv, qsort */
  50. #include "dsgw.h"
  51. static const char*
  52. fgetln(FILE* f, int* error)
  53. {
  54. auto size_t buflen = 128;
  55. auto char* buf = dsgw_ch_malloc (buflen);
  56. *buf = '\0';
  57. while (fgets (buf, buflen, f)) {
  58. auto const size_t read = strlen(buf);
  59. if (buf[read-1] == '\n') {
  60. buf[read-1] = '\0';
  61. return buf;
  62. }
  63. buflen *= 2;
  64. buf = dsgw_ch_realloc (buf, buflen);
  65. }
  66. if (feof(f) && *buf) return buf;
  67. free (buf);
  68. return NULL;
  69. }
  70. typedef struct keystring {
  71. const char* ks_val;
  72. struct berval* ks_key;
  73. } keystring_t;
  74. static int
  75. keystring_cmp (const void* Lv, const void* Rv)
  76. {
  77. auto const keystring_t** L = (const keystring_t**)Lv;
  78. auto const keystring_t** R = (const keystring_t**)Rv;
  79. return dsgw_keycmp (NULL, (*L)->ks_key, (*R)->ks_key);
  80. }
  81. int
  82. main( int argc, char* argv[] )
  83. {
  84. auto int error = 0;
  85. auto const int reqmethod = dsgw_init (argc, argv, DSGW_METHOD_GET);
  86. auto char* fname = getenv ("PATH_INFO");
  87. dsgw_send_header();
  88. dsgw_emits ("<HTML>\n");
  89. dsgw_head_begin();
  90. dsgw_emits ("\n</head>\n<body>\n");
  91. if (!fname) {
  92. dsgw_emits ("!PATH_INFO\n");
  93. error = 1;
  94. } else {
  95. auto FILE* f = fopen (fname, "r");
  96. if (!f) {
  97. dsgw_emitf ("%s: errno %i\n", fname, errno);
  98. error = 2;
  99. } else {
  100. auto const char* line;
  101. auto keystring_t* v = NULL;
  102. auto size_t vlen = 0;
  103. while (line = fgetln(f, &error)) {
  104. v = (keystring_t*) dsgw_ch_realloc (v, (vlen+1) * sizeof(keystring_t));
  105. v[vlen].ks_val = line;
  106. v[vlen].ks_key = dsgw_strkeygen (CASE_INSENSITIVE, line);
  107. ++vlen;
  108. }
  109. fclose (f);
  110. if (vlen) {
  111. auto keystring_t** vp;
  112. auto size_t i;
  113. vp = (keystring_t**) dsgw_ch_malloc (vlen * sizeof(keystring_t*));
  114. for (i = 0; i < vlen; ++i) {
  115. vp[i] = v + i;
  116. }
  117. qsort (vp, vlen, sizeof(keystring_t*), keystring_cmp);
  118. dsgw_emits ("<table align=left cols=5>\n");
  119. dsgw_emits (" <tr>"
  120. "<th width=20>" DSGW_UTF8_NBSP "</th>"
  121. "<th align=left>line</th>"
  122. "<th width=25>script</th>"
  123. "<th width=20>" DSGW_UTF8_NBSP "</th>"
  124. "<th align=left>Sort Key</th>"
  125. "</tr>\n");
  126. for (i = 0; i < vlen; ++i) {
  127. auto size_t j;
  128. dsgw_emits (" <tr valign=baseline>");
  129. dsgw_emitf ("<th align=right>%lu:</th>", 1 + (unsigned long)(vp[i]-v));
  130. dsgw_emitf ("<td>%s</td>", vp[i]->ks_val);
  131. dsgw_emits ("<td align=center>");
  132. if (vp[i]->ks_key->bv_len) {
  133. dsgw_emitf ("%u", 0xFF & (unsigned)(vp[i]->ks_key->bv_val[0]));
  134. } else {
  135. dsgw_emits (DSGW_UTF8_NBSP);
  136. }
  137. dsgw_emits ("</td>");
  138. dsgw_emitf ("<td align=right>%lu:</td>", (unsigned long)(vp[i]->ks_key->bv_len) - 2);
  139. dsgw_emits ("<td><font size=\"-2\">");
  140. for (j = 1; j < vp[i]->ks_key->bv_len - 1; ++j) {
  141. dsgw_emitf ("%02x", 0xFF & (unsigned)(vp[i]->ks_key->bv_val[j]));
  142. }
  143. dsgw_emits ("</font></td>");
  144. dsgw_emits ("</tr>\n");
  145. }
  146. dsgw_emits ("</table>\n");
  147. free (vp);
  148. for (i = 0; i < vlen; ++i) {
  149. dsgw_keyfree (NULL, v[i].ks_key);
  150. }
  151. free (v);
  152. }
  153. }
  154. }
  155. dsgw_emits ("</body></HTML>\n");
  156. return error;
  157. }