Archiving a bunch of old stuff
This commit is contained in:
3
oldstuff/gnu-c/.gitignore
vendored
Normal file
3
oldstuff/gnu-c/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/temperatures
|
||||
/qsort-example
|
||||
/getopt-example
|
6
oldstuff/gnu-c/Makefile
Normal file
6
oldstuff/gnu-c/Makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
TARGETS := $(patsubst %.c,%,$(wildcard *.c))
|
||||
CFLAGS += -g -Wall -Wextra -pedantic -pedantic-errors
|
||||
|
||||
all: $(TARGETS)
|
||||
|
||||
.PHONY: all
|
54
oldstuff/gnu-c/getopt-example.c
Normal file
54
oldstuff/gnu-c/getopt-example.c
Normal 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
oldstuff/gnu-c/qsort-example.c
Normal file
34
oldstuff/gnu-c/qsort-example.c
Normal 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);
|
||||
}
|
68
oldstuff/gnu-c/temperatures.c
Normal file
68
oldstuff/gnu-c/temperatures.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#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 die_usage(const char *prog, const char *msg) {
|
||||
fprintf(stderr, "Usage: %s <temp>[FC]\n", prog);
|
||||
if (msg != NULL) {
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
die_usage(prog, NULL);
|
||||
}
|
||||
|
||||
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)) {
|
||||
die_usage(prog, "No temperature provided!");
|
||||
}
|
||||
|
||||
if (ret == 1) {
|
||||
die_usage(prog, "No scale provided!");
|
||||
}
|
||||
|
||||
switch (scale) {
|
||||
case 'C':
|
||||
printf(out_fmt, c2f(temp), 'F');
|
||||
break;
|
||||
case 'F':
|
||||
printf(out_fmt, f2c(temp), 'C');
|
||||
break;
|
||||
default:
|
||||
sprintf(msg, "Unknown scale: '%c'", scale);
|
||||
die_usage(prog, msg);
|
||||
}
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
Reference in New Issue
Block a user