segunda-feira, 30 de novembro de 2009

My baby arrived!


Now I can read about C out of computer :P

Finally I got, my own get_opts function =D

I spent some good hours trying to get this working...
After a hundreds of attemps I decide to make a real
algorithm.. and then .. after a little setting in the logic
I got really working as I expect..

Here is
#include <stdio.h>

static int   Optindx = 1;
static char *Optarg  = NULL;

short int
chstr (char ch, char *str)
{
 for (; *str; str++)
  if (ch == *str)
   return 0;
 return -1;
}

char *
search_arg(char **argv)
{
 char *ptr;
 if (argv[Optindx+1] == NULL)
  return 0;
 while (*(ptr = argv[++Optindx]) != '-')
  if (argv[Optindx] = NULL)
   return 0;
 
 return ptr;
}

int
get_opts(char **argv, char *legal)
{
 int letter = 0;

 if (Optarg == NULL)
  if (*(Optarg = argv[Optindx]) != '-')
   if ((Optarg = search_arg (argv)) == 0)
    return -1;
  
 if (*Optarg == '\0')
  if ((Optarg = search_arg (argv)) == 0)
   return -1;

 if (*(Optarg+1) == '-') {
  return -1;
 Optarg++;
 return 0;
 }


 if (chstr (*Optarg, legal) == 0)
  return letter = *Optarg++;
 else 
  return '?';
}

/* to Test */
int
main (int argc, char *argv[])
{
 int ch;


 if (argc < 2)
  return -1;

 while ((ch = get_opts(argv, "abc")) != -1) {
  switch (ch) {
   case 'a':
    putchar ('a');
    break;
   case 'b':
    putchar ('b');
    break;
   case 'c':
    putchar ('c');
    break;
   case '?':
    fprintf (stderr, "Unknown argument %c\n", *Optarg);
    return -1;
  }
 }
 putchar ('\n');
 return 0;
}

It parses 1char arguments, that can be passed in any order.
The switch -- forces to stop the parser

To program point of view, an argument is a character that
belongs to a string prefixed with a '-'

domingo, 29 de novembro de 2009

):

/* 
* I know so little ): 
*/

sexta-feira, 27 de novembro de 2009

XOR encryption looks realy funny (: -> encrypt.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#define crpt(byte,key) (byte^key)

int
main (int argc, char *argv[])
{
 int f1, f2, n;
 char unbuf;
 
 if (argc < 4) 
  error (-1, EINVAL, "Error, argument fault",
     argv[0]);
 
 if ((strlen (argv[3])) <= 2)
  error (-1, EINVAL, "Error, encyrpt key is "
    "too short (less then 3 characters)");
 
 if ((f1 = open (argv[1], O_RDONLY)) == -1) 
  err (-1, "Error, while trying to open "
    "file %s for read", argv[1]);

 if ((f2 = creat (argv[2], 0644)) == -1)
  err (-1, "Error, while trying to create/open "
    "file %s for write", argv[2]);

 while ((n = read (f1, &unbuf, 1)) > 0) 
 {
  char *key = argv[3];
  
  if ((key+1) == '\0')
   key = argv[3];
  unbuf = crpt(unbuf, *key);

  if ((write (f2, &unbuf, 1)) == -1)
   err(-1, "Error while trying to write "
    "in file %s", argv[2]);
  key++; 
 }
  
 return 0;
}

usage: ./ecrypt source_file encrypted_file crypt_key
This little program will take the source_file, encrypt it, and write
the result in the encrypted_file using the crypt_key

You can decrypt the encrypted_file using the same crypt_key.. like
./encrypt encrypted_file decrypted_file same_crypt_key

this is funny! :-)

quinta-feira, 26 de novembro de 2009

XOR encryptation looks cool as well

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

#define ENCRYPT(a,b) (encrypt_decrypt (a, b))
#define DECRYPT(a,b) (encrypt_decrypt (a, b))


signed char *
encrypt_decrypt (char *str, char *key)
{
 char *crp_str, *addr;
 crp_str = addr = (char *) malloc (strlen (str));
 if (crp_str == 0)
  return (char *)-1;   
 for (; *str;
  str++, key++, crp_str++)
  *crp_str = *str ^ *key;
 *crp_str = '\0';
 return addr;
}

int 
main (void)
{
 char *str;
 str = ENCRYPT ("Hello World", "Im nice guy"); 
 printf ("%s\n", DECRYPT (str, "Im nice guy")); 
 return 0;
}

quarta-feira, 25 de novembro de 2009

obstack looks cool

/*
* An obstack is a pool of memory
* containing a stack of objects
*/


#include <stdio.h>
#include <stdlib.h>
#include <obstack.h>


#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
#define obstack_alloc_fail_handler  (fprintf (stderr, "Memory exhausted\n"); exit (EXIT_FAILURE);)

static struct obstack string_obstack;


void *
xmalloc (size_t size)
{
 register void *value = malloc (size);
 if (value == 0)
  return 0;
 
 return value;
}


char *
copystring (struct obstack *obstack_ptr, char *string)
{
 size_t len = strlen (string) + 1;
 char *s = (char *) obstack_alloc (obstack_ptr, len);
 memcpy (s, string, len);

 return s;
}


int 
main (void)
{
 char *str;

 obstack_init (&string_obstack);
 str = copystring (&string_obstack, "Hello World");
 printf ("%s\n", str);
 obstack_free (&string_obstack, str);
 return 0;
}

terça-feira, 24 de novembro de 2009

cat that print name before lines, just bullshit

#include <stdio.h>
#include <errno.h>

int
main (int argc, char **argv)
{
 FILE *fp;
 char *line, buf[99];

 if (argc < 2)
  return -1;
 
 argv++; 
 for (; argc > 1; argc--, argv++)
 {
  if (! (fp = fopen (*argv, "r")))
   error (1,errno);

  while ((line = fgets(buf, 99, fp)) != NULL)
   printf ("%s: %s",  *argv, line);
 }
 return 0;
}