bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Submission of new app for consideration


From: m . mohr
Subject: Submission of new app for consideration
Date: Sat, 27 Mar 2004 23:27:33 -0800 (PST)

Hello coreutils maintainers:

I wrote this program to satisfy a need of my own, and thought it might be
useful to others after I completed it.  For this reason, I am submitting
it to you.

I use Linux at home and for some time have wanted to contribute to the
FOSS community, but haven't really had a way.  I'm hoping this might be a
start :)

What this does is accept a series of hex bytes in ASCII and print them to
stdout as their respective characters.  It seemed to me that a hex editor
was really overkill for my simple uses, and this code has the bonus of
being scriptable in pipes.

Please tell me what you think.  I look forward to your reply.

Regards,
Michael Mohr

/* hex.c
 *
 * This source code is released under the GPL.
 *
 * This program is very basic: it takes character codes on the command line 
delimited by spaces
 * and prints the specified characters.  Input can be from stdin or the command 
line.
 *
 * Usage: 'echo "41 42 43" | hex -s3'   **OR**  'hex -c3 41 42 43'
 *
 * Author: Michael Mohr (m_dot_mohr_at_laposte_dot_net)
 *
 * Version 0.001
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef char bytes_c[2];
typedef char bytes_s[3];


void read_stdin(int argc, char *arg1)
{
  int i;
  int size_array = strtol(arg1+2, 0, 10);
  int final_bytes[size_array];

  bytes_s *buf = calloc(size_array, sizeof(bytes_s));
  bytes_c *bytes = calloc(size_array, sizeof(bytes_c));

  if(!buf || !bytes) { printf("cannot alloc mem"); }

  for(i=0; i<size_array; i++)
    {
      read(0, buf[i], 3);

      if(buf[i][2] != ' ') { puts("delimit with single spaces please!"); 
exit(1); }

      strncpy(bytes[i], buf[i], 2);

      final_bytes[i] = strtol(bytes[i], 0, 16);
      printf("%c", final_bytes[i]);
    }

  free(buf);
  free(bytes);

}

void read_args(int argc, char *argv[])
{
  int i, j;
  int size_array = strtol(argv[1]+2,0,10);
  int final_bytes[size_array];

  if(size_array > (argc-2)) { printf("Invalid # of bytes.\n"); exit(1); }

  bytes_c *buf = calloc(strtol(argv[2], 0, 10), sizeof(bytes_c));
  if(!buf){ printf("cannot alloc memory"); }

  for(i=0, j=2; i<size_array; i++, j++)
    {
      strncpy(buf[i], argv[j], 2);

      final_bytes[i] = strtol(buf[i], 0, 16);
      printf("%c", final_bytes[i]);
    }

  free(buf);

}


int main(int argc, char *argv[])
{

  if(argc == 1) { puts("hex.c v. 0.001"); return 1; }


  if(argv[1][0] != '-') { puts("need 1 command line arg. (-s || -c)"); return 
1; }

  if(argv[1][1] == 'c')
    {
      if(argc < 3) { printf("insufficient # of args.\n"); return 1; }
      read_args(argc, argv);
    }

  if(argv[1][1] == 's')
    {
      if(argc != 2) { puts("Specify # of bytes to go thru."); return 1; }
      read_stdin(argc, argv[1]);
    }

  return 0;

}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]