<commit_before>/* 
   Copyright (c) 1991-1999 <NAME>

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without
   restriction, including without limitation the rights to use, copy,
   modify, merge, publish, distribute, sublicense, and/or sell copies
   of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.
*/
/*======================================================
 * path.c -- Handle files with environment variables
 * Copyright (c) by <NAME>; all rights reserved
 *   2.3.4 - 24 Jun 93    2.3.5 - 12 Aug 93
 *   3.0.0 - 05 May 94    3.0.2 - 01 Dec 94
 *   3.0.3 - 06 Sep 95
 *=====================================================*/

#include "sys_inc.h"
#include "standard.h"
#include "llstdlib.h"

/*===========================================
 * filepath -- Find file in sequence of paths
 *=========================================*/
STRING
filepath (STRING name,
          STRING mode,
          STRING path,
          STRING  ext)
{
	unsigned char buf1[MAXLINELEN], buf2[MAXLINELEN];
	STRING p, q;
	INT c;
	int nlen, elen;

	if (ISNULL(name)) return NULL;
	if (ISNULL(path)) return name;
	if (*name == LLCHRDIRSEPARATOR || *name == '.') return name;
#ifdef WIN32
	if ((*name == '/') || ((name[1] == ':') && isalpha(*name))) return name;
#endif
	nlen = strlen(name);
	if(ext && *ext) {
	    elen = strlen(ext);
	    if((elen > nlen)
#ifdef WIN32
		&& (stricmp(name+nlen-elen, ext) == 0)
#else
		&& (strcmp(name+nlen-elen, ext) == 0)
#endif
		) {
		/*  name has an explicit extension the same as this one */
		ext = NULL;
		elen = 0;
	    }
	}
	else { ext = NULL; elen = 0; }
	if (nlen + strlen(path) + elen >= MAXLINELEN) return NULL;
	strcpy(buf1, path);
	p = buf1;
	while ((c = *p)) {
		if (c == LLCHRPATHSEPARATOR
#ifdef WIN32
	    	    || c == '/'
#endif
			) *p = 0;
		p++;
	}
	*(++p) = 0;
	p = buf1;
	while (*p) {
		q = buf2;
		strcpy(q, p);
		q += strlen(q);
		strcpy(q, LLSTRDIRSEPARATOR);
		q++;
		strcpy(q, name);
		if(ext) {
		    strcat(buf2, ext);
		    if(access(buf2, 0) == 0) return strsave(buf2);
		    nlen = strlen(buf2);
		    buf2[nlen-elen] = '\0'; /* remove extension */
		}
		if (access(buf2, 0) == 0) return strsave(buf2);
		p += strlen(p);
		p++;
	}
	if (mode[0] == 'r') return NULL;
	p = buf1;
	q = buf2;
	strcpy(q, p);
	q += strlen(q);
	strcpy(q, LLSTRDIRSEPARATOR);
	q++;
	strcpy(q, name);
	if(ext) strcat(q, ext);
	return strsave(buf2);
}
/*===========================================
 * fopenpath -- Open file using path variable
 *=========================================*/
FILE *
fopenpath (STRING name,
           STRING mode,
           STRING path,
           STRING ext,
           STRING *pfname)
{
	STRING str;
	if(pfname) *pfname = NULL;
	if (!(str = filepath(name, mode, path, ext))) return NULL;
	if(pfname) *pfname = str;
	return fopen(str, mode);
}
/*=================================================
 * lastpathname -- Return last component of a path
 *===============================================*/
STRING
lastpathname (STRING path)
{
	static unsigned char scratch[MAXLINELEN+1];
	INT len, c;
	STRING p = scratch, q;
	if (ISNULL(path)) return NULL;
	len = strlen(path);
	strcpy(p, path);
	if (p[len-1] == LLCHRDIRSEPARATOR
#ifdef WIN32
	    || p[len-1] == '/'
#endif
		) {
		len--;
		p[len] = 0;
	}
	q = p;
	while ((c = *p++)) {
		if (c == LLCHRDIRSEPARATOR
#ifdef WIN32
	    	    || c == '/'
#endif
			) q = p;
	}
	return q;
}
<commit_msg>Isolate win32 dependencies into separate functions.<commit_after>/* 
   Copyright (c) 1991-1999 <NAME> IV

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without
   restriction, including without limitation the rights to use, copy,
   modify, merge, publish, distribute, sublicense, and/or sell copies
   of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.
*/
/*======================================================
 * path.c -- Handle files with environment variables
 * Copyright (c) by <NAME>; all rights reserved
 *   2.3.4 - 24 Jun 93    2.3.5 - 12 Aug 93
 *   3.0.0 - 05 May 94    3.0.2 - 01 Dec 94
 *   3.0.3 - 06 Sep 95
 *=====================================================*/

