/*+JMJ * posopt.c - parse Posix command line options * Copyright 2011 David Meyer <papa@freeshell.org> */ #include <stdio.h> #include <string.h> #include "krcompat.h" /* Options m, n, o have arguments. */ int main(argc, argv) int argc; char *argv[]; { int endopt = 0; int a, n; char *marg, *narg, *oarg; for (a = 1; a < argc; a++) { if (!endopt && argv[a][0] == '-') { if (!strcmp(argv[a], "--")) endopt = 1; else if (!strcmp(argv[a], "-m")) { marg = malloc(strlen(argv[++a]) + 1); strcpy(marg, argv[a]); printf("opt: m = %s\n", marg); } else if (!strncmp(argv[a], "-m", 2)) { marg = malloc(strlen(argv[a]) - 1); strcpy(marg, argv[a]+2); printf("opt: m = %s\n", marg); } else if (!strcmp(argv[a], "-n")) { narg = malloc(strlen(argv[++a]) + 1); strcpy(narg, argv[a]); printf("opt: n = %s\n", narg); } else if (!strncmp(argv[a], "-n", 2)) { narg = malloc(strlen(argv[a]) - 1); strcpy(narg, argv[a]+2); printf("opt: n = %s\n", narg); } else if (!strcmp(argv[a], "-o")) { oarg = malloc(strlen(argv[++a]) + 1); strcpy(oarg, argv[a]); printf("opt: o = %s\n", oarg); } else if (!strncmp(argv[a], "-o", 2)) { oarg = malloc(strlen(argv[a]) - 1); strcpy(oarg, argv[a]+2); printf("opt: o = %s\n", oarg); } else { for (n = 1; n < strlen(argv[a]); n++) printf("opt: %c\n", argv[a][n]); } } else { printf("arg: %s\n", argv[a]); } } return 0; }