Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!decwrl!deccrl!news.crl.dec.com!shlump.nac.dec.com!decuac!hussar.dco.dec.com!mjr From: mjr@hussar.dco.dec.com (Marcus J. Ranum) Newsgroups: comp.unix.programmer Subject: Re: Unix binary/text files: is there a difference? Message-ID: <1991Mar20.215527.11585@decuac.dec.com> Date: 20 Mar 91 21:55:27 GMT References: <77384@bu.edu.bu.edu> Distribution: usa Organization: Digital Equipment Corp., Washington Ultrix Resource Center Lines: 26 jdubb@bucsf.bu.edu (jay dubb) writes: > I've looked in a bunch of C and Unix books, and can't seem to find >a good explanation of this - maybe someone can help... Is there a way >to tell (from a C program) whether a given file contains text or data? Lots of applications use a "magic number" scheme, whereby the first long int (just for example) in a file is some value, or some string. That's why we are stuck with stuff like: %%!PS-Adobe at the beginning of PostScript files, and so forth. It's a tricky problem. Other systems (not to name names) make arbitrary decisions based on naming conventions. If it ends in ".FOR" it is a FORTRAN program, etc. The short answer to your question is, "no." Whenever I have to tackle this kind of problem, I usually write a magic number in network byte order (see the man page on htonl()) at some known offset into the file, and scream loudly if it's not there. You can get arbitrarily insane trying to make sure that the file isn't one that someone accidentally picked out of a hat with that value. mjr. -- The world is just backing store for virtual reality.