Renaming digressions to gnu-c

since that better describes what I'm doing in there, plus removed the
silly java prog because it was silly and not GNU C.
This commit is contained in:
Dan Buch
2012-05-29 15:56:49 -04:00
parent 809c287645
commit 32aaca146b
7 changed files with 6 additions and 60 deletions

3
gnu-c/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/temperatures
/qsort-example
/getopt-example

6
gnu-c/Makefile Normal file
View File

@@ -0,0 +1,6 @@
TARGETS := $(patsubst %.c,%,$(wildcard *.c))
CFLAGS += -g -Wall -Wextra -pedantic -pedantic-errors
all: $(TARGETS)
.PHONY: all

54
gnu-c/getopt-example.c Normal file
View File

@@ -0,0 +1,54 @@
/*
* Example getopt usage mostly from getopt(3)
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
void die_usage(char *prog, char *msg) {
fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", prog);
if (msg != NULL) {
fprintf(stderr, "%s\n", msg);
}
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
char *prog;
int flags, opt;
int nsecs, tfnd;
nsecs = 0;
tfnd = 0;
flags = 0;
prog = basename(argv[0]);
while ((opt = getopt(argc, argv, "hnt:")) != -1) {
switch (opt) {
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi(optarg);
tfnd = 1;
break;
case 'h':
default: /* '?' */
die_usage(prog, NULL);
}
}
printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);
if (optind >= argc) {
die_usage(prog, "Expected argument after options");
}
printf("name argument = %s\n", argv[optind]);
printf("nsecs = %d\n", nsecs);
exit(EXIT_SUCCESS);
}

34
gnu-c/qsort-example.c Normal file
View File

@@ -0,0 +1,34 @@
/*
* C Sorting example mostly from qsort(3)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
static int cmpstringp(const void *p1, const void *p2) {
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(*(char * const *)p1, *(char * const *)p2);
}
int main(int argc, char *argv[]) {
int j;
if (argc < 2) {
fprintf(stderr, "Usage: %s <string>...\n", basename(argv[0]));
exit(EXIT_FAILURE);
}
qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);
for (j = 1; j < argc; j++) {
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}

73
gnu-c/temperatures.c Normal file
View File

@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
double c2f(double celsius) {
return ((9.0 / 5.0) * celsius) + 32.0;
}
double f2c(double fahrenheit) {
return (5.0 / 9.0) * (fahrenheit - 32.0);
}
void usage(const char *prog, const char *msg) {
fprintf(stderr, "Usage: %s <temp>[fFcC]\n", prog);
if (msg != NULL) {
fprintf(stderr, "%s\n", msg);
}
}
int main(int argc, char * argv[]) {
const char * prog = basename(argv[0]);
int ret;
char msg[255];
double temp;
char scale = '\0';
const char *in_fmt = "%lf%c";
const char *out_fmt = "%.2lf%c\n";
const char *temp_string;
if (argc < 2) {
usage(prog, NULL);
exit(EXIT_FAILURE);
}
if (((int)strlen(argv[1]) == 1) && (argv[1][0] == '-')) {
ret = fscanf(stdin, in_fmt, &temp, &scale);
} else {
temp_string = argv[1];
ret = sscanf(temp_string, in_fmt, &temp, &scale);
}
if ((ret == 0) || (ret == EOF)) {
usage(prog, "No temperature provided!");
exit(EXIT_FAILURE);
}
if (ret == 1) {
usage(prog, "No scale provided!");
exit(EXIT_FAILURE);
}
switch (scale) {
case 'c':
case 'C':
printf(out_fmt, c2f(temp), 'F');
break;
case 'f':
case 'F':
printf(out_fmt, f2c(temp), 'C');
break;
default:
sprintf(msg, "Unknown scale: '%c'", scale);
usage(prog, msg);
break;
}
exit(EXIT_SUCCESS);
}