Alexander Konovalov avatar Alexander Konovalov committed 1b13162

Changed everything to be more C++ish, changed codestyle, fixed output

Comments (0)

Files changed (4)

-/*	$Id: getopt.h,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */
-/*	$OpenBSD: getopt.h,v 1.1 2002/12/03 20:24:29 millert Exp $	*/
-/*	$NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $	*/
+/*  $Id: getopt.h,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */
+/*  $OpenBSD: getopt.h,v 1.1 2002/12/03 20:24:29 millert Exp $  */
+/*  $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $  */
 
 /*-
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
 #define optional_argument  2
 
 struct option {
-	/* name of long option */
-	const char *name;
-	/*
-	 * one of no_argument, required_argument, and optional_argument:
-	 * whether option takes an argument
-	 */
-	int has_arg;
-	/* if not NULL, set *flag to val when option found */
-	int *flag;
-	/* if flag not NULL, value to set *flag to; else return value */
-	int val;
+  /* name of long option */
+  const char *name;
+  /*
+   * one of no_argument, required_argument, and optional_argument:
+   * whether option takes an argument
+   */
+  int has_arg;
+  /* if not NULL, set *flag to val when option found */
+  int *flag;
+  /* if flag not NULL, value to set *flag to; else return value */
+  int val;
 };
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-int	 getopt_long(int, char * const *, const char *,
-	    const struct option *, int *);
-int	 getopt_long_only(int, char * const *, const char *,
-	    const struct option *, int *);
+int   getopt_long(int, char * const *, const char *,
+      const struct option *, int *);
+int   getopt_long_only(int, char * const *, const char *,
+      const struct option *, int *);
 #ifndef _GETOPT_DEFINED
 #define _GETOPT_DEFINED
-int	 getopt(int, char * const *, const char *);
-int	 getsubopt(char **, char * const *, char **);
+int   getopt(int, char * const *, const char *);
+int   getsubopt(char **, char * const *, char **);
 
 extern   char *optarg;                  /* getopt(3) external variables */
 extern   int opterr;

src/getopt_long.c

-/*	$Id: getopt_long.c,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $	*/
-/*	$OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $	*/
-/*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
+/*  $Id: getopt_long.c,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $  */
+/*  $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $  */
+/*  $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $  */
 
 /*
  * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
     fprintf(stderr,"\n");
 }
 
-#define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
+#define  REPLACE_GETOPT    /* use this getopt as the system getopt(3) */
 
 #ifdef REPLACE_GETOPT
-int	opterr = 1;		/* if error message should be printed */
-int	optind = 1;		/* index into parent argv vector */
-int	optopt = '?';		/* character checked for validity */
-int	optreset;		/* reset getopt */
-char    *optarg;		/* argument associated with option */
+int  opterr = 1;    /* if error message should be printed */
+int  optind = 1;    /* index into parent argv vector */
+int  optopt = '?';    /* character checked for validity */
+int  optreset;    /* reset getopt */
+char    *optarg;    /* argument associated with option */
 #endif
 
-#define PRINT_ERROR	((opterr) && (*options != ':'))
+#define PRINT_ERROR  ((opterr) && (*options != ':'))
 
-#define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
-#define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
-#define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
+#define FLAG_PERMUTE  0x01  /* permute non-options to the end of argv */
+#define FLAG_ALLARGS  0x02  /* treat non-options as args to option "-1" */
+#define FLAG_LONGONLY  0x04  /* operate as getopt_long_only */
 
 /* return values */
-#define	BADCH		(int)'?'
-#define	BADARG		((*options == ':') ? (int)':' : (int)'?')
-#define	INORDER 	(int)1
+#define  BADCH    (int)'?'
+#define  BADARG    ((*options == ':') ? (int)':' : (int)'?')
+#define  INORDER   (int)1
 
-#define	EMSG		""
+#define  EMSG    ""
 
 static int getopt_internal(int, char * const *, const char *,
-			   const struct option *, int *, int);
+         const struct option *, int *, int);
 static int parse_long_options(char * const *, const char *,
-			      const struct option *, int *, int);
+            const struct option *, int *, int);
 static int gcd(int, int);
 static void permute_args(int, int, int, char * const *);
 
 static int
 gcd(int a, int b)
 {
-	int c;
+  int c;
 
-	c = a % b;
-	while (c != 0) {
-		a = b;
-		b = c;
-		c = a % b;
-	}
+  c = a % b;
+  while (c != 0) {
+    a = b;
+    b = c;
+    c = a % b;
+  }
 
-	return (b);
+  return (b);
 }
 
 /*
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char * const *nargv)
+  char * const *nargv)
 {
-	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
-	char *swap;
+  int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
+  char *swap;
 
-	/*
-	 * compute lengths of blocks and number and size of cycles
-	 */
-	nnonopts = panonopt_end - panonopt_start;
-	nopts = opt_end - panonopt_end;
-	ncycle = gcd(nnonopts, nopts);
-	cyclelen = (opt_end - panonopt_start) / ncycle;
+  /*
+   * compute lengths of blocks and number and size of cycles
+   */
+  nnonopts = panonopt_end - panonopt_start;
+  nopts = opt_end - panonopt_end;
+  ncycle = gcd(nnonopts, nopts);
+  cyclelen = (opt_end - panonopt_start) / ncycle;
 
