-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompinfo.c
More file actions
171 lines (153 loc) · 4.32 KB
/
compinfo.c
File metadata and controls
171 lines (153 loc) · 4.32 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
//
// Copyright (C) 2001,2002,2003,2004 Jorge Daza Garcia-Blanes
// Copyright (C) 2010 Andreas Schroeder
//
// This file is part of DrQueue
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "libdrqueue.h"
void usage (void);
void print_computers (struct computer *computer, int ncomputers);
void print_computer (struct computer *computer);
void print_computer_details (struct computer *computer);
enum operation {
OP_NONE,
OP_NUMBER,
OP_LIST,
OP_DETAILS
};
int main (int argc,char *argv[]) {
int opt;
int icomp = -1;
struct computer *computer;
int ncomputers;
enum operation op = OP_NONE;
int nRet = 0;
if(network_initialize() != 0) {
fprintf (stderr,"Could not initialize the network: %s\n", drerrno_str());
return 1;
}
while ((opt = getopt (argc,argv,"lndc:vh")) != -1) {
switch (opt) {
case 'd':
op = OP_DETAILS;
break;
case 'c':
icomp = atoi (optarg);
break;
case 'v':
show_version (argv);
goto cleanup;
case 'l':
op = OP_LIST;
break;
case 'n':
op = OP_NUMBER;
break;
case '?':
case 'h':
usage();
nRet = 1;
goto cleanup;
}
}
if ((op == OP_NONE)) {
usage ();
nRet = 1;
goto cleanup;
}
set_default_env();
if (!common_environment_check()) {
fprintf (stderr,"Error checking the environment: %s\n",drerrno_str());
nRet = 1;
goto cleanup;
}
switch (op) {
case OP_NONE:
printf ("OP_NONE\n");
break;
case OP_LIST:
if ((ncomputers = request_computer_list (&computer,CLIENT)) == -1) {
fprintf (stderr,"ERROR: While trying to request the computer list: %s\n",drerrno_str());
nRet = 1;
goto cleanup;
}
print_computers (computer,ncomputers);
break;
case OP_NUMBER:
if ((ncomputers = request_computer_list (&computer,CLIENT)) == -1) {
fprintf (stderr,"ERROR: While trying to request the computer list: %s\n",drerrno_str());
nRet = 1;
goto cleanup;
}
printf ("%i\n",ncomputers);
break;
case OP_DETAILS:
if (icomp != -1) {
computer = (struct computer *) malloc (sizeof (struct computer));
computer_init(computer);
if (!computer) {
fprintf (stderr,"ERROR: Not enough memory\n");
nRet = 1;
goto cleanup;
}
if (!request_comp_xfer(icomp,computer,CLIENT)) {
fprintf (stderr,"ERROR: While trying to request the computer transfer: %s\n",drerrno_str());
nRet = 1;
goto cleanup;
}
print_computer_details (computer);
} else {
fprintf (stderr,"You need to specify the computer id using -c <computer_id>\n");
nRet = 1;
goto cleanup;
}
}
cleanup:
network_shutdown();
return nRet;
}
void print_computers (struct computer *computer, int ncomputers) {
int i;
for (i=0;i<ncomputers;i++) {
print_computer (&computer[i]);
}
}
void print_computer (struct computer *computer) {
printf ("ID: %i Name: %s\n",computer->hwinfo.id,computer->hwinfo.name);
// tidy up shared memory piece of computer
computer_free(computer);
}
void print_computer_details (struct computer *computer) {
printf ("ID: %i Name: %s\n",computer->hwinfo.id,computer->hwinfo.name);
// tidy up shared memory piece of computer
computer_free(computer);
}
void usage (void) {
fprintf (stderr,"Usage: compinfo [-vh] -l|-n\n"
"Valid options:\n"
"\t-l list computers\n"
"\t-d single computer details (needs -c)\n"
"\t-n returns the number of computers\n"
"\t-c <computer_id>\n"
"\t-v print version\n"
"\t-h print this help\n");
}