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

📄 ls.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    int                 i;
    char                wild[_MAX_PATH];
    char                *err;
    void                *crx;
    size_t              fname_len;
    size_t              max_fname_len = 0;

    /*
     * initialize for file scan
     */
    if( path != NULL ) {
        strcpy( filename, path );
        if( !isFILESEP( path[ strlen( path ) - 1 ] ) ) {
            strcat( filename, FILESEPSTR );
        }
        strcat( filename, name );
    } else {
        strcpy( filename, name );
        if( IsSpecialRoot( filename ) ) {
            strcat( filename, "\\" );
        }
    }
    _splitpath2( filename, filebuff, &drive, &dir, NULL, NULL );

    if( lflag ) {
        i = 0;
        if( filename[1] == ':' ) {
            i = tolower( filename[0] ) - 'a' + 1;
        }
        clsize = GetClusterSize( i );
    }

    if( rxflag ) {
        directory = OpenDirAll( filename, wild );
    } else {
        directory = opendir( filename );
    }


    if( directory == NULL ) {
        printf( "File (%s) not found.\n", filename );
        return;
    }

    /*
     * get all directory entries
     */
    if( rxflag ) {
        err = FileMatchInit( &crx, wild );
        if( err != NULL ) {
            Die( "\"%s\": %s\n", err, wild );
        }
    }
    while( ( nextdirentry = readdir( directory ) ) != NULL ) {

        FNameLower( nextdirentry->d_name );
        if( rxflag ) {
            if( !FileMatch( crx, nextdirentry->d_name ) ) {
                continue;
            }
        }
        if( hflag ) {
            if( nextdirentry->d_attr & (_A_HIDDEN|_A_SYSTEM) ) {
                continue;
            }
        }
        if( !( ( nextdirentry->d_attr & _A_SUBDIR )
             && IsDotOrDotDot( nextdirentry->d_name ) ) ) {
            files = realloc( files, ( filecnt+1 )*sizeof( struct dirent * ) );
            if( files == NULL ) {
                printf( "Out of memory!\n" );
                exit( 1 );
            }
            files[ filecnt ] = malloc( sizeof( struct dirent ) );
            if( files[filecnt] == NULL ) {
                break;
            }
            fname_len = strlen( nextdirentry->d_name );
            if( fname_len > max_fname_len ) {
                max_fname_len = fname_len;
            }
            memcpy( files[filecnt++], nextdirentry, sizeof( struct dirent ) );
        }

    }
    closedir( directory );
    if( rxflag ) {
        FileMatchFini( crx );
    }

    /*
     * now display files, if there are any
     */
    if( filecnt ) {

        /*
         * sort the data
         */
        if( rflag ) {
            if( tflag ) {
                fn = CompareDateReverse;
            } else if( sflag ) {
                fn = CompareSizeReverse;
            } else {
                fn = CompareReverse;
            }
        } else {
            if( tflag ) {
                fn = CompareDate;
            } else if( sflag ) {
                fn = CompareSize;
            } else {
                fn = Compare;
            }
        }
        qsort( files, filecnt, sizeof(struct dirent *), (int (*)(const void *, const void * ))fn );

        /*
         * print out results
         */
        if( lflag ) {
            long size = 0L;
            long sum = 0L;
            for( i = 0 ; i < filecnt ; i++ ) {
                file = files[i];
                size = file->d_size / clsize;
                if( (file->d_size % clsize != 0L)
                  ||(file->d_size == 0L) ) {
                    size++;
                }
                sum += (size * clsize) / 1024L;
            }
            printf( "total %ld\n", sum );
        }
        columnwidth = max_fname_len + GUTTER_WIDTH;
        maxfileperline = line_width / columnwidth;
        fileperlinecnt = 0;
        for( i = 0 ; i < filecnt ; i++ ) {
            PrintFile( drive, dir, files[i] );
        }
        if( fileperlinecnt != 0 ) {
            printf( "\n" );
        }

        if( Rflag ) {
            for( i = 0 ; i < filecnt ; i++ ) {
                if( files[i]->d_attr & _A_SUBDIR ) {
                    _makepath( filename, drive, dir, files[i]->d_name, NULL );
                    printf( "\n%s:\n", filename );
                    DoLS( filename, name );
                }
                free( files[i] );
            }
        }
        free( files );

    }

} /* DoLS */

