/*----------------------------------------------------------------------------*\ * lm - http://www.incubus.co.uk - 14-08-2001 * * Hangman - Curses Based (Linux). * * Updated 27/11/2007 - Improved code and style. * cc list_dir.c -Wall -o list_dir ; ./list_dir ./ \*----------------------------------------------------------------------------*/ /****************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ******************************************************************************/ /*----------------------------------------------------------------------------*\ * System includes. \*----------------------------------------------------------------------------*/ #include #include #include #include #include #include #include /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ #define MAX 1024 /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ void list_dir (char *path); /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { if (--argc) list_dir (argv[1]); else { printf ("Missing argument - Directory to list\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ void list_dir (char *path) { struct dirent *entry; struct stat status; DIR *dir; int mode, type; char *name, buff[MAX], dirBuff[MAX]; if ((dir = opendir(path))) { if ( path[strlen(path)-1] == '/' ) path[strlen(path)-1] = 0; while ((entry = readdir(dir))) { name=entry->d_name; memset(buff,0,MAX); sprintf(buff, "%s/%s",path,name); if (lstat(buff,&status)!= -1) mode=status.st_mode; if (S_ISDIR(mode)) { memset(dirBuff,0,MAX); sprintf( dirBuff, "%s/%s", path,name ); if ( (strcmp(name,".")) && (strcmp(name,".."))) list_dir(dirBuff); continue; }else if (mode & (S_IXUSR|S_IXGRP|S_IXOTH)) type='X'; /*-- Executabe File ---------------------*/ else if (S_ISLNK(mode)) type='L'; /*-- Symbolic Link ----------------------*/ else if (S_ISBLK(mode)) type='B'; /*-- Block Special File -----------------*/ else if (S_ISCHR(mode)) type='C'; /*-- Character Special File -------------*/ else if (S_ISFIFO(mode)) type='F'; /*-- Pipe or FIFO special file ----------*/ else if (S_ISREG(mode)) type='R'; /*-- Regular File -----------------------*/ else type='-'; fprintf(stdout, "%c\t %s/%s\n", type, path, name); fflush(stdout); } closedir(dir); }else fprintf(stderr, "Could not open %s\n\r", path); }
© Copyright 1999-2008 @incubus. All Rights Reserved. All trademarks acknowledged. incubus.co.uk || zenithpaints.co.uk || rankinstine.co.uk