C and default file types

J

Jerry Gaspard

I'd like to know how to open a document in a default app. like for example
if I wanted to open a .txt document I'd like my program open notepad if it
is the default for a windows machine text files or open openoffice if it is
the default application for linux.

is there a portable way to do this?
 
S

Stefan Ram

Jerry Gaspard said:
I'd like to know how to open a document in a default app. like for example
if I wanted to open a .txt document I'd like my program open notepad if it
is the default for a windows machine text files or open openoffice if it is
the default application for linux.
is there a portable way to do this?

#include <stdlib.h>
int main( void ){ system( "example.txt" ); }

will pass "example.txt" to the command processor to be
executed in a manner which the implementation shall document.

Unfortunately, it will not do what you want on most
implementations, but this is the best portable approximation I
am aware of.
 
R

Rouben Rostamian

I'd like to know how to open a document in a default app. like for example
if I wanted to open a .txt document I'd like my program open notepad if it
is the default for a windows machine text files or open openoffice if it is
the default application for linux.
is there a portable way to do this?

There is no such a thing as a "default app" associated with a
file in a UNIX system. You may build such an association
into your C program. Here's something to get you started with.
It will require quite a bit of enhancements to become something
workable.

enum filetypes { unknown, text, html, jpeg, gif };
char *default_apps[100];

default_apps[text] = "cat";
default_apps
HTML:
 = "w3m";
default_apps[jpeg] = "xv";
default_apps[gif]  = "xv";
....
default_apps[unknown]  = (you decide)


To determine the file type of a file, see the man page for the "file"
command on Linux and the references in its "See Also" section.

---------- WARNING!  WARNING! -----------------------------------

Having said that, I urge you strongly to resist the temptation of
giving a "default app" functionality in your program.  In general
such functionality opens a slew of possibilities for security breaches.
It's sufficient for one file to masquerade as something other than
what it is and have your program gullibly run an unexpected program.
DON'T DO IT!
 
J

Jerry Gaspard

There is no such a thing as a "default app" associated with a
file in a UNIX system. You may build such an association
into your C program. Here's something to get you started with.
It will require quite a bit of enhancements to become something
workable.

enum filetypes { unknown, text, html, jpeg, gif };
char *default_apps[100];

default_apps[text] = "cat";
default_apps
HTML:
 = "w3m";
default_apps[jpeg] = "xv";
default_apps[gif]  = "xv";
...
default_apps[unknown]  = (you decide)


To determine the file type of a file, see the man page for the "file"
command on Linux and the references in its "See Also" section.

---------- WARNING!  WARNING! -----------------------------------

Having said that, I urge you strongly to resist the temptation of
giving a "default app" functionality in your program.  In general
such functionality opens a slew of possibilities for security breaches.
It's sufficient for one file to masquerade as something other than
what it is and have your program gullibly run an unexpected program.
DON'T DO IT!
[/QUOTE]

the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?
 
B

Berry Kercheval

Jerry Gaspard said:
the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?

This is off topic in comp.lang.c, (hence the email, not posting) but
what you may want is the Windows "start" command. If you do

start http://www.foo.com/bar/xyzzy.html

then that will fire up the user's default browser and open that URL

In C you would do

sprintf(buf, "system %s", url);
system(buf);

with buf appropriately declared and url holding the URL you want to open.

Gnome and KDE on Unix-like systems *might* have similar commands but
I'm not sure.
 
K

Keith Thompson

Jerry Gaspard said:
the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?

Even assuming the question is meaningful, there is no way to do what
you're trying to do in portable C. On some systems (Windows?), there
may be a well-defined mapping between file types and applications, but
the way to determine that mapping is entirely implementation-specific.
On others, the mapping may be part of the user environment, and the
operating system itself may have no awareness of it. On yet others,
there may be no such mapping at all, but your program could provide
such a mapping (but I don't think that's what you're looking for).

The question is outside the scope of this newsgroup. Try a newsgroup
specific to the system(s) you're working with.
 
G

Gordon Burditt

Having said that, I urge you strongly to resist the temptation of
I second this opinion.
the main thin i want is to use the default web browser,

What's a default web browser? A web browser is started up with a
command like "lynx", "mozilla", "netscape", or whatever. There's
no browser named "default" that I've ever heard of (and in some
shells, it's treated as a builtin command for part of a switch/case
construct). If you want to start one, name it in a command.

i don't want to open
an IE window with firefox is the users default browser or i don't want to

open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?

Files don't have associated MIME types system-wide. I suppose you
could say all files have the associated MIME type
virus/malicious-dont-you-dare-run-this. There's a reason for having
execute permission bits on files, and most files *DON'T* have them
set. Even root needs at least one executable permission bit set.
An Apache *SERVER* will make such MIME-type associations. I have
no idea why the knowledge that a file with extension .html is
associated with MIME type text/html will help you with the problem
you are talking about.

I also think the idea of having *ONE* app associated with a MIME type
is silly. There are generally at least TWO apps that use a particular
file type: one produces them and one uses them.

Gordon L. Burditt
 
R

Rouben Rostamian

the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?

Don't assume that there is such a thing as a "default browser".
I suggest that you set up your program so that it prompts the user,
asking for which browser to use. You may provide a menu of options
if you think that makes your program more user-friendly. In that
case, you should allow the user to enter a choice other than those
you have suggested. In any case, avoid the temptation of making
decisions for the user. Experienced users don't like that. Let the
user have control on, and the responsibility for, his/her actions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top