Add 'PracticingC/' from commit 'ef128a996fc826339bbc3d9deea376932caf6981'

git-subtree-dir: PracticingC
git-subtree-mainline: fd87ec8fe9
git-subtree-split: ef128a996f
This commit is contained in:
Dan Buch 2013-01-09 23:49:46 -05:00
commit a04a502787
32 changed files with 769 additions and 0 deletions

6
PracticingC/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*
!gowrikumar/bin
!gdbtut/bin
!*.i
!*.s
!*.c

21
PracticingC/Makefile Normal file
View File

@ -0,0 +1,21 @@
CD = cd
RM = rm -v
CC := gcc
CFLAGS := -std=c99 -Wall -g
ALL_TARGETS := $(patsubst %.c,%,$(wildcard *.c))
export CD RM CFLAGS
all: $(ALL_TARGETS)
$(CD) gowrikumar && $(MAKE)
$(CD) gdbtut && $(MAKE)
clean:
$(CD) gowrikumar && $(MAKE) clean
$(CD) gdbtut && $(MAKE) clean
.PHONY: all clean

3
PracticingC/README Normal file
View File

@ -0,0 +1,3 @@
I don't get paid to write C for a living, so I have to sneak in practice here
and there. Maybe once the kiddo goes to college I can become a kernel hacker
:-P (assuming there's still such a thing as a kernel.)

View File

@ -0,0 +1,14 @@
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
BINDIR := $(PWD)/bin
export BINDIR
all:
$(CD) src && $(MAKE)
clean:
$(RM) $(BINDIR)/*
.PHONY: all clean

View File

View File

@ -0,0 +1,12 @@
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
$(BINDIR)/%: %.c
$(CC) $(CFLAGS) -o $@ $<
all: $(ALL_BIN)
.PHONY: all

View File

@ -0,0 +1,28 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char *buf;
long long huge = 8000000000000000000;
/* Okay, so this is *not* going to segfault because
* the way memory is allocated has changed since
* the tutorial was written. The segfault is supposed
* to happen when more memory is allocated than is
* available on the machine. So much for that exercise.
*/
buf = malloc(huge);
fgets(buf, 1024, stdin);
printf("%s\n", buf);
return 1;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,14 @@
# puzzles from http://www.gowrikumar.com/c/
BINDIR := $(PWD)/bin
export BINDIR
all:
$(CD) src && $(MAKE)
clean:
$(RM) $(BINDIR)/*
.PHONY: all clean

View File

View File

@ -0,0 +1,23 @@
#include <stdio.h>
int array[] = {23, 34, 12, 17, 204, 99, 16};
#define TOTAL_ELEMENTS sizeof(array) / sizeof(array[0])
int main()
{
int d;
#ifdef DEBUG
printf("sizeof(array) = %d\n", TOTAL_ELEMENTS);
#endif
for (d = 0; d < TOTAL_ELEMENTS ; d++)
printf("%d\n", array[d]);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,20 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
} while(++i < 15);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,22 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("(Hit ^C to stop the madness.)\n");
while(1)
{
fprintf(stdout, "hello-out ");
fprintf(stderr, "hello-err ");
sleep(1);
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,23 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("This time we'll include \\n!\n");
printf("(Hit ^C to stop the madness.)\n");
while(1)
{
fprintf(stdout, "hello-out\n");
fprintf(stderr, "hello-err\n");
sleep(1);
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,24 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("This time we'll flush stdout!\n");
printf("(Hit ^C to stop the madness.)\n");
while(1)
{
fprintf(stdout, "hello-out ");
fprintf(stderr, "hello-err ");
fflush(stdout);
sleep(1);
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,19 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#define f(a, b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n", h(f(1, 2)));
printf("%s\n", g(f(1, 2)));
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,42 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#define f(a, b) a##b
#define g(a) #a
#define h(a) g(a)
#define border(c) \
for (int i = 0; i < 60; i++) { \
printf(c); \
} \
printf("\n");
int main()
{
printf("%s\n", h(f(1, 2)));
printf("%s\n", g(f(1, 2)));
printf("%s\n", g(printf("dogs rule cats drool\n")));
char * hambones = "Cats! " g(ham);
char * bonesham = "Meats! " g(bones);
char * tmp;
border("-");
printf("bonesham = %s\n", bonesham);
printf("hambones = %s\n", hambones);
border("-");
tmp = f(ham, bones);
f(ham, bones) = f(bones, ham);
f(bones, ham) = tmp;
printf("bonesham = %s\n", bonesham);
printf("hambones = %s\n", hambones);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,24 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,32 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#define MAGIC_NUMBER 10
#include<stdio.h>
int main()
{
int a = MAGIC_NUMBER;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defalut:
printf("NO CAN SPELL\n");
break;
defau1t:
printf("SO CLOSE, YET SO FAR\n");
break;
default:
printf("NONE\n");
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,16 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdlib.h>
int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,29 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdlib.h>
#include <stdio.h>
int main()
{
int* p;
size_t size_of_int = sizeof(int);
printf("sizeof(int) = %d\n", size_of_int);
p = (int*)malloc(sizeof(int));
size_t size_of_p = sizeof(p);
printf("sizeof(p) = %d\n", size_of_p);
*p = 10;
size_t size_of_ptr_p = sizeof(*p);
printf("sizeof(*p) = %d\n", size_of_ptr_p);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,40 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
static unsigned int mask[] = {
0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF
};
int get_bit_count(unsigned int x)
{
int i;
int shift; /* Number of positions to shift to right*/
for (i = 0, shift = 1; i < 5; i++, shift *= 2) {
x = (x & mask[i]) + ((x >> shift) & mask[i]);
}
return x;
}
int main()
{
int to_test[] = {0, 5, 7};
int current;
for (int i = 0; i < 3 ; i++) {
current = to_test[i];
printf("get_bit_count(%d) = %d\n", current, get_bit_count(current));
}
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,67 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
static unsigned int mask[] = {
0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF
};
int get_bit_count(unsigned int x)
{
printf("get_bit_count(%d)\n", x);
int i;
int x_and_mask;
int x_shifted;
int x_shifted_and_mask;
int shift; /* Number of positions to shift to right*/
for (i = 0, shift = 1; i < 5; i++, shift *= 2) {
printf(" START loop\n");
printf(" i = %d, x = %d, shift = %d\n", i, x, shift);
x_and_mask = x & mask[i];
printf(" x & mask[i] = %d\n", x_and_mask);
x_shifted = x >> shift;
printf(" x >> shift = %d\n", x_shifted);
x_shifted_and_mask = x_shifted & mask[i];
printf(" (x >> shift) & mask[i] = %d\n", x_shifted_and_mask);
x = x_and_mask + x_shifted_and_mask;
printf(" x = %d\n", x);
printf(" END loop\n");
}
return x;
}
int main()
{
int to_test[] = {0, 5, 7};
int current;
/*
size_t mask_len = sizeof(mask) / sizeof(unsigned int);
for (int i = 0; i < mask_len; i++) {
printf("mask[%d] = %d\n", i, mask[i]);
}
*/
for (int i = 0; i < 3 ; i++) {
current = to_test[i];
printf("get_bit_count(%d) -> %d\n", current, get_bit_count(current));
}
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,26 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int main()
{
float f = 0.0f;
int i;
for (i = 0; i < 10; i++) {
f = f + 0.1f;
}
if (f == 1.0f) {
printf("f is 1.0 \n");
} else {
printf("f is NOT 1.0\n");
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,43 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
#include <math.h>
int main()
{
float f = 0.0f;
int i;
for (i = 0; i < 10; i++) {
f = f + 0.1f;
}
if (f == 1.0) {
printf("f is 1.0 \n");
/* nope! */
}
if (f == 1.000000) {
printf("f is perhaps 1.000000?\n");
/* nope again */
}
printf("f is neither 1.0 nor 1.000000\n");
printf("(f is really %.24f)\n", f);
printf("OH NOES!\n");
printf("But...\n");
double difference = fabs(f - 1.000000);
if (difference < 0.00001) {
printf("f is close enough to 1.000000 (off by %.24f)\n", difference);
/* yes? */
}
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,20 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int main()
{
/* FIXME: the comma after "a = 1" makes the compiler angry. Either the
* ", 2" should be removed or another assignment should be added.
int a = 1, 2;
*/
int a = 1, b = 2;
printf("a: %d\n", a);
printf("b: %d\n", b);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,15 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int main()
{
int i = 43;
printf("%d\n", printf("%d", printf("%d", i)));
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,17 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int main()
{
int i = 43, nprinted;
nprinted = printf("%d\n", printf("%d", printf("%d", i)));
nprinted = printf("(the last one was %d characters)\n", nprinted);
nprinted = printf("(and that was %d characters)\n", nprinted);
return 0;
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,37 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
void duff(register char *to, register char *from, register int count)
{
register int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}
int main()
{
char * to = "dogs cats babies";
char * from = "monkey hat pants";
int i = 16;
printf("to = %s\n", to);
printf("from = %s\n", from);
/* And here comes the segfault... */
duff(to, from, i);
printf("to = %s\n", to);
printf("from = %s\n", from);
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,40 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
void duff(register char *to, register char *from, register int count)
{
register int n;
int remainder = count % 8;
if (remainder == 0) {
n = (count + 7) / 8;
do {
*to++ = *from++;
} while (--n > 0);
} else if (remainder <= 7 && remainder > 0) {
int i;
int c;
for (i = 0; (c = from[i] && c != EOF); i++) {
to[i] = c;
}
}
}
int main()
{
char * to = "dogs cats babies";
char * from = "monkey hat pants";
int i = 11;
printf("to = %s\n", to);
printf("from = %s\n", from);
/* And here comes the segfault... */
duff(to, from, i);
printf("to = %s\n", to);
printf("from = %s\n", from);
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,32 @@
/**
* :author: Dan Buch (daniel.buch@gmail.com)
*/
#include <stdio.h>
int get_bit_count(unsigned int x)
{
int count=0;
while(x)
{
count++;
x = x&(x-1);
}
return count;
}
int main()
{
int to_test[] = {0, 5, 7};
int current;
for (int i = 0; i < 3 ; i++) {
current = to_test[i];
printf("get_bit_count(%d) = %d\n", current, get_bit_count(current));
}
}
/* vim:filetype=c:fileencoding=utf-8
*/

View File

@ -0,0 +1,12 @@
# puzzles from http://www.gowrikumar.com/c/
ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
$(BINDIR)/%: %.c
$(CC) $(CFLAGS) -o $@ $<
all: $(ALL_BIN)
.PHONY: all

48
PracticingC/socky.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
const char *SOCKNAME = "/tmp/socky";
int sfd;
struct sockaddr_un addr;
int main(int argc, char *argv[])
{
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) {
fprintf(stderr, "FAILED TO ALLOCATE SOCKET\n");
goto error;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
fprintf(stderr, "FAILED TO BIND SOCKET TO ADDRESS\n");
goto error;
}
if (listen(sfd, 128) == -1) {
fprintf(stderr, "FAILED TO LISTEN ON ADDRESS\n");
goto error;
}
for (;;) {
fprintf(stdout, "ATTEMPTING TO WAIT FOR REQUEST ON %s\n", SOCKNAME);
if (accept(sfd, NULL, NULL) == -1) {
fprintf(stderr, "FAILED TO ACCEPT REQUEST ON ADDRESS\n");
goto error;
}
}
return 0;
error:
return 1;
}