#include #include #include #include static void help(void) { printf("usage: ./checkenv :<...>:<...>\n"); printf("[-h] Display this help menu\n"); } static int check_envs(const char *envlist) { char *p, *tok; char *env; p = strdup(envlist); if (p == NULL) { printf("fatal: out of memory\n"); return -1; } tok = strtok(p, ":"); while (tok != NULL) { if ((env = getenv(tok)) == NULL) { printf("env '%s' not set\n", tok); return -1; } tok = strtok(NULL, ":"); } return 0; } int main(int argc, char **argv) { int opt; char *envlist; int error; if (argc < 2) { printf("fatal: too few arguments\n"); help(); return -1; } while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { case 'h': help(); return -1; } } while (optind < argc) { envlist = argv[optind++]; if ((error = check_envs(envlist)) < 0) { return error; } } return 0; }