Coding at you peoples

Here’s some C code:

“autofw.h”:

#ifndef _AUTOFW_H
#define _AUTOFW_H

#ifdef __cplusplus
extern "C" {
#endif

/* global to hold invoked name */
char invoked[0xff];

/* flags field for options tracking */
long flags = 0x00000000;
int  bits  = 0x00;

/**
 * Low word, Low byte:
 * 0111 0101 user
 * 0110 0110 file
 * 0111 0011 silent
 * 0101 0110 verbose
 * 0110 0100 debug
 * 0110 1000 help
 * 0111 0110 version
 * 0000 0000 NULL
 */

#define USER_FLAG    0x00000075
#define FILE_FLAG    0x00000066
#define SILENT_FLAG  0x00000073
#define VERBOSE_FLAG 0x00000056
#define DEBUG_FLAG   0x00000064
#define HELP_FLAG    0x00000068
#define VERSION_FLAG 0x00000076 
#define NULL_FLAG    0x00000000

#define USER_BIT     0x01
#define FILE_BIT     0x02
#define SILENT_BIT   0x04
#define VERBOSE_BIT  0x08
#define DEBUG_BIT    0x10
#define HELP_BIT     0x20
#define VERSION_BIT  0x40
#define NULL_BIT     0x00

/* character array for short args */
const char SHORTARGS[] = "u:f:sVdhv";

#define SHORT_USER    0x00000001
#define SHORT_FILE    0x00000003
#define SHORT_SILENT  0x00000005
#define SHORT_VERBOSE 0x00000006
#define SHORT_DEBUG   0x00000007
#define SHORT_HELP    0x00000008
#define SHORT_VERSION 0x00000009
#define SHORT_NULL    0x0000000a

/* GNU options struct for command-line args */
struct option options[] = {
/*       name     , has_arg, flag  , val */
	{"user"   , 1      , &flags, 'u'},
	{"file"   , 1      , &flags, 'f'},
	{"silent" , 0      , &flags, 's'},
	{"verbose", 0      , &flags, 'V'},
	{"debug"  , 0      , &flags, 'd'},
	{"help"   , 0      , &flags, 'h'},
	{"version", 0      , &flags, 'v'},
	{0        , 0      , 0     ,  0 }
}

/* turn flags into meaningful bitfields */
int flags_to_bits(long);

/* make procmailrc_path */
char *mkprocmailrc_path(int);

/* resolve errors */
int check_error(void);

/* print debug messages */
inline void dprintf(const char *);

#ifdef __cplusplus
}
#endif

#endif

“main.c”:

#include 
#include 
#include 
#include 
#include 

#include "autofw.h"

inline void dprintf(const char *text) {
	fprintf(stderr, "%s debug: %s\n", invoked, text);
}

int check_error() {
	if (errno != 0)
		perror(invoked);

	return errno;
}

char *mkprocmailrc_path(int uid) {
	struct passwd *pw;
	char          *home_dir;
	static char   procmailrc_path[PATH_MAX];

	errno    = 0;
	pw       = getpwuid(uid);

	if (check_error())
		exit(EXIT_FAILURE);

	home_dir = pw->pw_dir;
	
	strcpy(procmailrc_path, home_dir);
	strcat(procmailrc_path, "/.procmailrc");
	procmailrc_path[0xfe] = '';

	return(procmailrc_path);
}

int flags_to_bits(long flags) {
	switch (flags) {
		case USER_FLAG:
			bits |= USER_BIT;
			break;

		case FILE_FLAG:
			bits |= FILE_BIT;
			break;

		case SILENT_FLAG:
			bits |= SILENT_BIT;
			break;

		case VERBOSE_FLAG:
			bits |= VERBOSE_BIT;
			break;

		case DEBUG_FLAG:
			bits |= DEBUG_BIT;
			break;

		case HELP_FLAG:
			bits |= HELP_BIT;
			break;

		case NULL_FLAG:
			bits |= NULL_BIT;
			break;

		case VERSION_FLAG:
			bits |= VERSION_BIT;
			break;
	}

	return bits;
}
	
int main(int argc, char *argv[]) {
	/* parse command-line arguments */
	     strcpy(invoked, argv[0]);
	     invoked[0xfe]= '';
	int  option_index = 0;
	     errno        = 0;
	int  uid          = getuid();

	if (check_error()) {
		exit(EXIT_FAILURE);

	char c;
	char *procmailrc_path;

	procmailrc_path = mkprocmailrc_path(uid);
	
	while (c = getopt_long(argc, argv, SHORTARGS, options, &option_index) != -1) {
		flags_to_bits((long) c);
		switch (c) {
			case USER_FLAG:
				uid = optarg;
				procmailrc_path = mkprocmailrc_path(uid);
				break;

			case FILE_FLAG:
				strncpy(procmailrc_path, optarg, 0xff);
				procmailrc_path[0xfe] = '';
				break;

			case HELP_FLAG:
				print_help_msg();
				exit(EXIT_SUCCESS);
				
			case VERSION_FLAG:
				print_version_msg();
				exit(EXIT_SUCCESS);
		}

	}

	if (bits & DEBUG_BIT)
		dprintf("debug bit set");	




It’s obviously not finished yet. While I was trying to mail these files to myself (easier than cat+copy), I accidentally typed maul -s "main.c" imbrius < main.c instead of mail -s "main.c" imbrius < main.c. The C shell politely informed me that it didn’t know how to maul that code.

BTW this post is all about using WordPress as an online backup 😛

Advertisements
  1. #1 by Joshua on February 18, 2010 - 11:11 AM

  2. #2 by Joshua on February 18, 2010 - 12:39 PM

    Using #if 0
    ...some code
    #endif
    to disable a section of code in C is so widely used that many IDEs (and the default C syntax highlighting in Vim) treat it as a multiline comment delimiter.

  3. #3 by Joshua on February 18, 2010 - 12:47 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: