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