Getting the file name from a FILE *

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Tobin

    #31
    Re: Getting the file name from a FILE *

    In article <e4589517-08df-41c9-81a8-7a150ef2781e@a7 0g2000hsh.googl egroups.com>,
    christian.bau <christian.bau@ cbau.wanadoo.co .ukwrote:
    >You should really get used to looking a bit further than Windows.
    If you're selling a compiler that only works with Windows, it doesn't
    matter that your extensions rely on Windoziness.
    >What you are suggesting is in the end a function that returns the name
    >that was used to open a file, not the name of the file. That is
    >trivial to implement for anyone who wants it, and not very useful.
    It's very useful for producing better error messages, and can't be
    reasonably implemented by the user without changing existing
    interfaces.

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

    Comment

    • Richard Tobin

      #32
      Re: Getting the file name from a FILE *

      In article <pan.2008.06.20 .21.22.42.82817 4@spam.com>,
      Chris Peters <no@spam.comwro te:
      >This sounds like a terrible idea to me. Consider a program that opens tens
      >of thousands of files simultaneously, all with long filenames, say 100 or
      >200 bytes each. You could easily be wasting 1MB or more just storing
      >copies of all the filenames in your standard library.
      And each of them quite likely has a buffer associated with it. By
      comparison the filename is unlikely to be significant, and of course
      most filenames aren't anything like 200 bytes.

      A more important limitation, mentioned in various forms by other
      posters, is that many files just aren't opened by fopen() in the
      current process. They're inherited, or are pipes or sockets or
      who-knows-what. But perhaps that's rarer in Windows, and in any case
      it's handy to have the filename for those cases where it is possible.

      -- Richard

      --
      In the selection of the two characters immediately succeeding the numeral 9,
      consideration shall be given to their replacement by the graphics 10 and 11 to
      facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

      Comment

      • Ian Collins

        #33
        Re: Getting the file name from a FILE *

        Richard Tobin wrote:
        In article <e4589517-08df-41c9-81a8-7a150ef2781e@a7 0g2000hsh.googl egroups.com>,
        christian.bau <christian.bau@ cbau.wanadoo.co .ukwrote:
        >
        >You should really get used to looking a bit further than Windows.
        >
        If you're selling a compiler that only works with Windows, it doesn't
        matter that your extensions rely on Windoziness.
        Until you try to port either the compiler or the code that builds with
        it. Sounds like M$ standard practice :)

        --
        Ian Collins.

        Comment

        • CBFalconer

          #34
          Re: Getting the file name from a FILE *

          jacob navia wrote:
          >
          We are rewriting the libc for the 64 bit version of lcc-win
          and we have added a new field in the FILE structure:
          char *FileName;
          fopen() will save the file name and an accessor function
          will return the file name given a FILE *.
          >
          Questions:
          >
          What would be the best name for this function?
          char *fname(FILE *); // lower case, short, similar to other
          // file functions
          // clear name but maybe too long?
          char *FileNameFromFi leP(FILE *);
          >
          What problems could arise with this function? Why is not in the
          C API?
          Many large problems on Linux/Unix. A file may be accessed via a
          name, but once open it is nameless, and may well be identical to a
          file opened with another program under another name, including
          using identical storage. This is simply not a possible function.
          If you want to know what name you used to open it, all you have to
          do is remember it.

          Just because things work on primitive operating systems, such as
          Winders, doesn't mean they can work everywhere.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.


          ** Posted from http://www.teranews.com **

          Comment

          • Richard

            #35
            Re: Getting the file name from a FILE *

            CBFalconer <cbfalconer@yah oo.comwrites:
            jacob navia wrote:
            >>
            >We are rewriting the libc for the 64 bit version of lcc-win
            >and we have added a new field in the FILE structure:
            > char *FileName;
            >fopen() will save the file name and an accessor function
            >will return the file name given a FILE *.
            >>
            >Questions:
            >>
            >What would be the best name for this function?
            > char *fname(FILE *); // lower case, short, similar to other
            > // file functions
            > // clear name but maybe too long?
            > char *FileNameFromFi leP(FILE *);
            >>
            >What problems could arise with this function? Why is not in the
            >C API?
            >
            Many large problems on Linux/Unix. A file may be accessed via a
            name, but once open it is nameless, and may well be identical to a
            file opened with another program under another name, including
            using identical storage. This is simply not a possible function.
            If you want to know what name you used to open it, all you have to
            do is remember it.
            >
            Just because things work on primitive operating systems, such as
            Winders, doesn't mean they can work everywhere.
            There is no such OS as Winders as far as I know. Please refrain from
            using silly slang in a technical news group - as you frequently remind
            others.

            Comment

            • CBFalconer

              #36
              Re: Getting the file name from a FILE *

              "christian. bau" wrote:
              >
              .... snip ...
              >
              What you are suggesting is in the end a function that returns the
              name that was used to open a file, not the name of the file. That
              is trivial to implement for anyone who wants it, and not very
              useful. If you can implement a function that will either return
              the _correct_ name of a file or determine that there is no name,
              go ahead and do it.
              That last is impossible under Unix/Linux. A file, and its storage,
              is specified separately from directory entries. Any directory
              entry may be linked to that file. It can then be used to open the
              file, read from it, write to it, etc. (depending on permissions).
              Those are known as links. When the last link is deleted that file
              is no longer accessible, and the system deletes it. Users don't
              delete files, they delete links.

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.


              ** Posted from http://www.teranews.com **

              Comment

              • CBFalconer

                #37
                Re: Getting the file name from a FILE *

                jacob navia wrote:
                Walter Roberson wrote:
                >
                .... snip ...
                >
                >What is the "file name" of a socket or pipe? Of a file created
                >by tmpnam() ? Of stdin? If all the calling program hands you is
                >the open file, then -which- of the several possible names for it
                >are you going to find for the file, in a system which has true
                >symbolic links? In a system which has Alternate Data Streams
                >(ADS) that can be programs that have effects similar to symbolic
                >links or to loop-back mounts?
                >
                The file name is the first parameter of the fopen call. No matter
                what that is. That will be returned. Easy isn't it?
                >
                fopen remembers the file name, and that is what you get
                Nope. While the file was open, another process deleted the same
                file and opened a new file under the same name. Now any fopens
                will access the new file. However, the old file is still open,
                usable, and nameless. Under Linux/Unix/Posix.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.


                ** Posted from http://www.teranews.com **

                Comment

                • Serve Lau

                  #38
                  Re: Getting the file name from a FILE *


                  "jacob navia" <jacob@nospam.c omschreef in bericht
                  news:g3ghbs$j5j $1@aioe.org...
                  What problems could arise with this function? Why is not in the C API?
                  I wouldn't put this in libc, it's something people can add themselves easily
                  when they need it.

                  But anyway, how will you store the strings? You cant store pointers so you
                  have to go for string copying. But how large will you make the arrays you
                  will copy the names into? And will you provide a wide char version too for
                  consistency?

                  Comment

                  • Richard Tobin

                    #39
                    Re: Getting the file name from a FILE *

                    In article <cc5e9$485d14c1 $541fc2ec$13064 @cache2.tilbu1. nb.home.nl>,
                    Serve Lau <nihao@qinqin.c omwrote:
                    >But anyway, how will you store the strings? You cant store pointers so you
                    >have to go for string copying. But how large will you make the arrays you
                    >will copy the names into?
                    How about the right length? Are you suggesting that there's some
                    reason why fopen() shouldn't malloc() the space?

                    -- Richard
                    --
                    In the selection of the two characters immediately succeeding the numeral 9,
                    consideration shall be given to their replacement by the graphics 10 and 11 to
                    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                    Comment

                    • Serve Lau

                      #40
                      Re: Getting the file name from a FILE *


                      "Richard Tobin" <richard@cogsci .ed.ac.ukschree f in bericht
                      news:g3j9aq$14p a$1@pc-news.cogsci.ed. ac.uk...
                      In article <cc5e9$485d14c1 $541fc2ec$13064 @cache2.tilbu1. nb.home.nl>,
                      Serve Lau <nihao@qinqin.c omwrote:
                      >
                      >>But anyway, how will you store the strings? You cant store pointers so you
                      >>have to go for string copying. But how large will you make the arrays you
                      >>will copy the names into?
                      >
                      How about the right length? Are you suggesting that there's some
                      reason why fopen() shouldn't malloc() the space?
                      That will require more knowledge of internal workings of standard C
                      functions which is IMO never a good thing.


                      Comment

                      • Tor Rustad

                        #41
                        Re: Getting the file name from a FILE *

                        jacob navia skrev:
                        Tor Rustad wrote:
                        >jacob navia wrote:
                        [...]
                        >>Questions:
                        >>>
                        >>What would be the best name for this function?
                        >>
                        >_fname
                        >>
                        >>
                        >
                        Yes, it is better with the underscore
                        It avoid polluting the name space.

                        I have often used 'fname' as an identifier in my own projects, and
                        wouldn't take it lightly if the compiler I was using, didn't accept
                        strictly conforming code because of such a thing.

                        >>What problems could arise with this function?
                        >>
                        >Non-portable code.
                        >>
                        >
                        That is not a problem with the function.
                        From the point of a user, unless we talk about throw away programs,
                        it's a major problem if code can only be compiled with one compiler. In
                        lcc-win case, who knows how long that particular compiler will be supported?


                        When I write non-portable code, it does something useful. Typically, the
                        problem at hand, can't be solved via portable methods. I suspect you are
                        wasting your time, by implementing this feature.

                        --
                        Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>

                        Comment

                        • Richard Tobin

                          #42
                          Re: Getting the file name from a FILE *

                          In article <9ceb$485d2ad8$ 541fc2ec$12152@ cache2.tilbu1.n b.home.nl>,
                          >>>But anyway, how will you store the strings? You cant store pointers so you
                          >>>have to go for string copying. But how large will you make the arrays you
                          >>>will copy the names into?
                          >How about the right length? Are you suggesting that there's some
                          >reason why fopen() shouldn't malloc() the space?
                          >That will require more knowledge of internal workings of standard C
                          >functions which is IMO never a good thing.
                          I'm baffled. Aren't we talking about Jacob's implementation?
                          He's the implementor of the functions, he's supposed to know
                          about their internal workings.

                          -- Richard
                          --
                          In the selection of the two characters immediately succeeding the numeral 9,
                          consideration shall be given to their replacement by the graphics 10 and 11 to
                          facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                          Comment

                          • jacob navia

                            #43
                            Re: Getting the file name from a FILE *

                            Serve Lau wrote:
                            >
                            But anyway, how will you store the strings? You cant store pointers so
                            you have to go for string copying. But how large will you make the
                            arrays you will copy the names into? And will you provide a wide char
                            version too for consistency?
                            >
                            I *can* store pointers.

                            fopen:

                            File->FileName = strdup(fname);

                            fclose:
                            free(File->FileName);



                            --
                            jacob navia
                            jacob at jacob point remcomp point fr
                            logiciels/informatique

                            Comment

                            • Ralf Damaschke

                              #44
                              Re: Getting the file name from a FILE *

                              Serve Lau wrote:
                              "Richard Tobin" <richard@cogsci .ed.ac.ukschree f in bericht
                              >How about the right length? Are you suggesting that there's some
                              >reason why fopen() shouldn't malloc() the space?
                              >
                              That will require more knowledge of internal workings of standard C
                              functions which is IMO never a good thing.
                              What knowledge of internal workings am I required to know for writing:
                              #include <stdlib.h>
                              char *s = NULL;
                              void foo(char *bar) {if (s = malloc(strlen(b ar)+1)) strcpy(s, bar);}

                              Besides, the implementor of fopen() really should have knowledge of
                              the internal working of standard C functions like fgetc, fwrite, etc.

                              Ralf

                              Comment

                              • Walter Roberson

                                #45
                                Re: Getting the file name from a FILE *

                                In article <g3j9aq$14pa$1@ pc-news.cogsci.ed. ac.uk>,
                                Richard Tobin <richard@cogsci .ed.ac.ukwrote:
                                >In article <cc5e9$485d14c1 $541fc2ec$13064 @cache2.tilbu1. nb.home.nl>,
                                >Serve Lau <nihao@qinqin.c omwrote:
                                >>But anyway, how will you store the strings? You cant store pointers so you
                                >>have to go for string copying. But how large will you make the arrays you
                                >>will copy the names into?
                                >How about the right length? Are you suggesting that there's some
                                >reason why fopen() shouldn't malloc() the space?
                                I do not have my C89 hardcopy here to check against. I know that there
                                are various routines that are marked with wording similar to
                                "The implementation shall behave as if no library routine
                                calls <name>". I do not recall if malloc() is one of those routines ?
                                --
                                "They called it golf because all the other four letter words
                                were taken." -- Walter Hagen

                                Comment

                                Working...