docs: fix a typo in history file
[bbaumbach/samba-autobuild/.git] / examples / validchars / validchr.c
1 /* by tino@augsburg.net\r
2  */\r
3 \r
4 #include <stdio.h>\r
5 #include <string.h>\r
6 \r
7 #include <dirent.h>\r
8 \r
9 unsigned char\r
10 test(void)\r
11 {\r
12   DIR           *dir;\r
13   struct dirent *dp;\r
14   unsigned char c;\r
15 \r
16   if ((dir=opendir("."))==0)\r
17     {\r
18       perror("open .");\r
19       return 0;\r
20     }\r
21   c     = 0;\r
22   while ((dp=readdir(dir))!=0)\r
23     {\r
24       size_t len;\r
25 \r
26       len       = strlen(dp->d_name);\r
27       if (len<4)\r
28         continue;\r
29       if (strcmp(dp->d_name+len-4, ".TST"))\r
30         continue;\r
31       if (len!=5)\r
32         {\r
33           fprintf(stderr, "warning: %s\n", dp->d_name);\r
34           printf(" length");\r
35           continue;\r
36         }\r
37       if (c)\r
38         printf(" double%d\n", c);\r
39       c = dp->d_name[0];\r
40     }\r
41   if (closedir(dir))\r
42     perror("close .");\r
43   return c;\r
44 }\r
45 \r
46 int\r
47 main(void)\r
48 {\r
49   char          name[256];\r
50   unsigned char map[256], upper[256], lower[256];\r
51   int           i, j, c;\r
52   FILE          *fd;\r
53 \r
54   if (test())\r
55     {\r
56       printf("There are *.TST files, please remove\n");\r
57       return 0;\r
58     }\r
59   for (i=0; ++i<256; )\r
60     {\r
61       lower[i]  = i;\r
62       upper[i]  = 0;\r
63     }\r
64   for (i=256; --i; )\r
65     {\r
66       map[i]    = i;\r
67       strcpy(name, "..TST");\r
68       name[0]   = i;\r
69       printf("%d:", i);\r
70       if ((fd=fopen(name, "w"))==0)\r
71         printf(" open");\r
72       else\r
73         fclose(fd);\r
74       c = test();\r
75       if (unlink(name))\r
76         printf(" unlink");\r
77       if (c==i)\r
78         printf(" ok");\r
79       else\r
80         printf(" %d", c);\r
81       printf("\n");\r
82       if (c!=i)\r
83         {\r
84           upper[c]++;\r
85           lower[c]      = i;\r
86         }\r
87       map[i]    = c;\r
88     }\r
89 \r
90   /* Uppercase characters are detected above on:\r
91    * The character is mapped to itself and there is a\r
92    * character which maps to it.\r
93    * Lowercase characters are the lowest character pointing to another one.\r
94    * Else it is a one way character.\r
95    *\r
96    * For this reason we have to process the list\r
97    * 1) for 'one way' characters\r
98    *    'one way' is something which is no upper and no lower character.\r
99    *    This is an awful, crude and ugly hack due to missing Samba support.\r
100    * 2) for true uppercase/lowercase characters\r
101    * 3) for standalone characters\r
102    * Note that there might be characters which do not fall into 1 to 3.\r
103    */\r
104   printf("\n   valid chars =");\r
105   for (i=0; ++i<256; )\r
106     if (map[i] && map[i]!=i && lower[map[i]]!=i)\r
107       {\r
108         if (!upper[i])\r
109           printf(" %d:%d %d:%d %d:%d",                                  /*1*/\r
110                  map[i], i, i, map[i], map[i], map[i]);\r
111         else\r
112           fprintf(stderr, "ignoring map %d->%d because of %d->%d\n",\r
113                   lower[i], i, i, map[i]);\r
114       }\r
115   for (i=0; ++i<256; )\r
116     if (map[i] && map[i]==i)\r
117       if (upper[i])\r
118         printf(" %d:%d", lower[i], i);                                  /*2*/\r
119       else\r
120         printf(" %d", i);                                               /*3*/\r
121   printf("\n");\r
122   return 0;\r
123 }\r