ok Guys and gals i just started learning C about a week ago and im working on a program that makes calls to the system for user name, system info ect...
here is what i have so far.
#define _POSIX_SOURCE
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
//************ date - the current day and time (asctime) ***********************
printf(" **");
struct tm *newtime;
time_t ltime;
time(<ime);
newtime = localtime(<ime);
printf("The current date and time are %s",
asctime(newtime));
printf("** ");
//******** host - the name of the machine the program is running on (uname)*********
struct utsname uts;
if (uname(&uts) < 0)
perror("uname() error");
else {
printf("Sysname: %s\n", uts.sysname);
printf("Nodename: %s\n", uts.nodename);
printf("Release: %s\n", uts.release);
printf("Version: %s\n", uts.version);
printf("Machine: %s\n", uts.machine);
printf("**");
}
//******* uid - the users userid # (getuid) ******************************************
struct passwd *p;
uid_t uid;
if ((p = getpwuid(uid = getuid())) == NULL)
perror("getpwuid() error");
else {
puts("getpwuid() returned the following info for your userid:");
printf(" pw_name : %s\n", p->pw_name);
printf(" pw_uid : %d\n", (int) p->pw_uid);
printf(" pw_gid : %d\n", (int) p->pw_gid);
printf(" pw_dir : %s\n", p->pw_dir);
printf(" pw_shell : %s\n", p->pw_shell);
printf("** ");
}
//********* tty - the user's current terminal (ttyname) ***************************
char *ret, tty[40];
if ((ret = ttyname(STDIN_FILENO)) == NULL)
perror("ttyname() error");
else {
strcpy(tty, ret);
printf("The ttyname associated with my stdin is %s\n", tty);
printf("**");
}
//*********** cwd - the current working directory (getcwd)**************************
#define _POSIX_SOURCE
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>
char cwd[256];
if (chdir("/tmp") != 0)
perror("chdir() error()");
else {
if (getcwd(cwd, sizeof(cwd)) == NULL)
perror("getcwd() error");
else
printf("current working directory is: %s\n", cwd);
printf("** ");
}
//*********** term - the user's terminal type (getenv) ******************************
char *pathvar;
pathvar = getenv("PATH");
printf("pathvar=%s",pathvar);
//**********//* user - the login name of the user (getlogin) ***********************
/*
char *user;
if ((user = __getlogin1()) == NULL)
perror("__getlogin1() error");
else printf("__getlogin1() returned %s\n", user);
im still working on this one im getting this error :
log.c:104: warning: assignment makes pointer from integer without a cast
Undefined symbols:
"___getlogin1", referenced from:
_main in ccVqyZJS.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
*/
//********** nusers - the number of users on current machine (sys)********************
// ********** progname - the name of the running program (argv)**********************
}
and my output is
**The current date and time are Sun Feb 13 17:30:04 2011
** Sysname: Darwin
Nodename: Macintosh-8.local
Release: 10.6.0
Version: Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386
Machine: i386
**getpwuid() returned the following info for your userid:
pw_name : *******
pw_uid : 501
pw_gid : 20
pw_dir : /Users/ESt****
pw_shell : /bin/bash
** The ttyname associated with my stdin is /dev/ttys001
**current working directory is: /private/tmp
** pathvar=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/binlogout
[Process completed]
now what i want is is to store each this data in a sorted array. so when the user types "date" the info from the date and time call gets displayed. im just not sure where to start.. any help would be awesome :)