/*
   <p>An all-purpose byte display utility.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <environ.h>

/* 
   Returns a pointer to an internal static string containing 
   the byte <u>c</u> or its hexadecimal equivalent formatted as "<hh>".
   The string contains the unmodified byte provided it is an ASCII 
   character, is printable and is not a control character.

   This function should not appear more than once in a formatted print
   statement because all such calls be evaluated before the formatted
   output is generated. By this time the internal static string will
   contain the result of the final call and all such byte decodings
   will apparently result in the same (final) value. 

   If this is necessary, capture each decoded value in a separate
   string and print from these:

   <pre>
      char trap[5];
      strcpy(trap, byteout(cval));
      fprint("Caught %s\n", trap);
   </pre>

   This function is primarily intended as a debugging aid.
*/
char *byteout(char c)
{
   static char    buff[10];

   if (isascii(c) && isprint(c) && !iscntrl(c))
      snprintf(buff, 10, "%c", c);
   else
      snprintf(buff, 10, "<%02x>", c);

   return buff;
}


