UnixFilename.st
author Claus Gittinger <cg@exept.de>
Fri, 15 Jan 1999 21:53:59 +0100
changeset 3956 51f1a9a4d63f
parent 3664 7bcbf8c6c2cf
child 7939 3a8b75e8f804
permissions -rw-r--r--
changes for egcs (stdio uses __new / utsname)

"
 COPYRIGHT (c) 1997 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Filename subclass:#UnixFilename
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'OS-Unix'
!

!UnixFilename class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Filenames in Unix.
"

! !

!UnixFilename class methodsFor:'queries'!

isBadCharacter:aCharacter
    "return true, if aCharacter is unallowed in a filename."

    aCharacter == $/ ifTrue:[^ true].
    ^ super isBadCharacter:aCharacter

    "Created: 8.9.1997 / 00:13:14 / cg"
!

isCaseSensitive
    "return true, if filenames are case sensitive.return true, if filenames are case sensitive."

    ^ true
!

nullFilename
    "Return /dev/null"

    ^ '/dev/null'

    "
     Filename concreteClass nullFilename  
    "

    "Modified: / 14.1.1998 / 14:20:55 / stefan"
!

parentDirectoryName
    "return the name used for the parent directory.
     This is '..' for unix and dos-like systems.
     (there may be more in the future."

    ^ '..'
!

separator
    "return the file/directory separator."

     ^ $/

     "
      Filename concreteClass separator  
     "

    "Created: 8.9.1997 / 00:18:14 / cg"
! !

!UnixFilename methodsFor:'file queries'!

fileType
    "this returns a string describing the type of contents of
     the file. This is done using the unix 'file' command,
     (which usually is configurable by /etc/magic).
     Warning:
         Since the returned string differs among systems (and language settings),
         it is only useful for user-information; 
         NOT as a tag to be used by a program."

    |stream typeString|

    "/ since executing 'file' takes some time, do the most common
    "/ ones are checked first, using the general fileType implementation. 
    "/ (also, the file-command is only available on Unix systems.

    typeString := super fileType.
    typeString ~= 'file' ifTrue:[^ typeString].

    stream := PipeStream readingFrom:('file "' , self pathName , '"').
    stream notNil ifTrue:[
        typeString := stream contents asString.
        stream close.
        typeString := typeString copyFrom:(typeString indexOf:$:) + 1.
        typeString := typeString withoutSeparators
    ].
    ^ typeString

    "
     'Makefile' asFilename fileType 
     '.' asFilename fileType     
     '/dev/null' asFilename fileType        
     'smalltalk.rc' asFilename fileType    
     'bitmaps/SBrowser.xbm' asFilename fileType    
     '../../libtool/bitmaps/SBrowser.xbm' asFilename fileType    
    "

    "Modified: / 21.7.1998 / 11:26:32 / cg"
! !

!UnixFilename methodsFor:'queries'!

isExplicitRelative
    "return true, if this name is an explicit relative name
     (i.e. starts with './' or '../', to avoid path-prepending)"

    (nameString startsWith:'./') ifTrue:[
	^ true
    ].
    (nameString startsWith:'../') ifTrue:[
	^ true
    ].
    ^ false
!

isHidden
    "return true, if the receiver represents a hidden file.
     On UNIX, a name starting with a period is considered hidden."

    ^ self baseName startsWith:'.'
!

isImplicit
    "return true, if the receiver represents builtin names such as '.' or '..'.
     On UNIX, the current and parent dir names are considered implicit."

    |name|

    name := self baseName.
    ^ name = '.' or:[name = '..'].

    "Modified: / 18.9.1997 / 18:03:28 / stefan"
    "Modified: / 21.7.1998 / 11:10:42 / cg"
!

nameWithSpecialExpansions:aString
    "return the nameString, expanding any OS specific macros. 
     Here, a ~ prefix is expanded to the users home dir (as in csh)"

    |dir user cutIdx idx userInfo|

    (aString startsWith:'~') ifTrue:[
        dir := OperatingSystem getHomeDirectory.
        cutIdx := 2.
        "/ look for ~username
        (aString size > 1) ifTrue:[
            idx := aString indexOf:$/.
            idx == 0 ifTrue:[
                user := aString copyFrom:2.
                cutIdx := aString size + 1.
            ] ifFalse:[
                user := aString copyFrom:2 to:(idx - 1).
                cutIdx := idx.
            ].
            user size > 0 ifTrue:[
                userInfo := OperatingSystem userInfoOf:user.
                (userInfo notNil and:[userInfo includesKey:#dir]) ifTrue:[
                    dir := userInfo at:#dir
                ] ifFalse:[
                    dir := nil
                ]
            ].
            dir isNil ifTrue:[
"/                ('Filename [info]: unknown user: ' , user) infoPrintCR.
                ^ aString
            ].
        ].
        ^ dir , (aString copyFrom:cutIdx)
    ].
    ^ aString

    "
     UnixFilename new nameWithSpecialExpansions:'~'      
     UnixFilename new nameWithSpecialExpansions:'~/work'  
     UnixFilename new nameWithSpecialExpansions:'~sv'     
     UnixFilename new nameWithSpecialExpansions:'~sv/work' 
     UnixFilename new nameWithSpecialExpansions:'~foo'    
     UnixFilename new nameWithSpecialExpansions:'~foo/bar' 
    "

    "Modified: / 21.7.1998 / 11:09:59 / cg"
! !

!UnixFilename class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/UnixFilename.st,v 1.8 1998-07-21 14:26:50 cg Exp $'
! !