-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftware.c
More file actions
244 lines (205 loc) · 5.24 KB
/
software.c
File metadata and controls
244 lines (205 loc) · 5.24 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#define _GNU_SOURCE
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include "others.h"
#include <unistd.h>
#include <limits.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <pwd.h>
#define MAX 1024
typedef struct{
int npacks;
char *manager;
}Pack;
char *getOS(){
FILE *fpointer = fopen ("/etc/os-release","r");
char read [50];
char *result;
if ( fpointer != NULL ){
while (fgets(read, 50 , fpointer) != NULL && strstr(read,"PRETTY_NAME") == NULL);
}else{
printf("cannot read OS\n");
return NULL;
}
fclose(fpointer);
result = fixString(read,'"','"',1);
return result;
}
char *getBits(){
struct utsname *bits = malloc(sizeof(struct utsname));
if(uname(bits) != 0){
printf("Cannot read bits\n");
return NULL;
}
char *result = malloc(sizeof(char)*strlen(bits->machine)+1);
strcpy(result,bits->machine);
free(bits);
return result;
}
char *getHost(){
char *host = calloc(sizeof(char),HOST_NAME_MAX);
if(gethostname(host, HOST_NAME_MAX) != 0){
printf("Cannot read hostname\n");
return NULL;
}
return host;
}
char *getUser(){
struct passwd *pws;
if(( pws = getpwuid(geteuid())) == NULL){
printf("cannot read user\n");
return NULL;
}
return pws->pw_name;
}
char *getKernel(){
struct utsname *kernel = malloc(sizeof(struct utsname));
if(uname(kernel) != 0){
printf("Cannot read kernel");
return NULL;
}
char *result = malloc(sizeof(char)*strlen(kernel->release)+1);
strcpy(result,kernel->release);
free(kernel);
return result;
}
long getUptime(){
long result;
struct sysinfo *si = malloc(sizeof(struct sysinfo));
if(sysinfo(si) != 0){
printf("cannot read uptime\n");
return -1;
}
result = si->uptime;
free(si);
return result;
}
char *getPacks(){
//TO DO: read more pkg managers like slackware and gentoo//
FILE *fp;
char packs[6];
Pack *pkg = malloc(sizeof(Pack)*5);
struct stat stats;
int j = 0;
int i = 0;
char read[1024];
if(stat("/var/lib/pacman/local",&stats) == 0 && S_ISDIR(stats.st_mode) == 1){
pkg[j].manager = "pacman";
pkg[j].npacks = NumOfPackages("/var/lib/pacman/local") - 1;
j++;
}if(stat("/var/lib/snapd/snaps",&stats) == 0 && S_ISDIR(stats.st_mode) == 1){
pkg[j].manager = "snap";
pkg[j].npacks = NumOfPackages("/snap") - 1;
j++;
}if(stat("/var/lib/dpkg",&stats) == 0 && S_ISDIR(stats.st_mode) == 1){
pkg[j].manager = "dpkg";
fp = fopen("/var/lib/dpkg/status","r");
if(fp != NULL){
while(fgets(read,1024,fp) != NULL){
if(strstr(read,"Status:") != NULL ){
i++;
}
}
}
pkg[j].npacks = i;
fclose(fp);
j++;
}if(stat("/var/lib/rpm",&stats) == 0 && S_ISDIR(stats.st_mode) == 1){
pkg[j].manager = "rpm";
fp = popen("rpm -qa | wc -l","r");
pkg[j].npacks = atoi(fgets(packs,6,fp));
pclose(fp);
j++;
}if(stat("/var/lib/flatpak/app",&stats) == 0 && S_ISDIR(stats.st_mode) == 1){
pkg[j].manager = "flatpak";
pkg[j].npacks = NumOfPackages("/var/lib/flatpak/app");
j++;
}if(pkg[0].npacks == 0){
return NULL;
}
char *result = calloc(sizeof(char),1024);
char np [6];
for(int a = 0; a<j ; a++){
if(a+1 == j){
sprintf(np,"%d",pkg[a].npacks);
result = strcat(result,np);
result = strcat(result , " ");
result = strcat(result,"(");
result = strcat(result,pkg[a].manager);
result = strcat(result,")");
}else{
sprintf(np,"%d",pkg[a].npacks);
result = strcat(result,np);
result = strcat(result , " ");
result = strcat(result,"(");
result = strcat(result,pkg[a].manager);
result = strcat(result,"), ");
}
}
free(pkg);
return result;
}
char *getShell(){
char *shell;
if((shell = strrchr(getenv("SHELL"),'/')) == NULL){
printf("Cannot read shell\n");
return NULL;
}
shell[0] = ' ';
if(strcmp(shell," bash") == 0 || strcmp(shell," rbash") == 0){//get bash version
FILE *fp;
char tmp[35];
char *version;
fp = popen ("bash --version","r");
fgets(tmp,35,fp);
version = numUntilchar(tmp,'(');
shell = strcat(shell, " ");
shell = strcat(shell,version);
pclose(fp);
free(version);
}
return shell;
}
char *getDE(){
char *result = getenv("XDG_SESSION_DESKTOP");
if(result == NULL || strcmp(result,"") == 0){
result = getenv("XDG_CURRENT_DESKTOP");
if (result == NULL || strcmp(result,"") == 0){
return NULL;
}
}
if(strchr(result,':') != NULL ){
result = strchr(result,':');
result++;
}
return result;
}
char *getTerm(){
FILE *fp;
char tmp[MAX];
int x;
char *result;
fp = popen ("ps -o comm= -p 2>/dev/null \"$(($(ps -o ppid= -p 2>/dev/null \"$(($(ps -o sid= -p 2> /dev/null \"$$\")))\")))\"", "r");
fgets(tmp,MAX,fp);
if(tmp[0] == '\001'){
char *pointer = strtok(getenv("TERM"),"-");
result = malloc(sizeof(char)*strlen(pointer)+1);
strcpy(result,pointer);
}else{
x = strlen(tmp);
result = malloc(sizeof(char)*x+1);
strcpy(result,tmp);
if(result[x-2] == '-'){//weird gnome terminal bug
result[x-2] = '\0';
}else{//deleting \n //
result[x-1] = '\0';
}
}
pclose(fp);
return result;
}