-	for (i = 0; i < ncycle; i++) {
-		cstart = panonopt_end+i;
-		pos = cstart;
-		for (j = 0; j < cyclelen; j++) {
-			if (pos >= panonopt_end)
-				pos -= nnonopts;
-			else
-				pos += nopts;
-			swap = nargv[pos];
-			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
-			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
-		}
-	}
+  for (i = 0; i < ncycle; i++) {
+    cstart = panonopt_end+i;
+    pos = cstart;
+    for (j = 0; j < cyclelen; j++) {
+      if (pos >= panonopt_end)
+        pos -= nnonopts;
+      else
+        pos += nopts;
+      swap = nargv[pos];
+      /* LINTED const cast */
+      ((char **) nargv)[pos] = nargv[cstart];
+      /* LINTED const cast */
+      ((char **)nargv)[cstart] = swap;
+    }
+  }
 }
 
 /*
  * parse_long_options --
- *	Parse long options in argc/argv argument vector.
+ *  Parse long options in argc/argv argument vector.
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
 parse_long_options(char * const *nargv, const char *options,
-	const struct option *long_options, int *idx, int short_too)
+  const struct option *long_options, int *idx, int short_too)
 {
-	char *current_argv, *has_equal;
-	size_t current_argv_len;
-	int i, match;
+  char *current_argv, *has_equal;
+  size_t current_argv_len;
+  int i, match;
 
-	current_argv = place;
-	match = -1;
+  current_argv = place;
+  match = -1;
 
-	optind++;
+  optind++;
 
-	if ((has_equal = strchr(current_argv, '=')) != NULL) {
-		/* argument found (--option=arg) */
-		current_argv_len = has_equal - current_argv;
-		has_equal++;
-	} else
-		current_argv_len = strlen(current_argv);
+  if ((has_equal = strchr(current_argv, '=')) != NULL) {
+    /* argument found (--option=arg) */
+    current_argv_len = has_equal - current_argv;
+    has_equal++;
+  } else
+    current_argv_len = strlen(current_argv);
 
-	for (i = 0; long_options[i].name; i++) {
-		/* find matching long option */
-		if (strncmp(current_argv, long_options[i].name,
-		    current_argv_len))
-			continue;
+  for (i = 0; long_options[i].name; i++) {
+    /* find matching long option */
+    if (strncmp(current_argv, long_options[i].name,
+        current_argv_len))
+      continue;
 
-		if (strlen(long_options[i].name) == current_argv_len) {
-			/* exact match */
-			match = i;
-			break;
-		}
-		/*
-		 * If this is a known short option, don't allow
-		 * a partial match of a single character.
-		 */
-		if (short_too && current_argv_len == 1)
-			continue;
+    if (strlen(long_options[i].name) == current_argv_len) {
+      /* exact match */
+      match = i;
+      break;
+    }
+    /*
+     * If this is a known short option, don't allow
+     * a partial match of a single character.
+     */
+    if (short_too && current_argv_len == 1)
+      continue;
 
-		if (match == -1)	/* partial match */
-			match = i;
-		else {
-			/* ambiguous abbreviation */
-			if (PRINT_ERROR)
-				warnx(ambig, (int)current_argv_len,
-				     current_argv);
-			optopt = 0;
-			return (BADCH);
-		}
-	}
-	if (match != -1) {		/* option found */
-		if (long_options[match].has_arg == no_argument
-		    && has_equal) {
-			if (PRINT_ERROR)
-				warnx(noarg, (int)current_argv_len,
-				     current_argv);
-			/*
-			 * XXX: GNU sets optopt to val regardless of flag
-			 */
-			if (long_options[match].flag == NULL)
-				optopt = long_options[match].val;
-			else
-				optopt = 0;
-			return (BADARG);
-		}
-		if (long_options[match].has_arg == required_argument ||
-		    long_options[match].has_arg == optional_argument) {
-			if (has_equal)
-				optarg = has_equal;
-			else if (long_options[match].has_arg ==
-			    required_argument) {
-				/*
-				 * optional argument doesn't use next nargv
-				 */
-				optarg = nargv[optind++];
-			}
-		}
-		if ((long_options[match].has_arg == required_argument)
-		    && (optarg == NULL)) {
-			/*
-			 * Missing argument; leading ':' indicates no error
-			 * should be generated.
-			 */
-			if (PRINT_ERROR)
-				warnx(recargstring,
-				    current_argv);
-			/*
-			 * XXX: GNU sets optopt to val regardless of flag
-			 */
-			if (long_options[match].flag == NULL)
-				optopt = long_options[match].val;
-			else
-				optopt = 0;
-			--optind;
-			return (BADARG);
-		}
-	} else {			/* unknown option */
-		if (short_too) {
-			--optind;
-			return (-1);
-		}
-		if (PRINT_ERROR)
-			warnx(illoptstring, current_argv);
-		optopt = 0;
-		return (BADCH);
-	}
-	if (idx)
-		*idx = match;
-	if (long_options[match].flag) {
-		*long_options[match].flag = long_options[match].val;
-		return (0);
-	} else
-		return (long_options[match].val);
+    if (match == -1)  /* partial match */
+      match = i;
+    else {
+      /* ambiguous abbreviation */
+      if (PRINT_ERROR)
+        warnx(ambig, (int)current_argv_len,
+             current_argv);
+      optopt = 0;
+      return (BADCH);
+    }
+  }
+  if (match != -1) {    /* option found */
+    if (long_options[match].has_arg == no_argument
+        && has_equal) {
+      if (PRINT_ERROR)
+        warnx(noarg, (int)current_argv_len,
+             current_argv);
+      /*
+       * XXX: GNU sets optopt to val regardless of flag
+       */
+      if (long_options[match].flag == NULL)
+        optopt = long_options[match].val;
+      else
+        optopt = 0;
+      return (BADARG);
+    }
+    if (long_options[match].has_arg == required_argument ||
+        long_options[match].has_arg == optional_argument) {
+      if (has_equal)
+        optarg = has_equal;
+      else if (long_options[match].has_arg ==
+          required_argument) {
+        /*
+         * optional argument doesn't use next nargv
+         */
+        optarg = nargv[optind++];
+      }
+    }
+    if ((long_options[match].has_arg == required_argument)
+        && (optarg == NULL)) {
+      /*
+       * Missing argument; leading ':' indicates no error
+       * should be generated.
+       */
+      if (PRINT_ERROR)
+        warnx(recargstring,
+            current_argv);
+      /*
+       * XXX: GNU sets optopt to val regardless of flag
+       */
+      if (long_options[match].flag == NULL)
+        optopt = long_options[match].val;
+      else
+        optopt = 0;
+      --optind;
+      return (BADARG);
+    }
+  } else {      /* unknown option */
+    if (short_too) {
+      --optind;
+      return (-1);
+    }
+    if (PRINT_ERROR)
+      warnx(illoptstring, current_argv);
+    optopt = 0;
+    return (BADCH);
+  }
+  if (idx)
+    *idx = match;
+  if (long_options[match].flag) {
+    *long_options[match].flag = long_options[match].val;
+    return (0);
+  } else
+    return (long_options[match].val);
 }
 
 /*
  * getopt_internal --
- *	Parse argc/argv argument vector.  Called by user level routines.
+ *  Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
 getopt_internal(int nargc, char * const *nargv, const char *options,
-	const struct option *long_options, int *idx, int flags)
+  const struct option *long_options, int *idx, int flags)
 {
-	char *oli;				/* option letter list index */
-	int optchar, short_too;
-	static int posixly_correct = -1;
+  char *oli;        /* option letter list index */
+  int optchar, short_too;
+  static int posixly_correct = -1;
 
