<commit_before>/*
 * Copyright (c) 1995-2003 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * 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 2 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.
 */

#include "pmapi.h"
#include "impl.h"
#include <sys/stat.h>
#include <sys/file.h>

static int
mkdir_r(char *path)
{
    struct stat	sbuf;

    if (stat(path, &sbuf) < 0) {
	if (mkdir_r(dirname(strdup(path))) < 0)
	    return -1;
	return mkdir2(path, 0755);
    }
    else if ((sbuf.st_mode & S_IFDIR) == 0) {
	fprintf(stderr, "pmpost: \"%s\" is not a directory\n", path);
	exit(1);
    }
    return 0;
}

#define LAST_UNDEFINED	-1
#define LAST_NEWFILE	-2

int
main(int argc, char **argv)
{
    int		i;
    int		fd;
    FILE	*np;
    char	*dir;
    struct stat	sbuf;
    time_t	now;
    int		lastday = LAST_UNDEFINED;
    struct tm	*tmp;
    int		sts = 0;
    char	notices[MAXPATHLEN];
#ifndef IS_MINGW
    char	*tz = getenv("TZ");
    struct flock lock;
    extern char **environ;
    static char *newenviron[] =
	{"HOME=/nowhere", "SHELL=/noshell", "PATH=/nowhere", NULL};

    /*
     * Fix for bug #827972, do not trust the environment.
     * See also below.
     */
    environ = newenviron;
    if (tz && strlen(tz) < sizeof(notices)-4) {
    	snprintf(notices, sizeof(notices), "TZ=%s", tz);
	if ((tz = strdup(notices)) != NULL)
	    putenv(tz);
    }
#endif
    umask(0022);

    if ((argc == 1) || (argc == 2 && strcmp(argv[1], "-?") == 0)) {
	fprintf(stderr, "Usage: pmpost message\n");
	exit(1);
    }

    snprintf(notices, sizeof(notices), "%s%c" "NOTICES",
		pmGetConfig("PCP_LOG_DIR"), __pmPathSeparator());

    dir = dirname(strdup(notices));
    if (mkdir_r(dir) < 0) {
	fprintf(stderr, "pmpost: cannot create directory \"%s\": %s\n",
	    dir, osstrerror());
	exit(1);
    }

    if ((fd = open(notices, O_WRONLY|O_APPEND, 0)) < 0) {
	if ((fd = open(notices, O_WRONLY|O_CREAT|O_APPEND, 0644)) < 0) {<commit_msg>pmpost - refine environment handling<commit_after>/*
 * Copyright (c) 1995-2003 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * 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 2 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.
 */

#include "pmapi.h"
#include "impl.h"
#include <sys/stat.h>
#include <sys/file.h>

static int
mkdir_r(char *path)
{
    struct stat	sbuf;

    if (stat(path, &sbuf) < 0) {
	if (mkdir_r(dirname(strdup(path))) < 0)
	    return -1;
	return mkdir2(path, 0755);
    }
    else if ((sbuf.st_mode & S_IFDIR) == 0) {
	fprintf(stderr, "pmpost: \"%s\" is not a directory\n", path);
	exit(1);
    }
    return 0;
}

#define LAST_UNDEFINED	-1
#define LAST_NEWFILE	-2

int
main(int argc, char **argv)
{
    int		i;
    int		fd;
    FILE	*np;
    char	*dir;
    struct stat	sbuf;
    time_t	now;
    int		lastday = LAST_UNDEFINED;
    struct tm	*tmp;
    int		sts = 0;
    char	notices[MAXPATHLEN];
#ifndef IS_MINGW
    char	*ep;
    struct flock lock;
    extern char **environ;
    static char *newenviron[] =
	{ "HOME=/nowhere", "SHELL=/noshell", "PATH=/nowhere", NULL };
    static char *keepname[] =
	{ "TZ", "PCP_DIR", "PCP_CONF", NULL };
    char *keepval[] =
	{ NULL, NULL,      NULL,       NULL };

    /*
     * Fix for bug #827972, do not trust the environment.
     * See also below.
     */
    for (i = 0; keepname[i] != NULL; i++)
	keepval[i] = getenv(keepname[i]);
    environ = newenviron;
    for (i = 0; keepname[i] != NULL; i++) {
	if (keepval[i] != NULL) {
	    snprintf(notices, sizeof(notices), "%s=%s", keepname[i], keepval[i]);
	    if ((ep = strdup(notices)) != NULL)
		putenv(ep);
	}
    }
#endif
    umask(0022);

    if ((argc == 1) || (argc == 2 && strcmp(argv[1], "-?") == 0)) {
	fprintf(stderr, "Usage: pmpost message\n");
	exit(1);
    }

    snprintf(notices, sizeof(notices), "%s%c" "NOTICES",
		pmGetConfig("PCP_LOG_DIR"), __pmPathSeparator());

    dir = dirname(strdup(notices));
    if (mkdir_r(dir) < 0) {
	fprintf(stderr, "pmpost: cannot create directory \"%s\": %s\n",
	    dir, osstrerror());
	exit(1);
    }

    if ((fd = open(notices, O_WRONLY|O_APPEND, 0)) < 0) {
	if ((fd = open(notices, O_WRONLY|O_CREAT|O_APPEND, 0644)) < 0) {
{'fraction_non_alphanumeric': 0.15059874162776538, 'fraction_numerical': 0.01968743657398011, 'mean_word_length': 3.165680473372781, 'pattern_counts': {'":': 2, '<': 18, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087774', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1817, 'n_words': 549}