// miscutilities.c // Lisa Frye // October 20, 2000 // Program to show some date/time, user/group, and system // resource functions for CSC 451 #include #include #include #include #include #include #include #include #include #include #include #define TESTFILE1 "/usr/users/staff/frye/csc451/miscutilities.c" #define TESTFILE2 "/usr/users/staff/frye/csc451" void time_func(); void print_date(struct tm *tp); void user_func(); void group_func(); void sys_func(); int main() { time_func(); user_func(); group_func(); sys_func(); printf("\n=============================================\n\n"); return 0; } // Function to demonstrate time functions void time_func() { time_t clock; struct tm *tm; struct timeval *tv; struct timezone *tz; char buf[BUFSIZ]; printf("\n\n===================== TIME OUTPUT ====================\n"); // Get current time in clock to be used by other functions time(&clock); // Get GMT time tm = gmtime(&clock); print_date(tm); // Get local time tm = localtime(&clock); print_date(tm); printf("\n======================================================\n"); printf("%s\n", ctime(&clock)); printf("\n======================================================\n"); printf("Seconds since 1/1/1970:\t%ld\n", mktime(tm)); printf("\n======================================================\n"); strftime(buf, BUFSIZ, "%m/%d/%y %I:%M %p", tm); printf("%s\n", buf); return; } // end function date_func // Function to print tm structure void print_date(struct tm *tp) { char *monstr=(char *)calloc(10, sizeof(char)); printf("\n======================================================\n"); // Get string version of month switch (tp->tm_mon) { case 0: strcpy(monstr, "January"); break; case 1: strcpy(monstr, "February"); break; case 2: strcpy(monstr, "March"); break; case 3: strcpy(monstr, "April"); break; case 4: strcpy(monstr, "May"); break; case 5: strcpy(monstr, "June"); break; case 6: strcpy(monstr, "July"); break; case 7: strcpy(monstr, "August"); break; case 8: strcpy(monstr, "September"); break; case 9: strcpy(monstr, "October"); break; case 10: strcpy(monstr, "November"); break; case 11: strcpy(monstr, "December"); break; } printf("Month/Day/Year:\t%s %d, %d\n", monstr, tp->tm_mday, tp->tm_year+1900); printf("Time\t%d:%d:%d\n", tp->tm_hour, tp->tm_min, tp->tm_sec); printf("Day of week:\t"); // Get day as a string switch (tp->tm_wday) { case 0: printf("Sunday\n"); break; case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; } printf("Day of Year:\t%d\n", tp->tm_yday); // See if it is Daylight Savings Time if (tp->tm_isdst != 0) printf("Daylight Savings Time is in effect\n"); else printf("Daylight Savings Time is not in effect\n"); return; } // end function print_date void user_func() { struct passwd *pw; printf("\n\n===================== USER OUTPUT ====================\n"); // get uid of process printf("Real uid: %d, Effective uid: %d\n", getuid(), geteuid()); pw = getpwnam("lfrye920"); printf("\n=============================================\n"); printf("Name: %s\n", pw->pw_gecos); printf("Login: %s\n", pw->pw_name); printf("uid: %d\n", pw->pw_uid); printf("gif: %d\n", pw->pw_gid); printf("Home Directory: %s\n", pw->pw_dir); printf("Shell: %s\n", pw->pw_shell); pw = getpwent(); while (pw != NULL) { if (strncmp(pw->pw_name, "adm", 3) == 0) break; printf("\n=============================================\n"); printf("Name: %s\n", pw->pw_gecos); printf("Login: %s\n", pw->pw_name); printf("uid: %d\n", pw->pw_uid); printf("gif: %d\n", pw->pw_gid); printf("Home Directory: %s\n", pw->pw_dir); printf("Shell: %s\n", pw->pw_shell); pw = getpwent(); } // end while loop return; } // end function user_func void group_func() { struct group *gr; int i = 0; printf("\n\n===================== GROUP OUTPUT ====================\n"); // get gid of process printf("Real gid: %d, Effective gid: %d\n", getgid(), getegid()); gr = getgrnam("student"); printf("\n=============================================\n"); printf("Group Name:\t%s\n", gr->gr_name); printf("gid:\t\t%d\n", gr->gr_gid); printf("Members:\t"); while ((gr->gr_mem[i]) != NULL) { printf("%s ", gr->gr_mem[i]); i++; } // end while printf("\n"); return; } // end function group_func void sys_func() { struct utsname *info; struct rlimit *rp; long max_size, val; printf("\n\n===================== SYSTEM OUTPUT ====================\n"); // Get system information printf("\n=============================================\n"); if (uname(info) < 0) perror("uname"); else { printf("Operating System:\t%s\n", info->sysname); printf("Hostname:\t\t%s\n", info->nodename); printf("OS Release:\t\t%s\n", info->release); printf("OS Version:\t\t%s\n", info->version); printf("Architecture:\t\t%s\n", info->machine); } // end else // Get system limits printf("\n=============================================\n"); val = sysconf(_SC_OPEN_MAX); if (val < 0) perror("sysconf"); else printf("Max number of open files per process: %d\n", val); val = sysconf(_SC_PAGESIZE); if (val < 0) perror("sysconf"); else printf("System memory page size: %d\n", val); val = sysconf(_SC_PHYS_PAGES); if (val < 0) perror("sysconf"); else printf("Total number of pages of physical memory: %d\n", val); val = sysconf(_SC_LOGIN_NAME_MAX); if (val < 0) perror("sysconf"); else printf("Max length of a login name: %d\n", val); // Get system limits associated with specified file printf("\n=============================================\n"); val = pathconf(TESTFILE1, _PC_LINK_MAX); if (val < 0) perror("pathconf"); else printf("Max number of links to the file: %d\n", val); val = pathconf(TESTFILE2, _PC_PATH_MAX); if (val < 0) perror("pathconf"); else printf("Max number of characters in a pathname: %d\n", val); // Get process limits printf("\n=============================================\n"); max_size = ulimit(UL_GETFSIZE); printf("Max file size for process: %ld\n", max_size); max_size = ulimit(UL_GDESLIM); printf("Max number of open files for process: %ld\n", max_size); return; } // end function sys_func