Update pwd, rev, rm, rmail, rmdir, size, sleep, sort, split, sum, sync.

This commit is contained in:
Serge
2022-07-10 19:03:28 -07:00
parent 1253330adc
commit db74d0da32
11 changed files with 570 additions and 604 deletions

View File

@@ -10,10 +10,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <unistd.h>
char *getwd();
main()
int main()
{
char pathname[MAXPATHLEN + 1];

View File

@@ -8,23 +8,22 @@
char line[N];
FILE *input;
main(argc,argv)
char **argv;
int main(int argc, char **argv)
{
register i,c;
int i, c;
input = stdin;
do {
if(argc>1) {
if((input=fopen(argv[1],"r"))==NULL) {
fprintf(stderr,"rev: cannot open %s\n",
argv[1]);
if (argc > 1) {
if ((input = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "rev: cannot open %s\n", argv[1]);
exit(1);
}
}
for(;;){
for(i=0;i<N;i++) {
for (;;) {
for (i = 0; i < N; i++) {
line[i] = c = getc(input);
switch(c) {
switch (c) {
case EOF:
goto eof;
default:
@@ -34,13 +33,13 @@ char **argv;
}
break;
}
while(--i>=0)
putc(line[i],stdout);
putc('\n',stdout);
while (--i >= 0)
putc(line[i], stdout);
putc('\n', stdout);
}
eof:
fclose(input);
argc--;
argv++;
} while(argc>1);
} while (argc > 1);
}

View File

@@ -4,21 +4,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/file.h>
int fflg; /* -f force - supress error messages */
int iflg; /* -i interrogate user on each file */
int rflg; /* -r recurse */
int fflg; /* -f force - supress error messages */
int iflg; /* -i interrogate user on each file */
int rflg; /* -r recurse */
int errcode; /* true if errors occured */
int errcode; /* true if errors occured */
main(argc, argv)
char *argv[];
static int rm(char arg[], int level);
static int dotname(char *s);
static int yes(void);
static void append(char *name);
int main(int argc, char *argv[])
{
register char *arg;
char *arg;
fflg = !isatty(0);
iflg = 0;
@@ -34,7 +38,7 @@ main(argc, argv)
break;
while (*++arg != '\0')
switch(*arg) {
switch (*arg) {
case 'f':
fflg++;
break;
@@ -60,26 +64,25 @@ main(argc, argv)
}
while (--argc > 0)
(void) rm(*++argv, 0);
(void)rm(*++argv, 0);
exit(errcode != 0);
}
char *path; /* pointer to malloc'ed buffer for path */
char *pathp; /* current pointer to end of path */
int pathsz; /* size of path */
char *path; /* pointer to malloc'ed buffer for path */
char *pathp; /* current pointer to end of path */
int pathsz; /* size of path */
/*
* Return TRUE if sucessful. Recursive with -r (rflg)
*/
rm(arg, level)
char arg[];
int rm(char arg[], int level)
{
int ok; /* true if recursive rm succeeded */
struct stat buf; /* for finding out what a file is */
struct direct *dp; /* for reading a directory */
DIR *dirp; /* for reading a directory */
char prevname[MAXNAMLEN + 1]; /* previous name for -r */
int ok; /* true if recursive rm succeeded */
struct stat buf; /* for finding out what a file is */
struct direct *dp; /* for reading a directory */
DIR *dirp; /* for reading a directory */
char prevname[MAXNAMLEN + 1]; /* previous name for -r */
char *cp;
if (dotname(arg)) {
@@ -91,9 +94,9 @@ rm(arg, level)
fprintf(stderr, "rm: %s nonexistent\n", arg);
errcode++;
}
return (0); /* error */
return (0); /* error */
}
if ((buf.st_mode&S_IFMT) == S_IFDIR) {
if ((buf.st_mode & S_IFMT) == S_IFDIR) {
if (!rflg) {
if (!fflg) {
fprintf(stderr, "rm: %s directory\n", arg);
@@ -106,14 +109,14 @@ rm(arg, level)
if (!yes())
return (0); /* didn't remove everything */
}
if (access(arg, R_OK|W_OK|X_OK) != 0) {
if (access(arg, R_OK | W_OK | X_OK) != 0) {
if (rmdir(arg) == 0)
return (1); /* salvaged: removed empty dir */
if (!fflg) {
fprintf(stderr, "rm: %s not changed\n", arg);
errcode++;
}
return (0); /* error */
return (0); /* error */
}
if ((dirp = opendir(arg)) == NULL) {
if (!fflg) {
@@ -133,7 +136,7 @@ rm(arg, level)
append(dp->d_name);
closedir(dirp);
ok = rm(path, level + 1);
for (cp = pathp; *--cp != '/' && cp > path; )
for (cp = pathp; *--cp != '/' && cp > path;)
;
pathp = cp;
*cp++ = '\0';
@@ -146,17 +149,15 @@ rm(arg, level)
}
/* pick up where we left off */
if (prevname[0] != '\0') {
while ((dp = readdir(dirp)) != NULL &&
strcmp(prevname, dp->d_name) != 0)
while ((dp = readdir(dirp)) != NULL && strcmp(prevname, dp->d_name) != 0)
;
}
/* skip the one we just failed to delete */
if (!ok) {
dp = readdir(dirp);
if (dp != NULL && strcmp(cp, dp->d_name)) {
fprintf(stderr,
"rm: internal synchronization error: %s, %s, %s\n",
arg, cp, dp->d_name);
fprintf(stderr, "rm: internal synchronization error: %s, %s, %s\n", arg, cp,
dp->d_name);
}
strcpy(prevname, dp->d_name);
}
@@ -186,9 +187,8 @@ rm(arg, level)
if (!yes())
return (0);
} else if (!fflg) {
if ((buf.st_mode&S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) {
printf("rm: override protection %o for %s? ",
buf.st_mode&0777, arg);
if ((buf.st_mode & S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) {
printf("rm: override protection %o for %s? ", buf.st_mode & 0777, arg);
if (!yes())
return (0);
}
@@ -206,24 +206,25 @@ rm(arg, level)
/*
* boolean: is it "." or ".." ?
*/
dotname(s)
char *s;
int dotname(char *s)
{
if (s[0] == '.')
if (s[1] == '.')
if (s[0] == '.') {
if (s[1] == '.') {
if (s[2] == '\0')
return (1);
else
return (0);
else if (s[1] == '\0')
} else if (s[1] == '\0') {
return (1);
}
}
return (0);
}
/*
* Get a yes/no answer from the user.
*/
yes()
int yes()
{
int i, b;
@@ -236,10 +237,9 @@ yes()
/*
* Append 'name' to 'path'.
*/
append(name)
char *name;
void append(char *name)
{
register int n;
int n;
n = strlen(name);
if (path == NULL) {

View File

@@ -6,68 +6,61 @@
** It calls sendmail giving it a -f option built from these
** lines.
*/
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sysexits.h>
#include <paths.h>
typedef char bool;
#define TRUE 1
#define FALSE 0
typedef char bool;
#define TRUE 1
#define FALSE 0
bool Debug;
bool Debug;
main(argc, argv)
char **argv;
int main(int argc, char **argv)
{
FILE *out; /* output to sendmail */
char lbuf[1024]; /* one line of the message */
char from[512]; /* accumulated path of sender */
char ufrom[512]; /* user on remote system */
char sys[512]; /* a system in path */
char junk[1024]; /* scratchpad */
FILE *out; /* output to sendmail */
char lbuf[1024]; /* one line of the message */
char from[512]; /* accumulated path of sender */
char ufrom[512]; /* user on remote system */
char sys[512]; /* a system in path */
char junk[1024]; /* scratchpad */
char cmd[2000];
register char *cp;
register char *uf = ufrom; /* ptr into ufrom */
char *cp;
char *uf = ufrom; /* ptr into ufrom */
int i;
#ifdef DEBUG
if (argc > 1 && strcmp(argv[1], "-T") == 0)
{
if (argc > 1 && strcmp(argv[1], "-T") == 0) {
Debug = TRUE;
argc--;
argv++;
}
#endif
if (argc < 2)
{
if (argc < 2) {
fprintf(stderr, "Usage: rmail user ...\n");
exit(EX_USAGE);
}
(void) strcpy(from, "");
(void) strcpy(ufrom, "/dev/null");
(void)strcpy(from, "");
(void)strcpy(ufrom, "/dev/null");
for (;;)
{
(void) fgets(lbuf, sizeof lbuf, stdin);
for (;;) {
(void)fgets(lbuf, sizeof lbuf, stdin);
if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
break;
(void) sscanf(lbuf, "%s %s", junk, ufrom);
(void)sscanf(lbuf, "%s %s", junk, ufrom);
cp = lbuf;
for (;;)
{
cp = index(cp+1, 'r');
if (cp == NULL)
{
register char *p = rindex(uf, '!');
for (;;) {
cp = index(cp + 1, 'r');
if (cp == NULL) {
char *p = rindex(uf, '!');
if (p != NULL)
{
if (p != NULL) {
*p = '\0';
(void) strcpy(sys, uf);
(void)strcpy(sys, uf);
uf = p + 1;
break;
}
@@ -77,29 +70,28 @@ main(argc, argv)
if (Debug)
printf("cp='%s'\n", cp);
#endif
if (strncmp(cp, "remote from ", 12)==0)
if (strncmp(cp, "remote from ", 12) == 0)
break;
}
if (cp != NULL)
(void) sscanf(cp, "remote from %s", sys);
(void) strcat(from, sys);
(void) strcat(from, "!");
(void)sscanf(cp, "remote from %s", sys);
(void)strcat(from, sys);
(void)strcat(from, "!");
#ifdef DEBUG
if (Debug)
printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
#endif
}
(void) strcat(from, uf);
(void)strcat(from, uf);
(void) sprintf(cmd, "%s -ee -f%s -i", _PATH_SENDMAIL, from);
while (*++argv != NULL)
{
(void) strcat(cmd, " '");
(void)sprintf(cmd, "%s -ee -f%s -i", _PATH_SENDMAIL, from);
while (*++argv != NULL) {
(void)strcat(cmd, " '");
if (**argv == '(')
(void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
(void)strncat(cmd, *argv + 1, strlen(*argv) - 2);
else
(void) strcat(cmd, *argv);
(void) strcat(cmd, "'");
(void)strcat(cmd, *argv);
(void)strcat(cmd, "'");
}
#ifdef DEBUG
if (Debug)
@@ -110,8 +102,7 @@ main(argc, argv)
while (fgets(lbuf, sizeof lbuf, stdin))
fputs(lbuf, out);
i = pclose(out);
if ((i & 0377) != 0)
{
if ((i & 0377) != 0) {
fprintf(stderr, "pclose: status 0%o\n", i);
exit(EX_OSERR);
}

View File

@@ -9,10 +9,9 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main(argc,argv)
int argc;
char **argv;
int main(int argc, char **argv)
{
int errors = 0;
@@ -20,11 +19,12 @@ main(argc,argv)
fprintf(stderr, "usage: %s directory ...\n", argv[0]);
exit(1);
}
while (--argc)
while (--argc) {
if (rmdir(*++argv) < 0) {
fprintf(stderr, "rmdir: ");
perror(*argv);;
perror(*argv);
errors++;
}
}
exit(errors != 0);
}

View File

@@ -2,19 +2,17 @@
* size
*/
#ifdef CROSS
# include </usr/include/stdio.h>
#include </usr/include/stdio.h>
#else
# include <stdio.h>
#include <stdio.h>
#endif
#include <a.out.h>
#include <stdlib.h>
#include <unistd.h>
#include <a.out.h>
int header;
int
main(argc, argv)
char **argv;
int main(int argc, char **argv)
{
struct exec buf;
long sum;
@@ -22,12 +20,12 @@ char **argv;
FILE *f;
while ((ch = getopt(argc, argv, "h")) != EOF) {
switch(ch) {
switch (ch) {
case 'h':
default:
fprintf(stderr, "Usage:\n");
fprintf(stderr, " size file...\n");
return(1);
return (1);
}
}
argc -= optind;
@@ -38,14 +36,13 @@ char **argv;
argc++;
}
for (nfiles=argc; argc--; argv++) {
if ((f = fopen(*argv, "r"))==NULL) {
for (nfiles = argc; argc--; argv++) {
if ((f = fopen(*argv, "r")) == NULL) {
printf("size: %s not found\n", *argv);
err++;
continue;
}
if (fread((char *)&buf, sizeof(buf), 1, f) != 1 ||
N_BADMAG(buf)) {
if (fread((char *)&buf, sizeof(buf), 1, f) != 1 || N_BADMAG(buf)) {
printf("size: %s not an object file\n", *argv);
fclose(f);
err++;
@@ -55,8 +52,8 @@ char **argv;
printf("text\tdata\tbss\tdec\thex\n");
header = 1;
}
printf("%u\t%u\t%u\t", buf.a_text,buf.a_data,buf.a_bss);
sum = (long) buf.a_text + (long) buf.a_data + (long) buf.a_bss;
printf("%u\t%u\t%u\t", buf.a_text, buf.a_data, buf.a_bss);
sum = (long)buf.a_text + (long)buf.a_data + (long)buf.a_bss;
printf("%ld\t%lx", sum, sum);
if (nfiles > 1)
printf("\t%s", *argv);

View File

@@ -1,24 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main(argc, argv)
char **argv;
int main(int argc, char **argv)
{
int c, n;
char *s;
n = 0;
if(argc < 2) {
if (argc < 2) {
printf("arg count\n");
exit(0);
}
s = argv[1];
while(c = *s++) {
if(c<'0' || c>'9') {
while ((c = *s++)) {
if (c < '0' || c > '9') {
printf("bad character\n");
exit(0);
}
n = n*10 + c - '0';
n = n * 10 + c - '0';
}
sleep(n);
}

File diff suppressed because it is too large Load Diff

View File

@@ -3,22 +3,20 @@
unsigned count = 1000;
int fnumber;
char fname[100];
char *ifil;
char *ofil;
FILE *is;
FILE *os;
char fname[100];
char *ifil;
char *ofil;
FILE *is;
FILE *os;
main(argc, argv)
char *argv[];
int main(int argc, char *argv[])
{
register i, c, f;
int i, c, f;
int iflg = 0;
for(i=1; i<argc; i++)
if(argv[i][0] == '-')
switch(argv[i][1]) {
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1]) {
case '\0':
iflg = 1;
continue;
@@ -33,50 +31,49 @@ char *argv[];
case '7':
case '8':
case '9':
count = atoi(argv[i]+1);
count = atoi(argv[i] + 1);
continue;
}
else if(iflg)
else if (iflg)
ofil = argv[i];
else {
ifil = argv[i];
iflg = 2;
}
if(iflg != 2)
if (iflg != 2)
is = stdin;
else
if((is=fopen(ifil,"r")) == NULL) {
perror(ifil);
exit(1);
}
if(ofil == 0)
else if ((is = fopen(ifil, "r")) == NULL) {
perror(ifil);
exit(1);
}
if (ofil == 0)
ofil = "x";
loop:
f = 1;
for(i=0; i<count; i++)
do {
c = getc(is);
if(c == EOF) {
if(f == 0)
fclose(os);
exit(0);
}
if(f) {
for(f=0; ofil[f]; f++)
fname[f] = ofil[f];
fname[f++] = fnumber/26 + 'a';
fname[f++] = fnumber%26 + 'a';
fname[f] = '\0';
fnumber++;
if((os=fopen(fname,"w")) == NULL) {
fprintf(stderr,"Cannot create output\n");
exit(1);
for (i = 0; i < count; i++)
do {
c = getc(is);
if (c == EOF) {
if (f == 0)
fclose(os);
exit(0);
}
f = 0;
}
putc(c, os);
} while(c != '\n');
if (f) {
for (f = 0; ofil[f]; f++)
fname[f] = ofil[f];
fname[f++] = fnumber / 26 + 'a';
fname[f++] = fnumber % 26 + 'a';
fname[f] = '\0';
fnumber++;
if ((os = fopen(fname, "w")) == NULL) {
fprintf(stderr, "Cannot create output\n");
exit(1);
}
f = 0;
}
putc(c, os);
} while (c != '\n');
fclose(os);
goto loop;
}

View File

@@ -4,18 +4,17 @@
#include <stdio.h>
#include <stdlib.h>
main(argc,argv)
char **argv;
int main(int argc, char **argv)
{
register unsigned sum;
register i, c;
register FILE *f;
register long nbytes;
unsigned sum;
int i, c;
FILE *f;
long nbytes;
int errflg = 0;
i = 1;
do {
if(i < argc) {
if (i < argc) {
if ((f = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "sum: Can't open %s\n", argv[i]);
errflg += 10;
@@ -27,8 +26,8 @@ char **argv;
nbytes = 0;
while ((c = getc(f)) != EOF) {
nbytes++;
if (sum&01)
sum = (sum>>1) + 0x8000;
if (sum & 01)
sum = (sum >> 1) + 0x8000;
else
sum >>= 1;
sum += c;
@@ -36,13 +35,13 @@ char **argv;
}
if (ferror(f)) {
errflg++;
fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
fprintf(stderr, "sum: read error on %s\n", argc > 1 ? argv[i] : "-");
}
printf("%05u%6ld", sum, (nbytes+BUFSIZ-1)/BUFSIZ);
if(argc > 2)
printf("%05u%6ld", sum, (nbytes + BUFSIZ - 1) / BUFSIZ);
if (argc > 2)
printf(" %s", argv[i]);
printf("\n");
fclose(f);
} while(++i < argc);
} while (++i < argc);
exit(errflg);
}

View File

@@ -1,3 +1,5 @@
#include <unistd.h>
int main()
{
sync();