[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

file-retriever



Here is an implementation of file-retriever in C, since its TODO file
expressed an interest in having one.

2 files attached.


Regards: David Weinehall
-- 
 /> David Weinehall <tao@acc.umu.se> /> Northern lights wander      <\
//  Maintainer of the v2.0 kernel   //  Dance across the winter sky //
\>  http://www.acc.umu.se/~tao/    </   Full colour fire           </
/* file-retriever -- file-retriever for debian-installer
 *
 * Copyright (C) 2002 David Weinehall
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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, write to the Free Software Foundation,
 *  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef PATH_MAX
static int pathmax = PATH_MAX;
#else
static int pathmax = 0;
#endif /* PATH_MAX */

#define PATH_MAX_GUESS	1024

int main(int argc, char **argv)
{
	char *oldpath = NULL;
	char *buf = NULL;
	char *wd = NULL;
	char *p = NULL;
	int status = 0;

	errno = 0;

	if (argc != 3) {
		status = EINVAL;
		goto EXIT;
	}

	/* `pwd` */
	if (!pathmax) {
		if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
			if (!errno)
				pathmax = PATH_MAX_GUESS;
			else
				goto EXIT;
		} else {
			pathmax++;
		}
	}

	if (!(buf = malloc(pathmax + 1))) {
		status = errno;
		goto EXIT;
	}

	if (!(wd = getcwd(buf, pathmax))) {
		status = errno;
		goto EXIT;
	}

	/* `pwd`/debs/$1 */
	if (!(oldpath = malloc(strlen(wd) +
			       strlen("/debs/") + strlen(argv[1]) + 1))) {
		status = errno;
		goto EXIT;
	}

	p = oldpath;
	strcpy(p, wd);
	p = oldpath + strlen(oldpath);
	strcpy(p, "/debs/");
	p = oldpath + strlen(oldpath);
	strcpy(p, argv[1]);

	/* ln -sf `pwd`/debs/$1 $2 */
	if (!unlink(argv[2]) && errno && errno != ENOENT) {
		status = errno;
		goto EXIT;
	}

	errno = 0;

	if (!(symlink(oldpath, argv[2])))
		status = errno;

EXIT:
	free(oldpath);
	free(buf);

	return status;
}
# Makefile for file-retriever
# Copyright (C) 2002 David Weinehall

CFLAGS += -O2 -g
CFLAGS += -W -Wall -Wshadow -Wpointer-arith -Wundef -Wcast-align
CFLAGS += -Wbad-function-cast -Wwrite-strings -Wsign-compare
CFLAGS += -Waggregate-return -Wmissing-noreturn -Wnested-externs

TARGETS = file-retriever

.PHONY: bin
bin: $(TARGETS)

$(TARGETS): %: %.c
	$(CC) $(CFLAGS) -o $@ $^

.PHONY: small
small: CFLAGS := -Os $(CFLAGS)
small: $(TARGETS)
	
.PHONY: clean
clean:
	rm -f $(TARGETS) core

Reply to: