1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7 ** File: mbcs.c
8 **
9 ** Synopsis: mbcs {dirName}
10 **
11 ** where dirName is the directory to be traversed. dirName is required.
12 **
13 ** Description:
14 ** mbcs.c tests use of multi-byte characters, as would be passed to
15 ** NSPR funtions by internationalized applications.
16 **
17 ** mbcs.c, when run on any single-byte platform, should run correctly.
18 ** In truth, running the mbcs test on a single-byte platform is
19 ** really meaningless. mbcs.c, nor any NSPR library or test is not
20 ** intended for use with any wide character set, including Unicode.
21 ** mbcs.c should not be included in runtests.ksh because it requires
22 ** extensive user intervention to set-up and run.
23 **
24 ** mbcs.c should be run on a platform using some form of multi-byte
25 ** characters. The initial platform for this test is a Japanese
26 ** language Windows NT 4.0 machine. ... Thank you Noriko Hoshi.
27 **
28 ** To run mbcs.c, the tester should create a directory tree containing
29 ** some files in the same directory from which the test is run; i.e.
30 ** the current working directory. The directory and files should be
31 ** named such that when represented in the local multi-byte character
32 ** set, one or more characters of the name is longer than a single
33 ** byte.
34 **
35 */
36 
37 #include <plgetopt.h>
38 #include <nspr.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 /*
44 ** Test harness infrastructure
45 */
46 PRLogModuleInfo *lm;
47 PRLogModuleLevel msgLevel = PR_LOG_NONE;
48 PRIntn  debug = 0;
49 PRUint32  failed_already = 0;
50 /* end Test harness infrastructure */
51 
52 char *dirName =  NULL;  /* directory name to traverse */
53 
54 /*
55 ** Traverse directory
56 */
TraverseDirectory(unsigned char * dir)57 static void TraverseDirectory( unsigned char *dir )
58 {
59     PRDir *cwd;
60     PRDirEntry *dirEntry;
61     PRFileInfo info;
62     PRStatus rc;
63     PRInt32 err;
64     PRFileDesc *fd;
65     char    nextDir[256];
66     char    file[256];
67 
68     printf("Directory: %s\n", dir );
69     cwd = PR_OpenDir( dir );
70     if ( NULL == cwd )  {
71         printf("PR_OpenDir() failed on directory: %s, with error: %d, %d\n",
72                dir, PR_GetError(), PR_GetOSError());
73         exit(1);
74     }
75     while( NULL != (dirEntry = PR_ReadDir( cwd, PR_SKIP_BOTH | PR_SKIP_HIDDEN )))  {
76         sprintf( file, "%s/%s", dir, dirEntry->name );
77         rc = PR_GetFileInfo( file, &info );
78         if ( PR_FAILURE == rc ) {
79             printf("PR_GetFileInfo() failed on file: %s, with error: %d, %d\n",
80                    dirEntry->name, PR_GetError(), PR_GetOSError());
81             exit(1);
82         }
83         if ( PR_FILE_FILE == info.type )  {
84             printf("File: %s \tsize: %ld\n", dirEntry->name, info.size );
85             fd = PR_Open( file, PR_RDONLY, 0 );
86             if ( NULL == fd )  {
87                 printf("PR_Open() failed. Error: %ld, OSError: %ld\n",
88                        PR_GetError(), PR_GetOSError());
89             }
90             rc = PR_Close( fd );
91             if ( PR_FAILURE == rc )  {
92                 printf("PR_Close() failed. Error: %ld, OSError: %ld\n",
93                        PR_GetError(), PR_GetOSError());
94             }
95         } else if ( PR_FILE_DIRECTORY == info.type ) {
96             sprintf( nextDir, "%s/%s", dir, dirEntry->name );
97             TraverseDirectory(nextDir);
98         } else {
99             printf("type is not interesting for file: %s\n", dirEntry->name );
100             /* keep going */
101         }
102     }
103     /* assume end-of-file, actually could be error */
104 
105     rc = PR_CloseDir( cwd );
106     if ( PR_FAILURE == rc ) {
107         printf("PR_CloseDir() failed on directory: %s, with error: %d, %d\n",
108                dir, PR_GetError(), PR_GetOSError());
109     }
110 
111 } /* end TraverseDirectory() */
112 
main(int argc,char ** argv)113 int main(int argc, char **argv)
114 {
115     {   /* get command line options */
116         /*
117         ** Get command line options
118         */
119         PLOptStatus os;
120         PLOptState *opt = PL_CreateOptState(argc, argv, "dv");
121 
122         while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
123         {
124             if (PL_OPT_BAD == os) {
125                 continue;
126             }
127             switch (opt->option)
128             {
129                 case 'd':  /* debug */
130                     debug = 1;
131                     msgLevel = PR_LOG_ERROR;
132                     break;
133                 case 'v':  /* verbose mode */
134                     msgLevel = PR_LOG_DEBUG;
135                     break;
136                 default:
137                     dirName = strdup(opt->value);
138                     break;
139             }
140         }
141         PL_DestroyOptState(opt);
142     } /* end get command line options */
143 
144     lm = PR_NewLogModule("Test");       /* Initialize logging */
145 
146 
147     if ( dirName == NULL )  {
148         printf("you gotta specify a directory as an operand!\n");
149         exit(1);
150     }
151 
152     TraverseDirectory( dirName );
153 
154     if (debug) {
155         printf("%s\n", (failed_already)? "FAIL" : "PASS");
156     }
157     return( (failed_already == PR_TRUE )? 1 : 0 );
158 }  /* main() */
159 /* end template.c */
160