mbcs.c 5.0 KB

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