hey everyone this is my situation. i am taking a programing course that introduces you to a few languages. the first one is C
now im a java guy (not very good with it yet) im still learning but i understand the basic concepts so im not totally in the dark.
here is the task at hand:
The main purpose of this program is to use C library and system calls. The program maintains a sorted data structure (for example a binary search tree) of items with two fields. The first, "key" should hold a string representing the name for a value, and the second, "contents" should hold the string value of this item, as obtained from some sytem or library command. The running program allows queries of keys, as well as listing of all the pairs, The keys contain the following (hints on where to find these are in parentheses)
* progname - the name of the running program (argv)
* user - the login name of the user (getlogin)
* host - the name of the machine the program is running on (uname)
* uid - the users userid # (getuid)
* tty - the user's current terminal (ttyname)
* date - the current day and time (asctime)
* cwd - the current working directory (getcwd)
* term - the user's terminal type (getenv)
* nusers - the number of users on current machine (sys)
I have only been looking at the C language for only about a week now and i have to say i might be in a bit over my head. I have written some code and was wondering if I am on the right track for the above goal. i have spent hours and hours on this already so if this is just junk code then boo on me :(
also am running on MAC OSX and i believe i am writing code for unix so I'm not sure if that makes a difference. also i am using Aquamacs as my editor.
Well here goes
#include <stdio.h>
#include <stdlib.h>
#define _POSIX_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#undef _POSIX_SOURCE
int main(int argc, char** argv) {
int gethostname(char *name, int namelen); //host name
main() { //geteuid
uid_t getuid(void);
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);
}
}
main() { //getlogin
char *getlogin(void);
char *user;
if ((user = __getlogin1()) == NULL)
perror("__getlogin1() error");
else printf("__getlogin1() returned %s\n", user);
}
char *ret, tty[40];
main() { //the user's current terminal
if ((ret = ttyname(STDIN_FILENO)) == NULL)
perror("ttyname() error");
else {
strcpy(tty, ret);
printf("The ttyname associated with my stdin is %s\n", tty);
}
}
int main(void) { //day and time
char *asctime(const struct tm * timeptr);
struct tm *newtime;
time_t ltime;
/* Get the time in seconds */
time(<ime);
/* Break it down & store it in the structure tm */
newtime = localtime(<ime);
/* Print the local time as a string */
printf("The current date and time are %s",
asctime(newtime));
}
main() {
char *getcwd(char *buffer, size_t size);
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);
}
}
int main(void){ //the user's terminal type (getenv)
char *getenv(const char *varname);
char *pathvar;
pathvar = getenv("PATH");
printf("pathvar=%s",pathvar);
}
return 0;
}
// return (EXIT_SUCCESS);
}
here is my out put the only one that seemes to work is my TTY function:
Compilation started at Sat Feb 12 13:58:12
gethostname()
/bin/bash: -c: line 1: syntax error: unexpected end of file
Compilation exited abnormally with code 2 at Sat Feb 12 13:58:12
.
. all of them have this error
.
TTY function:
Compilation started at Sat Feb 12 14:15:24
tty
/dev/ttys000
Compilation finished at Sat Feb 12 14:15:25
so yea im not really sure what im doing but i think/hope im on the right track
any suggestions, comments, help would be awesome :)
Thanks guys and gals