add ln, util.a

This commit is contained in:
Connor Lane Smith 2011-05-24 13:00:30 +01:00
parent 026e63c005
commit fbb80983ce
7 changed files with 98 additions and 7 deletions

View File

@ -1,22 +1,27 @@
include config.mk include config.mk
SRC = basename.c cat.c date.c echo.c false.c grep.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c LIB = util/enmasse.o util/eprintf.o
OBJ = $(SRC:.c=.o) util.o SRC = basename.c cat.c date.c echo.c false.c grep.c ln.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c
OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=) BIN = $(SRC:.c=)
MAN = $(SRC:.c=.1) MAN = $(SRC:.c=.1)
all: $(BIN) all: $(BIN)
$(OBJ): util.h $(OBJ): util.h
$(BIN): util.o $(BIN): util.a
.o: .o:
@echo CC -o $@ @echo CC -o $@
@$(CC) -o $@ $< util.o $(LDFLAGS) @$(CC) -o $@ $< util.a $(LDFLAGS)
.c.o: .c.o:
@echo CC -c $< @echo CC -c $<
@$(CC) -c $< $(CFLAGS) @$(CC) -c -o $@ $< $(CFLAGS)
util.a: $(LIB)
@echo AR rc $@
@$(AR) rc $@ $(LIB)
dist: clean dist: clean
@echo creating dist tarball @echo creating dist tarball
@ -28,4 +33,4 @@ dist: clean
clean: clean:
@echo cleaning @echo cleaning
@rm -f $(BIN) $(OBJ) @rm -f $(BIN) $(OBJ) $(LIB) util.a

View File

@ -4,6 +4,8 @@ VERSION = 0.0
#CC = cc #CC = cc
CC = musl-gcc CC = musl-gcc
AR = ar
CPPFLAGS = -D_BSD_SOURCE CPPFLAGS = -D_BSD_SOURCE
CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS) CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS)
LDFLAGS = -s -static LDFLAGS = -s -static

22
ln.1 Normal file
View File

@ -0,0 +1,22 @@
.TH LN 1 sbase\-VERSION
.SH NAME
ln \- make links between files
.SH SYNOPSIS
.B ln
.RB [ \-s ]
.I file
.RI [ name ]
.P
.B ln
.RB [ \-s ]
.RI [ files ...]
.RI [ directory ]
.SH DESCRIPTION
.B ln
creates a hard link to a given file, with the given name. If no name is given
it is linked into the current directory. If multiple files are listed they will
be linked into the given directory.
.SH OPTIONS
.TP
.B \-s
create a symbolic link.

23
ln.c Normal file
View File

@ -0,0 +1,23 @@
/* See LICENSE file for copyright and license details. */
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "util.h"
int
main(int argc, char *argv[])
{
bool sflag = false;
char c;
while((c = getopt(argc, argv, "s")) != -1)
switch(c) {
case 's':
sflag = true;
break;
default:
exit(EXIT_FAILURE);
}
enmasse(argc - optind, &argv[optind], sflag ? symlink : link);
return EXIT_SUCCESS;
}

1
util.h
View File

@ -1,3 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
void enmasse(int, char **, int (*)(const char *, const char *));
void eprintf(const char *, ...); void eprintf(const char *, ...);

38
util/enmasse.c Normal file
View File

@ -0,0 +1,38 @@
/* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "../util.h"
void
enmasse(int argc, char **argv, int (*fn)(const char *, const char *))
{
char *buf, *dir;
int i;
long size;
struct stat st;
if(argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
if(fn(argv[0], argv[1]) != 0)
eprintf("%s:", argv[1]);
return;
}
else if(argc == 1)
dir = ".";
else
dir = argv[--argc];
if((size = pathconf(dir, _PC_PATH_MAX)) < 0)
size = BUFSIZ;
if(!(buf = malloc(size)))
eprintf("malloc:");
for(i = 0; i < argc; i++) {
snprintf(buf, size, "%s/%s", dir, basename(argv[i]));
if(fn(argv[i], buf) != 0)
eprintf("%s:", buf);
}
free(buf);
}

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "util.h" #include "../util.h"
void void
eprintf(const char *fmt, ...) eprintf(const char *fmt, ...)