Solved

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

Posted on 2003-11-21
3
1,089 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__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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