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(&ltime);
            /* Break it down & store it in the structure tm */
            newtime = localtime(&ltime);

            /* 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

You have five main functions? You should have only one main function.

yea i kind of figured that :/ how do i declare a function?

would it be something like this

getlogin() { //getlogin <-------replaced main with getlogin??

char *getlogin(void);

char *user;
 
if ((user = __getlogin1()) == NULL)
perror("__getlogin1() error");
else printf("__getlogin1() returned %s\n", user);

}

Here's a simple function definition and call.

#include <stdio.h>
#include <stdlib.h>

char* getlogin()/*C does not support function overloading*/ 
{
	char *user;
	/*do something here*/
	return user; 
}

int main(int argc, char** argv) 
{
	char *ans = getlogin();
	return (EXIT_SUCCESS);
}

Thanks that makes sense

is the main function always the last and you only make calls in the main function.

this is what my program looks like (i created a fresh one

#include <stdio.h>
#include <stdlib.h>

char* getlogin(){

    char *user;

    printf("__getlogin1() returned %s\n", user);
    return user;
}

int main(int argc, char** argv)
 {
    char *ans = getlogin();
    return (EXIT_SUCCESS);
}

im useing the aquamacs built in compiler.
it asks me for a compile command: i put in getlogin()
and it gives me this

Compilation started at Sat Feb 12 15:32:19

getlogin()
/bin/bash: -c: line 1: syntax error: unexpected end of file

Compilation exited abnormally with code 2 at Sat Feb 12 15:32:19

im probably missing something :/
also what are the * for?

i know these are really beginner questions but i truly appreciate the help :) googleing this stuff seems to be a bit overwhelming

wow well i guess i am a noob :p
im use to netbeans for java where all i have to is press the play button

im downloading the GCC compiler for mac and maybe i can get something to happen
i guess my first hint was what it said compiler commend

boy o boy this is going to take me a while :P

// edit also : where does *ans come from? is that what i use to get ---getlogin();

#
char *ans = getlogin();

ok does the following look correct ?? im still waiting for the tools to download fro apple

#include <stdio.h>
#include <stdlib.h>
#define _POSIX_SOURCE
#include <unistd.h>
#undef _POSIX_SOURCE
#include <time.h>

  char* getcwd(char *buffer, size_t size){
            char cwd[256];

                    printf("current working directory is: %s\n", cwd);
		    return cwd;
            }
        

char* getlogin()/*C does not support function overloading*/ {

    char *user;

    printf("__getlogin1() returned %s\n", user);
    return user;
}
int time(void) {
        struct tm *newtime;
        time_t ltime;
        /* Get the time in seconds */
        time(&ltime);
        /* Break it down & store it in the structure tm */
        newtime = localtime(&ltime);
        /* Print the local time as a string */
        printf("The current date and time are %s",
                asctime(newtime));
    }

int main(int argc, char** argv)
 {
    int *time=time();
    char *cwd = getcwd();
    char *ans = getlogin();
    return (EXIT_SUCCESS);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.