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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//Copyright 2018 #UlinProject Денис Котляров

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
// limitations under the License.


//#Ulin Project 1718
//


/*!
Name and information about the current kernel.

# Print
```
extern crate cluuname;
use cluuname::uname;

fn main() {
	let uname = uname().unwrap();
	println!("{}", uname);
	//"Linux" "cluComp" "4.15.15-1-zen" "#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018" "x86_64"
}
```
# 2Print

```
extern crate cluuname;
use cluuname::uname;
use cluuname::UtsName;
use cluuname::build;

fn main() {
	let uname = uname().unwrap();
	nodename(uname);
	//NODENAME "R510"
	
	let custom_uname = build::linux_216_86();
	nodename(custom_uname);
	//NODENAME "cluComp"
}

fn nodename<T: UtsName>(uname: T) {
	println!("NODENAME {:?}", uname.as_nodename());
}
```


# CustomPrint
```
extern crate cluuname;
use cluuname::uname;
use cluuname::UtsName;

fn main() {
	let uname = uname().unwrap();

	let sysname = uname.as_sysname();
	let nodename = uname.as_nodename();
	let release = uname.as_release();
	let version = uname.as_version();
	let machine = uname.as_machine();

	println!("{:?} {:?} {:?} {:?} {:?}", sysname, nodename, release, version, machine);
	//"Linux "cluComp" "4.15.15-1-zen" "#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018" "x86_64"
}
```

# Hash + Hash Version
```
extern crate cluuname;
use cluuname::uname;
use cluuname::UtsName;

fn main() {
    let uname = uname().unwrap();

    let machine_all_hash = uname.uname_hash();
    let machive_version_hash = uname.version_hash();

    println!("UNAME_HASH {}", machine_all_hash);
    //12821596144084292007
    println!("UNAME_V_HASH {}", machive_version_hash);
    //2978006705337010168
}
```

# CustomUname

```
#![feature(plugin)]
#![plugin(clucstr)]
extern crate cluuname;
use cluuname::build;

use std::ffi::CStr;

fn main() {
	let uname = build::custom(
		cstr!("Linux"),
		cstr!("cluComp"),
		cstr!("2.16-localhost"),
		cstr!("#1 SMP PREEMPT Sat Mar 31 23:59:18 UTC 2008"),
		cstr!("x86"),
	);
	println!("{}", uname);
	//"Linux" "cluComp" "2.16-localhost" "#1 SMP PREEMPT Sat Mar 31 23:59:18 UTC 2008" "x86"
}
```

# Flags
enable_domainname - Additional item `domainname`

```
[dependencies]
cluuname = { version = "*", features = ["enable_domainname"] }
```

*/
#![feature(plugin)]
#![plugin(clucstr)]

use std::ffi::CStr;


pub mod hash_version;
pub mod uts_struct;

use hash_version::HashVersion;
use uts_struct::buf::UtsNameBuf;
use uts_struct::slice::UtsNameSlice;

use std::fmt::Debug;
use std::fmt::Display;

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

///Basic uname trait
pub trait UtsName: Hash + HashVersion + Display + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + Clone {
	///Get sysname for this structure.
	fn as_sysname(&self) -> &CStr;
	///Get nodename for this structure.
	fn as_nodename(&self) -> &CStr;
	///Get release for this structure.
	fn as_release(&self) -> &CStr;
	///Get version for this structure.
	fn as_version(&self) -> &CStr;
	///Get machine for this structure.
	fn as_machine(&self) -> &CStr;
	
	///Get domainname for this structure.
	#[cfg(feature = "enable_domainname")]
	fn as_domainname(&self) -> &CStr;
	
	
	fn uname_hash(&self) -> u64 {
		let mut hasher = DefaultHasher::new();
		self.hash(&mut hasher);
		hasher.finish()  
	}
	fn version_hash(&self) -> u64 {
		let mut hasher = DefaultHasher::new();
		self.hash_version(&mut hasher);
		hasher.finish()  
	}
}







///Getting and creating a custom uname.
pub mod build {
	use uts_struct::slice::UtsNameSlice;
	use uts_struct::buf::UtsNameBuf;
	use std::ffi::CStr;
	
	
	
	///Create custom uname
	///```
	///sysname:	a1
	///nodename:	a2
	///release:	a3
	///version:	a4
	///machine:	a5
	///
	///#[cfg(feature = "enable_domainname")]
	///domainname:	a6
	///```
	#[cfg(feature = "enable_domainname")]
	#[inline]
	pub fn custom<'a>(a1: &'a CStr, a2: &'a CStr, a3: &'a CStr, a4: &'a CStr, a5: &'a CStr, a6: &'a CStr) -> UtsNameSlice<'a> {
		UtsNameSlice::new(a1, a2, a3, a4, a5, a6)
	}
	
