Solved

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

Posted on 2003-11-21
3
1,082 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

14 Experts available now in Live!

Get 1:1 Help Now