i2l.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <stdlib.h>
  6. #include "prio.h"
  7. #include "prinit.h"
  8. #include "prprf.h"
  9. #include "prlong.h"
  10. #include "plerror.h"
  11. #include "plgetopt.h"
  12. typedef union Overlay_i
  13. {
  14. PRInt32 i;
  15. PRInt64 l;
  16. } Overlay_i;
  17. typedef union Overlay_u
  18. {
  19. PRUint32 i;
  20. PRUint64 l;
  21. } Overlay_u;
  22. static PRFileDesc *err = NULL;
  23. static void Help(void)
  24. {
  25. PR_fprintf(err, "Usage: -i n | -u n | -h\n");
  26. PR_fprintf(err, "\t-i n treat following number as signed integer\n");
  27. PR_fprintf(err, "\t-u n treat following number as unsigned integer\n");
  28. PR_fprintf(err, "\t-h This message and nothing else\n");
  29. } /* Help */
  30. static PRIntn PR_CALLBACK RealMain(PRIntn argc, char **argv)
  31. {
  32. Overlay_i si;
  33. Overlay_u ui;
  34. PLOptStatus os;
  35. PRBool bsi = PR_FALSE, bui = PR_FALSE;
  36. PLOptState *opt = PL_CreateOptState(argc, argv, "hi:u:");
  37. err = PR_GetSpecialFD(PR_StandardError);
  38. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  39. {
  40. if (PL_OPT_BAD == os) {
  41. continue;
  42. }
  43. switch (opt->option)
  44. {
  45. case 'i': /* signed integer */
  46. si.i = (PRInt32)atoi(opt->value);
  47. bsi = PR_TRUE;
  48. break;
  49. case 'u': /* unsigned */
  50. ui.i = (PRUint32)atoi(opt->value);
  51. bui = PR_TRUE;
  52. break;
  53. case 'h': /* user wants some guidance */
  54. default:
  55. Help(); /* so give him an earful */
  56. return 2; /* but not a lot else */
  57. }
  58. }
  59. PL_DestroyOptState(opt);
  60. #if defined(HAVE_LONG_LONG)
  61. PR_fprintf(err, "We have long long\n");
  62. #else
  63. PR_fprintf(err, "We don't have long long\n");
  64. #endif
  65. if (bsi)
  66. {
  67. PR_fprintf(err, "Converting %ld: ", si.i);
  68. LL_I2L(si.l, si.i);
  69. PR_fprintf(err, "%lld\n", si.l);
  70. }
  71. if (bui)
  72. {
  73. PR_fprintf(err, "Converting %lu: ", ui.i);
  74. LL_I2L(ui.l, ui.i);
  75. PR_fprintf(err, "%llu\n", ui.l);
  76. }
  77. return 0;
  78. } /* main */
  79. int main(int argc, char **argv)
  80. {
  81. PRIntn rv;
  82. PR_STDIO_INIT();
  83. rv = PR_Initialize(RealMain, argc, argv, 0);
  84. return rv;
  85. } /* main */
  86. /* i2l.c */