Processing and Printing Command Line Arguments in Hexadecimal
#include
#include
int main(int argc, char **argv, char **envp)
{
int i, j;
char *temp;
/* sort environment variables using bubble sort */
for (i = 0; envp[i] != NULL; i++) /* repeat until a NULL is found */
{
strtok(envp[i], "="); /* separate variable name from value */
for (j = i + 1; envp[j] != NULL; j++) /* compare remaining variables */
{
strtok(envp[j], "="); /* separate variable name from value */
/* if not in order */
if (strcmp(envp[i], envp[j]) > 0)
{
/* swap */
temp = envp[i];
envp[i] = envp[j];
envp[j] = temp;
}
}
}
/* print all variables */
for (i = 0; envp[i] != NULL; i++)
{
envp[i][strlen(envp[i])] = '='; /* add missing = removed by strtok */
printf("%s\n", envp[i]);
}
return 0;
}
#include
#include
#include
/* Prints the given pointer contents as a series of 8 bytes */
void print_hex_pointer(void *value)
{
int i;
char *c;
c = (char *) value;
for (i = 7; i >= 0; i--) /* print from msb to lsb */
{
if (i < 7)
printf(" ");
printf("%02hhx", c[i]);
}
}
int main(int argc, char **argv)
{
int i;
char c;
char *pointer;
char *last;
/* print argv contents and pointer */
printf("argv | ");
print_hex_pointer(&argv);
printf(" | %p\n", &argv);
printf("\n");
/* print all argv argument pointers */
for (i = 0; i < argc; i++)
{
printf("argv[%d] | ", i);
print_hex_pointer(&argv[i]);
printf(" | %p\n", &argv[i]);
}
printf("\n");
/* print memory contents */
pointer = argv[0]; /* start of memory to be printed */
pointer = (char *)((long)pointer & -8); /* align to 8 bytes */
last = argv[argc - 1] + strlen(argv[argc - 1]); /* address of last char */
while (pointer <= last)
{
printf(" | ");
for (i = 7; i >= 0; i--) /* print from msb to lsb */
{
c = pointer[i];
if (isprint(c)) /* if printable */
printf("%02hhx(%c) ", c, c);
else /*non printable, use escape */
printf("%02hhx(\\%hhu)", c, c);
printf(" ");
}
printf("| %p\n", pointer);
pointer += 8; /* advance pointer 8 bytes */
}
printf("\n");
return 0;
}
#include
#include
struct node
{
int value;
struct node *next;
};
int main(int argc, char **argv, char **envp)
{
int number;
struct node *head, *tail, *new_node, *temp;
struct node *inode, *jnode, *iprev, *jprev;
head = NULL; /* initially list is empty */
tail = NULL; /* tail is null */
while(scanf("%d", &number) != EOF)
{
new_node = (struct node *) malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("Out of memory!\n");
return 1;
}
new_node->value = number; /* save read number */
new_node->next = NULL;
if (head == NULL) /* if new list, use node as list */
{
head = new_node;
tail = head; /* tail is first node */
}
else /* else, attach at tail */
{
tail->next = new_node;
tail = new_node; /* new node is the new tail */
}
}
/* sort nodes using bubble sort */
for (inode = head, iprev = NULL; inode != NULL; iprev = inode, inode = inode->next) /* repeat until a NULL is found */
{
for (jnode = inode->next, jprev = inode; jnode != NULL; jprev = jnode, jnode = jnode->next) /* compare remaining variables */
{
/* if not in order */
if (inode->value > jnode->value)
{
/* swap links */
if (jprev != inode) /* if not near */
{
temp = jnode->next;
jnode->next = inode->next;
if (iprev != NULL)
iprev->next = jnode;
inode->next = temp;
jprev->next = inode;
}
else
{
if (iprev != NULL)
iprev->next = jnode;
inode->next = jnode->next;
jnode->next = inode;
}
/* fix head and tail pointers */
if (inode == head)
head = jnode;
if (jnode == tail)
tail = inode;
/* swap i and j pointers */
temp = inode;
inode = jnode;
jnode = temp;
}
}
}
/* print all numbers */
for (inode = head; inode != NULL; inode = inode->next)
{
printf("%d\n", inode->value);
}
/* free all list */
inode = head;
while (inode != NULL)
{
temp = inode;
inode = inode->next;
free(temp);
}
return 0;
}
#include
#include
int main(int argc, char **argv)
{
int opt;
int copt, dopt, uopt, fopt, sopt;
char *ffields, *schar, *infile, *outfile;
copt = 0; /* no -c */
dopt = 0; /* no -d */
uopt = 0; /* no -u */
fopt = 0; /* no -f */
sopt = 0; /* no -s */
ffields = NULL;
ffields = NULL;
schar = NULL;
infile = NULL;
outfile = NULL;
opterr = 0; /* don't print errors from getopt */
/* read all options using the command line parameters:
my_uniq [-c|-d|-u] [-f fields] [-s char] [input_file [output_file]]
*/
while ((opt = getopt(argc, argv, ":cduf:s:")) != -1)
{
switch(opt)
{
case 'c':
copt = 1;
break;
case 'd':
dopt = 1;
break;
case 'u':
uopt = 1;
break;
case 'f':
fopt = 1;
ffields = optarg;
break;
case 's':
sopt = 1;
schar = optarg;
break;
case ':':
if (optopt == 'f')
printf("Option -f requires an argument\n");
else if (optopt == 's')
printf("Option -s requires an argument\n");
return 1;
case '?': /* ? */
printf("Invalid option -%c\n", optopt);
return 1;
default:
printf("Invalid option %c\n", opt);
return 1;
}
}
/* print the options given: */
printf("-c = %d\n", copt);
printf("-d = %d\n", dopt);
printf("-u = %d\n", uopt);
printf("-f = %d, fields = %s\n", fopt, ffields);
printf("-s = %d, char = %s\n", sopt, schar);
if (optind < argc) /* if there an input file was given */
infile = argv[optind];
if (optind + 1 < argc) /* if there an output file was given */
outfile = argv[optind + 1];
printf("input_filename = %s\n", infile);
printf("output_filename = %s\n", outfile);
return 0;
}