Files
haiku-beta6/src/bin/printenv.c
T

70 lines
1.4 KiB
C
Raw Normal View History

2002-10-08 18:41:03 +00:00
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
2002-10-10 19:05:58 +00:00
// Copyright (c) 2001-2003, OpenBeOS
2002-10-08 18:41:03 +00:00
//
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
//
// File: printenv.c
// Author: Daniel Reinhold ([email protected])
// Description: prints environment variables
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include <OS.h>
2002-10-08 18:41:03 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern char **environ;
int print_env(char *);
2002-10-08 18:41:03 +00:00
int
main(int argc, char *argv[])
{
char *arg = (argc == 2 ? argv[1] : NULL);
2002-10-08 18:41:03 +00:00
if ((argc > 2) || (arg && !strcmp(arg, "--help"))) {
2002-10-10 19:05:58 +00:00
printf("Usage: printenv [VARIABLE]\n"
"If no environment VARIABLE is specified, print them all.\n");
return 1;
2002-10-08 18:41:03 +00:00
}
return print_env(arg);
2002-10-08 18:41:03 +00:00
}
int
2002-10-08 18:41:03 +00:00
print_env(char *arg)
{
char **env = environ;
if (arg == NULL) {
// print all environment 'key=value' pairs (one per line)
2002-10-10 19:05:58 +00:00
while (*env)
printf("%s\n", *env++);
return 0;
2002-10-10 19:05:58 +00:00
} else {
2002-10-08 18:41:03 +00:00
// print only the value of the specified variable
char *s;
int len = strlen(arg);
bool found = false;
2002-10-08 18:41:03 +00:00
2002-10-10 19:05:58 +00:00
while ((s = *env++) != NULL)
if (!strncmp(s, arg, len)) {
char *p = strchr(s, '=');
if (p) {
2002-10-08 18:41:03 +00:00
printf("%s\n", p+1);
found = true;
2002-10-08 18:41:03 +00:00
}
}
2002-10-10 19:05:58 +00:00
return (found ? 0 : 1);
2002-10-08 18:41:03 +00:00
}
}