当前位置: 技术问答>linux和unix
gnu的程序的代码貌似好难读的样子,相比之下freebsd的程序的代码则更好读一些。。大家有什么感觉呢?
来源: 互联网 发布时间:2016-09-28
本文导语: gnu的程序的代码貌似好难读的样子,相比之下freebsd的程序代码则更好读一些。。大家有什么感觉呢? 但是好像freebsd的代码都是挺老的。。 /* * Copyright (c) 1988, 1993, 1994 * The Regents of the University of California....
gnu的程序的代码貌似好难读的样子,相比之下freebsd的程序代码则更好读一些。。大家有什么感觉呢?
但是好像freebsd的代码都是挺老的。。
但是好像freebsd的代码都是挺老的。。
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1988, 1993, 1994n
The Regents of the University of California. All rights reserved.n";
#endif /* not lint */
#if 0
#ifndef lint
static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94";
#endif /* not lint */
#endif
#include
__FBSDID("$FreeBSD: src/usr.bin/env/env.c,v 1.20.2.1.4.1 2010/06/14 02:09:06 kensmith Exp $");
#include
#include
#include
#include
#include
#include
#include "envopts.h"
extern char **environ;
int env_verbosity;
static void usage(void);
int
main(int argc, char **argv)
{
char *altpath, **ep, *p, **parg;
char *cleanenv[1];
int ch, want_clear;
int rtrn;
altpath = NULL;
want_clear = 0;
while ((ch = getopt(argc, argv, "-iP:S:u:v")) != -1)
switch(ch) {
case '-':
case 'i':
want_clear = 1;
break;
case 'P':
altpath = strdup(optarg);
break;
case 'S':
/*
* The -S option, for "split string on spaces, with
* support for some simple substitutions"...
*/
split_spaces(optarg, &optind, &argc, &argv);
break;
case 'u':
if (env_verbosity)
fprintf(stderr, "#env unset:t%sn", optarg);
rtrn = unsetenv(optarg);
if (rtrn == -1)
err(EXIT_FAILURE, "unsetenv %s", optarg);
break;
case 'v':
env_verbosity++;
if (env_verbosity > 1)
fprintf(stderr, "#env verbosity now at %dn",
env_verbosity);
break;
case '?':
default:
usage();
}
if (want_clear) {
environ = cleanenv;
cleanenv[0] = NULL;
if (env_verbosity)
fprintf(stderr, "#env clearing environn");
}
for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
if (env_verbosity)
fprintf(stderr, "#env setenv:t%sn", *argv);
*p = '';
rtrn = setenv(*argv, p + 1, 1);
*p = '=';
if (rtrn == -1)
err(EXIT_FAILURE, "setenv %s", *argv);
}
if (*argv) {
if (altpath)
search_paths(altpath, argv);
if (env_verbosity) {
fprintf(stderr, "#env executing:t%sn", *argv);
for (parg = argv, argc = 0; *parg; parg++, argc++)
fprintf(stderr, "#env arg[%d]=t'%s'n",
argc, *parg);
if (env_verbosity > 1)
sleep(1);
}
execvp(*argv, argv);
err(errno == ENOENT ? 127 : 126, "%s", *argv);
}
for (ep = environ; *ep; ep++)
(void)printf("%sn", *ep);
exit(0);
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: env [-iv] [-P utilpath] [-S string] [-u name]n"
" [name=value ...] [utility [argument ...]]n");
exit(1);
}
/* env - run a program in a modified environment
Copyright (C) 1986, 1991-2005, 2007-2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see . */
/* Richard Mlynarik and David MacKenzie */
#include
#include
#include
#include
#include "system.h"
#include "error.h"
#include "quote.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "env"
#define AUTHORS
proper_name ("Richard Mlynarik"),
proper_name ("David MacKenzie")
static struct option const longopts[] =
{
{"ignore-environment", no_argument, NULL, 'i'},
{"null", no_argument, NULL, '0'},
{"unset", required_argument, NULL, 'u'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.n"),
program_name);
else
{
printf (_("
Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]n"),
program_name);
fputs (_("
Set each NAME to VALUE in the environment and run COMMAND.n
n
-i, --ignore-environment start with an empty environmentn
-0, --null end each output line with 0 byte rather than newlinen
-u, --unset=NAME remove variable from the environmentn
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("
n
A mere - implies -i. If no COMMAND, print the resulting environment.n
"), stdout);
emit_ancillary_info ();
}
exit (status);
}
int
main (int argc, char **argv)
{
int optc;
bool ignore_environment = false;
bool opt_nul_terminate_output = false;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
initialize_exit_failure (EXIT_CANCELED);
atexit (close_stdout);
while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
{
switch (optc)
{
case 'i':
ignore_environment = true;
break;
case 'u':
break;
case '0':
opt_nul_terminate_output = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_CANCELED);
}
}
if (optind