tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mbcs.c (4660B)


      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 */
     57 static void TraverseDirectory(unsigned char* dir) {
     58  PRDir* cwd;
     59  PRDirEntry* dirEntry;
     60  PRFileInfo info;
     61  PRStatus rc;
     62  PRInt32 err;
     63  PRFileDesc* fd;
     64  char nextDir[256];
     65  char file[256];
     66 
     67  printf("Directory: %s\n", dir);
     68  cwd = PR_OpenDir(dir);
     69  if (NULL == cwd) {
     70    printf("PR_OpenDir() failed on directory: %s, with error: %d, %d\n", dir,
     71           PR_GetError(), PR_GetOSError());
     72    exit(1);
     73  }
     74  while (NULL != (dirEntry = PR_ReadDir(cwd, PR_SKIP_BOTH | PR_SKIP_HIDDEN))) {
     75    sprintf(file, "%s/%s", dir, dirEntry->name);
     76    rc = PR_GetFileInfo(file, &info);
     77    if (PR_FAILURE == rc) {
     78      printf("PR_GetFileInfo() failed on file: %s, with error: %d, %d\n",
     79             dirEntry->name, PR_GetError(), PR_GetOSError());
     80      exit(1);
     81    }
     82    if (PR_FILE_FILE == info.type) {
     83      printf("File: %s \tsize: %ld\n", dirEntry->name, info.size);
     84      fd = PR_Open(file, PR_RDONLY, 0);
     85      if (NULL == fd) {
     86        printf("PR_Open() failed. Error: %ld, OSError: %ld\n", PR_GetError(),
     87               PR_GetOSError());
     88      }
     89      rc = PR_Close(fd);
     90      if (PR_FAILURE == rc) {
     91        printf("PR_Close() failed. Error: %ld, OSError: %ld\n", PR_GetError(),
     92               PR_GetOSError());
     93      }
     94    } else if (PR_FILE_DIRECTORY == info.type) {
     95      sprintf(nextDir, "%s/%s", dir, dirEntry->name);
     96      TraverseDirectory(nextDir);
     97    } else {
     98      printf("type is not interesting for file: %s\n", dirEntry->name);
     99      /* keep going */
    100    }
    101  }
    102  /* assume end-of-file, actually could be error */
    103 
    104  rc = PR_CloseDir(cwd);
    105  if (PR_FAILURE == rc) {
    106    printf("PR_CloseDir() failed on directory: %s, with error: %d, %d\n", dir,
    107           PR_GetError(), PR_GetOSError());
    108  }
    109 
    110 } /* end TraverseDirectory() */
    111 
    112 int main(int argc, char** argv) {
    113  { /* get command line options */
    114    /*
    115    ** Get command line options
    116    */
    117    PLOptStatus os;
    118    PLOptState* opt = PL_CreateOptState(argc, argv, "dv");
    119 
    120    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    121      if (PL_OPT_BAD == os) {
    122        continue;
    123      }
    124      switch (opt->option) {
    125        case 'd': /* debug */
    126          debug = 1;
    127          msgLevel = PR_LOG_ERROR;
    128          break;
    129        case 'v': /* verbose mode */
    130          msgLevel = PR_LOG_DEBUG;
    131          break;
    132        default:
    133          dirName = strdup(opt->value);
    134          break;
    135      }
    136    }
    137    PL_DestroyOptState(opt);
    138  } /* end get command line options */
    139 
    140  lm = PR_NewLogModule("Test"); /* Initialize logging */
    141 
    142  if (dirName == NULL) {
    143    printf("you gotta specify a directory as an operand!\n");
    144    exit(1);
    145  }
    146 
    147  TraverseDirectory(dirName);
    148 
    149  if (debug) {
    150    printf("%s\n", (failed_already) ? "FAIL" : "PASS");
    151  }
    152  return ((failed_already == PR_TRUE) ? 1 : 0);
    153 } /* main() */
    154 /* end template.c */