diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/parse-passwd/index.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/parse-passwd/index.js')
-rw-r--r-- | node_modules/parse-passwd/index.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/node_modules/parse-passwd/index.js b/node_modules/parse-passwd/index.js new file mode 100644 index 000000000..7524520bc --- /dev/null +++ b/node_modules/parse-passwd/index.js @@ -0,0 +1,56 @@ +'use strict'; + +/** + * Parse the content of a passwd file into a list of user objects. + * This function ignores blank lines and comments. + * + * ```js + * // assuming '/etc/passwd' contains: + * // doowb:*:123:123:Brian Woodward:/Users/doowb:/bin/bash + * console.log(parse(fs.readFileSync('/etc/passwd', 'utf8'))); + * + * //=> [ + * //=> { + * //=> username: 'doowb', + * //=> password: '*', + * //=> uid: '123', + * //=> gid: '123', + * //=> gecos: 'Brian Woodward', + * //=> homedir: '/Users/doowb', + * //=> shell: '/bin/bash' + * //=> } + * //=> ] + * ``` + * @param {String} `content` Content of a passwd file to parse. + * @return {Array} Array of user objects parsed from the content. + * @api public + */ + +module.exports = function(content) { + if (typeof content !== 'string') { + throw new Error('expected a string'); + } + return content + .split('\n') + .map(user) + .filter(Boolean); +}; + +function user(line, i) { + if (!line || !line.length || line.charAt(0) === '#') { + return null; + } + + // see https://en.wikipedia.org/wiki/Passwd for field descriptions + var fields = line.split(':'); + return { + username: fields[0], + password: fields[1], + uid: fields[2], + gid: fields[3], + // see https://en.wikipedia.org/wiki/Gecos_field for GECOS field descriptions + gecos: fields[4], + homedir: fields[5], + shell: fields[6] + }; +} |