dslinux/user/fileutils COPYING COPYING.sash Makefile README WARRANTY cat.c chgrp.c chmod.c chown.c cmp.c cp.c dd.c futils.h grep.c l.c ln.c ls.c mkdir.c mkfifo.c mknod.c more.c mv.c rm.c rmdir.c sync.c touch.c
amadeus
dslinux_amadeus at user.in-berlin.de
Thu Aug 31 11:32:17 CEST 2006
Update of /cvsroot/dslinux/dslinux/user/fileutils
In directory antilope:/tmp/cvs-serv14346/user/fileutils
Added Files:
COPYING COPYING.sash Makefile README WARRANTY cat.c chgrp.c
chmod.c chown.c cmp.c cp.c dd.c futils.h grep.c l.c ln.c ls.c
mkdir.c mkfifo.c mknod.c more.c mv.c rm.c rmdir.c sync.c
touch.c
Log Message:
Add some more applications
--- NEW FILE: WARRANTY ---
THIS PACKAGES IS PROVIDED WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES OF ANY
KIND. IT IS ALPHA CODE TO BE USED FOR TESTING PURPOSES AND SHOULD NOT BE
USED FOR ANY PURPOSE WHICH COULD RESULT IN LOSS OF DATA.
--- NEW FILE: COPYING.sash ---
Portions of this code were extracted from the sash 2.1 program,
which has the following copyright:
Copyright (c) 1998 by David I. Bell
Permission is granted to use, distribute, or modify this source,
provided that this copyright notice remains intact.
This message is included in accordance with David Bell's wishes. These terms
are compatible with the GNU GPL contained in COPYING.
--- NEW FILE: chown.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
char *cp;
int uid;
struct passwd *pwd;
struct stat statbuf;
cp = argv[1];
if (isdecimal(*cp)) {
uid = 0;
while (isdecimal(*cp))
uid = uid * 10 + (*cp++ - '0');
if (*cp) {
fprintf(stderr, "Bad uid value\n");
exit(1);
}
} else {
pwd = getpwnam(cp);
if (pwd == NULL) {
fprintf(stderr, "Unknown user name\n");
exit(1);
}
uid = pwd->pw_uid;
}
argc--;
argv++;
while (argc-- > 1) {
argv++;
if ((stat(*argv, &statbuf) < 0) ||
(chown(*argv, uid, statbuf.st_gid) < 0))
perror(*argv);
}
exit(0);
}
--- NEW FILE: ls.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* The "ls" built-in command.
*
* 1/17/97 stevew at home.com Added -C option to print 5 across
* screen.
*
* 30-Jan-98 ajr at ecs.soton.ac.uk Made -C default behavoir.
*
* Mon Feb 2 19:04:14 EDT 1998 - claudio at pos.inf.ufpr.br (Claudio Matsuoka)
* Options -a, -F and simple multicolumn output added
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define LISTSIZE 256
#ifndef __P
#define __P(x)
#endif
/* Prototypes */
int main __P((int, char **));
static void lsfile __P((char *, struct stat *, int));
int namesort __P((__const void *, __const void *));
char * buildname __P((char *, char *));
/* klugde */
#define COLS 80
#ifdef S_ISLNK
#define LSTAT lstat
#else
#define LSTAT stat
#endif
/*
* Flags for the LS command.
*/
#define LSF_LONG 0x01
#define LSF_DIR 0x02
#define LSF_INODE 0x04
#define LSF_MULT 0x08
#define LSF_ALL 0x10 /* List files starting with `.' */
#define LSF_CLASS 0x20 /* Classify files (append symbol) */
static char **list;
static int listsize;
static int listused;
static int cols = 0, col = 0;
static char fmt[10] = "%s";
int
main(argc, argv)
char **argv;
{
char *cp;
char *name;
int flags;
int i;
DIR *dirp;
BOOL endslash;
char **newlist;
struct dirent *dp;
char fullname[PATHLEN];
struct stat statbuf;
static char *def[2] = {"-ls", "."};
int len, maxlen;
int num;
if (listsize == 0) {
list = (char **) malloc(LISTSIZE * sizeof(char *));
if (list == NULL) {
fprintf(stderr, "No memory for ls buffer\n");
exit(1);
}
listsize = LISTSIZE;
}
listused = 0;
flags = 0;
if ((argc > 1) && (argv[1][0] == '-'))
{
argc--;
cp = *(++argv) + 1;
while (*cp) switch (*cp++) {
case 'l': flags |= LSF_LONG; break;
case 'd': flags |= LSF_DIR; break;
case 'i': flags |= LSF_INODE; break;
case 'a': flags |= LSF_ALL; break;
case 'F': flags |= LSF_CLASS; break;
default:
fprintf(stderr, "Unknown option -%c\n", cp[-1]);
exit(1);
}
}
if (argc <= 1) {
argc = 2;
argv = def;
}
if (argc > 2)
flags |= LSF_MULT;
while (argc-- > 1) {
if ((name = malloc (strlen (*(++argv)) + 2)) == NULL) {
fprintf(stderr, "No memory for filenames\n");
exit(1);
}
strcpy (name, *argv);
endslash = (*name && (name[strlen(name) - 1] == '/'));
if (LSTAT(name, &statbuf) < 0) {
perror(name);
free(name);
continue;
}
if ((flags & LSF_DIR) || (!S_ISDIR(statbuf.st_mode))) {
lsfile(name, &statbuf, flags);
fputc('\n', stdout);
free(name);
continue;
}
/*
* Do all the files in a directory.
*/
dirp = opendir(name);
if (dirp == NULL) {
perror(name);
free(name);
continue;
}
if (flags & LSF_MULT)
printf("\n%s:\n", name);
while ((dp = readdir(dirp)) != NULL) {
fullname[0] = '\0';
if ((*name != '.') || (name[1] != '\0')) {
strcpy(fullname, name);
if (!endslash)
strcat(fullname, "/");
}
strcat(fullname, dp->d_name);
if (listused >= listsize) {
newlist = realloc(list,
((sizeof(char **)) * (listsize + LISTSIZE)));
if (newlist == NULL) {
fprintf(stderr, "No memory for ls buffer\n");
break;
}
list = newlist;
listsize += LISTSIZE;
}
strcat(fullname, " ");
list[listused] = strdup(fullname);
if (list[listused] == NULL) {
fprintf(stderr, "No memory for filenames\n");
break;
}
list[listused][strlen (fullname) - 1] = 0;
listused++;
}
free(name);
closedir(dirp);
/*
* Sort the files.
*/
qsort((char *) list, listused, sizeof(char *), namesort);
/*
* Get list entry size for multi-column output.
*/
if (~flags & LSF_LONG) {
for (maxlen = i = 0; i < listused; i++) {
if (cp = strrchr(list[i], '/'))
cp++;
else
cp = list[i];
if ((len = strlen (cp)) > maxlen)
maxlen = len;
}
maxlen += 2;
cols = (COLS - 1) / maxlen;
sprintf (fmt, "%%-%d.%ds", maxlen, maxlen);
}
/*
* Now finally list the filenames.
*/
for (num = i = 0; i < listused; i++) {
name = list[i];
if (LSTAT(name, &statbuf) < 0) {
perror(name);
free(name);
continue;
}
cp = strrchr(name, '/');
if (cp)
cp++;
else
cp = name;
if (flags & LSF_ALL || *cp != '.') {
lsfile(cp, &statbuf, flags);
num++;
}
free(name);
}
if ((~flags & LSF_LONG) && (num % cols))
fputc('\n', stdout);
listused = 0;
}
/* fflush(stdout); */
exit(0);
}
/*
* Do an LS of a particular file name according to the flags.
*/
static void
lsfile(name, statbuf, flags)
char *name;
struct stat *statbuf;
{
char *cp;
struct passwd *pwd;
struct group *grp;
long len;
char buf[PATHLEN];
static char username[12];
static int userid;
static BOOL useridknown;
static char groupname[12];
static int groupid;
static BOOL groupidknown;
char *class;
cp = buf;
*cp = '\0';
if (flags & LSF_INODE) {
sprintf(cp, "%5d ", statbuf->st_ino);
cp += strlen(cp);
}
if (flags & LSF_LONG) {
strcpy(cp, modestring(statbuf->st_mode));
cp += strlen(cp);
sprintf(cp, "%3d ", statbuf->st_nlink);
cp += strlen(cp);
if (!useridknown || (statbuf->st_uid != userid)) {
pwd = getpwuid(statbuf->st_uid);
if (pwd)
strcpy(username, pwd->pw_name);
else
sprintf(username, "%d", statbuf->st_uid);
userid = statbuf->st_uid;
useridknown = TRUE;
}
sprintf(cp, "%-8s ", username);
cp += strlen(cp);
if (!groupidknown || (statbuf->st_gid != groupid)) {
grp = getgrgid(statbuf->st_gid);
if (grp)
strcpy(groupname, grp->gr_name);
else
sprintf(groupname, "%d", statbuf->st_gid);
groupid = statbuf->st_gid;
groupidknown = TRUE;
}
sprintf(cp, "%-8s ", groupname);
cp += strlen(cp);
if (S_ISBLK(statbuf->st_mode) || S_ISCHR(statbuf->st_mode))
sprintf(cp, "%3d, %3d ", statbuf->st_rdev >> 8,
statbuf->st_rdev & 0xff);
else
sprintf(cp, "%8ld ", statbuf->st_size);
cp += strlen(cp);
sprintf(cp, " %-12s ", timestring(statbuf->st_mtime));
}
fputs(buf, stdout);
class = name + strlen(name);
*class = 0;
if (flags & LSF_CLASS) {
if (S_ISLNK (statbuf->st_mode))
*class = '@';
else if (S_ISDIR (statbuf->st_mode))
*class = '/';
else if (S_IEXEC & statbuf->st_mode)
*class = '*';
else if (S_ISFIFO (statbuf->st_mode))
*class = '|';
else if (S_ISSOCK (statbuf->st_mode))
*class = '=';
}
printf(fmt, name);
#ifdef S_ISLNK
if ((flags & LSF_LONG) && S_ISLNK(statbuf->st_mode)) {
len = readlink(name, buf, PATHLEN - 1);
if (len >= 0) {
buf[len] = '\0';
printf(" -> %s", buf);
}
}
#endif
if (flags & LSF_LONG || ++col == cols) {
fputc('\n', stdout);
col = 0;
}
}
#define BUF_SIZE 1024
typedef struct chunk CHUNK;
#define CHUNKINITSIZE 4
struct chunk {
CHUNK *next;
char data[CHUNKINITSIZE]; /* actually of varying length */
};
static CHUNK * chunklist;
/*
* Return the standard ls-like mode string from a file mode.
* This is static and so is overwritten on each call.
*/
char *
modestring(mode)
{
static char buf[12];
strcpy(buf, "----------");
/*
* Fill in the file type.
*/
if (S_ISDIR(mode))
buf[0] = 'd';
if (S_ISCHR(mode))
buf[0] = 'c';
if (S_ISBLK(mode))
buf[0] = 'b';
if (S_ISFIFO(mode))
buf[0] = 'p';
#ifdef S_ISLNK
if (S_ISLNK(mode))
buf[0] = 'l';
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(mode))
buf[0] = 's';
#endif
/*
* Now fill in the normal file permissions.
*/
if (mode & S_IRUSR)
buf[1] = 'r';
if (mode & S_IWUSR)
buf[2] = 'w';
if (mode & S_IXUSR)
buf[3] = 'x';
if (mode & S_IRGRP)
buf[4] = 'r';
if (mode & S_IWGRP)
buf[5] = 'w';
if (mode & S_IXGRP)
buf[6] = 'x';
if (mode & S_IROTH)
buf[7] = 'r';
if (mode & S_IWOTH)
buf[8] = 'w';
if (mode & S_IXOTH)
buf[9] = 'x';
/*
* Finally fill in magic stuff like suid and sticky text.
*/
if (mode & S_ISUID)
buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
if (mode & S_ISGID)
buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
if (mode & S_ISVTX)
buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
return buf;
}
/*
* Get the time to be used for a file.
* This is down to the minute for new files, but only the date for old files.
* The string is returned from a static buffer, and so is overwritten for
* each call.
*/
char *
timestring(t)
long t;
{
long now;
char *str;
static char buf[26];
time(&now);
str = ctime(&t);
strcpy(buf, &str[4]);
buf[12] = '\0';
if ((t > now) || (t < now - 365*24*60*60L)) {
strcpy(&buf[7], &str[20]);
buf[11] = '\0';
}
return buf;
}
/*
* Build a path name from the specified directory name and file name.
* If the directory name is NULL, then the original filename is returned.
* The built path is in a static area, and is overwritten for each call.
*/
char *
buildname(dirname, filename)
char *dirname;
char *filename;
{
char *cp;
static char buf[PATHLEN];
if ((dirname == NULL) || (*dirname == '\0'))
return filename;
cp = strrchr(filename, '/');
if (cp)
filename = cp + 1;
strcpy(buf, dirname);
strcat(buf, "/");
strcat(buf, filename);
return buf;
}
/*
* Sort routine for list of filenames.
*/
int
namesort(pp1, pp2)
const void *pp1;
const void *pp2;
{
char **p1 = (char **)pp1;
char **p2 = (char **)pp2;
return strcmp(*p1, *p2);
}
--- NEW FILE: cp.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
BOOL dirflag;
char *srcname;
char *destname;
char *lastarg;
lastarg = argv[argc - 1];
dirflag = isadir(lastarg);
if ((argc > 3) && !dirflag) {
fprintf(stderr, "%s: not a directory\n", lastarg);
exit(1);
}
while (argc-- > 2) {
srcname = argv[1];
destname = lastarg;
if (dirflag)
destname = buildname(destname, srcname);
(void) copyfile(*++argv, destname, FALSE);
}
exit(0);
}
#define BUF_SIZE 1024
typedef struct chunk CHUNK;
#define CHUNKINITSIZE 4
struct chunk {
CHUNK *next;
char data[CHUNKINITSIZE]; /* actually of varying length */
};
static CHUNK * chunklist;
/*
* Return TRUE if a filename is a directory.
* Nonexistant files return FALSE.
*/
BOOL
isadir(name)
char *name;
{
struct stat statbuf;
if (stat(name, &statbuf) < 0)
return FALSE;
return S_ISDIR(statbuf.st_mode);
}
/*
* Copy one file to another, while possibly preserving its modes, times,
* and modes. Returns TRUE if successful, or FALSE on a failure with an
* error message output. (Failure is not indicted if the attributes cannot
* be set.)
*/
BOOL
copyfile(srcname, destname, setmodes)
char *srcname;
char *destname;
BOOL setmodes;
{
int rfd;
int wfd;
int rcc;
int wcc;
char *bp;
char *buf;
struct stat statbuf1;
struct stat statbuf2;
struct utimbuf times;
if (stat(srcname, &statbuf1) < 0) {
perror(srcname);
return FALSE;
}
if (stat(destname, &statbuf2) < 0) {
statbuf2.st_ino = -1;
statbuf2.st_dev = -1;
}
if ((statbuf1.st_dev == statbuf2.st_dev) &&
(statbuf1.st_ino == statbuf2.st_ino))
{
fprintf(stderr, "Copying file \"%s\" to itself\n", srcname);
return FALSE;
}
rfd = open(srcname, 0);
if (rfd < 0) {
perror(srcname);
return FALSE;
}
wfd = creat(destname, statbuf1.st_mode);
if (wfd < 0) {
perror(destname);
close(rfd);
return FALSE;
}
buf = malloc(BUF_SIZE);
while ((rcc = read(rfd, buf, BUF_SIZE)) > 0) {
bp = buf;
while (rcc > 0) {
wcc = write(wfd, bp, rcc);
if (wcc < 0) {
perror(destname);
goto error_exit;
}
bp += wcc;
rcc -= wcc;
}
}
if (rcc < 0) {
perror(srcname);
goto error_exit;
}
close(rfd);
if (close(wfd) < 0) {
perror(destname);
return FALSE;
}
if (setmodes) {
(void) chmod(destname, statbuf1.st_mode);
(void) chown(destname, statbuf1.st_uid, statbuf1.st_gid);
times.actime = statbuf1.st_atime;
times.modtime = statbuf1.st_mtime;
(void) utime(destname, ×);
}
return TRUE;
error_exit:
close(rfd);
close(wfd);
return FALSE;
}
/*
* Build a path name from the specified directory name and file name.
* If the directory name is NULL, then the original filename is returned.
* The built path is in a static area, and is overwritten for each call.
*/
char *
buildname(dirname, filename)
char *dirname;
char *filename;
{
char *cp;
static char buf[PATHLEN];
if ((dirname == NULL) || (*dirname == '\0'))
return filename;
cp = strrchr(filename, '/');
if (cp)
filename = cp + 1;
strcpy(buf, dirname);
strcat(buf, "/");
strcat(buf, filename);
return buf;
}
--- NEW FILE: chmod.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
char *cp;
int mode;
mode = 0;
cp = argv[1];
while (isoctal(*cp))
mode = mode * 8 + (*cp++ - '0');
if (*cp) {
fprintf(stderr, "Mode must be octal\n");
exit(1);
}
argc--;
argv++;
while (argc-- > 1) {
if (chmod(argv[1], mode) < 0)
perror(argv[1]);
argv++;
}
exit(0);
}
--- NEW FILE: Makefile ---
EXECS = cat chgrp chmod chown cmp cp dd grep l ln ls mkdir mkfifo mknod \
more mv rm rmdir sync touch
OBJS = cat.o chgrp.o chmod.o chown.o cmp.o cp.o dd.o grep.o l.o ln.o ls.o \
mkdir.o mkfifo.o mknod.o more.o mv.o rm.o rmdir.o sync.o touch.o
all: $(EXECS)
$(EXECS): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $@.o $(LDLIBS)
romfs:
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CAT /bin/cat
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CHGRP /bin/chgrp
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CHMOD /bin/chmod
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CHOWN /bin/chown
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CMP /bin/cmp
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_CP /bin/cp
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_DD /bin/dd
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_GREP /bin/grep
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_L /bin/l
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_LN /bin/ln
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_LS /bin/ls
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_MKDIR /bin/mkdir
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_MKFIFO /bin/mkfifo
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_MKNOD /bin/mknod
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_MORE /bin/more
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_MV /bin/mv
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_RM /bin/rm
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_RMDIR /bin/rmdir
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_SYNC /bin/sync
$(ROMFSINST) -e CONFIG_USER_FILEUTILS_TOUCH /bin/touch
clean:
rm -f $(EXECS) *.elf *.gdb *.o
--- NEW FILE: touch.c ---
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int
main (argc,argv)
int argc;
char **argv;
{
int i, ncreate = 0;
struct stat sbuf;
int fd,er;
if ((argv[1][0] == '-') && (argv[1][1] == 'c'))
ncreate = 1;
for(i=ncreate+1;i<argc;i++) {
if (argv[i][0] != '-') {
if (stat(argv[i],&sbuf)) {
if (!ncreate)
er = close(creat(argv[i], 0666));
} else
er = utime(argv[i],NULL);
}
}
}
--- NEW FILE: mv.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
int dirflag;
char *srcname;
char *destname;
char *lastarg;
lastarg = argv[argc - 1];
dirflag = isadir(lastarg);
if ((argc > 3) && !dirflag) {
write(STDERR_FILENO, lastarg, strlen(lastarg));
write(STDERR_FILENO, ": not a directory\n", 18);
exit(1);
}
while (argc-- > 2) {
srcname = *(++argv);
if (access(srcname, 0) < 0) {
perror(srcname);
continue;
}
destname = lastarg;
if (dirflag)
destname = buildname(destname, srcname);
if (rename(srcname, destname) >= 0)
continue;
if (errno != EXDEV) {
perror(destname);
continue;
}
if (!copyfile(srcname, destname, TRUE))
continue;
if (unlink(srcname) < 0)
perror(srcname);
}
exit(0);
}
#define BUF_SIZE 1024
typedef struct chunk CHUNK;
#define CHUNKINITSIZE 4
struct chunk {
CHUNK *next;
char data[CHUNKINITSIZE]; /* actually of varying length */
};
static CHUNK * chunklist;
/*
* Return TRUE if a filename is a directory.
* Nonexistant files return FALSE.
*/
BOOL
isadir(name)
char *name;
{
struct stat statbuf;
if (stat(name, &statbuf) < 0)
return FALSE;
return S_ISDIR(statbuf.st_mode);
}
/*
* Copy one file to another, while possibly preserving its modes, times,
* and modes. Returns TRUE if successful, or FALSE on a failure with an
* error message output. (Failure is not indicted if the attributes cannot
* be set.)
*/
BOOL
copyfile(srcname, destname, setmodes)
char *srcname;
char *destname;
BOOL setmodes;
{
int rfd;
int wfd;
int rcc;
int wcc;
char *bp;
char *buf;
struct stat statbuf1;
struct stat statbuf2;
struct utimbuf times;
if (stat(srcname, &statbuf1) < 0) {
perror(srcname);
return FALSE;
}
if (stat(destname, &statbuf2) < 0) {
statbuf2.st_ino = -1;
statbuf2.st_dev = -1;
}
if ((statbuf1.st_dev == statbuf2.st_dev) &&
(statbuf1.st_ino == statbuf2.st_ino))
{
write(STDERR_FILENO, "Copying file \"", 14);
write(STDERR_FILENO, srcname, strlen(srcname));
write(STDERR_FILENO, "\" to itself\n", 12);
return FALSE;
}
rfd = open(srcname, 0);
if (rfd < 0) {
perror(srcname);
return FALSE;
}
wfd = creat(destname, statbuf1.st_mode);
if (wfd < 0) {
perror(destname);
close(rfd);
return FALSE;
}
buf = malloc(BUF_SIZE);
while ((rcc = read(rfd, buf, BUF_SIZE)) > 0) {
bp = buf;
while (rcc > 0) {
wcc = write(wfd, bp, rcc);
if (wcc < 0) {
perror(destname);
goto error_exit;
}
bp += wcc;
rcc -= wcc;
}
}
if (rcc < 0) {
perror(srcname);
goto error_exit;
}
close(rfd);
if (close(wfd) < 0) {
perror(destname);
return FALSE;
}
if (setmodes) {
(void) chmod(destname, statbuf1.st_mode);
(void) chown(destname, statbuf1.st_uid, statbuf1.st_gid);
times.actime = statbuf1.st_atime;
times.modtime = statbuf1.st_mtime;
(void) utime(destname, ×);
}
return TRUE;
error_exit:
close(rfd);
close(wfd);
return FALSE;
}
/*
* Build a path name from the specified directory name and file name.
* If the directory name is NULL, then the original filename is returned.
* The built path is in a static area, and is overwritten for each call.
*/
char *
buildname(dirname, filename)
char *dirname;
char *filename;
{
char *cp;
static char buf[PATHLEN];
if ((dirname == NULL) || (*dirname == '\0'))
return filename;
cp = strrchr(filename, '/');
if (cp)
filename = cp + 1;
strcpy(buf, dirname);
strcat(buf, "/");
strcat(buf, filename);
return buf;
}
--- NEW FILE: README ---
This is version 0.1 of my port of the the GNU fileutils. This package
consists of clones of some of the file utilities rewritten to be as compact
as possible for use in the elks project. Included in this version are pretty
complete versions of mkdir, mkfifo mknod, rmdir, sync and touch and partly
complete but usable versions of rm and mv.
While the code is based around the GNU fileutils 3.14, with inspiration from
tiny-utils, I have re-written alot of code, and this should not be
considered to be a version of fileutils.
To compile all the utilities type make, and they will be built and copied
into the bin directory. From here, install where you want to use them.
Almost all of the utilities have had features removed to make them smaller.
All of them no longer recognise the --help and --version switches and many
have had other switches removed. Switches must be specified in the form -s,
not --switch as they can be in the GNU utils. The options available for each
util are as follows. Please view the relevant man pages for info on what
these options do. (Eventually I will write man pages for these, if noone
else does it first.) If I have removed an option which you think is vital,
please E-mail ajr at ecs.soton.ac.uk me letting me know why and I may add it,
or better still send me a patch.
mkdir [-p] dir...
mkfifo file-name...
mknod filename {bcu} major minor
mknod filename p
rmdir [-p] dir...
sync
The following two are incomplete and should be used with great care. They
have no switches and virtually no error checking, but are usable if you need
them.
mv source dest
rm file-name..
Please note above, mv will only move one file, and then destination must
also be a filename, and it does not check to see if destination already
exists. rm can only remove files, and does not check anything about them.
I will be releasing better versions of these very soon. The reason these
simples versions are being releases is that I decided to release what I had
done as soon as minish started working on elks.
If you think that there is anything wrong or missing about any of this
package please let me know and I will fix it, but remember that the purpose
fo the package is a set of very small utilities to be used on low poweres
machines running elks, and features that carry alot of bulk will need a very
good reason to be included.
Alistair Riddoch
ajr at ecs.soton.ac.uk
--- NEW FILE: chgrp.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
char *cp;
int gid;
struct group *grp;
struct stat statbuf;
cp = argv[1];
if (isdecimal(*cp)) {
gid = 0;
while (isdecimal(*cp))
gid = gid * 10 + (*cp++ - '0');
if (*cp) {
fprintf(stderr, "Bad gid value\n");
exit(1);
}
} else {
grp = getgrnam(cp);
if (grp == NULL) {
fprintf(stderr, "Unknown group name\n");
exit(1);
}
gid = grp->gr_gid;
}
argc--;
argv++;
while (argc-- > 1) {
argv++;
if ((stat(*argv, &statbuf) < 0) ||
(chown(*argv, statbuf.st_uid, gid) < 0))
perror(*argv);
}
exit(0);
}
--- NEW FILE: COPYING ---
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
--- NEW FILE: ln.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
int dirflag;
char *srcname;
char *destname;
char *lastarg;
if (argv[1][0] == '-') {
if (strcmp(argv[1], "-s")) {
write(STDERR_FILENO, "Unknown option\n", 15);
exit(1);
}
if (argc != 4) {
write(STDERR_FILENO, "Wrong number of arguments for symbolic link\n", 44);
exit(1);
}
if (symlink(argv[2], argv[3]) < 0) {
perror(argv[3]);
exit(1);
}
exit(0);
}
/*
* Here for normal hard links.
*/
lastarg = argv[argc - 1];
dirflag = isadir(lastarg);
if ((argc > 3) && !dirflag) {
write(STDERR_FILENO, lastarg, strlen(lastarg));
write(STDERR_FILENO, ": not a directory\n", 18);
exit(1);
}
while (argc-- > 2) {
srcname = *(++argv);
if (access(srcname, 0) < 0) {
perror(srcname);
continue;
}
destname = lastarg;
if (dirflag)
destname = buildname(destname, srcname);
if (link(srcname, destname) < 0) {
perror(destname);
continue;
}
}
exit(0);
}
#define BUF_SIZE 1024
typedef struct chunk CHUNK;
#define CHUNKINITSIZE 4
struct chunk {
CHUNK *next;
char data[CHUNKINITSIZE]; /* actually of varying length */
};
static CHUNK * chunklist;
/*
* Return TRUE if a filename is a directory.
* Nonexistant files return FALSE.
*/
BOOL
isadir(name)
char *name;
{
struct stat statbuf;
if (stat(name, &statbuf) < 0)
return FALSE;
return S_ISDIR(statbuf.st_mode);
}
/*
* Build a path name from the specified directory name and file name.
* If the directory name is NULL, then the original filename is returned.
* The built path is in a static area, and is overwritten for each call.
*/
char *
buildname(dirname, filename)
char *dirname;
char *filename;
{
char *cp;
static char buf[PATHLEN];
if ((dirname == NULL) || (*dirname == '\0'))
return filename;
cp = strrchr(filename, '/');
if (cp)
filename = cp + 1;
strcpy(buf, dirname);
strcat(buf, "/");
strcat(buf, filename);
return buf;
}
--- NEW FILE: cat.c ---
/*
* /bin/cat for ELKS; mark II
*
* 1997 MTK, other insignificant people
*/
#define __USE_BSD
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#define CAT_BUF_SIZE 4096
int cat_read_size = CAT_BUF_SIZE;
char colon[2] = { ':', ' ' };
char nl = '\n';
void dumpfile(fd)
int fd;
{
int nred;
static char readbuf[CAT_BUF_SIZE];
while ((nred=read(fd,readbuf,cat_read_size)) > 0) {
write(STDOUT_FILENO,readbuf,nred);
}
}
int main(argc,argv)
int argc;
char **argv;
{
int i, fd;
if(argc <= 1) {
dumpfile(STDIN_FILENO);
} else {
for(i=1;i<argc;i++) {
fd = open(argv[i], O_RDONLY);
if(fd == -1) {
write(STDERR_FILENO, argv[0], strlen(argv[0]));
write(STDERR_FILENO, colon, 2);
write(STDERR_FILENO, argv[i], strlen(argv[i]));
write(STDERR_FILENO, colon, 2);
write(STDERR_FILENO, strerror (errno), strlen (strerror (errno)));
write(STDERR_FILENO, &nl, 1);
} else {
dumpfile(fd);
close(fd);
}
}
}
}
--- NEW FILE: grep.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* The "grep" built-in command.
*/
#include "futils.h"
#include <ctype.h>
static BOOL search();
int
main(argc, argv)
char **argv;
{
FILE *fp;
char *word;
char *name;
char *cp;
BOOL tellname;
BOOL ignorecase;
BOOL tellline;
long line;
char buf[8192];
ignorecase = FALSE;
tellline = FALSE;
argc--;
argv++;
if (**argv == '-') {
argc--;
cp = *argv++;
while (*++cp) switch (*cp) {
case 'i':
ignorecase = TRUE;
break;
case 'n':
tellline = TRUE;
break;
default:
fprintf(stderr, "Unknown option\n");
exit(1);
}
}
word = *argv++;
argc--;
tellname = (argc > 1);
while (argc-- > 0) {
name = *argv++;
fp = fopen(name, "r");
if (fp == NULL) {
perror(name);
continue;
}
line = 0;
while (fgets(buf, sizeof(buf), fp)) {
cp = &buf[strlen(buf) - 1];
if (*cp != '\n')
fprintf(stderr, "%s: Line too long\n", name);
if (search(buf, word, ignorecase)) {
if (tellname)
printf("%s: ", name);
if (tellline)
printf("%d: ", line);
fputs(buf, stdout);
}
}
if (ferror(fp))
perror(name);
fclose(fp);
}
exit(0);
}
/*
* See if the specified word is found in the specified string.
*/
static BOOL
search(string, word, ignorecase)
char *string;
char *word;
BOOL ignorecase;
{
char *cp1;
char *cp2;
int len;
int lowfirst;
int ch1;
int ch2;
len = strlen(word);
if (!ignorecase) {
while (TRUE) {
string = strchr(string, word[0]);
if (string == NULL)
return FALSE;
if (memcmp(string, word, len) == 0)
return TRUE;
string++;
}
}
/*
* Here if we need to check case independence.
* Do the search by lower casing both strings.
*/
lowfirst = *word;
if (isupper(lowfirst))
lowfirst = tolower(lowfirst);
while (TRUE) {
while (*string && (*string != lowfirst) &&
(!isupper(*string) || (tolower(*string) != lowfirst)))
string++;
if (*string == '\0')
return FALSE;
cp1 = string;
cp2 = word;
do {
if (*cp2 == '\0')
return TRUE;
ch1 = *cp1++;
if (isupper(ch1))
ch1 = tolower(ch1);
ch2 = *cp2++;
if (isupper(ch2))
ch2 = tolower(ch2);
} while (ch1 == ch2);
string++;
}
}
/* END CODE */
--- NEW FILE: rm.c ---
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
char *
basename (name)
char *name;
{
char *base;
base = rindex (name, '/');
return base ? base + 1 : name;
}
int
main (argc,argv)
int argc;
char **argv;
{
int i/*, recurse = 0, interact =0*/;
struct stat sbuf;
int fd,er;
/* if (((argv[1][0] == '-') && (argv[1][1] == 'r')) || ((argv[2][0] == '-') && (argv[2][1] == 'r')))
recurse = 1;
if (((argv[1][0] == '-') && (argv[1][1] == 'i')) || ((argv[2][0] == '-') && (argv[2][1] == 'i')))
interact = 1;
*/
for(i=/*recurse+interact+*/1;i<argc;i++) {
if (argv[i][0] != '-') {
if (!lstat(argv[i],&sbuf)) {
if (unlink(argv[i])) {
write(STDERR_FILENO,"rm: could not remove ",21);
write(STDERR_FILENO,argv[i],strlen(argv[i]));
write(STDERR_FILENO,"\n",1);
}
}
}
}
}
--- NEW FILE: l.c ---
/* l.c - A short version of `ls'
* Sun Feb 8 16:28:03 EST 1998 - claudio at pos.inf.ufpr.br (Claudio Matsuoka)
*
* Based on the ls.c sources Copyright (c) 1993 by David I. Bell
*/
/* This works like `ls -m', displaying files horizontally and separated
* by commas
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#define LISTSIZE 256
#define PATHLEN 256
#ifndef __P
#define __P(x)
#endif
/* Prototypes */
int main __P((int, char **));
void lsfile __P((char *));
int namesort __P((__const void *, __const void *));
char * buildname __P((char *, char *));
#define COLS 80 /* Hardcoded number of columns (Yuck!) */
static char **list;
static int listsize;
static int listused;
static int comma = 0, col = 0, len;
static char *err_mem = "out of memory\n";
int
main(argc, argv)
char **argv;
{
char *cp;
char *name = NULL;
int mult = 0;
int isdir = 0;
int lf_flag = 0;
int i;
DIR *dirp;
int endslash;
char **newlist;
struct dirent *dp;
char fullname[PATHLEN];
struct stat statbuf;
static char *def[2] = {"-ls", "."};
if (listsize == 0) {
list = (char **) malloc(LISTSIZE * sizeof(char *));
if (list == NULL) {
fputs (err_mem, stderr);
return;
}
listsize = LISTSIZE;
}
listused = 0;
if (argc <= 1) {
argc = 2;
argv = def;
}
mult = argc > 2;
while (argc-- > 1) {
name = *(++argv);
endslash = (*name && (name[strlen(name) - 1] == '/'));
if (lstat (name, &statbuf) < 0) {
perror (name);
continue;
}
if (!S_ISDIR (statbuf.st_mode)) {
if (isdir) {
fputs ("\n\n", stdout);
comma = 0;
col = 0;
}
lsfile (name);
isdir = 0;
lf_flag = 1;
continue;
}
if (lf_flag)
fputs ("\n\n", stdout);
comma = 0;
col = 0;
lf_flag = 1;
isdir = 1;
/*
* Do all the files in a directory.
*/
dirp = opendir (name);
if (dirp == NULL) {
perror (name);
continue;
}
if (mult) {
fputs (name, stdout);
fputs (":\n", stdout);
}
while ((dp = readdir(dirp)) != NULL) {
fullname[0] = '\0';
if ((*name != '.') || (name[1] != '\0')) {
strcpy(fullname, name);
if (!endslash)
strcat(fullname, "/");
}
strcat(fullname, dp->d_name);
if (listused >= listsize) {
newlist = realloc(list,
((sizeof(char **)) * (listsize + LISTSIZE)));
if (newlist == NULL) {
fputs (err_mem, stderr);
break;
}
list = newlist;
listsize += LISTSIZE;
}
list[listused] = strdup(fullname);
if (list[listused] == NULL) {
fputs (err_mem, stderr);
break;
}
listused++;
}
closedir(dirp);
/*
* Sort the files.
*/
qsort((char *) list, listused, sizeof(char *), namesort);
/*
* Now finally list the filenames.
*/
for (i = 0; i < listused; i++) {
name = list[i];
if (lstat (name, &statbuf) < 0) {
perror(name);
free(name);
continue;
}
cp = strrchr(name, '/');
if (cp)
cp++;
else
cp = name;
lsfile (cp);
free(name);
}
listused = 0;
}
fputs ("\n", stdout);
}
/*
* Do an LS of a particular file name
*/
void
lsfile (cp)
char *cp;
{
if (*cp != '.') {
len = strlen (cp);
if ((col += len) >= (COLS - 3)) {
fputs (",\n", stdout);
col = len;
} else if (comma) {
fputs (", ", stdout);
col += 2;
} else comma = 1;
fputs (cp, stdout);
}
}
/*
* Build a path name from the specified directory name and file name.
* If the directory name is NULL, then the original filename is returned.
* The built path is in a static area, and is overwritten for each call.
*/
char *
buildname(dirname, filename)
char *dirname;
char *filename;
{
char *cp;
static char buf[PATHLEN];
if ((dirname == NULL) || (*dirname == '\0'))
return filename;
cp = strrchr (filename, '/');
if (cp)
filename = cp + 1;
strcpy (buf, dirname);
strcat (buf, "/");
strcat (buf, filename);
return buf;
}
/*
* Sort routine for list of filenames.
*/
int
namesort(pp1, pp2)
const void *pp1;
const void *pp2;
{
char **p1 = (char **)pp1;
char **p2 = (char **)pp2;
return strcmp(*p1, *p2);
}
--- NEW FILE: dd.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* The "dd" built-in command.
*/
#include "futils.h"
#include <unistd.h>
#define PAR_NONE 0
#define PAR_IF 1
#define PAR_OF 2
#define PAR_BS 3
#define PAR_COUNT 4
#define PAR_SEEK 5
#define PAR_SKIP 6
typedef struct {
char *name;
int value;
} PARAM;
static PARAM params[] = {
{ "if", PAR_IF },
{ "of", PAR_OF },
{ "bs", PAR_BS },
{ "count", PAR_COUNT },
{ "seek", PAR_SEEK },
{ "skip", PAR_SKIP },
{ NULL, PAR_NONE }
};
static long getnum();
char localbuf[8192];
int
main(argc, argv)
char **argv;
{
char *str;
char *cp;
PARAM *par;
char *infile;
char *outfile;
int infd;
int outfd;
int incc;
int outcc;
int blocksize;
long count;
long seekval;
long skipval;
long intotal;
long outtotal;
char *buf;
infile = NULL;
outfile = NULL;
seekval = 0;
skipval = 0;
blocksize = 512;
count = 0x7fffffff;
while (--argc > 0) {
str = *++argv;
cp = strchr(str, '=');
if (cp == NULL) {
write(STDERR_FILENO, "Bad dd argument\n", 16);
return;
}
*cp++ = '\0';
for (par = params; par->name; par++) {
if (strcmp(str, par->name) == 0)
break;
}
switch (par->value) {
case PAR_IF:
if (infile) {
write(STDERR_FILENO, "Multiple input files illegal\n", 29);
return;
}
infile = cp;
break;
case PAR_OF:
if (outfile) {
write(STDERR_FILENO, "Multiple output files illegal\n", 30);
return;
}
outfile = cp;
break;
case PAR_BS:
blocksize = getnum(cp);
if (blocksize <= 0) {
write(STDERR_FILENO, "Bad block size value\n", 21);
return;
}
break;
case PAR_COUNT:
count = getnum(cp);
if (count < 0) {
write(STDERR_FILENO, "Bad count value\n", 16);
return;
}
break;
case PAR_SEEK:
seekval = getnum(cp);
if (seekval < 0) {
write(STDERR_FILENO, "Bad seek value\n", 15);
return;
}
break;
case PAR_SKIP:
skipval = getnum(cp);
if (skipval < 0) {
write(STDERR_FILENO, "Bad skip value\n", 15);
return;
}
break;
default:
write(STDERR_FILENO, "Unknown dd parameter\n", 21);
return;
}
}
if (infile == NULL) {
write(STDERR_FILENO, "No input file specified\n", 24);
return;
}
if (outfile == NULL) {
write(STDERR_FILENO, "No output file specified\n", 25);
return;
}
buf = localbuf;
if (blocksize > sizeof(localbuf)) {
buf = malloc(blocksize);
if (buf == NULL) {
write(STDERR_FILENO, "Cannot allocate buffer\n", 23);
return;
}
}
intotal = 0;
outtotal = 0;
infd = open(infile, 0);
if (infd < 0) {
perror(infile);
if (buf != localbuf)
free(buf);
return;
}
outfd = creat(outfile, 0666);
if (outfd < 0) {
perror(outfile);
close(infd);
if (buf != localbuf)
free(buf);
return;
}
if (skipval) {
if (lseek(infd, skipval * blocksize, 0) < 0) {
while (skipval-- > 0) {
incc = read(infd, buf, blocksize);
if (incc < 0) {
perror(infile);
goto cleanup;
}
if (incc == 0) {
write(STDERR_FILENO, "End of file while skipping\n", 27);
goto cleanup;
}
}
}
}
if (seekval) {
if (lseek(outfd, seekval * blocksize, 0) < 0) {
perror(outfile);
goto cleanup;
}
}
while ((incc = read(infd, buf, blocksize)) > 0) {
intotal += incc;
cp = buf;
while (incc > 0) {
outcc = write(outfd, cp, incc);
if (outcc < 0) {
perror(outfile);
goto cleanup;
}
outtotal += outcc;
cp += outcc;
incc -= outcc;
}
}
if (incc < 0)
perror(infile);
cleanup:
close(infd);
if (close(outfd) < 0)
perror(outfile);
if (buf != localbuf)
free(buf);
printf("%d+%d records in\n", intotal / blocksize,
(intotal % blocksize) != 0);
printf("%d+%d records out\n", outtotal / blocksize,
(outtotal % blocksize) != 0);
}
/*
* Read a number with a possible multiplier.
* Returns -1 if the number format is illegal.
*/
static long
getnum(cp)
char *cp;
{
long value;
if (!isdecimal(*cp))
return -1;
value = 0;
while (isdecimal(*cp))
value = value * 10 + *cp++ - '0';
switch (*cp++) {
case 'k':
value *= 1024;
break;
case 'b':
value *= 512;
break;
case 'w':
value *= 2;
break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
return value;
}
/* END CODE */
--- NEW FILE: futils.h ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Definitions for stand-alone shell for system maintainance for Linux.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#define PATHLEN 256
#define CMDLEN 512
#define MAXARGS 500
#define ALIASALLOC 20
#define STDIN 0
#define STDOUT 1
#define MAXSOURCE 10
#ifndef isblank
#define isblank(ch) (((ch) == ' ') || ((ch) == '\t'))
#endif
#define isquote(ch) (((ch) == '"') || ((ch) == '\''))
#define isdecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
#define isoctal(ch) (((ch) >= '0') && ((ch) <= '7'))
typedef int BOOL;
#define FALSE ((BOOL) 0)
#define TRUE ((BOOL) 1)
extern void do_alias(), do_cd(), do_exec(), do_exit(), do_prompt();
extern void do_source(), do_umask(), do_unalias(), do_help(), do_ln();
extern void do_cp(), do_mv(), do_rm(), do_chmod(), do_mkdir(), do_rmdir();
extern void do_mknod(), do_chown(), do_chgrp(), do_sync(), do_printenv();
extern void do_more(), do_cmp(), do_touch(), do_ls(), do_dd(), do_tar();
extern void do_mount(), do_umount(), do_setenv(), do_pwd(), do_echo();
extern void do_kill(), do_grep(), do_ed();
extern char *buildname();
extern char *modestring();
extern char *timestring();
extern BOOL isadir();
extern BOOL copyfile();
extern BOOL match();
extern BOOL makestring();
extern BOOL makeargs();
extern int expandwildcards();
extern int namesort();
extern char *getchunk();
extern void freechunks();
extern BOOL intflag;
/* END CODE */
--- NEW FILE: mknod.c ---
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#define makedev(maj, min) (((maj) << 8) | (min))
int
main (argc,argv)
int argc;
char **argv;
{
unsigned short newmode;
unsigned short filetype;
int major,minor;
newmode = 0666 & ~umask(0);
if (argc == 5) {
switch(argv[2][0]) {
case 'b':
filetype = S_IFBLK;
break;
case 'c':
case 'u':
filetype = S_IFCHR;
break;
default:
write(STDERR_FILENO,"mknod: usage error\n",19);
exit(1);
}
major = (int)strtol(argv[3],NULL,0);
minor = (int)strtol(argv[4],NULL,0);
if ( errno != ERANGE )
if (mknod (argv[1], newmode | filetype, makedev(major, minor))) {
write(STDERR_FILENO,"mknod: cannot make device ",27);
write(STDERR_FILENO,argv[1],strlen(argv[1]));
write(STDERR_FILENO,"\n",1);
exit(1);
}
} else if ((argc == 3) && (argv[2][0] == 'p')) {
/* The second line mith mkfifo is used in the GNU version but there
is no mkfifo call in elks libc yet */
if (mknod (argv[1],newmode | S_IFIFO, 0))
/* if (mkfifo (argv[1],newmode)) */
{
write(STDERR_FILENO,"mknod: cannot make fifo ",25);
write(STDERR_FILENO,argv[1],strlen(argv[1]));
write(STDERR_FILENO,"\n",1);
exit(1);
}
} else {
write(STDERR_FILENO,"mknod: usage error\n",19);
}
exit(0);
}
--- NEW FILE: mkfifo.c ---
#include <unistd.h>
#include <sys/stat.h>
int
main (argc,argv)
int argc;
char **argv;
{
unsigned short newmode;
int i,er=0;
newmode = 0666 & ~umask(0);
for(i=1;i<argc;i++)
{
/* The first line below mith mkfifo is used in the GNU version but there
is no mkfifo call in elks libc yet */
/* if (mkfifo (argv[i],newmode)) */
if (mknod (argv[i],newmode | S_IFIFO, 0))
{
write(STDERR_FILENO,"mkfifo: cannot make fifo ",25);
write(STDERR_FILENO,argv[i],strlen(argv[i]));
write(STDERR_FILENO,"\n",1);
er&=1;
}
}
exit(er);
}
--- NEW FILE: cmp.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
int fd1;
int fd2;
int cc1;
int cc2;
long pos;
char *srcname;
char *destname;
char *lastarg;
char *bp1;
char *bp2;
char buf1[512];
char buf2[512];
struct stat statbuf1;
struct stat statbuf2;
if (stat(argv[1], &statbuf1) < 0) {
perror(argv[1]);
exit(2);
}
if (stat(argv[2], &statbuf2) < 0) {
perror(argv[2]);
exit(2);
}
if ((statbuf1.st_dev == statbuf2.st_dev) &&
(statbuf1.st_ino == statbuf2.st_ino))
{
printf("Files are links to each other\n");
exit(0);
}
if (statbuf1.st_size != statbuf2.st_size) {
printf("Files are different sizes\n");
exit(1);
}
fd1 = open(argv[1], 0);
if (fd1 < 0) {
perror(argv[1]);
exit(2);
}
fd2 = open(argv[2], 0);
if (fd2 < 0) {
perror(argv[2]);
close(fd1);
exit(2);
}
pos = 0;
while (TRUE) {
cc1 = read(fd1, buf1, sizeof(buf1));
if (cc1 < 0) {
perror(argv[1]);
exit(2);
}
cc2 = read(fd2, buf2, sizeof(buf2));
if (cc2 < 0) {
perror(argv[2]);
goto differ;
}
if ((cc1 == 0) && (cc2 == 0)) {
printf("Files are identical\n");
goto same;
}
if (cc1 < cc2) {
printf("First file is shorter than second\n");
goto differ;
}
if (cc1 > cc2) {
printf("Second file is shorter than first\n");
goto differ;
}
if (memcmp(buf1, buf2, cc1) == 0) {
pos += cc1;
continue;
}
bp1 = buf1;
bp2 = buf2;
while (*bp1++ == *bp2++)
pos++;
printf("Files differ at byte position %ld\n", pos);
goto differ;
}
same:
exit(0);
differ:
exit(1);
}
--- NEW FILE: mkdir.c ---
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
unsigned short newmode;
int
make_dir(name,f)
char *name;
int f;
{
char iname[256];
char *line;
strcpy(iname, name);
if (((line = rindex(iname,'/')) != NULL) && f) {
while ((line > iname) && (*line == '/'))
--line;
line[1] = 0;
make_dir(iname,1);
}
if (mkdir(name, newmode) && !f)
return(1);
else
return(0);
}
int
main (argc,argv)
int argc;
char **argv;
{
int i, parent = 0, er = 0;
if ((argv[1][0] == '-') && (argv[1][1] == 'p'))
parent = 1;
newmode = 0666 & ~umask(0);
for(i=parent+1;i<argc;i++) {
if (argv[i][0] != '-') {
if (argv[i][strlen(argv[i])-1] == '/')
argv[i][strlen(argv[i])-1] = '\0';
if (make_dir(argv[i],parent)) {
write(STDERR_FILENO,"mkdir: cannot create directory ",31);
write(STDERR_FILENO,argv[i],strlen(argv[i]));
write(STDERR_FILENO,"\n",1);
er = 1;
}
} else {
write(STDERR_FILENO,"mkdir: usage error.\n",20);
exit(1);
}
}
exit(er);
}
--- NEW FILE: rmdir.c ---
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
unsigned short newmode;
int
remove_dir(name,f)
char *name;
int f;
{
int er,era=2;
char *line;
while (((er = rmdir(name)) == 0) && ((line = rindex(name,'/')) != NULL) && f) {
while ((line > name) && (*line == '/'))
--line;
line[1] = 0;
era=0;
}
return(er && era);
}
int
main (argc,argv)
int argc;
char **argv;
{
int i, parent = 0, er = 0;
if ((argv[1][0] == '-') && (argv[1][1] == 'p'))
parent = 1;
newmode = 0666 & ~umask(0);
for(i=parent+1;i<argc;i++) {
if (argv[i][0] != '-') {
while (argv[i][strlen(argv[i])-1] == '/')
argv[i][strlen(argv[i])-1] = '\0';
if (remove_dir(argv[i],parent)) {
write(STDERR_FILENO,"rmdir: cannot remove directory ",31);
write(STDERR_FILENO,argv[i],strlen(argv[i]));
write(STDERR_FILENO,"\n",1);
er = 1;
}
} else {
write(STDERR_FILENO,"rmdir: usage error.\n",20);
exit(1);
}
}
exit(er);
}
--- NEW FILE: more.c ---
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "futils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
int
main(argc, argv)
char **argv;
{
FILE *fp;
int fd;
char *name;
unsigned char ch;
int line;
int col;
char buf[80];
while (argc-- > 1) {
name = *(++argv);
fd = open(name, O_RDONLY);
if (fd == -1) {
perror(name);
exit(1);
}
write(STDOUT_FILENO,"<< ",3);
write(STDOUT_FILENO,name,strlen(name));
write(STDOUT_FILENO," >>\n",4);
line = 1;
col = 0;
while ((fd > -1) && ((read(fd, &ch, 1)) != 0)) {
switch (ch) {
case '\r':
col = 0;
break;
case '\n':
line++;
col = 0;
break;
case '\t':
col = ((col + 1) | 0x07) + 1;
break;
case '\b':
if (col > 0)
col--;
break;
default:
col++;
}
putchar(ch);
if (col >= 80) {
col -= 80;
line++;
}
if (line < 24)
continue;
if (col > 0)
putchar('\n');
write(STDOUT_FILENO,"--More--",8);
fflush(stdout);
if ((read(0, buf, sizeof(buf)) < 0)) {
if (fd > -1)
close(fd);
exit(0);
}
ch = buf[0];
if (ch == ':')
ch = buf[1];
switch (ch) {
case 'N':
case 'n':
close(fd);
fd = -1;
break;
case 'Q':
case 'q':
close(fd);
exit(0);
}
col = 0;
line = 1;
}
if (fd)
close(fd);
}
exit(0);
}
--- NEW FILE: sync.c ---
#include <unistd.h>
#include <string.h>
int
main ()
{
sync();
exit(0);
}
More information about the dslinux-commit
mailing list