Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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