Question

In: Computer Science

Using linux; how would you construct a void function using strfttime() to display time. Also a...

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!

Solutions

Expert Solution

#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


Related Solutions

Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Using Linux in VirtualBox To display your system date: Type date in the command line, and...
Using Linux in VirtualBox To display your system date: Type date in the command line, and press Enter. What do you see?                                            You might see the abbreviation EDT (Eastern Daylight Time) instead of EST (Eastern Standard Time), or another time zone abbreviation, such as MDT (Mountain Daylight Time) or CST (Central Standard Time). Notice also that UNIX/Linux uses a 24-hour clock. Type Date in the command line, and press Enter. What error do you see?                                    The system error...
Using the concept of of "Matched Pairs t Procedures" , describe how you would construct a...
Using the concept of of "Matched Pairs t Procedures" , describe how you would construct a comparison between two Automotive Repair facilities to see if there was a better alternative, in general. This means you need to determine sample size, how the the design would be implemented, and what the hypotheses are. This is similar to Round Robin testing in a laboratory or a "pretest/post test" study at the university. This discussion should be more than a single paragraph and...
Linux Commands 8.How would you find out information about systemdon your Linux workstation? 9.A user would...
Linux Commands 8.How would you find out information about systemdon your Linux workstation? 9.A user would like to put the firefoxprogram in the background –what would you type into the command prompt to do this? 10.For the program in above, how would you bring it back into the foreground? 11.What is the fork()function call? 12.In Linux what command sequence do we enter to stop a program? 13.What is the UIDcolumn in the output of the command ps -ef? 14.What is...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
In MongoDB using Linux how do you Execute the command to find the size of a...
In MongoDB using Linux how do you Execute the command to find the size of a single document of your choosing from the enron database Execute the command to find the size of the collection of documents in the enron database
USING COMMAND PROMPT ON LINUX. show how... Linux Users, Groups, and Permissions Lab Objective: Understand how...
USING COMMAND PROMPT ON LINUX. show how... Linux Users, Groups, and Permissions Lab Objective: Understand how to create user accounts Understand how to create group accounts Creating good password policies Understanding permissions and file sharing Setup: You will need a Ubuntu Server VM Walk Through: You are a system administrator who has been tasked to create user accounts for new users on your system. You can pick whatever theme you like as long as there are protagonist and antagonist users:...
How would you leverage the Internet to develop a business? Also, how would you manage the...
How would you leverage the Internet to develop a business? Also, how would you manage the increased competition?
What would be the wiring setup and coding using an Arduino for a display of the...
What would be the wiring setup and coding using an Arduino for a display of the temperature of the room it is in?
how to create a function to compute PACF of a time series in MATLAB without using...
how to create a function to compute PACF of a time series in MATLAB without using built-in function ''parcorr''?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT