示例#1
0
/*
 *	strpush()
 *
 *	Split the string between [@begin, @end].
 */
char* strpush(const char *begin, const char *end) {
	char *ret;

	ret = (char*) malloc((end - begin + 2)*sizeof(char));
	if (!ret)
		return(NULL);

	*(ret + strplen(begin, end)) = '\0';
	memcpy(ret, begin, strplen(begin, end) * sizeof(char));

	return(ret);
}
示例#2
0
void 
showusers(int n, int mode, char *ucomp, char raw)
{
	char		status    [20];
	char		online    [20];
	char           *filename = 0;
	char		realfile  [512];
	char		bar       [20];
	struct timeval	tstop;
	double		mb_xfered = 0;
	double		speed  , pct = 0;
	double		my_filesize = 0;
	int		mask;
	int		noshow;
	char		maskchar;
	int		i         , x, m;
	unsigned	hours;
	unsigned char	minutes;
	unsigned	seconds;

	gettimeofday(&tstop, (struct timezone *)0);

	if (!strncasecmp(count_hidden, "false", 5))
		chidden = 0;

	for (x = 0; x < n; x++) {
		if (!user[x].procid)
			continue;

		maskchar = ' ';
		mask = noshow = 0;

		if (strcomp(husers, user[x].username) || strcomp(hgroups, get_g_name(user[x].groupid))) {
			if (showall)
				maskchar = '*';
			else
				noshow++;
		}
		if (noshow == 0 && strlen(mpaths)) {
			if (maskchar == ' ' && matchpath(mpaths, user[x].currentdir)) {
				if (showall)
					maskchar = '*';
				else
					mask++;
			}
		}
		if (strplen(user[x].status) > 5)
			filename = malloc(strplen(user[x].status) - 5 + 1);
		else
			filename = malloc(1);

		if ((!strncasecmp(user[x].status, "STOR ", 5) ||
		     !strncasecmp(user[x].status, "APPE ", 5)) &&
		    user[x].bytes_xfer) {

			speed = user[x].bytes_xfer / 1024. /
				((tstop.tv_sec - user[x].tstart.tv_sec) * 1. + (tstop.tv_usec - user[x].tstart.tv_usec) / 1000000.);

			if (debug) {
				printf("DEBUG SPEED INFO: username   = %s\nDEBUG SPEED INFO: time spent = %.2f seconds\nDEBUG SPEED INFO: transfered = %.0f KB\nDEBUG SPEED INFO: speed      = %.2f KB/s\n", user[x].username, ((tstop.tv_sec - user[x].tstart.tv_sec) * 1. + (tstop.tv_usec - user[x].tstart.tv_usec) / 1000000.), (double)(user[x].bytes_xfer / 1024.), speed);
				}
			if ((!noshow && !mask && !(maskchar == '*')) || chidden) {
				total_up_speed += speed;
				uploads++;
			}
			if (!mask) {
				pct = -1;
				m = strplen(user[x].status) - 5;
				if (m < 15 || raw)
					sprintf(filename, "%.*s", m, user[x].status + 5);
				else
					sprintf(filename, "%.15s", user[x].status + m - 10);

				strcpy(bar, "?->");
				if (!raw)
					if (speed > threshold) {
						speed = (speed / 1024.0);
						sprintf(status, "Up: %7.2fMB/s", speed);
					} else {
						sprintf(status, "Up: %7.0fKB/s", speed);
					}
				else if (raw == 1)
					sprintf(status, "\"UP\" \"%.0f\"", speed);
				else
					sprintf(status, "upld|%.0f", speed);

				mb_xfered = user[x].bytes_xfer * 1.0 / 1024 / 1024;
			}
		} else if ((!strncasecmp(user[x].status, "RETR ", 5) && user[x].bytes_xfer)) {
			mb_xfered = 0;

			sprintf(realfile, "%s", user[x].currentdir);

			/*
			 * Dirty way to get around the fact that the buffered
			 * reading will change user[x].currentdir to not
			 * include filename once it's done reading the entire
			 * file "to memory". This means user[x].currentdir in
			 * fact will be _currentdir_ and this cannot tell us
			 * a true filesize since it's calculated from
			 * filesize(/site/incoming/path) - w/o filename :(
			 */
			my_filesize = filesize(realfile);
			if (my_filesize < user[x].bytes_xfer) {
				my_filesize = user[x].bytes_xfer;
			}
			pct = (user[x].bytes_xfer * 1. / my_filesize) * 100;
			i = 15 * user[x].bytes_xfer * 1. / my_filesize;
			i = (i > 15 ? 15 : i);

			bar[i] = 0;

			for (m = 0; m < i; m++)
				bar[m] = 'x';
			speed = user[x].bytes_xfer / 1024. /
				((tstop.tv_sec - user[x].tstart.tv_sec) * 1. +
				(tstop.tv_usec - user[x].tstart.tv_usec) / 1000000.);

			if (debug) {
				printf("DEBUG SPEED INFO: username   = %s\nDEBUG SPEED INFO: time spent = %.2f seconds\nDEBUG SPEED INFO: transfered = %.0f KB\nDEBUG SPEED INFO: speed      = %.2f KB/s\n", user[x].username, ((tstop.tv_sec - user[x].tstart.tv_sec) * 1. + (tstop.tv_usec - user[x].tstart.tv_usec) / 1000000.), (double)(user[x].bytes_xfer / 1024.), speed);
				}

			if ((!noshow && !mask && !(maskchar == '*')) || chidden) {
				total_dn_speed += speed;
				downloads++;
			}
			m = strplen(user[x].status) - 5;
			if (!mask) {
				if (m < 15 || raw)
					sprintf(filename, "%.*s", m, user[x].status + 5);
				else
					sprintf(filename, "%.15s", user[x].status + m - 10);

				if (!raw)
					if (speed > threshold) {
						speed = (speed / 1024.0);
						sprintf(status, "Dn: %7.2fMB/s", speed);
					} else {
						sprintf(status, "Dn: %7.0fKB/s", speed);
					}
				else if (raw == 1)
					sprintf(status, "\"DN\" \"%.1f\"", speed);
				else
					sprintf(status, "dnld|%.1f", speed);
			}
		} else {
			pct = *bar = *filename = hours = minutes = mb_xfered = 0;
			seconds = tstop.tv_sec - user[x].tstart.tv_sec;
			while (seconds >= 3600) {
				hours++;
				seconds -= 3600;
			}
			while (seconds >= 60) {
				minutes++;
				seconds -= 60;
			}
			if ((!noshow && !mask && !(maskchar == '*')) || chidden) {
				if ((int)seconds > idle_barrier)
					idlers++;
				else
					browsers++;
			}
			if (!raw)
				sprintf(status, "Idle: %02d:%02d:%02d", hours, minutes, seconds);
			else if (raw == 1)
				sprintf(status, "\"ID\" \"%d\"", (hours * 60 * 60) + (minutes * 60) + seconds);
			else
				sprintf(status, "idle|%02d:%02d:%02d", hours, minutes, seconds);
		}

		hours = minutes = 0;
		seconds = tstop.tv_sec - user[x].login_time;
		while (seconds >= 3600) {
			hours++;
			seconds -= 3600;
		}
		while (seconds >= 60) {
			minutes++;
			seconds -= 60;
		}
		sprintf(online, "%02d:%02d:%02d", hours, minutes, seconds);

		if (mode == 0 && raw != 3 ) {
			if (!raw && (showall || (!noshow && !mask && !(maskchar == '*')))) {
				if (mb_xfered)
					printf("|%1c%-16.16s/%-10.10s | %-15s | XFER: %13.1fMB |\n", maskchar, user[x].username, get_g_name(user[x].groupid), status, mb_xfered);
				else
					printf("|%1c%-16.16s/%-10.10s | %-15s | %3.0f%%: %-15.15s |\n", maskchar, user[x].username, get_g_name(user[x].groupid), status, pct, bar);

				printf("| %-27.27s | since %8.8s  | file: %-15.15s |\n", user[x].tagline, online, filename);
				printf("+-----------------------------------------------------------------------+\n");
			} else if (raw == 1 && (showall || (!noshow && !mask && !(maskchar == '*')))) {
				/*
				 * Maskeduser / Username / GroupName / Status
				 * / TagLine / Online / Filename / Part
				 * up/down-loaded / Current dir / PID
				 */
				printf("\"USER\" \"%1c\" \"%s\" \"%s\" %s \"%s\" \"%s\" \"%s\" \"%.1f%s\" \"%s\" \"%d\"\n", maskchar, user[x].username, get_g_name(user[x].groupid), status, user[x].tagline, online, filename, (pct >= 0 ? pct : mb_xfered), (pct >= 0 ? "%" : "MB"), user[x].currentdir, user[x].procid);
			} else if (showall || (!noshow && !mask && !(maskchar == '*'))) {
				printf("%s|%s|%s|%s|%s\n", user[x].username, get_g_name(user[x].groupid), user[x].tagline, status, filename);
			}
			if ((!noshow && !mask && !(maskchar == '*')) || chidden) {
				onlineusers++;
			}
		} else if (raw == 3) {
			if ((!noshow && !mask && !(maskchar == '*')) || chidden) {
				onlineusers++;
			}
		} else if (user[x].username && !strcmp(ucomp, user[x].username)) {
#ifdef _WITH_ALTWHO
			if (!raw && (showall || (!noshow && !mask && !(maskchar == '*')))) {
				if (mb_xfered)
					printf("%s : %1c%s/%s has xfered %.1fMB of %s and has been online for %8.8s.\n", status, maskchar, user[x].username, get_g_name(user[x].groupid), mb_xfered, filename, online);
				else if (strcmp(filename, ""))
					printf("%s : %1c%s/%s has xfered %.0f%% of %s and has been online for %8.8s.\n", status, maskchar, user[x].username, get_g_name(user[x].groupid), pct, filename, online);
				else
					printf("%s : %1c%s/%s has been online for %8.8s.\n", status, maskchar, user[x].username, get_g_name(user[x].groupid), online);
			} else if (raw == 1 && (showall || (!noshow && !mask && !(maskchar == '*')))) {
				printf("\"USER\" \"%1c\" \"%s\" \"%s\" %s \"%s\" \"%s\" \"%s\" \"%.1f%s\" \"%s\" \"%d\"\n", maskchar, user[x].username, get_g_name(user[x].groupid), status, user[x].tagline, online, filename, (pct >= 0 ? pct : mb_xfered), (pct >= 0 ? "%" : "MB"), user[x].currentdir, user[x].procid);
			} else if (showall || (!noshow && !mask && !(maskchar == '*'))) {
				printf("%s|%s|%s|%s|%s\n", user[x].username, get_g_name(user[x].groupid), user[x].tagline, status, filename);
			}
#else
			if (!onlineusers) {
				if (!raw && (showall || (!noshow && !mask && !(maskchar == '*'))))
					printf("\002%s\002 - %s", user[x].username, status);
				else if (raw == 1 && (showall || (!noshow && !mask && !(maskchar == '*'))))
					printf("\"USER\" \"%s\" %s", user[x].username, status);
				else if (showall || (!noshow && !mask && !(maskchar == '*')))
					printf("\002%s\002 - %s", user[x].username, status);
			} else {
				if (!raw && (showall || (!noshow && !mask && !(maskchar == '*'))))
					printf(" - %s", status);
				else if (raw == 1 && (showall || (!noshow && !mask && !(maskchar == '*'))))
					printf("\"USER\" \"\" %s", status);
				else if (showall || (!noshow && !mask && !(maskchar == '*')))
					printf(" - %s", status);
			}
#endif
			if (!noshow && !mask && !(maskchar == '*'))
				onlineusers++;
			else if (chidden)
				onlineusers++;

		}
		free(filename);
	}
}
示例#3
0
/*
 *	solve_molecule()
 *
 *	Analyze the composition of a molecular formula without hydrate
 *
 *	@begin, @end: pointer 'begin' points to the head of a string, pointer 'end' points to the end of the string.
 *	@eptr: storage pool of elements
 *	@ecount: counter of the storage pool 'eptr'
 *	@suffix: digits after or before the molecular formula
 */
