summaryrefslogtreecommitdiff
path: root/examples/validchars/validchr.c
blob: 415546cb84111bfc476c5fad60fa5e3aa1904321 (plain)
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
/* by tino@augsburg.net
 */

#include <stdio.h>
#include <string.h>

#include <dirent.h>

unsigned char
test(void)
{
  DIR		*dir;
  struct dirent	*dp;
  unsigned char	c;

  if ((dir=opendir("."))==0)
    {
      perror("open .");
      return 0;
    }
  c	= 0;
  while ((dp=readdir(dir))!=0)
    {
      size_t len;

      len	= strlen(dp->d_name);
      if (len<4)
	continue;
      if (strcmp(dp->d_name+len-4, ".TST"))
	continue;
      if (len!=5)
	{
	  fprintf(stderr, "warning: %s\n", dp->d_name);
	  printf(" length");
	  continue;
	}
      if (c)
	printf(" double%d\n", c);
      c	= dp->d_name[0];
    }
  if (closedir(dir))
    perror("close .");
  return c;
}

int
main(void)
{
  char		name[256];
  unsigned char	map[256], upper[256], lower[256];
  int		i, j, c;
  FILE		*fd;

  if (test())
    {
      printf("There are *.TST files, please remove\n");
      return 0;
    }
  for (i=0; ++i<256; )
    {
      lower[i]	= i;
      upper[i]	= 0;
    }
  for (i=256; --i; )
    {
      map[i]	= i;
      strcpy(name, "..TST");
      name[0]	= i;
      printf("%d:", i);
      if ((fd=fopen(name, "w"))==0)
	printf(" open");
      else
	fclose(fd);
      c	= test();
      if (unlink(name))
	printf(" unlink");
      if (c==i)
	printf(" ok");
      else
	printf(" %d", c);
      printf("\n");
      if (c!=i)
	{
	  upper[c]++;
	  lower[c]	= i;
        }
      map[i]	= c;
    }

  /* Uppercase characters are detected above on:
   * The character is mapped to itself and there is a
   * character which maps to it.
   * Lowercase characters are the lowest character pointing to another one.
   * Else it is a one way character.
   *
   * For this reason we have to process the list
   * 1) for 'one way' characters
   *	'one way' is something which is no upper and no lower character.
   *	This is an awful, crude and ugly hack due to missing Samba support.
   * 2) for true uppercase/lowercase characters
   * 3) for standalone characters
   * Note that there might be characters which do not fall into 1 to 3.
   */
  printf("\n   valid chars =");
  for (i=0; ++i<256; )
    if (map[i] && map[i]!=i && lower[map[i]]!=i)
      {
	if (!upper[i])
	  printf(" %d:%d %d:%d %d:%d",					/*1*/
		 map[i], i, i, map[i], map[i], map[i]);
	else
	  fprintf(stderr, "ignoring map %d->%d because of %d->%d\n",
		  lower[i], i, i, map[i]);
      }
  for (i=0; ++i<256; )
    if (map[i] && map[i]==i)
      if (upper[i])
	printf(" %d:%d", lower[i], i);					/*2*/
      else
	printf(" %d", i);						/*3*/
  printf("\n");
  return 0;
}