# HG changeset patch # User John Tsiombikas # Date 1314251581 -10800 # Node ID 4f628556fa3e4983d0d7e7b199305690f6595238 uuprog initial commit diff -r 000000000000 -r 4f628556fa3e .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,3 @@ +\.d$ +\.o$ +\.swp$ diff -r 000000000000 -r 4f628556fa3e Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,28 @@ +CC = gcc +CFLAGS = -pedantic -Wall -g + +.PHONY: all +all: cat cp echo false ls mkdir pwd rm rmdir sleep sort tee true uname + +cat: cat.o +cp: cp.o +echo: echo.o +false: false.o +ls: ls.o +mkdir: mkdir.o +pwd: pwd.o +rm: rm.o +rmdir: rmdir.o +sleep: sleep.o +sort: sort.o +tee: tee.o +true: true.o +uname: uname.o + +.PHONY: clean +clean: + rm -f cat cp echo false ls mkdir pwd rm rmdir sleep sort tee true uname *.o + +.PHONY: distclean +distclean: clean + rm -f Makefile diff -r 000000000000 -r 4f628556fa3e cat.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cat.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,23 @@ +/*! cc -o cat cat.c */ +#include +#include +#include + +int main(int argc, char **argv) +{ + char buf[4096]; + size_t rbytes; + FILE *fp; + + while(*++argv) { + if(!(fp = fopen(*argv, "rb"))) { + fprintf(stderr, "cat: %s: %s\n", *argv, strerror(errno)); + } else { + while((rbytes = fread(buf, 1, sizeof buf, fp)) > 0) { + fwrite(buf, 1, rbytes, stdout); + } + fclose(fp); + } + } + return 0; +} diff -r 000000000000 -r 4f628556fa3e configure --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configure Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,27 @@ +#!/bin/sh + +echo 'CC = gcc' >Makefile +echo 'CFLAGS = -pedantic -Wall -g' >>Makefile +echo >>Makefile + +allfiles=`echo *.c | sed 's/\.c//g'` + +echo '.PHONY: all' >>Makefile +echo "all: $allfiles" >>Makefile +echo >>Makefile + +for i in *.c; do + bname=`basename $i .c` + + echo "$bname: $bname.o" >>Makefile +done + +echo >>Makefile +echo '.PHONY: clean' >>Makefile +echo 'clean:' >>Makefile +echo " rm -f $allfiles *.o" >>Makefile + +echo >>Makefile +echo '.PHONY: distclean' >>Makefile +echo 'distclean: clean' >>Makefile +echo ' rm -f Makefile' >>Makefile diff -r 000000000000 -r 4f628556fa3e cp.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cp.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,60 @@ +/*! cc -o cp cp.c */ +#include +#include +#include +#include +#include +#include + +int copy_file(const char *sname, const char *dname); + +char buf[4096]; + +int main(int argc, char **argv) +{ + int i, src_count = argc - 2; + + if(src_count > 1) { + struct stat st; + char *to_dir = argv[argc - 1]; + + if(stat(to_dir, &st) == -1) { + fprintf(stderr, "failed to stat %s: %s\n", to_dir, strerror(errno)); + return 1; + } + + for(i=0; i 0) { + fwrite(buf, 1, brd, dst); + } + + fclose(dst); + fclose(src); + return 0; +} diff -r 000000000000 -r 4f628556fa3e echo.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/echo.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,14 @@ +/*! cc -o echo echo.c */ +#include + +int main(int argc, char **argv) +{ + while(*++argv) { + fputs(*argv, stdout); + if(*(argv + 1)) { + putchar(' '); + } + } + putchar('\n'); + return 0; +} diff -r 000000000000 -r 4f628556fa3e false.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/false.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,5 @@ +/*! cc -o false false.c */ +int main(void) +{ + return 1; +} diff -r 000000000000 -r 4f628556fa3e ls.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ls.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,313 @@ +/*! gcc -pedantic -Wall -g -o ls ls.c */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum {SORT_NONE, SORT_NAME}; + +enum { + OPT_LONG = 1, + OPT_ALL = 2, + OPT_DIR = 4, + OPT_CLASS = 8, + OPT_HUMAN = 16, + OPT_INODES = 32 +}; + +int proc_opt(char opt); +int ls_dir(char *dir); + +unsigned int flags = 0; +int sort_by = SORT_NAME; +int term_col = 80; /* TODO: detect columns */ + +int main(int argc, char **argv) +{ + int i, did_one = 0; + + for(i=1; iname); + + if(flags & OPT_LONG) { + int i; + struct passwd *pwd; + struct group *grp; + char tmbuf[128]; + + switch(ent->st.st_mode & S_IFMT) { + case S_IFSOCK: + putchar('s'); + break; + case S_IFLNK: + putchar('l'); + break; + case S_IFBLK: + putchar('b'); + break; + case S_IFDIR: + putchar('d'); + break; + case S_IFCHR: + putchar('c'); + break; + case S_IFIFO: + putchar('f'); + break; + default: + putchar('-'); + } + + for(i=0; i<3; i++) { + int s = (2 - i) * 3; + unsigned int val = (ent->st.st_mode >> s) & 7; + + putchar(val & 4 ? 'r' : '-'); + putchar(val & 2 ? 'w' : '-'); + putchar(val & 1 ? 'x' : '-'); + } + printf(" %3d ", (int)ent->st.st_nlink); + + if(!(pwd = getpwuid(ent->st.st_uid))) { + printf("%d ", ent->st.st_uid); + } else { + printf("%s ", pwd->pw_name); + } + if(!(grp = getgrgid(ent->st.st_gid))) { + printf("%d ", ent->st.st_gid); + } else { + printf("%s ", grp->gr_name); + } + + sprintf(tmbuf, "%s", ctime(&ent->st.st_mtime)); + tmbuf[strlen(tmbuf) - 1] = 0; + + printf("%6ld %s ", (long)ent->st.st_size, tmbuf); + } + + fputs(ent->name, stdout); + + if(flags & OPT_CLASS) { + if(S_ISDIR(ent->st.st_mode)) { + putchar('/'); + len++; + } else if(ent->st.st_mode & S_IXUSR) { + putchar('*'); + len++; + } + } + + return len; +} + +void print_list(struct entry *arr, int count) +{ + int i, j, ncol, longest = 0; + + for(i=0; i longest) { + longest = len; + } + } + + if(isatty(1) && !(flags & OPT_LONG)) { + ncol = term_col / longest; + if(!ncol) ncol = 1; + } else { + ncol = 1; + } + + for(i=0; iname; + n2 = ent2->name; + } + return strcmp(n1, n2); +} + +int ls_dir(char *dname) +{ + int i, cwd_fd = -1, saved_err, ent_count = 0; + DIR *dir = 0; + struct dirent *ent = 0; + struct entry *earr = 0, *elist = 0, *enode = 0; + + + if(strcmp(dname, ".") != 0) { + if((cwd_fd = open(".", O_RDONLY)) == -1) { + goto err; + } + chdir(dname); + } + + if(!(dir = opendir("."))) { + goto err; + } + + if(!(elist = malloc(sizeof *elist))) { + goto err; + } + elist->name = 0; + elist->next = 0; + + while((ent = readdir(dir))) { + struct stat st; + + if(ent->d_name[0] == '.' && !(flags & OPT_ALL)) { + continue; + } + + if(lstat(ent->d_name, &st) == -1) { + goto err; + } + + if(!(enode = malloc(sizeof *enode))) { + goto err; + } + if(!(enode->name = malloc(strlen(ent->d_name) + 1))) { + goto err; + } + strcpy(enode->name, ent->d_name); + memcpy(&enode->st, &st, sizeof st); + + enode->next = elist->next; + elist->next = enode; + ent_count++; + } + closedir(dir); + dir = 0; + + /* change the list into an array, don't feel like writing list mergesorts */ + if(!(earr = malloc(ent_count * sizeof *earr))) { + goto err; + } + + i = 0; + enode = elist->next; + while(enode) { + earr[i].name = enode->name; + earr[i++].st = enode->st; + enode = enode->next; + } + if(sort_by != SORT_NONE) { + qsort(earr, ent_count, sizeof *earr, cmp_func); + } + + print_list(earr, ent_count); + + if(cwd_fd != -1) { + fchdir(cwd_fd); + } + return 0; + +err: + saved_err = errno; + free(earr); + /*free_entry_list(elist);*/ + free(enode->name); + free(enode); + if(dir) { + closedir(dir); + } + if(cwd_fd != -1) { + fchdir(cwd_fd); + close(cwd_fd); + } + errno = saved_err; + return -1; +} diff -r 000000000000 -r 4f628556fa3e mkdir.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mkdir.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,18 @@ +/*! cc -o mkdir mkdir.c */ +#include +#include +#include + +int main(int argc, char **argv) +{ + if(argc < 2) { + fprintf(stderr, "must supply the directory name as argument\n"); + return 1; + } + + if(mkdir(argv[1], 0755) == -1) { + perror("mkdir failed"); + return 1; + } + return 0; +} diff -r 000000000000 -r 4f628556fa3e pwd.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pwd.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,39 @@ +/*! gcc -std=c89 -pedantic -Wall -o pwd pwd.c */ +#include +#include +#include +#include +#include + +int main(void) +{ + char *tmp, *wdbuf; + size_t bufsz = 64; + + if(!(wdbuf = malloc(bufsz))) { + perror("malloc failed"); + return 1; + } + + while(!(tmp = getcwd(wdbuf, bufsz)) && errno == ERANGE) { + size_t newsz = bufsz ? bufsz * 2 : 2; + + if(!(tmp = realloc(wdbuf, newsz))) { + fprintf(stderr, "failed to allocate: %u bytes: %s\n", (unsigned int)newsz, strerror(errno)); + free(wdbuf); + return 1; + } + wdbuf = tmp; + bufsz = newsz; + } + + if(!tmp) { + perror("getcwd failed"); + free(wdbuf); + return 1; + } + + puts(wdbuf); + free(wdbuf); + return 0; +} diff -r 000000000000 -r 4f628556fa3e rm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rm.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,154 @@ +/*! cc -o rm rm.c */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int rm(const char *path); +int rec_rm(const char *path); +char askuser(const char *fmt, ...); +void sigfunc(int sig); + +int ask, stop_proc, force, rec, verbose; + +int main(int argc, char **argv) +{ + int i; + + signal(SIGINT, sigfunc); + + for(i=1; id_name, &st) == -1) { + if(force) continue; + closedir(dir); + return -1; + } + + if(S_ISDIR(st.st_mode)) { + if(rec_rm(dent->d_name) == -1) { + closedir(dir); + return -1; + } + } else { + if(rm(dent->d_name) == -1) { + closedir(dir); + return -1; + } + } + } + + if(verbose) { + printf("removed directory: `%s'\n", path); + } + + closedir(dir); + return 0; +} + +void sigfunc(int sig) +{ + exit(1); +} diff -r 000000000000 -r 4f628556fa3e rmdir.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rmdir.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,15 @@ +/*! cc -o rmdir rmdir.c */ +#include +#include + +int main(int argc, char **argv) +{ + int i; + for(i=1; i +#include +#include +#include + +int main(int argc, char **argv) +{ + if(argc < 2) { + puts("seconds argument missing..."); + return 1; + } + if(!isdigit(argv[1][0])) { + fprintf(stderr, "invalid argument: %s\n", argv[1]); + return 1; + } + + sleep(atoi(argv[1])); + return 0; +} diff -r 000000000000 -r 4f628556fa3e sort.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sort.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,95 @@ +/*! cc -o sort sort.c */ +#include +#include +#include + +struct lnode { + char *line; + struct lnode *next; +}; + +struct lnode *get_line(void); +int cmp(const void *a, const void *b); + +int main(void) +{ + struct lnode *tmp, *list = 0; + char **arr; + size_t i, line_count = 0; + + while((tmp = get_line())) { + if(list) { + tmp->next = list; + list = tmp; + } else { + list = tmp; + } + line_count++; + } + + if(!(arr = malloc(line_count * sizeof *arr))) { + perror("malloc failed"); + return 1; + } + + for(i=0; iline; + + tmp = list; + list = list->next; + free(tmp); + } + + qsort(arr, line_count, sizeof *arr, cmp); + + for(i=0; i= 0 && c != '\n') { + if(used_buf >= bufsz - 1) { + size_t newsz = bufsz * 2; + if(!(buf = realloc(buf, newsz))) { + perror("malloc failed"); + exit(1); + } + bufsz = newsz; + } + buf[used_buf++] = c; + } + buf[used_buf] = 0; + + if(!used_buf) { + return 0; + } + + if(!(node = malloc(sizeof *node))) { + perror("malloc failed"); + exit(1); + } + node->line = buf; + node->next = 0; + return node; +} + +int cmp(const void *a, const void *b) +{ + return strcmp(*(char**)a, *(char**)b); +} diff -r 000000000000 -r 4f628556fa3e tee.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tee.c Thu Aug 25 08:53:01 2011 +0300 @@ -0,0 +1,47 @@ +/*! cc -o tee tee.c */ +#include +#include +#include +#include + +void output(FILE **farr, int count, char *data, size_t bytes); + +int main(int argc, char **argv) +{ + char buf[512]; + size_t rdbytes; + FILE **files; + int i, fnum = 0; + + if(!(files = malloc((argc - 1) * sizeof *files))) { + perror("malloc failed"); + return EXIT_FAILURE; + } + + for(i=1; i +#include + +enum { + SYS = 1, + NODE = 2, + REL = 4, + VER = 8, + MACHINE = 16, + OS = 32 +}; + +int main(int argc, char **argv) +{ + int i; + unsigned int sel = 0; + struct utsname un; + + for(i=1; i