<commit_before>/****************************************************************************************************************/
/*                                                                                                              */
/* NG-CHOWN v0.1 (c) 2004 Team pzs-ng                                                                           */
/*                                                                                                              */
/* This util will cange the uid or gid of the dir/file passed as argument, or                                   */
/* dir and all files inside, or just the files inside the dir. Uhm.. anyway,                                    */
/* here's how you use it:                                                                                       */
/*                                                                                                              */
/* ng-chown <uid> <gid> <uid_flag> <gid_flag> <files_flag> <dir/file_flag> <u_name> <g_name> <name of dir/file> */
/*                                                                                                              */
/* <uid> == user id (number between 1 and 65535)                                                                */
/* <gid> == group id (number between 1 and 65535)                                                               */
/* <uid_flag> == if set to 1, the uid of the file/dir will be modified                                          */
/* <gid_flag> == if set to 1, the gid of the file/dir will be modified                                          */
/* <files_flag> == if set to 1, the files in the dir <name of dir/file> will be modified                        */
/* <dir/file_flag> == if set to 1, the dir/file <name of dir/file> will be modified                             */
/* <u_name> == name of user - only used if uid=0 *and* gid=0 - use '-' to disable                               */
/* <g_name> == name of group - only used if uid=0 *and* gid=0 - use '-' to disable                              */
/* <name of dir/file> == name of the file dir to which the changes is to be applied                             */
/*                                                                                                              */
/****************************************************************************************************************/

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>

#ifndef PATH_MAX
 #define _LIMITS_H_
 #ifdef _SunOS_
  #include <syslimits.h>
 #else
  #include <sys/syslimits.h>
 #endif
#endif

#include "objects.h"
#include "../conf/zsconfig.h"
#include "zsconfig.defaults.h"

struct dirent **dirlist;
unsigned int	direntries, n = 0;


#if defined(__linux__)
int 
selector3(const struct dirent *d)
#elif defined(__NetBSD__)
	int		selector3  (const struct dirent *d)
#else
int 
selector3(struct dirent *d)
#endif
{
	struct stat	st;
	if ((stat(d->d_name, &st) < 0) || S_ISDIR(st.st_mode))
		return 0;
	return 1;
}

