From 9c47418b7cd6f04239f9ab4995e620d5631b0a35 Mon Sep 17 00:00:00 2001 From: Daniel Reinhold Date: Tue, 8 Oct 2002 18:41:03 +0000 Subject: [PATCH] initial checkin git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1458 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/bin/printenv.c | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/apps/bin/printenv.c diff --git a/src/apps/bin/printenv.c b/src/apps/bin/printenv.c new file mode 100644 index 0000000000..30ac4db5d3 --- /dev/null +++ b/src/apps/bin/printenv.c @@ -0,0 +1,77 @@ +// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ +// +// Copyright (c) 2001-2002, OpenBeOS +// +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// +// File: printenv.c +// Author: Daniel Reinhold (danielre@users.sf.net) +// Description: prints environment variables +// +// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ + + +#include +#include +#include + + +extern char **environ; + +void print_env(char *); + + +void +usage() +{ + printf("usage: printenv [VARIABLE]\n" + "If no environment VARIABLE is specified, print them all.\n"); +} + + +int +main(int argc, char *argv[]) +{ + if (argc > 2) + usage(); + + else { + char *arg = (argc == 2 ? argv[1] : NULL); + + if (arg && !strcmp(arg, "--help")) + usage(); + + print_env(arg); + } + + return 0; +} + + +void +print_env(char *arg) +{ + char **env = environ; + + if (arg) { + // print only the value of the specified variable + char *s; + int len = strlen(arg); + + while ((s = *env++) != NULL) + if (!strncmp(s, arg, len)) { + char *p = strchr(s, '='); + if (p) { + printf("%s\n", p+1); + break; + } + } + } + else { + // print all environment 'key=value' pairs (one per line) + while (*env) + printf("%s\n", *env++); + } +}