#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define HALTPATH	"/sbin"


//Because of an asprintf declaration error in stdio.h
extern int asprintf (char **__restrict __ptr,
		     __const char *__restrict __fmt, ...)
     __THROW __attribute__ ((__format__ (__printf__, 2, 3)));

int main(int argc, char *argv[]) {
	char *cmd;
	char *oldpath;
	char *newpath;
	int oldlen = 0;
	int newlen = 0;

	if (argc == 2) {
		newlen = strlen(HALTPATH);
		oldpath = getenv("PATH");
		if(oldpath) {
			oldlen = strlen(oldpath);
			newlen += oldlen;
			newlen += 1; /* : */
		}
		newlen += 1; /*  \0 */
		newpath = malloc(sizeof(char) * newlen);
		if(!newpath) {
			perror("No memory!");
			exit(-1);
		}
		if(oldpath) {
			strcpy(newpath, oldpath);
			strcpy(newpath + oldlen, ":");
			oldlen++;
		}
		strcpy(newpath + oldlen, HALTPATH);

		setenv("PATH", newpath, 1);

		asprintf(&cmd, "/usr/sbin/vzctl stop %s", argv[1]);
		setreuid(0,0);
		system(cmd);
	}
	return 0;
}
