summaryrefslogtreecommitdiff
path: root/utils.c
blob: 1b0312ded8138c4a5ff0601bbbffb0dbaf412d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int checkIfFileExists(char *path)
{
	return access(path, F_OK);
}

int detectOsType()
{
	int exists = ERROR;
	for ( int i = 0; i < ENUMSIZE && exists == ERROR; i++ )
	{
		switch (i)
		{
			case apt:
				if ( checkIfFileExists("/usr/bin/apt" ) == 0 )
				{
					exists = i;
				}
				break;

			case dnf:
				if ( checkIfFileExists("/usr/bin/dnf" ) == 0 )
				{
					exists = i;
				}
				break;

			case emerge:
				if ( checkIfFileExists("/usr/bin/emerge" ) == 0 )
				{
					exists = i;
				}
				break;

			case pacman:
				if ( checkIfFileExists("/usr/bin/pacman" ) == 0 )
				{
					exists = i;
				}
				break;

			case xbps:
				if ( checkIfFileExists("/usr/bin/xbps-install" ) == 0 )
				{
					exists = i;
				}
				break;

			case zypper:
				if ( checkIfFileExists("/usr/bin/zypper" ) == 0 )
				{
					exists = i;
				}

			default:
				puts("Unrecognized Package Manager" );
		}
	}
	return exists;
}

char* stringedArgument(int argc, char** argv)
{
	int numberOfTotalPackages = argc - 2;
	char* arg = (char *) calloc(((numberOfTotalPackages * 80) + numberOfTotalPackages), sizeof(char));

	for( int i = 2; i < argc; i++ )
	{
		strcat(arg, argv[i]);
		strcat(arg, " ");
	}
	return arg;
}

void commandProcessor(char* packages, char* initCommand)
{
	char* command = (char *) calloc(strlen(packages) + 80, sizeof(char));
	strcpy(command, initCommand);
	strcat(command, packages);
	system(command);
	free(command);
}