Solved

C++/Linux Getting Users home directory/ Checking dev drivers

Posted on 2003-11-21
3
1,085 Views
Last Modified: 2012-06-21
Gday All,


I have a few problems programming in linux/C++ that a just cant seem to find a answer for on the net.
I am using Kylix 3 pro and its help also hasent helped.

Problem 1:
I need to get the path of the user that has executed my program.
For example i need to do the following code

      char* homePath = USERS HOME DIR?
       FILE *outFile;
      outFile = fopen(homePath, "w");
     
      etc..

Problem 2:

Is there an easy method of checking weather or not dev files are resedent?

I have a set of serial card drivers have been installed on the system and i need to check wether or not they have been installed.

The drivers have two dev files.
/dev/esccp0 and  /dev/esccp1.

I have tried to use a fopen and a fstatus on these files to see if they are there, with no luck.
fopen returns NULL evan when they are peresent.

any help would be great

regards,
Michael
 
0
Comment
Question by:__Michael__
3 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9801442
Problem 1:
The home directory of a user on a UNIX (or Linux) system is always ~<username>/
So if you want to get the home directory for the user "test", and open a file named "myfile" in this home dir, you would use the following path: "~test/myfile"

Problem 2:
You should be able to use fstat() to find out if a file is a device. The st_mode field in the returned structure does contain this information (test for S_IFBLK and S_IFCHR).
0
 
LVL 86

Accepted Solution

by:
jkr earned 440 total points
ID: 9801447
>>     char* homePath = USERS HOME DIR?

     char* homePath = getenv("HOME");

You can simply evaluate and rely on that environment varaible, it will always be set.

>>Is there an easy method of checking weather or not dev files are resedent?
>>/dev/esccp0 and  /dev/esccp1.

You are referring to "special files", not drivers. Just use 'stat()' to check for their existance:

#include <sys/types.h>
#include <sys/stat.h>

struct stat st;

if ( -1 == stat ( "/dev/esccp0", &st) {

    // does not exist

} else {

    // exists
}

0
 

Author Comment

by:__Michael__
ID: 9801758
Thankyou jkr.

Probems now solved.

Sometimes it the simple things that take the longest!

regards,
Michael
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now