From 383e81a765ff7de551ca668f511bda3d5c7d5b0b Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 1 Apr 2011 00:54:40 +0000 Subject: [PATCH] Added ps --- Makefile | 1 + src/.cvsignore | 1 + src/Makefile | 17 +++++++-- src/project.conf | 7 +++- src/ps.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/utils.c | 16 +++++++++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/ps.c diff --git a/Makefile b/Makefile index a3f2b36..9dc42e7 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ dist: $(PACKAGE)-$(VERSION)/src/nice.c \ $(PACKAGE)-$(VERSION)/src/pr.c \ $(PACKAGE)-$(VERSION)/src/printf.c \ + $(PACKAGE)-$(VERSION)/src/ps.c \ $(PACKAGE)-$(VERSION)/src/pwd.c \ $(PACKAGE)-$(VERSION)/src/renice.c \ $(PACKAGE)-$(VERSION)/src/rm.c \ diff --git a/src/.cvsignore b/src/.cvsignore index 0a431dc..6f6f163 100644 --- a/src/.cvsignore +++ b/src/.cvsignore @@ -29,6 +29,7 @@ mv nice pr printf +ps pwd renice rm diff --git a/src/Makefile b/src/Makefile index 6c8fe84..75625b1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -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 more 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 more mv nice pr printf ps pwd renice rm rmdir sleep strings tail test time touch true tty uname uniq unlink wc who PREFIX = /usr/local DESTDIR = BINDIR = $(PREFIX)/bin @@ -225,6 +225,13 @@ printf_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) printf: $(printf_OBJS) $(CC) -o printf $(printf_OBJS) $(printf_LDFLAGS) +ps_OBJS = ps.o +ps_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +ps_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +ps: $(ps_OBJS) + $(CC) -o ps $(ps_OBJS) $(ps_LDFLAGS) + pwd_OBJS = pwd.o pwd_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) pwd_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) @@ -434,6 +441,9 @@ pr.o: pr.c printf.o: printf.c $(CC) $(printf_CFLAGS) -c printf.c +ps.o: ps.c + $(CC) $(ps_CFLAGS) -c ps.c + pwd.o: pwd.c $(CC) $(pwd_CFLAGS) -c pwd.c @@ -486,7 +496,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) $(find_OBJS) $(head_OBJS) $(id_OBJS) $(kill_OBJS) $(link_OBJS) $(ln_OBJS) $(locale_OBJS) $(logname_OBJS) $(ls_OBJS) $(mkdir_OBJS) $(mkfifo_OBJS) $(more_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) $(more_OBJS) $(mv_OBJS) $(nice_OBJS) $(pr_OBJS) $(printf_OBJS) $(ps_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) @@ -553,6 +563,8 @@ install: $(TARGETS) $(MKDIR) $(DESTDIR)$(BINDIR) $(INSTALL) -m 0755 -- printf $(DESTDIR)$(BINDIR)/printf $(MKDIR) $(DESTDIR)$(BINDIR) + $(INSTALL) -m 0755 -- ps $(DESTDIR)$(BINDIR)/ps + $(MKDIR) $(DESTDIR)$(BINDIR) $(INSTALL) -m 0755 -- pwd $(DESTDIR)$(BINDIR)/pwd $(MKDIR) $(DESTDIR)$(BINDIR) $(INSTALL) -m 0755 -- renice $(DESTDIR)$(BINDIR)/renice @@ -618,6 +630,7 @@ uninstall: $(RM) -- $(DESTDIR)$(BINDIR)/nice $(RM) -- $(DESTDIR)$(BINDIR)/pr $(RM) -- $(DESTDIR)$(BINDIR)/printf + $(RM) -- $(DESTDIR)$(BINDIR)/ps $(RM) -- $(DESTDIR)$(BINDIR)/pwd $(RM) -- $(DESTDIR)$(BINDIR)/renice $(RM) -- $(DESTDIR)$(BINDIR)/rm diff --git a/src/project.conf b/src/project.conf index 175a948..3b29c9b 100644 --- a/src/project.conf +++ b/src/project.conf @@ -1,4 +1,4 @@ -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,more,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,more,mv,nice,pr,printf,ps,pwd,renice,rm,rmdir,sleep,strings,tail,test,time,touch,true,tty,uname,uniq,unlink,wc,who cflags_force=-W cflags=-Wall -g -O2 -pedantic dist=Makefile,common.c @@ -163,6 +163,11 @@ type=binary sources=printf.c install=$(BINDIR) +[ps] +type=binary +sources=ps.c +install=$(BINDIR) + [pwd] type=binary sources=pwd.c diff --git a/src/ps.c b/src/ps.c new file mode 100644 index 0000000..2dfa714 --- /dev/null +++ b/src/ps.c @@ -0,0 +1,93 @@ +/* $Id$ */ +/* Copyright (c) 2011 Pierre Pronchery */ +/* This file is part of DeforaOS Unix utils */ +/* 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, version 3 of the License. + * + * This program 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include +#include +#include + + +/* ps */ +static int _ps_error(char const * message, int ret); + +static int _ps(void) +{ + char const proc[] = "/proc"; + char buf[256]; + DIR * dir; + struct dirent * de; + long pid; + char * p; + FILE * fp; + size_t s; + + if((dir = opendir(proc)) == NULL) + return -_ps_error(proc, 1); + printf("%5s %s\n", "PID", "CMD"); + while((de = readdir(dir)) != NULL) + { + pid = strtol(de->d_name, &p, 10); + if(de->d_name[0] == '\0' || *p != '\0' || pid < 0) + continue; + snprintf(buf, sizeof(buf), "%s/%s/%s", proc, de->d_name, + "cmdline"); + if((fp = fopen(buf, "r")) != NULL) + { + s = fread(buf, sizeof(*buf), sizeof(buf) - 1, fp); + fclose(fp); + buf[s] = '\0'; + } + else + buf[0] = '\0'; + printf("%5lu %s\n", pid, buf); + } + closedir(dir); + return 0; +} + +static int _ps_error(char const * message, int ret) +{ + fputs("ps: ", stderr); + perror(message); + return ret; +} + + +/* usage */ +static int _usage(void) +{ + fputs("Usage: ps\n", stderr); + return 1; +} + + +/* main */ +int main(int argc, char * argv[]) +{ + int o; + + while((o = getopt(argc, argv, "")) != -1) + switch(o) + { + default: + return _usage(); + } + if(optind != argc) + return _usage(); + return (_ps() == 0) ? 0 : 2; +} diff --git a/tools/utils.c b/tools/utils.c index db08829..ceb4c25 100644 --- a/tools/utils.c +++ b/tools/utils.c @@ -468,6 +468,21 @@ #undef _prefs_parse #undef _printf +/* ps.c */ +#define main _ps_main +#define _usage _ps_usage +#define _Prefs _ps_Prefs +#define Prefs ps_Prefs +#define _prefs_parse _ps_prefs_parse +#define _ps __ps +#include "../src/ps.c" +#undef main +#undef _usage +#undef _Prefs +#undef Prefs +#undef _prefs_parse +#undef _ps + /* pwd.c */ #define main _pwd_main #define _usage _pwd_usage @@ -757,6 +772,7 @@ Call calls[] = { "nice", _nice_main }, { "pr", _pr_main }, { "printf", _printf_main }, + { "ps", _ps_main }, { "pwd", _pwd_main }, { "renice", _renice_main }, { "rm", _rm_main },