-	if (options == NULL)
-		return (-1);
+  if (options == NULL)
+    return (-1);
 
-	/*
-	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
-	 * string begins with a '+'.
-	 */
-	if (posixly_correct == -1)
-		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
-	if (posixly_correct || *options == '+')
-		flags &= ~FLAG_PERMUTE;
-	else if (*options == '-')
-		flags |= FLAG_ALLARGS;
-	if (*options == '+' || *options == '-')
-		options++;
+  /*
+   * Disable GNU extensions if POSIXLY_CORRECT is set or options
+   * string begins with a '+'.
+   */
+  if (posixly_correct == -1)
+    posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
+  if (posixly_correct || *options == '+')
+    flags &= ~FLAG_PERMUTE;
+  else if (*options == '-')
+    flags |= FLAG_ALLARGS;
+  if (*options == '+' || *options == '-')
+    options++;
 
-	/*
-	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
-	 * XXX using optreset.  Work around this braindamage.
-	 */
-	if (optind == 0)
-		optind = optreset = 1;
+  /*
+   * XXX Some GNU programs (like cvs) set optind to 0 instead of
+   * XXX using optreset.  Work around this braindamage.
+   */
+  if (optind == 0)
+    optind = optreset = 1;
 
-	optarg = NULL;
-	if (optreset)
-		nonopt_start = nonopt_end = -1;
+  optarg = NULL;
+  if (optreset)
+    nonopt_start = nonopt_end = -1;
 start:
-	if (optreset || !*place) {		/* update scanning pointer */
-		optreset = 0;
-		if (optind >= nargc) {          /* end of argument vector */
-			place = EMSG;
-			if (nonopt_end != -1) {
-				/* do permutation, if we have to */
-				permute_args(nonopt_start, nonopt_end,
-				    optind, nargv);
-				optind -= nonopt_end - nonopt_start;
-			}
-			else if (nonopt_start != -1) {
-				/*
-				 * If we skipped non-options, set optind
-				 * to the first of them.
-				 */
-				optind = nonopt_start;
-			}
-			nonopt_start = nonopt_end = -1;
-			return (-1);
-		}
-		if (*(place = nargv[optind]) != '-' ||
-		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
-			place = EMSG;		/* found non-option */
-			if (flags & FLAG_ALLARGS) {
-				/*
-				 * GNU extension:
-				 * return non-option as argument to option 1
-				 */
-				optarg = nargv[optind++];
-				return (INORDER);
-			}
-			if (!(flags & FLAG_PERMUTE)) {
-				/*
-				 * If no permutation wanted, stop parsing
-				 * at first non-option.
-				 */
-				return (-1);
-			}
-			/* do permutation */
-			if (nonopt_start == -1)
-				nonopt_start = optind;
-			else if (nonopt_end != -1) {
-				permute_args(nonopt_start, nonopt_end,
-				    optind, nargv);
-				nonopt_start = optind -
-				    (nonopt_end - nonopt_start);
-				nonopt_end = -1;
-			}
-			optind++;
-			/* process next argument */
-			goto start;
-		}
-		if (nonopt_start != -1 && nonopt_end == -1)
-			nonopt_end = optind;
+  if (optreset || !*place) {    /* update scanning pointer */
+    optreset = 0;
+    if (optind >= nargc) {          /* end of argument vector */
+      place = EMSG;
+      if (nonopt_end != -1) {
+        /* do permutation, if we have to */
+        permute_args(nonopt_start, nonopt_end,
+            optind, nargv);
+        optind -= nonopt_end - nonopt_start;
+      }
+      else if (nonopt_start != -1) {
+        /*
+         * If we skipped non-options, set optind
+         * to the first of them.
+         */
+        optind = nonopt_start;
+      }
+      nonopt_start = nonopt_end = -1;
+      return (-1);
+    }
+    if (*(place = nargv[optind]) != '-' ||
+        (place[1] == '\0' && strchr(options, '-') == NULL)) {
+      place = EMSG;    /* found non-option */
+      if (flags & FLAG_ALLARGS) {
+        /*
+         * GNU extension:
+         * return non-option as argument to option 1
+         */
+        optarg = nargv[optind++];
+        return (INORDER);
+      }
+      if (!(flags & FLAG_PERMUTE)) {
+        /*
+         * If no permutation wanted, stop parsing
+         * at first non-option.
+         */
+        return (-1);
+      }
+      /* do permutation */
+      if (nonopt_start == -1)
+        nonopt_start = optind;
+      else if (nonopt_end != -1) {
+        permute_args(nonopt_start, nonopt_end,
+            optind, nargv);
+        nonopt_start = optind -
+            (nonopt_end - nonopt_start);
+        nonopt_end = -1;
+      }
+      optind++;
+      /* process next argument */
+      goto start;
+    }
+    if (nonopt_start != -1 && nonopt_end == -1)
+      nonopt_end = optind;
 
-		/*
-		 * If we have "-" do nothing, if "--" we are done.
-		 */
-		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
-			optind++;
-			place = EMSG;
-			/*
-			 * We found an option (--), so if we skipped
-			 * non-options, we have to permute.
-			 */
-			if (nonopt_end != -1) {
-				permute_args(nonopt_start, nonopt_end,
-				    optind, nargv);
-				optind -= nonopt_end - nonopt_start;
-			}
-			nonopt_start = nonopt_end = -1;
-			return (-1);
-		}
-	}
+    /*
+     * If we have "-" do nothing, if "--" we are done.
+     */
+    if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
+      optind++;
+      place = EMSG;
+      /*
+       * We found an option (--), so if we skipped
+       * non-options, we have to permute.
+       */
+      if (nonopt_end != -1) {
+        permute_args(nonopt_start, nonopt_end,
+            optind, nargv);
+        optind -= nonopt_end - nonopt_start;
+      }
+      nonopt_start = nonopt_end = -1;
+      return (-1);
+    }
+  }
 
