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.

#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;                                                               
    newtime = localtime(&ltime);                                                
    printf("The current date and time are %s",                                  
		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);
//******* 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);
//*********** cwd - the current working directory (getcwd)**************************

#include <unistd.h>
#include <stdio.h>
		char cwd[256];
		if (chdir("/tmp") != 0)
			perror("chdir() error()");
		else {
			if (getcwd(cwd, sizeof(cwd)) == NULL)
				perror("getcwd() error");
				printf("current working directory is: %s\n", cwd);
			printf("** ");

//*********** term - the user's terminal type (getenv) ******************************

	char *pathvar;
	pathvar = getenv("PATH");
//**********//* 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 :)

When you have several fields which all relate to the same object, you create a struct which will group the different fields (members of the struct), into one record (struct).

Then you use the dot member to access any member of the struct:



Which you will prototype right above main() so any function can make a local stuct for data it needs to work on.

struct system {
  char date1[10];
  char type[25];
  float version;
  int somevariableName;

When you want to get a bunch of these together into an array, you simply create an array of structs:

struct system systems[SIZE];

Structs are a great thing, but be aware that the compiler will add a small amount of padding for alignment purposes, and not every compiler will pad them the same. That makes portability a real issue. Same compiler on a different system, may have different padding. ;)

Of course, you can sort the data according to any member of the struct.

Edited 5 Years Ago by Adak: n/a

Wow Thanks you so much!! those Structs look really cool. I'm off to play with them :P

im a bit confused. i dont know how to save the stuff into a struct. for example

how do i get this

struct tm *newtime;                                                         
    time_t ltime;                                                               
    newtime = localtime(&ltime);                                                
    printf("The current date and time are %s",                                  

into this :

struct system {

char date1[10];

so i can say printf(system.date) and it will print out the first chunk of code. i need to save the system calls in some sort of data structure so when the user asks for them the will appear :/ very confused and stressed

The struct members (aka fields), are just the same thing as variables:

int myVariable = 0;
mystructName.myStructVariable = 0; //or = any int variable you have

if myStructVariable is an int, and a member of the struct called mystructName, then the above assignments are the same.

You can use scanf("%d", &mystructName.myStructVariable), same as any other integer. You can string copy into struct members that are char arrays or pointers to same.

The main difference with using a struct is that access is through the dot operator: structName.structMember.

This article has been dead for over six months. Start a new discussion instead.