Unix Programming - LS command in C

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > LS command in C





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author LS command in C
Ondras

2004-04-20, 1:34 pm

Do you know how to make 'ls' command in C without using functions from
standart libraries? I can use only printf, scanf and functions
implemented in man getpwnam and man getgrnam.
Thanx a lot.
Måns Rullgård

2004-04-20, 1:34 pm

ondrejhavlak@centrum.cz (Ondras) writes:

> Do you know how to make 'ls' command in C without using functions from
> standart libraries? I can use only printf, scanf and functions
> implemented in man getpwnam and man getgrnam.


That's impossible. You need at least opendir() and readdir() also,
unless you're allowed to use scandir(). Are you sure you read the
assignment properly?

--
Måns Rullgård
mru@kth.se
Billy N. Patton

2004-04-20, 1:34 pm

Needs to be converted to return char**, but this is what I use.

#include <dirent.h>
#include <regexpr.h>
#include <stdio.h>
#include <stdlib.h>

HashTable* ListDirectory(String& in,char* regx)
{
DIR *dirp;
struct dirent *dp;
HashTable* files;
char *expbuf=NULL;

if (in.IsEmpty())
{
lERROR("Received null pointer. Expected char*.");
return NULL;
}
files = new HashTable;
dirp = opendir(in.Get());
if (dirp EQ NULL)
{
lERROR_A("Unable to open directory \"%s\".",in.Get());
return NULL;
}
while ( (dp = readdir( dirp )) != NULL )
{
if (regx EQ NULL) files->Put(dp->d_name,(void*)NULL);
else
if ((expbuf = compile(regx,(char*)0,(char*)0)))
if (step(dp->d_name,expbuf)) files->Put(dp->d_name,(void*)NULL);
free(expbuf);
}
closedir( dirp );
return files;
}
/*
main (void)
{
int i;
list_directory(".",&i,".*\\.c$");
}
*/


Ondras wrote:
> Do you know how to make 'ls' command in C without using functions from
> standart libraries? I can use only printf, scanf and functions
> implemented in man getpwnam and man getgrnam.
> Thanx a lot.



--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com

Billy N. Patton

2004-04-20, 3:35 pm

dirp = opendir(in.Get()); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (dirp EQ NULL)
{
lERROR_A("Unable to open directory \"%s\".",in.Get());
return NULL;
}
while ( (dp = readdir( dirp )) != NULL ) <<<<<<<<<<<<<<<<<<<<<<<<<<<<

Måns Rullgård wrote:
> ondrejhavlak@centrum.cz (Ondras) writes:
>
>
>
>
> That's impossible. You need at least opendir() and readdir() also,
> unless you're allowed to use scandir(). Are you sure you read the
> assignment properly?
>



--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com

Billy N. Patton

2004-04-20, 3:35 pm

Guess I got taken in by a student. Didn't think this was an assignment
for someone. I sometimes ask for small functions that someone else has
already done just to keep from writing them. Spend more time on hte
problem I'm trying to solve that writeing tools to solve them.


Måns Rullgård wrote:
> ondrejhavlak@centrum.cz (Ondras) writes:
>
>
>
>
> That's impossible. You need at least opendir() and readdir() also,
> unless you're allowed to use scandir(). Are you sure you read the
> assignment properly?
>



--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com