In: Computer Science
Using linux; how would you construct a void function using strfttime() to display time. Also a function using user_from_uid and group_from_gid to show the users name or group name. Trying to recreate the ls -l command.
Thank you in advance!
#include <stdio.h>
#include <time.h>// to use time_t,struct tm,time(),localtime(),strftime()
#include <pwd.h>// to use struct passwd,getpwuid()
#include <grp.h>// to use struct group,getgrgid()
// here we can return a string instead of printing it in the function,
// since you requested to make it void, i have done so
void get_time(void);
// returns the user name from user id
char* user_from_uid(uid_t);
// returns the group name from group id
char* group_from_gid(int);
int main () {
get_time();
printf("%s\n",user_from_uid(1000));
printf("%s\n",group_from_gid(1000));
return(0);
}
void get_time(void){
// to store raw time
time_t rawtime;
// to store local time
struct tm *time_info;
// to store formated time string
char time_formated_string[80];
// gets the rawtime
time( &rawtime );
// converts to localtime
time_info = localtime( &rawtime );
// converts to ls -l's format Eg: Oct 28 14:07
strftime(time_formated_string,80,"%b %d %H:%M", time_info);
// prints the formatted time
printf("%s\n", time_formated_string );
}
char* user_from_uid(uid_t uid)
{
// to store current pws
struct passwd *pws;
// gets current pws from given pid
pws = getpwuid(uid);
// prints the pw_name of current pws
return pws->pw_name;
}
char* group_from_gid(int gid)
{
// to store current group
struct group *g;
// gets the current group from given gid
g = getgrgid(gid);
// returns the group name
return g->gr_name;
}
Code Screenshot:
Output:(blured the username and groupname due to some privacy issues)
Edited code(changed the get_time() function to return string):
#include <stdio.h>
#include <time.h>// to use time_t,struct tm,time(),localtime(),strftime()
#include <pwd.h>// to use struct passwd,getpwuid()
#include <grp.h>// to use struct group,getgrgid()
#include <stdlib.h>// to use malloc
// returns the time string
char* get_time(void);
// returns the user name from user id
char* user_from_uid(uid_t);
// returns the group name from group id
char* group_from_gid(int);
int main () {
printf("%s\n",get_time());
printf("%s\n",user_from_uid(1000));
printf("%s\n",group_from_gid(1000));
return(0);
}
char* get_time(void){
// to store raw time
time_t rawtime;
// to store local time
struct tm *time_info;
// to store formated time string
char *time_formated_string = malloc(80*sizeof(char));
// gets the rawtime
time( &rawtime );
// converts to localtime
time_info = localtime( &rawtime );
// converts to ls -l's format Eg: Oct 28 14:07
strftime(time_formated_string,80,"%b %d %H:%M", time_info);
// returns the formatted time as string
return time_formated_string;
}
char* user_from_uid(uid_t uid)
{
// to store current pws
struct passwd *pws;
// gets current pws from given pid
pws = getpwuid(uid);
// prints the pw_name of current pws
return pws->pw_name;
}
char* group_from_gid(int gid)
{
// to store current group
struct group *g;
// gets the current group from given gid
g = getgrgid(gid);
// returns the group name
return g->gr_name;
}
Code screenshot:
PS: If you have any problems/doubts please comment below.Thank You