	///Create custom uname
	///```
	///sysname:	a1
	///nodename:	a2
	///release:	a3
	///version:	a4
	///machine:	a5
	///
	///#[cfg(feature = "enable_domainname")]
	///domainname:	a6
	///```
	#[cfg(not(feature = "enable_domainname"))]
	#[inline]
	pub fn custom<'a>(a1: &'a CStr, a2: &'a CStr, a3: &'a CStr, a4: &'a CStr, a5: &'a CStr) -> UtsNameSlice<'a> {
		UtsNameSlice::new(a1, a2, a3, a4, a5)
	}
	
	
	///Getting the current uname
	#[inline]
	pub fn this_machine() -> Result<UtsNameBuf, i32> {
		UtsNameBuf::this_machine()
	}
	
	///"Linux" "cluComp" "2.16-localhost" "#1 SMP PREEMPT Sat Mar 31 23:59:18 UTC 2008" "x86" "(none)"
	///```
	///sysname:	cstr!("Linux")
	///nodename:	cstr!("cluComp")
	///release:	cstr!("2.16-localhost")
	///version:	cstr!("#1 SMP PREEMPT Sat Mar 31 23:59:18 UTC 2008")
	///machine:	cstr!("x86")
	///
	///#[cfg(feature = "enable_domainname")]
	///domainname:	cstr!("(none)")
	///```
	pub fn linux_216_86<'a>() -> UtsNameSlice<'a> {
		custom (
			cstr!("Linux"),
			cstr!("cluComp"),
			cstr!("2.16-localhost"),
			cstr!("#1 SMP PREEMPT Sat Mar 31 23:59:18 UTC 2008"),
			cstr!("x86"),
			
			#[cfg(feature = "enable_domainname")]
			cstr!("(none)"),
		)
	}
	
	///"Linux" "cluComp" "4.15.15-1-zen" "#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018" "x86_64" "(none)"
	///```
	///sysname:	cstr!("Linux")
	///nodename:	cstr!("cluComp")
	///release:	cstr!("4.15.15-1-zen")
	///version:	cstr!("#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018")
	///machine:	cstr!("x86_64")
	///
	///#[cfg(feature = "enable_domainname")]
	///domainname:	cstr!("(none)")
	///```
	///
	pub fn linux_415_86_64<'a>() -> UtsNameSlice<'a> {
		custom (
			cstr!("Linux"),
			cstr!("cluComp"),
			cstr!("4.15.15-1-zen"),
			cstr!("#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018"),
			cstr!("x86_64"),
			
			#[cfg(feature = "enable_domainname")]
			cstr!("(none)"),
		)
	}
	
	
}


///Getting the current uname.
///```
///extern crate cluuname;
///use cluuname::uname;
///
///fn main() {
///	let uname = uname().unwrap();
///	println!("{}", uname);
///	//"Linux" "cluComp" "4.15.15-1-zen" "#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018" "x86_64"
///}
#[inline]
pub fn uname() -> Result<UtsNameBuf, i32> {
	build::this_machine()
}

///Create custom uname.
///```
///sysname:	a1
///nodename:	a2
///release:	a3
///version:	a4
///machine:	a5
///
///#[cfg(feature = "enable_domainname")]
///domainname:	a6
///```
#[cfg(feature = "enable_domainname")]
#[inline]
pub fn custom_uname<'a>(a1: &'a CStr, a2: &'a CStr, a3: &'a CStr, a4: &'a CStr, a5: &'a CStr, a6: &'a CStr) -> UtsNameSlice<'a> {
	build::custom(a1, a2, a3, a4, a5, a6)
}

///Create custom uname.
///```
///sysname:	a1
///nodename:	a2
///release:	a3
///version:	a4
///machine:	a5
///
///#[cfg(feature = "enable_domainname")]
///domainname:	a6
///```
#[cfg(not(feature = "enable_domainname"))]
#[inline]
pub fn custom_uname<'a>(a1: &'a CStr, a2: &'a CStr, a3: &'a CStr, a4: &'a CStr, a5: &'a CStr) -> UtsNameSlice<'a> {
	build::custom(a1, a2, a3, a4, a5)
}

#[inline]
pub fn uname_hash<I: UtsName>(uts: &I) -> u64 {
	uts.uname_hash()
}

#[inline]
pub fn version_hash<I: UtsName>(uts: &I) -> u64 {
	uts.version_hash()
}




#[cfg(test)]
mod tests {
	use super::*;
	
	
	#[test]
	#[cfg(target_os = "linux")]
	fn linux() {
		if let Ok(uts) = uname() {
			assert_eq!(uts.as_sysname(), cstr!("Linux"));
		}
	}
	
	#[test]
	fn custom() {
		let uts = custom_uname (
			cstr!("Linux"),
			cstr!("cluComp"),
			cstr!("4.15.15-1-zen"),
			cstr!("#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018"),
			cstr!("x86_64"),
			
			#[cfg(feature = "enable_domainname")]
			cstr!("(none)"),
		);
		
		assert_eq!(uts.as_sysname(), cstr!("Linux"));
		assert_eq!(uts.as_nodename(), cstr!("cluComp"));
		assert_eq!(uts.as_release(), cstr!("4.15.15-1-zen"));
		assert_eq!(uts.as_version(), cstr!("#1 ZEN SMP PREEMPT Sat Mar 31 23:59:18 UTC 2018"));
		assert_eq!(uts.as_machine(), cstr!("x86_64"));
		
		#[cfg(feature = "enable_domainname")]
		assert_eq!(uts.as_domainname(), cstr!("(none)"));
	}
}