Unix Programming - Whats the standard?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2007 > Whats the standard?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Whats the standard?
SpreadTooThin

2007-06-20, 7:29 pm

I have a file name that I want to split into drive, path, filename and
extension....
Is there a bsd / unix standard to do this?

Måns Rullgård

2007-06-20, 7:29 pm

SpreadTooThin <bjobrien62@gmail.com> writes:

> I have a file name that I want to split into drive, path, filename and
> extension....
> Is there a bsd / unix standard to do this?


dirname() and basename() are probably what you are after.

--
Måns Rullgård
mans@mansr.com
Lew Pitcher

2007-06-20, 7:29 pm

On Jun 20, 2:59 pm, SpreadTooThin <bjobrie...@gmail.com> wrote:
> I have a file name that I want to split into drive, path, filename and
> extension....


How do you establish (with any certainty) a "drive" from a Unix file
name? For that matter, what would consider an "extension"?

> Is there a bsd / unix standard to do this?




SpreadTooThin

2007-06-20, 7:29 pm

On Jun 20, 1:46 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:[vbcol=seagreen]
> On Jun 20, 2:59 pm, SpreadTooThin <bjobrie...@gmail.com> wrote:
>
>
> How do you establish (with any certainty) a "drive" from a Unix file
> name? For that matter, what would consider an "extension"?
>

Sorry Drive is an MSDOS leftover in my psyche...
To me extension is also and old MSDOS notion of a file type... .txt (a
text file) .sh (a shell script) .py (a Python script)

Eric Sosman

2007-06-20, 7:29 pm

SpreadTooThin wrote On 06/20/07 16:50,:
> On Jun 20, 1:46 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>
>
>
> Sorry Drive is an MSDOS leftover in my psyche...
> To me extension is also and old MSDOS notion of a file type... .txt (a
> text file) .sh (a shell script) .py (a Python script)


Figure out what you want to do with these:

foo.txt foo + txt, or foo + .txt?
foo.txt.old foo.txt + .old, or foo + .txt.old?
foo foo + nothing, or error?
foo. (etc.)
.foo nothing + foo, .foo + nothing, error?
.foo.bar (etc.)
. nothing + nothing, . + nothing, other?
.. (etc.)
foo.dir/foo probably not foo + .dir/foo
foo.dir/ foo + .dir? empty?

Once you've decided how to treat such cases in a way that
makes the most sense for your program, implementing it is
just a matter of some string-bashing. A plausible start
might be something along the lines of

- find the rightmost slash; the file name starts right
after it (or at the start of the string if there's
no slash at all)

- consider any sequence of leading dots as part of the
name rather than as punctuation (controversial)

- find the rightmost dot that isn't a leading dot;
the extension starts with the dot and the name ends
just before it (if there's no such dot, the extension
is empty and the whole business is the name)

Helpful functions for doing this include strrchr (note
the doubled r) and strspn. I emphasize that this is just
one way you might decide to parse the names, not the
Ultimate Answer to Life, The Universe, and Everything.

--
Eric.Sosman@sun.com
Chris Friesen

2007-06-21, 1:25 pm

SpreadTooThin wrote:
> I have a file name that I want to split into drive, path, filename and
> extension....
> Is there a bsd / unix standard to do this?


Under unix, drive doesn't matter as everything is part of one tree.

Generally, unix apps don't care about file extensions but rather use
"magic numbers" embedded within the file to identify the type of data
contained within the file. (See the "file" utility, for instance.)

As for the specifics, Mr. Sosman had some quite detailed suggestions.

Chris
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com