mirror of
https://github.com/neovim/neovim.git
synced 2025-09-12 22:38:16 +00:00
moved mch_get_user_name() and mch_get_uname() into os/users.c
This commit is contained in:

committed by
Thiago de Arruda

parent
6fd9f090fc
commit
ce31410c79
@@ -1739,7 +1739,7 @@ static time_t swapfile_info(char_u *fname)
|
||||
time_t x = (time_t)0;
|
||||
char *p;
|
||||
#ifdef UNIX
|
||||
char_u uname[B0_UNAME_SIZE];
|
||||
char uname[B0_UNAME_SIZE];
|
||||
#endif
|
||||
|
||||
/* print the swap file date */
|
||||
@@ -1748,7 +1748,7 @@ static time_t swapfile_info(char_u *fname)
|
||||
/* print name of owner of the file */
|
||||
if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) {
|
||||
MSG_PUTS(_(" owned by: "));
|
||||
msg_outtrans(uname);
|
||||
msg_outtrans((char_u *)uname);
|
||||
MSG_PUTS(_(" dated: "));
|
||||
} else
|
||||
#endif
|
||||
|
@@ -1886,7 +1886,7 @@ int vim_chdir(char_u *new_dir)
|
||||
int get_user_name(char_u *buf, int len)
|
||||
{
|
||||
if (username == NULL) {
|
||||
if (mch_get_user_name(buf, len) == FAIL)
|
||||
if (mch_get_user_name((char *)buf, len) == FAIL)
|
||||
return FAIL;
|
||||
username = vim_strsave(buf);
|
||||
} else
|
||||
|
@@ -14,5 +14,7 @@ const char *mch_getenv(const char *name);
|
||||
int mch_setenv(const char *name, const char *value, int overwrite);
|
||||
char *mch_getenvname_at_index(size_t index);
|
||||
int mch_get_usernames(garray_T *usernames);
|
||||
int mch_get_user_name(char *s, size_t len);
|
||||
int mch_get_uname(uid_t uid, char *s, size_t len);
|
||||
|
||||
#endif
|
||||
|
@@ -55,3 +55,32 @@ int mch_get_usernames(garray_T *users)
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert user name in s[len].
|
||||
* Return OK if a name found.
|
||||
*/
|
||||
int mch_get_user_name(char *s, size_t len)
|
||||
{
|
||||
return mch_get_uname(getuid(), s, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert user name for "uid" in s[len].
|
||||
* Return OK if a name found.
|
||||
* If the name is not found, write the uid into s[len] and return FAIL.
|
||||
*/
|
||||
int mch_get_uname(uid_t uid, char *s, size_t len)
|
||||
{
|
||||
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
|
||||
struct passwd *pw;
|
||||
|
||||
if ((pw = getpwuid(uid)) != NULL
|
||||
&& pw->pw_name != NULL && *(pw->pw_name) != NUL) {
|
||||
vim_strncpy((char_u *)s, (char_u *)pw->pw_name, len - 1);
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
snprintf(s, len, "%d", (int)uid);
|
||||
return FAIL; // a number is not a name
|
||||
}
|
||||
|
||||
|
@@ -1077,34 +1077,6 @@ int vim_is_fastterm(char_u *name)
|
||||
|| STRNICMP(name, "dtterm", 6) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert user name in s[len].
|
||||
* Return OK if a name found.
|
||||
*/
|
||||
int mch_get_user_name(char_u *s, int len)
|
||||
{
|
||||
return mch_get_uname(getuid(), s, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert user name for "uid" in s[len].
|
||||
* Return OK if a name found.
|
||||
*/
|
||||
int mch_get_uname(uid_t uid, char_u *s, int len)
|
||||
{
|
||||
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
|
||||
struct passwd *pw;
|
||||
|
||||
if ((pw = getpwuid(uid)) != NULL
|
||||
&& pw->pw_name != NULL && *(pw->pw_name) != NUL) {
|
||||
vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
|
||||
return FAIL; /* a number is not a name */
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert host name is s[len].
|
||||
*/
|
||||
|
@@ -24,8 +24,6 @@ int use_xterm_mouse(void);
|
||||
int vim_is_iris(char_u *name);
|
||||
int vim_is_vt300(char_u *name);
|
||||
int vim_is_fastterm(char_u *name);
|
||||
int mch_get_user_name(char_u *s, int len);
|
||||
int mch_get_uname(uid_t uid, char_u *s, int len);
|
||||
void mch_get_host_name(char_u *s, int len);
|
||||
long mch_get_pid(void);
|
||||
void slash_adjust(char_u *p);
|
||||
|
@@ -12,6 +12,9 @@ typedef struct growarray {
|
||||
void *ga_data;
|
||||
} garray_T;
|
||||
int mch_get_usernames(garray_T *usernames);
|
||||
int mch_get_user_name(char *s, size_t len);
|
||||
int mch_get_uname(int uid, char *s, size_t len);
|
||||
int getuid(void);
|
||||
]]
|
||||
|
||||
NULL = ffi.cast 'void*', 0
|
||||
@@ -30,11 +33,11 @@ garray_get_item = (array, index) ->
|
||||
|
||||
describe 'users function', ->
|
||||
|
||||
describe 'mch_get_usernames', ->
|
||||
|
||||
-- will probably not work on windows
|
||||
current_username = os.getenv 'USER'
|
||||
|
||||
describe 'mch_get_usernames', ->
|
||||
|
||||
it 'returns FAIL if called with NULL', ->
|
||||
eq FAIL, users.mch_get_usernames NULL
|
||||
|
||||
@@ -50,3 +53,25 @@ describe 'users function', ->
|
||||
current_username_found = true
|
||||
assert.is_true current_username_found
|
||||
|
||||
describe 'mch_get_user_name', ->
|
||||
|
||||
it 'should write the username into the buffer and return OK', ->
|
||||
name_out = ffi.new 'char[100]'
|
||||
eq OK, users.mch_get_user_name(name_out, 100)
|
||||
eq current_username, ffi.string name_out
|
||||
|
||||
describe 'mch_get_uname', ->
|
||||
|
||||
it 'should write the username into the buffer and return OK', ->
|
||||
name_out = ffi.new 'char[100]'
|
||||
user_id = lib.getuid!
|
||||
eq OK, users.mch_get_uname(user_id, name_out, 100)
|
||||
eq current_username, ffi.string name_out
|
||||
|
||||
it 'should FAIL if the userid is not found', ->
|
||||
name_out = ffi.new 'char[100]'
|
||||
-- hoping nobody has this uid
|
||||
user_id = 2342
|
||||
eq FAIL, users.mch_get_uname(user_id, name_out, 100)
|
||||
eq '2342', ffi.string name_out
|
||||
|
||||
|
Reference in New Issue
Block a user