Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-21
3
Medium Priority
?
1,096 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 1760 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

886 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