short int 
matchpath2(char *instr, char *path)
{
	int		pos = 0;

	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (strncmp(instr - pos, path, pos - 1) == 0) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

int 
myscandir(char *my_path, char *my_name)
{
	if ((chdir(my_path) != -1) && ((matchpath2(group_dirs, my_path)) || (matchpath2(zip_dirs, my_path)) || (matchpath2(sfv_dirs, my_path)))) {
		if (direntries > 0) {
			while (direntries--) {
				free(dirlist[direntries]);
			}
			free(dirlist);
		}
		direntries = scandir(".", &dirlist, selector3, alphasort);
		return 0;
	} else {
		printf("%s - Error: Unable to chdir to %s\n", my_name, my_path);
		return 1;
	}
}

int 
myscan(int my_result, int new_user, int new_group, int user_flag, int group_flag, int files_flag, int dir_flag, char *my_path, char *my_name)
{
	int		my_total = 0;

	/* change the uid or gid depending on the flag */
	if (user_flag == 0) {
		new_user = -1;
	}
	if (group_flag == 0) {
		new_group = -1;
	}
	if (files_flag == 1) {
		if (!myscandir(my_path, my_name)) {
			n = direntries;
			while (n--) {
				my_total += close(open(dirlist[n]->d_name, O_NONBLOCK));
				chown(dirlist[n]->d_name, new_user, new_group);
			}
			if ((my_result == 0) && (dir_flag == 1)) {
				chown(my_path, new_user, new_group);
			}
			return my_total;
		} else {
			return 1;
		}
	} else {
		if (dir_flag == 1) {
			my_result = close(open(my_path, O_NONBLOCK));
			if (my_result == 0) {
				chown(my_path, new_user, new_group);
			} else {
				printf("%s - Error: Unable to read file %s\n", my_name, my_path);
			}
		}
		return my_result;
	}
}

uid_t
get_gluid(char *passwdfile, char *user_name)
{
	char	       *f_buf = 0;<commit_msg>minor bugfix in ng-chown.<commit_after>/****************************************************************************************************************/
/*                                                                                                              */
/* NG-CHOWN v0.1 (c) 2004 Team pzs-ng                                                                           */
/*                                                                                                              */
/* This util will cange the uid or gid of the dir/file passed as argument, or                                   */
/* dir and all files inside, or just the files inside the dir. Uhm.. anyway,                                    */
/* here's how you use it:                                                                                       */
/*                                                                                                              */
/* ng-chown <uid> <gid> <uid_flag> <gid_flag> <files_flag> <dir/file_flag> <u_name> <g_name> <name of dir/file> */
/*                                                                                                              */
/* <uid> == user id (number between 1 and 65535)                                                                */
/* <gid> == group id (number between 1 and 65535)                                                               */
/* <uid_flag> == if set to 1, the uid of the file/dir will be modified                                          */
/* <gid_flag> == if set to 1, the gid of the file/dir will be modified                                          */
/* <files_flag> == if set to 1, the files in the dir <name of dir/file> will be modified                        */
/* <dir/file_flag> == if set to 1, the dir/file <name of dir/file> will be modified                             */
/* <u_name> == name of user - only used if uid=0 *and* gid=0 - use '-' to disable                               */
/* <g_name> == name of group - only used if uid=0 *and* gid=0 - use '-' to disable                              */
/* <name of dir/file> == name of the file dir to which the changes is to be applied                             */
/*                                                                                                              */
/****************************************************************************************************************/

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>

#ifndef PATH_MAX
 #define _LIMITS_H_
 #ifdef _SunOS_
  #include <syslimits.h>
 #else
  #include <sys/syslimits.h>
 #endif
#endif

#include "objects.h"
#include "../conf/zsconfig.h"
#include "zsconfig.defaults.h"

struct dirent **dirlist;
unsigned int	direntries, n = 0;


#if defined(__linux__)
int 
selector3(const struct dirent *d)
#elif defined(__NetBSD__)
	int		selector3  (const struct dirent *d)
#else
int 
selector3(struct dirent *d)
#endif
{
	struct stat	st;
	if ((stat(d->d_name, &st) < 0) || S_ISDIR(st.st_mode))
		return 0;
	return 1;
}

short int 
matchpath2(char *instr, char *path)
{
	int		pos = 0;

	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (strncmp(instr - pos, path, pos - 1) == 0) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

int 
myscandir(char *my_path, char *my_name)
{
	if ((chdir(my_path) != -1) && ((matchpath2(group_dirs, my_path)) || (matchpath2(zip_dirs, my_path)) || (matchpath2(sfv_dirs, my_path)))) {
		if (direntries > 0) {
			while (direntries--) {
				free(dirlist[direntries]);
			}
			free(dirlist);
		}
		direntries = scandir(".", &dirlist, selector3, alphasort);
		return 0;
	} else {
		printf("%s - Error: Unable to chdir to %s\n", my_name, my_path);
		return 1;
	}
}

int 
myscan(int my_result, int new_user, int new_group, int user_flag, int group_flag, int files_flag, int dir_flag, char *my_path, char *my_name)
{
	int		my_total = 0;

	/* change the uid or gid depending on the flag */
	if (user_flag == 0) {
		new_user = -1;
	}
	if (group_flag == 0) {
		new_group = -1;
	}
	if (files_flag == 1) {
		if (!myscandir(my_path, my_name)) {
			n = direntries;
			while (n--) {
				my_total += close(open(dirlist[n]->d_name, O_NONBLOCK));
				if (new_user != 0 && new_group != 0) {
					chown(dirlist[n]->d_name, new_user, new_group);
				}
			}
			if ((my_result == 0) && (dir_flag == 1)) {
				if (new_user != 0 && new_group != 0) {
					chown(my_path, new_user, new_group);
				}
			}
			return my_total;
		} else {
			return 1;
		}
	} else {
		if (dir_flag == 1) {
			my_result = close(open(my_path, O_NONBLOCK));
			if (my_result == 0) {
				if (new_user != 0 && new_group != 0) {
					chown(my_path, new_user, new_group);
				}
			} else {
				printf("%s - Error: Unable to read file %s\n", my_name, my_path);
			}
		}
		return my_result;
	}
}

uid_t
get_gluid(char *passwdfile, char *user_name)
{
	char	       *f_buf = 0;
{'fraction_non_alphanumeric': 0.16659975913287836, 'fraction_numerical': 0.012846246487354477, 'mean_word_length': 1.4993729621269125, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 80, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087918', 'n_tokens_mistral': 3288, 'n_tokens_neox': 2980, 'n_words': 855}