-	/*
-	 * Check long options if:
-	 *  1) we were passed some
-	 *  2) the arg is not just "-"
-	 *  3) either the arg starts with -- we are getopt_long_only()
-	 */
-	if (long_options != NULL && place != nargv[optind] &&
-	    (*place == '-' || (flags & FLAG_LONGONLY))) {
-		short_too = 0;
-		if (*place == '-')
-			place++;		/* --foo long option */
-		else if (*place != ':' && strchr(options, *place) != NULL)
-			short_too = 1;		/* could be short option too */
+  /*
+   * Check long options if:
+   *  1) we were passed some
+   *  2) the arg is not just "-"
+   *  3) either the arg starts with -- we are getopt_long_only()
+   */
+  if (long_options != NULL && place != nargv[optind] &&
+      (*place == '-' || (flags & FLAG_LONGONLY))) {
+    short_too = 0;
+    if (*place == '-')
+      place++;    /* --foo long option */
+    else if (*place != ':' && strchr(options, *place) != NULL)
+      short_too = 1;    /* could be short option too */
 
-		optchar = parse_long_options(nargv, options, long_options,
-		    idx, short_too);
-		if (optchar != -1) {
-			place = EMSG;
-			return (optchar);
-		}
-	}
+    optchar = parse_long_options(nargv, options, long_options,
+        idx, short_too);
+    if (optchar != -1) {
+      place = EMSG;
+      return (optchar);
+    }
+  }
 
-	if ((optchar = (int)*place++) == (int)':' ||
-	    (optchar == (int)'-' && *place != '\0') ||
-	    (oli = strchr(options, optchar)) == NULL) {
-		/*
-		 * If the user specified "-" and  '-' isn't listed in
-		 * options, return -1 (non-option) as per POSIX.
-		 * Otherwise, it is an unknown option character (or ':').
-		 */
-		if (optchar == (int)'-' && *place == '\0')
-			return (-1);
-		if (!*place)
-			++optind;
-		if (PRINT_ERROR)
-			warnx(illoptchar, optchar);
-		optopt = optchar;
-		return (BADCH);
-	}
-	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
-		/* -W long-option */
-		if (*place)			/* no space */
-			/* NOTHING */;
-		else if (++optind >= nargc) {	/* no arg */
-			place = EMSG;
-			if (PRINT_ERROR)
-				warnx(recargchar, optchar);
-			optopt = optchar;
-			return (BADARG);
-		} else				/* white space */
-			place = nargv[optind];
-		optchar = parse_long_options(nargv, options, long_options,
-		    idx, 0);
-		place = EMSG;
-		return (optchar);
-	}
-	if (*++oli != ':') {			/* doesn't take argument */
-		if (!*place)
-			++optind;
-	} else {				/* takes (optional) argument */
-		optarg = NULL;
-		if (*place)			/* no white space */
-			optarg = place;
-		else if (oli[1] != ':') {	/* arg not optional */
-			if (++optind >= nargc) {	/* no arg */
-				place = EMSG;
-				if (PRINT_ERROR)
-					warnx(recargchar, optchar);
-				optopt = optchar;
-				return (BADARG);
-			} else
-				optarg = nargv[optind];
-		}
-		place = EMSG;
-		++optind;
-	}
-	/* dump back option letter */
-	return (optchar);
+  if ((optchar = (int)*place++) == (int)':' ||
+      (optchar == (int)'-' && *place != '\0') ||
+      (oli = strchr(options, optchar)) == NULL) {
+    /*
+     * If the user specified "-" and  '-' isn't listed in
+     * options, return -1 (non-option) as per POSIX.
+     * Otherwise, it is an unknown option character (or ':').
+     */
+    if (optchar == (int)'-' && *place == '\0')
+      return (-1);
+    if (!*place)
+      ++optind;
+    if (PRINT_ERROR)
+      warnx(illoptchar, optchar);
+    optopt = optchar;
+    return (BADCH);
+  }
+  if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
+    /* -W long-option */
+    if (*place)      /* no space */
+      /* NOTHING */;
+    else if (++optind >= nargc) {  /* no arg */
+      place = EMSG;
+      if (PRINT_ERROR)
+        warnx(recargchar, optchar);
+      optopt = optchar;
+      return (BADARG);
+    } else        /* white space */
+      place = nargv[optind];
+    optchar = parse_long_options(nargv, options, long_options,
+        idx, 0);
+    place = EMSG;
+    return (optchar);
+  }
+  if (*++oli != ':') {      /* doesn't take argument */
+    if (!*place)
+      ++optind;
+  } else {        /* takes (optional) argument */
+    optarg = NULL;
+    if (*place)      /* no white space */
+      optarg = place;
+    else if (oli[1] != ':') {  /* arg not optional */
+      if (++optind >= nargc) {  /* no arg */
+        place = EMSG;
+        if (PRINT_ERROR)
+          warnx(recargchar, optchar);
+        optopt = optchar;
+        return (BADARG);
+      } else
+        optarg = nargv[optind];
+    }
+    place = EMSG;
+    ++optind;
+  }
+  /* dump back option letter */
+  return (optchar);
 }
 
 #ifdef REPLACE_GETOPT
 /*
  * getopt --
- *	Parse argc/argv argument vector.
+ *  Parse argc/argv argument vector.
  *
  * [eventually this will replace the BSD getopt]
  */
 getopt(int nargc, char * const *nargv, const char *options)
 {
 
-	/*
-	 * We don't pass FLAG_PERMUTE to getopt_internal() since
-	 * the BSD getopt(3) (unlike GNU) has never done this.
-	 *
-	 * Furthermore, since many privileged programs call getopt()
-	 * before dropping privileges it makes sense to keep things
-	 * as simple (and bug-free) as possible.
-	 */
-	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
+  /*
+   * We don't pass FLAG_PERMUTE to getopt_internal() since
+   * the BSD getopt(3) (unlike GNU) has never done this.
+   *
+   * Furthermore, since many privileged programs call getopt()
+   * before dropping privileges it makes sense to keep things
+   * as simple (and bug-free) as possible.
+   */
+  return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
 }
 #endif /* REPLACE_GETOPT */
 
 /*
  * getopt_long --
- *	Parse argc/argv argument vector.
+ *  Parse argc/argv argument vector.
  */
 int
 getopt_long(int nargc, char * const *nargv, const char *options,
     const struct option *long_options, int *idx)
 {
 
-	return (getopt_internal(nargc, nargv, options, long_options, idx,
-	    FLAG_PERMUTE));
+  return (getopt_internal(nargc, nargv, options, long_options, idx,
+      FLAG_PERMUTE));
 }
 
 /*
  * getopt_long_only --
- *	Parse argc/argv argument vector.
+ *  Parse argc/argv argument vector.
  */
 int
 getopt_long_only(int nargc, char * const *nargv, const char *options,
     const struct option *long_options, int *idx)
 {
 
-	return (getopt_internal(nargc, nargv, options, long_options, idx,
-	    FLAG_PERMUTE|FLAG_LONGONLY));
+  return (getopt_internal(nargc, nargv, options, long_options, idx,
+      FLAG_PERMUTE|FLAG_LONGONLY));
 }
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <ctype.h>
+#include <cstdio>
+#include <cstdlib>
+#include <cassert>
+#include <cctype>
+#include <cstdarg>
+
+#include <iostream>
+#include <vector>
 
 #include "getopt.h"
 #include "pstdint.h"
 