/*
 * PrintFile - print a file entry
 */
void PrintFile( char *drive, char *dir, DIR *file )
{
    static char months[13][4] = {
        "***", "Jan", "Feb", "Mar", "Apr",
        "May", "Jun", "Jul", "Aug", "Sep",
        "Oct", "Nov", "Dec"
    };
    char name[_MAX_PATH + 1];
    char buff[80 + _MAX_PATH];
    long size;
    size_t      len;
    int         extra_char;

    strcpy( name, file->d_name );
    len = strlen( name );

    extra_char = 0;
    if( Fflag ) {
        if( file->d_attr & _A_SUBDIR ) {
            name[len] = '/';
            ++len;
            name[len] = 0;
            ++extra_char;
        } else if( IsX( file->d_name ) ) {
            name[len] = '*';
            ++len;
            name[len] = 0;
            ++extra_char;
        }
    }

    if( !lflag ) {

        if( !N1flag ) {
            printf( "%-*s", extra_char + columnwidth - GUTTER_WIDTH, name );
            if( ++fileperlinecnt == maxfileperline ) {
                printf( "\n" );
                fileperlinecnt = 0;
            } else {
                printf( "%-*s", GUTTER_WIDTH - extra_char, "" );
            }
        } else {
            if( pflag ) {
                if( drive != NULL && drive[0] != '\0' ) {
                    printf( "%s",  drive );
                }
                if( dir != NULL && dir[0] != '\0' ) {
                    printf( "%s", dir );
                }
            }
            printf( "%s\n", name );
        }

    } else {

        /*
         * build attributes
         */
        strcpy( buff, "-------" );
        size = file->d_size;
        if( file->d_attr & _A_SUBDIR ) {
            buff[0] = 'd';
            size = 0;
        }
        if( file->d_attr & _A_ARCH ) {
            buff[1] = 'a';
        }
        if( file->d_attr & _A_HIDDEN ) {
            buff[2] = 'h';
        }
        if( file->d_attr & _A_SYSTEM ) {
            buff[3] = 's';
        }
        buff[4] = 'r';
        if( !(file->d_attr & _A_RDONLY ) ) {
            buff[5] = 'w';
        }
        if( IsX( file->d_name ) ) {
            buff[6] = 'x';
        }

        printf( "%s %10ld  %3s %2d %d  %2d:%02d  ", buff, size,
                months[(int)((dosdate *)&file->d_date)->month],
                (int)((dosdate *)&file->d_date)->day,
                (int)((dosdate *)&file->d_date)->year+1980,
                (int)((dostime *)&file->d_time)->hour,
                (int)((dostime *)&file->d_time)->min );

        if( pflag ) {
            if( drive != NULL && drive[0] != '\0' ) {
                printf( "%s", drive );
            }
            if( dir != NULL && dir[0] != '\0' ) {
                printf( "%s", dir );
            }
        }
        printf( "%s\n", name );

    }

} /* PrintFile */

/*
 * IsX - check if a file has .com, .bat, or .exe at the end
 */
int IsX( char *file )
{
    char        *f;

    f = file + strlen( file ) - 4;
    if( f < file ) return( FALSE );
    if( !FNameCompare( f, ".com" )
      ||!FNameCompare( f, ".bat" )
      ||!FNameCompare( f, ".exe" )
#if defined( __OS_os2386__ ) || defined( __OS_dosos2__ )
      ||!FNameCompare( f, ".cmd" )
#endif
       ) {
        return( TRUE );
    }
    return( FALSE );

} /* IsX */

int IsSpecialRoot( char * filename )
/**********************************/
// Check if 'filename' is of the form 'd:'
{
    char                filebuff[_MAX_PATH2];
    char                *drive;
    char                *dir;

    _splitpath2( filename, filebuff, &drive, &dir, NULL, NULL );
    return( drive[0] != '\0' && dir[0] == '\0' );
} /* IsSpecialRoot */

⌨️ 快捷键说明

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