⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ls.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  POSIX ls utility.
*
****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <direct.h>
#include <io.h>
#include <dos.h>
#if defined(__OS_dosos2__) || defined(__OS_os2386__)
#include <os2.h>
#endif
#include "misc.h"
#include "getopt.h"
#include "filerx.h"
#include "fnutils.h"
#include "console.h"

#define LINE_WIDTH      80
#define GUTTER_WIDTH    2

char *OptEnvVar="ls";

typedef struct {
    unsigned    sec:5;
    unsigned    min:6;
    unsigned    hour:5;
} dostime;

typedef struct {
    unsigned    day:5;
    unsigned    month:4;
    unsigned    year:7;
} dosdate;

int  line_width;
long clsize;
int  fileperlinecnt;
int  maxfileperline;
size_t columnwidth;
int  N1flag=FALSE,
     Fflag=FALSE,
     pflag=FALSE,
     rxflag=FALSE,
     Rflag=FALSE,
     lflag=FALSE,
     hflag=FALSE,
     rflag=FALSE,
     sflag=FALSE,
     tflag=FALSE;

static const char *usageMsg[] = {
    "Usage: ls [-?1CFRlhprstX] [files]",
    "\tfiles       : directories/files to list",
    "\tOptions: -? : display this message",
    "\t\t -1 : single column display",
    "\t\t -C : multi-column display",
    "\t\t -F : display file type indicator (/ for dir, * for x)",
    "\t\t -R : recursive ls",
    "\t\t -l : print long info about each file",
    "\t\t -h : dont't include hidden and system files",
    "\t\t -p : display relative path for file",
    "\t\t -r : reverse sort",
    "\t\t -s : sort by size, rather than name (largest first)",
    "\t\t -t : sort by time, rather than name (most recent first)",
    "\t\t -X : match files by regular expressions",
    NULL
};

/*
 * start of mainline
 */
int main( int argc, char *argv[] )
{
    DIR         *d;
    char        filebuff[_MAX_PATH];
    char        **todo = NULL;
    int         todocnt = 0;
    int         i;
    int         ch;


#ifndef __QNX__
    line_width = GetConsoleWidth();
#else
    line_width = LINE_WIDTH;            /* for now */
#endif

    if( !isatty( fileno( stdout ) ) ) {
        N1flag = TRUE;
    }

    /*
     * process options
     */
    while( 1 ) {
        ch = GetOpt( &argc, argv, "XRphrtsFl1C", usageMsg );
        if( ch == -1 ) {
            break;
        }
        switch( ch ) {
        case 'R':
            Rflag=TRUE;
            break;
        case 'X':
            rxflag=TRUE;
            break;
        case 'p':
            N1flag=TRUE;
            pflag=TRUE;
            break;
        case 'r':
            rflag=TRUE;
            break;
        case 't':
            tflag=TRUE;
            break;
        case 's':
            sflag=TRUE;
            break;
        case 'F':
            Fflag=TRUE;
            break;
        case 'l':
            lflag=TRUE;
            break;
        case '1':
            N1flag=TRUE;
            break;
        case 'C':
            N1flag=FALSE;
            break;
        case 'h':
            hflag=TRUE;
            break;
        }
    }

    /*
     * if no files specified, list current directory
     */
    if( argc == 1 ) {
        if( rxflag ) {
            DoLS( ".", "*" );
        } else {
            DoLS( ".", "*.*" );
        }
        exit( 0 );
    }

    /*
     * process any directories specified
     */
    for( i = 1 ; i < argc ; i++ ) {
        if( FileNameWild( argv[i], rxflag ) ) {
            d = NULL;
        } else if( IsSpecialRoot( argv[i] ) ) {
            d = opendir( strcat( strcpy( filebuff, argv[i] ), "\\" ) );
        } else {
            d = opendir( argv[i] );
        }
        if( d != NULL && ( d->d_attr & _A_SUBDIR ) ) {
            printf( "%s:\n", argv[i] );
            if( rxflag ) {
                DoLS( argv[i], "*" );
            } else {
                DoLS( argv[i], "*.*" );
            }
            printf( "\n" );
        } else {
            todo = realloc( todo, (todocnt+1)*sizeof( char * ) );
            if( todo == NULL ) {
                printf( "Out of memory!\n" );
                exit( 1 );
            }
            todo[todocnt++] = argv[i];
        }
        if( d != NULL ) {
            closedir( d );
        }
    }

    /*
     * run through all specified files
     */
    for( i = 0 ; i < todocnt ; i++ ) {
        DoLS( NULL, todo[i] );
    }
    free( todo );
    return( 0 );
} /* main */

/*
 * Compare routines follow
 */
int Compare( struct dirent **p1, struct dirent **p2 )
{
    return( strcmp( (*p1)->d_name, (*p2)->d_name ) );
}
int CompareReverse( struct dirent **p1, struct dirent **p2 )
{
    return( strcmp( (*p2)->d_name, (*p1)->d_name ) );
}
int CompareDate( struct dirent **p1, struct dirent **p2 )
{
    if( (*p1)->d_date < (*p2)->d_date ) {
        return( 1 );
    }
    if( (*p1)->d_date > (*p2)->d_date ) {
        return( -1 );
    }
    if( (*p1)->d_time < (*p2)->d_time ) {
        return( 1 );
    }
    if( (*p1)->d_time > (*p2)->d_time ) {
        return( -1 );
    }
    return( Compare( p1, p2 ) );
}
int CompareDateReverse( struct dirent **p1, struct dirent **p2 )
{
    if( (*p1)->d_date > (*p2)->d_date ) {
        return( 1 );
    }
    if( (*p1)->d_date < (*p2)->d_date ) {
        return( -1 );
    }
    if( (*p1)->d_time > (*p2)->d_time ) {
        return( 1 );
    }
    if( (*p1)->d_time < (*p2)->d_time ) {
        return( -1 );
    }
    return( CompareReverse( p1, p2 ) );
}
int CompareSize( struct dirent **p1, struct dirent **p2 )
{
    if( (*p1)->d_size < (*p2)->d_size ) {
        return( 1 );
    }
    if( (*p1)->d_size > (*p2)->d_size ) {
        return( -1 );
    }
    return( Compare( p1, p2 ) );
}
int CompareSizeReverse( struct dirent **p1, struct dirent **p2 )
{
    if( (*p1)->d_size > (*p2)->d_size ) {
        return( 1 );
    }
    if( (*p1)->d_size < (*p2)->d_size ) {
        return( -1 );
    }
    return( CompareReverse( p1, p2 ) );
}

/*
 * DoLS - perform LS on a specified directory
 */
void DoLS( char *path, char *name )
{
    char                filename[_MAX_PATH];
    char                filebuff[_MAX_PATH2];
    char                *drive;
    char                *dir;
    int                 filecnt = 0;
    DIR                 *directory;
    struct dirent       **files = NULL;
    struct dirent       *nextdirentry;
    struct dirent       *file;
    int                 (*fn)(struct dirent **, struct dirent **);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -