moved mch_get_user_name() and mch_get_uname() into os/users.c

This commit is contained in:
Stefan Hoffmann
2014-03-06 23:20:29 +01:00
committed by Thiago de Arruda
parent 6fd9f090fc
commit ce31410c79
7 changed files with 62 additions and 36 deletions

View File

@@ -1739,7 +1739,7 @@ static time_t swapfile_info(char_u *fname)
time_t x = (time_t)0; time_t x = (time_t)0;
char *p; char *p;
#ifdef UNIX #ifdef UNIX
char_u uname[B0_UNAME_SIZE]; char uname[B0_UNAME_SIZE];
#endif #endif
/* print the swap file date */ /* print the swap file date */
@@ -1748,7 +1748,7 @@ static time_t swapfile_info(char_u *fname)
/* print name of owner of the file */ /* print name of owner of the file */
if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) { if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) {
MSG_PUTS(_(" owned by: ")); MSG_PUTS(_(" owned by: "));
msg_outtrans(uname); msg_outtrans((char_u *)uname);
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
} else } else
#endif #endif

View File

@@ -1886,7 +1886,7 @@ int vim_chdir(char_u *new_dir)
int get_user_name(char_u *buf, int len) int get_user_name(char_u *buf, int len)
{ {
if (username == NULL) { if (username == NULL) {
if (mch_get_user_name(buf, len) == FAIL) if (mch_get_user_name((char *)buf, len) == FAIL)
return FAIL; return FAIL;
username = vim_strsave(buf); username = vim_strsave(buf);
} else } else

View File

@@ -14,5 +14,7 @@ const char *mch_getenv(const char *name);
int mch_setenv(const char *name, const char *value, int overwrite); int mch_setenv(const char *name, const char *value, int overwrite);
char *mch_getenvname_at_index(size_t index); char *mch_getenvname_at_index(size_t index);
int mch_get_usernames(garray_T *usernames); 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 #endif

View File

@@ -55,3 +55,32 @@ int mch_get_usernames(garray_T *users)
return OK; 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
}

View File

@@ -1077,34 +1077,6 @@ int vim_is_fastterm(char_u *name)
|| STRNICMP(name, "dtterm", 6) == 0; || 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]. * Insert host name is s[len].
*/ */

View File

@@ -24,8 +24,6 @@ int use_xterm_mouse(void);
int vim_is_iris(char_u *name); int vim_is_iris(char_u *name);
int vim_is_vt300(char_u *name); int vim_is_vt300(char_u *name);
int vim_is_fastterm(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); void mch_get_host_name(char_u *s, int len);
long mch_get_pid(void); long mch_get_pid(void);
void slash_adjust(char_u *p); void slash_adjust(char_u *p);

View File

@@ -12,6 +12,9 @@ typedef struct growarray {
void *ga_data; void *ga_data;
} garray_T; } garray_T;
int mch_get_usernames(garray_T *usernames); 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 NULL = ffi.cast 'void*', 0
@@ -30,10 +33,10 @@ garray_get_item = (array, index) ->
describe 'users function', -> describe 'users function', ->
describe 'mch_get_usernames', -> -- will probably not work on windows
current_username = os.getenv 'USER'
-- will probably not work on windows describe 'mch_get_usernames', ->
current_username = os.getenv 'USER'
it 'returns FAIL if called with NULL', -> it 'returns FAIL if called with NULL', ->
eq FAIL, users.mch_get_usernames NULL eq FAIL, users.mch_get_usernames NULL
@@ -50,3 +53,25 @@ describe 'users function', ->
current_username_found = true current_username_found = true
assert.is_true current_username_found 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