Added target find

This commit is contained in:
Pierre Pronchery 2007-09-09 15:59:26 +00:00
parent f136e46342
commit dceb83a246
4 changed files with 158 additions and 3 deletions

View File

@ -35,6 +35,7 @@ dist:
$(PACKAGE)-$(VERSION)/src/echo.c \
$(PACKAGE)-$(VERSION)/src/false.c \
$(PACKAGE)-$(VERSION)/src/file.c \
$(PACKAGE)-$(VERSION)/src/find.c \
$(PACKAGE)-$(VERSION)/src/head.c \
$(PACKAGE)-$(VERSION)/src/id.c \
$(PACKAGE)-$(VERSION)/src/kill.c \

View File

@ -1,4 +1,4 @@
TARGETS = basename cat chgrp chmod chown cksum cmp cp df dirname du echo false file head id kill link ln locale logname ls mkdir mkfifo mv nice pr printf pwd renice rm rmdir sleep strings tail test time touch true tty uname uniq unlink wc who
TARGETS = basename cat chgrp chmod chown cksum cmp cp df dirname du echo false file find head id kill link ln locale logname ls mkdir mkfifo mv nice pr printf pwd renice rm rmdir sleep strings tail test time touch true tty uname uniq unlink wc who
PREFIX = /usr/local
DESTDIR =
BINDIR = $(PREFIX)/bin
@ -98,6 +98,12 @@ file_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
file: $(file_OBJS)
$(CC) -o file $(file_OBJS) $(LDFLAGSF) $(LDFLAGS)
find_OBJS = find.o
find_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
find: $(find_OBJS)
$(CC) -o find $(find_OBJS) $(LDFLAGSF) $(LDFLAGS)
head_OBJS = head.o
head_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
@ -326,6 +332,9 @@ false.o: false.c
file.o: file.c
$(CC) $(file_CFLAGS) -c file.c
find.o: find.c
$(CC) $(find_CFLAGS) -c find.c
head.o: head.c
$(CC) $(head_CFLAGS) -c head.c
@ -420,7 +429,7 @@ who.o: who.c
$(CC) $(who_CFLAGS) -c who.c
clean:
$(RM) $(basename_OBJS) $(cat_OBJS) $(chgrp_OBJS) $(chmod_OBJS) $(chown_OBJS) $(cksum_OBJS) $(cmp_OBJS) $(cp_OBJS) $(df_OBJS) $(dirname_OBJS) $(du_OBJS) $(echo_OBJS) $(false_OBJS) $(file_OBJS) $(head_OBJS) $(id_OBJS) $(kill_OBJS) $(link_OBJS) $(ln_OBJS) $(locale_OBJS) $(logname_OBJS) $(ls_OBJS) $(mkdir_OBJS) $(mkfifo_OBJS) $(mv_OBJS) $(nice_OBJS) $(pr_OBJS) $(printf_OBJS) $(pwd_OBJS) $(renice_OBJS) $(rm_OBJS) $(rmdir_OBJS) $(sleep_OBJS) $(strings_OBJS) $(tail_OBJS) $(test_OBJS) $(time_OBJS) $(touch_OBJS) $(true_OBJS) $(tty_OBJS) $(uname_OBJS) $(uniq_OBJS) $(unlink_OBJS) $(wc_OBJS) $(who_OBJS)
$(RM) $(basename_OBJS) $(cat_OBJS) $(chgrp_OBJS) $(chmod_OBJS) $(chown_OBJS) $(cksum_OBJS) $(cmp_OBJS) $(cp_OBJS) $(df_OBJS) $(dirname_OBJS) $(du_OBJS) $(echo_OBJS) $(false_OBJS) $(file_OBJS) $(find_OBJS) $(head_OBJS) $(id_OBJS) $(kill_OBJS) $(link_OBJS) $(ln_OBJS) $(locale_OBJS) $(logname_OBJS) $(ls_OBJS) $(mkdir_OBJS) $(mkfifo_OBJS) $(mv_OBJS) $(nice_OBJS) $(pr_OBJS) $(printf_OBJS) $(pwd_OBJS) $(renice_OBJS) $(rm_OBJS) $(rmdir_OBJS) $(sleep_OBJS) $(strings_OBJS) $(tail_OBJS) $(test_OBJS) $(time_OBJS) $(touch_OBJS) $(true_OBJS) $(tty_OBJS) $(uname_OBJS) $(uniq_OBJS) $(unlink_OBJS) $(wc_OBJS) $(who_OBJS)
distclean: clean
$(RM) $(TARGETS)
@ -441,6 +450,7 @@ install: all
$(INSTALL) -m 0755 echo $(DESTDIR)$(BINDIR)/echo
$(INSTALL) -m 0755 false $(DESTDIR)$(BINDIR)/false
$(INSTALL) -m 0755 file $(DESTDIR)$(BINDIR)/file
$(INSTALL) -m 0755 find $(DESTDIR)$(BINDIR)/find
$(INSTALL) -m 0755 head $(DESTDIR)$(BINDIR)/head
$(INSTALL) -m 0755 id $(DESTDIR)$(BINDIR)/id
$(INSTALL) -m 0755 kill $(DESTDIR)$(BINDIR)/kill
@ -488,6 +498,7 @@ uninstall:
$(RM) $(DESTDIR)$(BINDIR)/echo
$(RM) $(DESTDIR)$(BINDIR)/false
$(RM) $(DESTDIR)$(BINDIR)/file
$(RM) $(DESTDIR)$(BINDIR)/find
$(RM) $(DESTDIR)$(BINDIR)/head
$(RM) $(DESTDIR)$(BINDIR)/id
$(RM) $(DESTDIR)$(BINDIR)/kill