bool solve_molecule(char *begin, char *end, bmem *eptr, int *ecount, int suffix) {
	char *ptr, *b_ptr, *temp, *start;
	int stack, prefix;
	element *eap_ptr;

	if (strplen(begin, end) <= 0)
		return(true);

	/*  Get the prefix number  */
	prefix = strptoi(begin, end, &temp);
	if (!temp)
		return(false);

	if (prefix <= 0)
		prefix = 1;

	/*  Fix the string  */
	if (temp != begin) {
		start = temp;
		suffix *= prefix;
	} else {
		start = begin;
	}

	if (strplen(start, end) <= 0)
		return(true);

	if (strpcomp(start, end, SPECIAL_SIGNATURE_SOLID) || strpcomp(start, end, SPECIAL_SIGNATURE_LIQUIT) || strpcomp(start, end, SPECIAL_SIGNATURE_GAS))
		return(true);

	/*  Recognize whether the string is a electronic expression  */
	if (strpcomp(start, end, ELECTRIC_E_PLUS) || strpcomp(start, end, ELECTRIC_E_MINUS)) {
		for (eap_ptr = (element*)eptr->ptr; eap_ptr < (element*)eptr->ptr + *ecount; eap_ptr++)
			if (strpcomp(eap_ptr->begin, eap_ptr->end, ELECTRIC_E_NAME)) {
				/*  Modify the value  */
				eap_ptr->count += strpcomp(start, end, ELECTRIC_E_PLUS) ? suffix : -suffix;
				return(true);
			}

		/*  Add the electronic descriptor in the element stack  */
		if (!reallocate_block_memory(eptr, (++(*ecount)) * sizeof(element)))
			return(false);

		eap_ptr = (element*)eptr->ptr + (*ecount) - 1;

		eap_ptr->begin = start;
		eap_ptr->end = start;
		eap_ptr->count = strpcomp(start, end, ELECTRIC_E_PLUS) ? suffix : -suffix;

		return(true);
	}

	/*  Solve the bracket  */
	for (stack = 0, ptr = start; ptr <= end; ptr++) {
		if (!strchr(BRACKET_LEFT, *ptr))
			continue;

		for (b_ptr = ptr; b_ptr <= end; b_ptr++) {
			if (strchr(BRACKET_LEFT, *b_ptr))
				stack++;

			if (strchr(BRACKET_RIGHT, *b_ptr))
				stack--;

			if (!stack) {
				/*  Solve the molecular on the left  */
				if (!solve_molecule_hydrate(start, ptr - 1, eptr, ecount, suffix))
					return(false);

				/*  Get the suffix after the bracket  */
				prefix = strptoi(b_ptr + 1, end, &temp);
				if (!temp)
					return(false);
				if (prefix <= 0)
					prefix = 1;

				/*  Solve the molecular in the bracket  */
				if (!solve_molecule_hydrate(ptr + 1, b_ptr - 1, eptr, ecount, suffix * prefix))
					return(false);

				/*  Solve the molecular on the right  */
				if (!solve_molecule_hydrate(temp, end, eptr, ecount, suffix))
					return(false);

				return (true);
			}
		}
	}

	/*  Look for the next upcase character  */
	for (ptr = start + 1; strplen(ptr, end) > 0 && (!isupper((int)(*ptr))); ptr++)
		;

	if (strplen(ptr, end) <= 0) {
		/*  Only one element, divide it into a symbol and a number  */
		for (ptr = start; strplen(ptr, end) >= 0; ptr++) {
			if (strpisnum(ptr, end) == true) {
				/*  Get the suffix number  */
				prefix = strptoi(ptr, end, NULL);
				if (prefix <= 0)
					prefix = 1;

				for (eap_ptr = (element*)eptr->ptr; eap_ptr < (element*)eptr->ptr + *ecount; eap_ptr++)
					if (strpqcomp(eap_ptr->begin, eap_ptr->end, start, ptr - 1)) {
						/*  Modify the value  */
						eap_ptr->count += suffix * prefix;
						return(true);
					}

				/*  Add the electronic descriptor in the element stack  */
				if (!reallocate_block_memory(eptr, (++(*ecount)) * sizeof(element)))
					return(false);
				eap_ptr = (element*)eptr->ptr + *ecount - 1;
				eap_ptr->begin = start;
				eap_ptr->end = ptr - 1;
				eap_ptr->count = prefix * suffix;

				break;
			}
		}
	} else {
		if (!solve_molecule_hydrate(start, ptr - 1, eptr, ecount, suffix))
			return(false);
		if (!solve_molecule_hydrate(ptr, end, eptr, ecount, suffix))
			return(false);
	}

	return(true);
}