Projekt

Obecné

Profil

Stáhnout (1.35 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
/**
4
 * Parse the content of a passwd file into a list of user objects.
5
 * This function ignores blank lines and comments.
6
 *
7
 * ```js
8
 * // assuming '/etc/passwd' contains:
9
 * // doowb:*:123:123:Brian Woodward:/Users/doowb:/bin/bash
10
 * console.log(parse(fs.readFileSync('/etc/passwd', 'utf8')));
11
 *
12
 * //=> [
13
 * //=>   {
14
 * //=>     username: 'doowb',
15
 * //=>     password: '*',
16
 * //=>     uid: '123',
17
 * //=>     gid: '123',
18
 * //=>     gecos: 'Brian Woodward',
19
 * //=>     homedir: '/Users/doowb',
20
 * //=>     shell: '/bin/bash'
21
 * //=>   }
22
 * //=> ]
23
 * ```
24
 * @param  {String} `content` Content of a passwd file to parse.
25
 * @return {Array} Array of user objects parsed from the content.
26
 * @api public
27
 */
28

    
29
module.exports = function(content) {
30
  if (typeof content !== 'string') {
31
    throw new Error('expected a string');
32
  }
33
  return content
34
    .split('\n')
35
    .map(user)
36
    .filter(Boolean);
37
};
38

    
39
function user(line, i) {
40
  if (!line || !line.length || line.charAt(0) === '#') {
41
    return null;
42
  }
43

    
44
  // see https://en.wikipedia.org/wiki/Passwd for field descriptions
45
  var fields = line.split(':');
46
  return {
47
    username: fields[0],
48
    password: fields[1],
49
    uid: fields[2],
50
    gid: fields[3],
51
    // see https://en.wikipedia.org/wiki/Gecos_field for GECOS field descriptions
52
    gecos: fields[4],
53
    homedir: fields[5],
54
    shell: fields[6]
55
  };
56
}
(3-3/4)