139
src/find.c Normal file
View File

@ -0,0 +1,139 @@
/* $Id$ */
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Unix utils */
/* utils is not free software; you can redistribute it and/or modify it under
* the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
* Unported as published by the Creative Commons organization.
*
* utils is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
* ShareAlike 3.0 Unported license for more details.
*
* You should have received a copy of the Creative Commons Attribution-
* NonCommercial-ShareAlike 3.0 along with utils; if not, browse to
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* types */
typedef int Prefs;
#define PREFS_H 1
#define PREFS_L 2
/* find */
static int _find_error(char const * message, int ret);
static int _find_do(Prefs * prefs, char const * pathname);
static int _find(Prefs * prefs, int argc, char * argv[])
{
int ret = 0;
int i;
for(i = 0; i < argc; i++)
ret |= _find_do(prefs, argv[i]);
return ret;
}
static int _find_error(char const * message, int ret)
{
fputs("find: ", stderr);
perror(message);
return ret;
}
/* find_do */
static int _do_dir(Prefs * prefs, char const * pathname);
static int _find_do(Prefs * prefs, char const * pathname)
{
struct stat st;
if(lstat(pathname, &st) != 0) /* XXX TOCTOU */
return _find_error(pathname, 1);
if(S_ISDIR(st.st_mode))
return _do_dir(prefs, pathname);
printf("%s\n", pathname);
return 0;
}
static int _do_dir(Prefs * prefs, char const * pathname)
{
int ret = 0;
DIR * dir;
struct dirent * de;
size_t len = strlen(pathname) + 2;
char * path;
char * p;
if((dir = opendir(pathname)) == NULL)
return _find_error(pathname, 1);
if((path = malloc(len)) == NULL)
{
closedir(dir);
return _find_error(pathname, 1);
}
sprintf(path, "%s/", pathname);
while((de = readdir(dir)) != NULL)
{
if(de->d_name[0] == '.' && (de->d_name[1] == '\0'
|| (de->d_name[1] == '.'
&& de->d_name[2] == '\0')))
continue;
if((p = realloc(path, len + strlen(de->d_name))) == NULL)
break;
path = p;
strcpy(&path[len - 1], de->d_name);
ret |= _find_do(prefs, path);
}
free(path);
if(de != NULL)
ret |= _find_error(path, 1);
if(closedir(dir) != 0)
ret |= _find_error(path, 1);
return ret;
}
/* usage */
static int _usage(void)
{
fputs("Usage: find\n", stderr);
return 1;
}
/* main */
int main(int argc, char * argv[])
{
int o;
Prefs prefs;
memset(&prefs, 0, sizeof(prefs));
while((o = getopt(argc, argv, "HL")) != -1)
switch(o)
{
case 'H':
prefs &= ~PREFS_L;
prefs |= PREFS_H;
break;
case 'L':
prefs &= ~PREFS_H;
prefs |= PREFS_L;
break;
default:
return _usage();
}
if(argc - optind == 0)
return _usage();
return _find(&prefs, argc - optind, &argv[optind]) == 0 ? 0 : 2;
}

View File

@ -1,4 +1,4 @@
targets=basename,cat,chgrp,chmod,chown,cksum,cmp,cp,df,dirname,du,echo,false,file,head,id,kill,link,ln,locale,logname,ls,mkdir,mkfifo,mv,nice,pr,printf,pwd,renice,rm,rmdir,sleep,strings,tail,test,time,touch,true,tty,uname,uniq,unlink,wc,who
targets=basename,cat,chgrp,chmod,chown,cksum,cmp,cp,df,dirname,du,echo,false,file,find,head,id,kill,link,ln,locale,logname,ls,mkdir,mkfifo,mv,nice,pr,printf,pwd,renice,rm,rmdir,sleep,strings,tail,test,time,touch,true,tty,uname,uniq,unlink,wc,who
cflags_force=-W
cflags=-Wall -g -O2 -ansi
dist=Makefile,common.c
@ -60,6 +60,10 @@ sources=false.c
type=binary
sources=file.c
[find]
type=binary
sources=find.c
[head]
type=binary
sources=head.c