#include "sys_inc.h"
#include "standard.h"
#include "llstdlib.h"

/*================================================
 * IS_PATH_SEP -- Is directory separator character
 *  handle WIN32 characters
 *==============================================*/
#ifdef WIN32
#define IS_PATH_SEP(qq) ((qq) == LLCHRPATHSEPARATOR || (qq) == '/')
#else
#define IS_PATH_SEP(qq) ((qq) == LLCHRPATHSEPARATOR)
#endif
/*===============================================
 * is_absolute_path -- Begins with directory info
 *  handle WIN32 characters
 *=============================================*/
static BOOLEAN
is_absolute_path (STRING dir)
{
	if (*dir == LLCHRDIRSEPARATOR || *dir == '.') return TRUE;
#ifdef WIN32
	if ((*dir == '/') || (*dir && dir[1] == ':' && isalpha(*dir))) return TRUE;
#endif
	return FALSE;
}
/*=================================
 * path_match -- paths are the same
 *  handle WIN32 filename case insensitivity
 *===============================*/
static BOOLEAN
path_match (STRING path1, STRING path2)
{
#ifdef WIN32
	return !stricmp(path1, path2);
#else
	return !strcmp(path1, path2);
#endif
}
/*===========================================
 * filepath -- Find file in sequence of paths
 *=========================================*/
STRING
filepath (STRING name,
          STRING mode,
          STRING path,
          STRING  ext)
{
	unsigned char buf1[MAXLINELEN], buf2[MAXLINELEN];
	STRING p, q;
	INT c;
	INT nlen, elen, dirs;

	if (ISNULL(name)) return NULL;
	if (ISNULL(path)) return name;
	if (is_absolute_path(name)) return name;
	nlen = strlen(name);
	if(ext && *ext) {
		elen = strlen(ext);
		if((elen > nlen) && path_match(name+nlen-elen, ext)) {
		/*  name has an explicit extension the same as this one */
			ext = NULL;
			elen = 0;
		}
	}
	else { ext = NULL; elen = 0; }
	if (nlen + strlen(path) + elen >= MAXLINELEN) return NULL;
	strcpy(buf1, path);
	p = buf1;
	dirs = 1; /* count dirs in path */
	while ((c = *p)) {
		if (IS_PATH_SEP(c)) {
			*p = 0;
			dirs++;
		}
		p++;
	}
	*(++p) = 0;
	p = buf1;
	while (*p) {
		q = buf2;
		strcpy(q, p);
		q += strlen(q);
		strcpy(q, LLSTRDIRSEPARATOR);
		q++;
		strcpy(q, name);
		if(ext) {
		    strcat(buf2, ext);
		    if(access(buf2, 0) == 0) return strsave(buf2);
		    nlen = strlen(buf2);
		    buf2[nlen-elen] = '\0'; /* remove extension */
		}
		if (access(buf2, 0) == 0) return strsave(buf2);
		p += strlen(p);
		p++;
	}
	if (mode[0] == 'r') return NULL;
	p = buf1;
	q = buf2;
	strcpy(q, p);
	q += strlen(q);
	strcpy(q, LLSTRDIRSEPARATOR);
	q++;
	strcpy(q, name);
	if(ext) strcat(q, ext);
	return strsave(buf2);
}
/*===========================================
 * fopenpath -- Open file using path variable
 *=========================================*/
FILE *
fopenpath (STRING name,
           STRING mode,
           STRING path,
           STRING ext,
           STRING *pfname)
{
	STRING str;
	if(pfname) *pfname = NULL;
	if (!(str = filepath(name, mode, path, ext))) return NULL;
	if(pfname) *pfname = str;
	return fopen(str, mode);
}
/*=================================================
 * lastpathname -- Return last component of a path
 *===============================================*/
STRING
lastpathname (STRING path)
{
	static unsigned char scratch[MAXLINELEN+1];
	INT len, c;
	STRING p = scratch, q;
	if (ISNULL(path)) return NULL;
	len = strlen(path);
	strcpy(p, path);
	if (p[len-1] == LLCHRDIRSEPARATOR
#ifdef WIN32
	    || p[len-1] == '/'
#endif
		) {
		len--;
		p[len] = 0;
	}
	q = p;
	while ((c = *p++)) {
		if (c == LLCHRDIRSEPARATOR
#ifdef WIN32
	    	    || c == '/'
#endif
			) q = p;
	}
	return q;
}

{'fraction_non_alphanumeric': 0.2298283261802575, 'fraction_numerical': 0.01984978540772532, 'mean_word_length': 3.524757281553398, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '2834854', 'n_tokens_mistral': 3274, 'n_tokens_neox': 3021, 'n_words': 1019}