-struct packet {
-	uint32_t number;
-	uint32_t src;
-	uint16_t srcPort;
-	uint32_t dst;
-	uint16_t dstPort;
-	uint32_t length;
-	uint8_t type;
-	uint8_t* data;
+struct Packet {
+  enum Type {
+    TYPE_SEND,
+    TYPE_RECV
+  };
+
+  uint32_t srcAddress;
+  uint16_t srcPort;
+  
+  uint32_t dstAddress;
+  uint16_t dstPort;
+  
+  Type type;
+  
+  std::vector<uint8_t> data;
 };
 
-enum {
-	TYPE_SEND,
-	TYPE_RECV
+struct Stream {
+  FILE* file;
+  int current;
+  
+  bool Open(const char* path) {
+    assert(path != NULL);
+
+    // Open file.
+    file = fopen(path, "rb");
+    if (file == NULL) {
+      return false;
+    }
+    
+    current = fgetc(file);
+    
+    return true;
+  }
+  void Close() {
+    if (file != NULL) {
+      fclose(file);
+      file = NULL;
+    }
+  }
+  int Current() {
+    return current;
+  }
+  int Next() {
+    current = fgetc(file);
+    return current;
+  }
 };
 
-struct packet_list_item {
-	struct packet packet;
-	
-	struct packet_list_item* prev;
-	struct packet_list_item* next;
+void Error(const char* fmt, ...) {
+  char buf[1024];
+  
+  va_list argp;
+  va_start(argp, fmt);
+  vsnprintf(buf, sizeof(buf) - 1, fmt, argp);
+  va_end(argp);
+  
+  fprintf(stderr, "Error: %s\n", buf);
+  // Not necessary, we do it just in case.
+  fflush(stderr);
+  
+  abort();
+}
+
+int ReadInteger(Stream& stream) {
+  int result = 0;
+  
+  if(!isdigit(stream.Current())) {
+    Error("expected digit");
+  }
+  
+  while (isdigit(stream.Current())) {
+    result *= 10;
+    result += stream.Current() - '0';
+    stream.Next();  
+  }
+  
+  return result;
+}
+
+uint8_t ReadHexDigit(Stream& stream) {
+  int r = 0;
+  
+  if(!isxdigit(stream.Current())) {
+    Error("expected hex digit");
+  }
+  
+  r = stream.Current();
+  if (r >= '0' && r <= '9') {
+    r = r - '0';
+  } else if (r >= 'a' && r <= 'f') {
+    r = r - 'a' + 10;
+  } else if (r >= 'A' && r <= 'F') {
+    r = r - 'A' + 10;
+  } else Error("expected hex digit");
+  
+  stream.Next();
+  return static_cast<uint8_t>(r);
+}
+
+uint8_t ReadHexByte(Stream& stream) {
+  uint8_t c1, c2;
+  
+  c1 = ReadHexDigit(stream);
+  c2 = ReadHexDigit(stream);
+  
+  return (c1 << 4) + c2;
+}
+
+void Match(Stream& stream, char ch) {
+  if (stream.Current() != ch) {
+    Error("expected %c", ch);
+  }
+  stream.Next();
+}
+void Match(Stream& stream, const char* s) {
+  while(*s != 0) {
+    Match(stream, *s++);
+  }
+}
+void SkipUntil(Stream& stream, char c) {
+  while (stream.Current() != c) {
+    stream.Next();  
+  }
+}
+
+void ReadIP(Stream& stream, uint32_t& ip, uint16_t& port) {
+  uint8_t b1 = static_cast<uint8_t>(ReadInteger(stream));
+  Match(stream, '.');
+  uint8_t b2 = static_cast<uint8_t>(ReadInteger(stream));
+  Match(stream, '.');
+  uint8_t b3 = static_cast<uint8_t>(ReadInteger(stream));
+  Match(stream, '.');
+  uint8_t b4 = static_cast<uint8_t>(ReadInteger(stream));
+  
+  ip = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
+  
+  Match(stream, ':');
+  port = static_cast<uint16_t>(ReadInteger(stream));
+}
+
+void SkipLine(Stream& stream) {
+  while (stream.Current() != -1 && stream.Current() != '\n') {
+    stream.Next();
+  }
+  
+  stream.Next();
+  
+  if (stream.Current() == '\r') {
+    stream.Next();
+  }
+}
+
+void ReadData(Stream& stream, std::vector<uint8_t>& data) {
+  size_t bytesLeft = data.size();
+  size_t dataIndex = 0;
+  
+  while (bytesLeft != 0) {
+    size_t bytesOnLine;
+    SkipUntil(stream, ' '); // don't care about offset.
+    Match(stream, "  ");
+
+    if (bytesLeft > 16) {
+      bytesOnLine = 16;
+    } else {
+      bytesOnLine = bytesLeft;  
+    }
+
+    for (size_t k = 0; k < bytesOnLine; k++) {
+      data[dataIndex + k]  = ReadHexByte(stream);
+      Match(stream, ' ');
+    }
+
+    bytesLeft -= bytesOnLine;
+    dataIndex += bytesOnLine;
+    SkipLine(stream);
+  }
+  
+  SkipLine(stream);
+}
+
+void ReadPackets(const char* path, std::vector<Packet>& result) {
+  Stream stream;
+  stream.Open(path);
+  
+  Packet packet;
+  
+  while (stream.Current() != -1) {
+    // skip packet number
+    ReadInteger(stream);
+    Match(stream, "  ");
+    
+    ReadIP(stream, packet.srcAddress, packet.srcPort);
+    Match(stream, "  ");
+    
+    ReadIP(stream, packet.dstAddress, packet.dstPort);
+    Match(stream, "  ");
+    
+    size_t length = ReadInteger(stream);
+    Match(stream, "  ");
+    
+    packet.data.resize(length);
+    
+    if (stream.Current() == 'S') {
+      packet.type = Packet::TYPE_SEND;
+    } else if (stream.Current() == 'R') {
+      packet.type = Packet::TYPE_RECV;  
+    } else {
+      assert(0);  
+    }
+    
+    SkipLine(stream);
+    
+    ReadData(stream, packet.data);
+    
+    result.push_back(packet);
+  }
+  
+  stream.Close();
+}
+
+const char* programName = NULL;
+const char* inputFileName = NULL;
+
+void PrintUsage(FILE* stream, int exitCode) {
+  fprintf(stream, "Usage: %s options inputfile\n", programName);
+  fprintf(stream, " -h --help  Display this usage information.\n");
+  exit(exitCode);
+}
+
+struct IP {
+  uint32_t addr;
+  IP(uint32_t addr) : addr(addr) { }
+  
+  friend std::ostream& operator<< (std::ostream& o, const IP& addr);
 };
 
-struct stream {
-	FILE* file;
-	int current;
+std::ostream& operator<< (std::ostream& o, const IP& addr) {
+  return o << ((addr.addr >> 24) & 0xFF) << "."
+           << ((addr.addr >> 16) & 0xFF) << "."
+           << ((addr.addr >> 8) & 0xFF) << "."
+           << ((addr.addr >> 0) & 0xFF);
+}
+
+struct BinaryData {
+  const std::vector<uint8_t>* data;
+  BinaryData(const std::vector<uint8_t>& data) : data(&data) { }
+  
+  friend std::ostream& operator<< (std::ostream& o, const BinaryData& data);
 };
 
-struct stream* stream_open(const char* path) {
-  struct stream* result;
-  
-	assert(path != NULL);
-	
-	result = (struct stream*) malloc(sizeof(struct stream));
-	assert(result != NULL);
-	
-	result->file = fopen(path, "rb");
-	// XXX[7.8.2012 alex]: what if there is no such file
-	
-	result->current = fgetc(result->file);
-	
-	return result;
-}
-
-void stream_close(struct stream* stream) {
-  assert(stream != NULL);
-  
-  fclose(stream->file);
-  free(stream);
-}
-
-void stream_next(struct stream* stream) {
-	stream->current = fgetc(stream->file);
-}
-
-int read_integer(struct stream* stream) {
-	int result = 0;
-	assert(isdigit(stream->current));
-	
-	while (isdigit(stream->current)) {
-		result *= 10;
-		result += stream->current - '0';
-		stream_next(stream);	
-	}
-	
-	return result;
-}
-int read_hex_digit(struct stream* stream) {
-	int r = 0;
-	
-	assert(isxdigit(stream->current));
-	r = stream->current;
-	if (r >= '0' && r <= '9') {
-		r = r - '0';
-	} else if (r >= 'a' && r <= 'f') {
-		r = r - 'a' + 10;
-	} else if (r >= 'A' && r <= 'F') {
-		r = r - 'A' + 10;
-	} else assert(0);
-	stream_next(stream);
-	
-	return r;
-}
-uint8_t read_ubyte_hex(struct stream* stream) {
-	int c1, c2;
-	
-	c1 = read_hex_digit(stream);
-	c2 = read_hex_digit(stream);
-	
-	return (c1 << 4) + c2;
-}
-
-void match_current(struct stream* stream, char c) {
-	assert(stream->current == c);
-	stream_next(stream);
-}
-void match_string(struct stream* stream, const char* s) {
-	while(*s != 0) {
-		match_current(stream, *s++);	
-	} 	
-}
-void skip_until(struct stream* stream, char c) {
-	while (stream->current != c) {
-		stream_next(stream);	
-	}	 
-}
-
-void read_ip(struct stream* stream, uint32_t* ip, uint16_t* port) {
-	uint8_t b1;
-	uint8_t b2;
-	uint8_t b3;
-	uint8_t b4;
-	
-	b1 = read_integer(stream);
-	match_current(stream, '.');
-	b2 = read_integer(stream);
-	match_current(stream, '.');
-	b3 = read_integer(stream);
-	match_current(stream, '.');
-	b4 = read_integer(stream);
-	
-	*ip = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
-	
-	match_current(stream, ':');
-	*port = read_integer(stream);
-}
-
-void skip_line(struct stream* stream) {
-	while (stream->current != -1 && stream->current != '\n') {
-		stream_next(stream);
-	}
-	stream_next(stream);
-	if (stream->current == '\r') stream_next(stream);
-}
-
-int read_packets(const char* path, struct packet_list_item** result) {
-	struct stream* stream;
-	int i = 0;
-	int j = 0;
-	struct packet_list_item* current;
-	
-	stream = stream_open(path);
-	*result = (struct packet_list_item*) malloc(sizeof(struct packet_list_item));
-	current = *result;
-	
-	while (stream->current != -1) {
-		current->packet.number = read_integer(stream);
-		match_string(stream, "  ");
-		read_ip(stream, &current->packet.src, &current->packet.srcPort);
-		match_string(stream, "  ");
-		read_ip(stream, &current->packet.dst, &current->packet.dstPort);
-		match_string(stream, "  ");
-		current->packet.length = read_integer(stream);
-		
-		match_string(stream, "  ");
-		if (stream->current == 'S') {
-			current->packet.type = TYPE_SEND;
-		} else if (stream->current == 'R') {
-			current->packet.type = TYPE_RECV;	
-		} else {
-			assert(0);	
-		}
-		skip_line(stream);
-		
-		current->packet.data = (uint8_t*) malloc(current->packet.length);
-		i = current->packet.length;
-		j = 0;
-		while (i != 0) {
-			int count;
-			int k;
-			skip_until(stream, ' '); // don't care about offset.
-			match_string(stream, "  ");
-			
-			if (i > 16) {
-				count = 16;
-			} else {
-				count = i;	
-			}
-			
-			for (k = 0; k < count; k++) {
-				current->packet.data[j + k]	= read_ubyte_hex(stream);
-				match_current(stream, ' ');
-			}
-			
-			i -= count;
-			j += count;
-			skip_line(stream);
-		}
-		//for (i = 0; i < (current->packet.length + 15) / 16; i++) skip_line(stream);
-		skip_line(stream);
-		
-		if (stream->current != -1) {
-			current->next = (struct packet_list_item*) malloc(sizeof(struct packet_list_item));
-			current->next->prev = current;
-			current = current->next;
-		} else {
-			current->next = 0;
-			current = 0;
-		}
-	}
-	
-	stream_close(stream);
-	
-	return 0;
-}
-
-const char* program_name = NULL;
-const char* input_filename = NULL;
-const char* output_filename = NULL;
-
-void print_usage(FILE* stream, int exit_code) {
-	fprintf(stream, "Usage: %s options inputfile outputfile\n", program_name);
-	fprintf(stream, " -h --help               Display this usage information.\n");
-	exit(exit_code);
+std::ostream& operator<< (std::ostream& o, const BinaryData& data) {
+  o << std::hex;
+  for (size_t i = 0; i < data.data->size(); i++) {
+    o << ((data.data->at(i) >> 4) & 0xF)
+      << ((data.data->at(i) >> 0) & 0xF);
+  }
+  return o << std::dec;
 }
 
 void run() {
-	FILE* output;
-	struct packet_list_item* first;
-	struct packet_list_item* current;
-		
-	read_packets(input_filename, &first);
-	output = fopen(output_filename, "w+");
-	
-	fprintf(output, "{\"packets\":[\n");
-	
-	current = first;
-	while (current != NULL) {
-		// XXX[7.8.2012 alex]: endian?
-		
-		fprintf(output, "{\n");
-		
-		fprintf(output, "\t\"number\":%d,\n", current->packet.number);
-		fprintf(output, "\t\"src\":%d,\n", current->packet.src);
-		fprintf(output, "\t\"srcPort\":%d,\n", current->packet.srcPort);
-		fprintf(output, "\t\"dst\":%d,\n", current->packet.dst);
-		fprintf(output, "\t\"dstPort\":%d,\n", current->packet.dstPort);
-		fprintf(output, "\t\"length\":%d,\n", current->packet.length);
-		fprintf(output, "\t\"type\":%d\n", current->packet.type);
-		//fwrite(&current->packet.dst, 4, 1, output);
-		//fwrite(&current->packet.dstPort, 2, 1, output);
-		//fwrite(&current->packet.length, 4, 1, output);
-		//fwrite(&current->packet.type, 1, 1, output);
-		//fwrite(current->packet.data, current->packet.length, 1, output);
-		
-		fprintf(output, "}");
-		if (current->next != NULL) {
-		  fprintf(output, ",");
-		}
-		fprintf(output, "\n");
-		
-		current = current->next;
-	}
-	
-	fprintf(output, "]}");
-	
-	current = first;
-	while (current != NULL) {
-		struct packet_list_item* next = current->next;
-		if (current->packet.data != NULL) {
-			free(current->packet.data);
-		}
-		free(current);
-		current = next;
-	}
+  std::vector<Packet> packets;
+  ReadPackets(inputFileName, packets);
+  
+  std::cout << "{ \"packets\" : [" << std::endl;
+  
+  for(size_t i = 0; i < packets.size(); i++) {
+    const Packet& packet = packets[i];
+    
+    std::cout << "{" << std::endl
+              << "  \"srcAddress\" : " << "\"" << IP(packet.srcAddress) << "\"" << "," << std::endl
+              << "  \"srcPort\"    : " << packet.srcPort << "," << std::endl
+              << "  \"dstAddress\" : " << "\"" << IP(packet.dstAddress) << "\"" << "," << std::endl
+              << "  \"dstPort\"    : " << packet.dstPort << "," << std::endl
+              << "  \"type\"       : " << (packet.type == Packet::TYPE_SEND ? "\"send\"" : "\"recv\"") << "," << std::endl
+              << "  \"data\"       : " << "\"" << BinaryData(packet.data) << "\"" << std::endl
+              << "}";
+    if (i != packets.size() - 1) {
+      std::cout << ",";
+    }
+    std::cout << std::endl;
+  }
+  
+  std::cout << "]}";
 }
 
 int main(int argc, char** argv) {
-	int next_option;
-	
-	const char* const short_options = "ho:";
-	const struct option long_options[] = {
-		{ "help", 0, NULL, 'h' },
-		{ NULL, 0, NULL, 0 }
-	};
-	
-    program_name = argv[0];
+  int next_option;
+  
+  const char* const short_options = "ho:";
+  const struct option long_options[] = {
+    { "help", 0, NULL, 'h' },
+    { NULL, 0, NULL, 0 }
+  };
+  
+  programName = argv[0];
     
-	do {
-		next_option = getopt_long(argc, argv, short_options, long_options, NULL);
-		
-		switch (next_option) {
-			case 'h': /* -h or --help */
-				/**
-				 * User has requested usage information. Print it to standard
-				 * output, and exit with exit code zero (normal termination). 
-				 */
-				print_usage (stdout, EXIT_SUCCESS);
-				
-			case '?': /* invalid option */
-				/**
-				 * Print usage information to standard error, and exit with exit
-				 * code one (indicating abnormal termination).
-				 */
-				fprintf(stderr, "Invalid option.\n");
-				print_usage (stderr, EXIT_FAILURE);
-			case -1: /* done with options */
-				break;
-			default: /* unexpected */
-				abort();
-		}
-	} while (next_option != -1);
-	
-	if (optind != argc - 2) {
-		fprintf(stderr, "Incorrect number of input files.\n");
-		print_usage(stderr, EXIT_FAILURE);	
-	}
-	
-	input_filename = argv[argc - 2];
-	output_filename = argv[argc - 1];
-	
-	run();
-	
-	return EXIT_SUCCESS;
+  do {
+    next_option = getopt_long(argc, argv, short_options, long_options, NULL);
+    
+    switch (next_option) {
+      case 'h': /* -h or --help */
+        /**
+         * User has requested usage information. Print it to standard
+         * output, and exit with exit code zero (normal termination). 
+         */
+        PrintUsage (stdout, EXIT_SUCCESS);
+        
+      case '?': /* invalid option */
+        /**
+         * Print usage information to standard error, and exit with exit
+         * code one (indicating abnormal termination).
+         */
+        fprintf(stderr, "Invalid option.\n");
+        PrintUsage (stderr, EXIT_FAILURE);
+      case -1: /* done with options */
+        break;
+      default: /* unexpected */
+        abort();
+    }
+  } while (next_option != -1);
+  
+  if (optind != argc - 1) {
+    fprintf(stderr, "Incorrect number of input files.\n");
+    PrintUsage(stderr, EXIT_FAILURE);  
+  }
+  
+  inputFileName = argv[argc - 1];
+  
+  run();
+  
+  return EXIT_SUCCESS;
 }
 #define TESTUMAX(bits) glue3(u,bits,=) glue3(~,u,bits); if (glue3(UINT,bits,_MAX) glue3(!=,u,bits)) printf ("Something wrong with UINT%d_MAX\n", bits)
  
 int main () {
-	DECL(I,8)
-	DECL(U,8)
-	DECL(I,16)
-	DECL(U,16)
-	DECL(I,32)
-	DECL(U,32)
+  DECL(I,8)
+  DECL(U,8)
+  DECL(I,16)
+  DECL(U,16)
+  DECL(I,32)
+  DECL(U,32)
 #ifdef INT64_MAX
-	DECL(I,64)
-	DECL(U,64)
+  DECL(I,64)
+  DECL(U,64)
 #endif
-	intmax_t imax = INTMAX_C(0);
-	uintmax_t umax = UINTMAX_C(0);
-	char str0[256], str1[256];
+  intmax_t imax = INTMAX_C(0);
+  uintmax_t umax = UINTMAX_C(0);
+  char str0[256], str1[256];
 
-	sprintf (str0, "%d %x\n", 0, ~0);
-	
-	sprintf (str1, "%d %x\n",  i8, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1);
-	sprintf (str1, "%u %x\n",  u8, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1);
-	sprintf (str1, "%d %x\n",  i16, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1);
-	sprintf (str1, "%u %x\n",  u16, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1);	
-	sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n",  i32, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1);
-	sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n",  u32, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1);
-#ifdef INT64_MAX	
-	sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n",  i64, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1);
+  sprintf (str0, "%d %x\n", 0, ~0);
+  
+  sprintf (str1, "%d %x\n",  i8, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1);
+  sprintf (str1, "%u %x\n",  u8, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1);
+  sprintf (str1, "%d %x\n",  i16, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1);
+  sprintf (str1, "%u %x\n",  u16, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1);  
+  sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n",  i32, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1);
+  sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n",  u32, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1);
+#ifdef INT64_MAX  
+  sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n",  i64, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1);
 #endif
-	sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n",  imax, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1);
-	sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n",  umax, ~0);
-	if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1);	
-	
-	TESTUMAX(8);
-	TESTUMAX(16);
-	TESTUMAX(32);
+  sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n",  imax, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1);
+  sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n",  umax, ~0);
+  if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1);  
+  
+  TESTUMAX(8);
+  TESTUMAX(16);
+  TESTUMAX(32);
 #ifdef INT64_MAX
-	TESTUMAX(64);
+  TESTUMAX(64);
 #endif
 
-	return EXIT_SUCCESS;
+  return EXIT_SUCCESS;
 }
 
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.