view rvinterf/etmsync/fsupload.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents e7502631a0f9
children
line wrap: on
line source

/*
 * upload-fs implementation
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "etm.h"
#include "ffs.h"
#include "ffserr.h"
#include "tmffs2.h"
#include "limits.h"
#include "ffslimits.h"
#include "localtypes.h"
#include "localstruct.h"
#include "exitcodes.h"

uploadfs_level(srcpath, depth, prefix)
	char *srcpath, *prefix;
{
	char ffs_childpath[MAX_FULL_PATHNAME+1], *ffs_childp;
	DIR *rdd;
	struct dirent *dirent;
	char hostpath_child[MAXPATHLEN];
	struct stat hst;
	int rc;

	strcpy(ffs_childpath, prefix);
	ffs_childp = index(ffs_childpath, '\0');
	*ffs_childp++ = '/';
	rdd = opendir(srcpath);
	if (!rdd) {
		perror(srcpath);
		return(ERROR_UNIX);
	}
	while (dirent = readdir(rdd)) {
		if (dirent->d_name[0] == '.')
			continue;
		if (strlen(dirent->d_name) > MAX_FN_COMPONENT) {
			fprintf(stderr,
		"error: \"%s\" in %s exceeds the FFS component name limit\n",
				dirent->d_name, srcpath);
			closedir(rdd);
			return(ERROR_USAGE);
		}
		if (strlen(srcpath) + strlen(dirent->d_name) + 2 >
		    sizeof hostpath_child) {
			fprintf(stderr,
				"error: host side pathname buffer overflow\n");
			closedir(rdd);
			return(ERROR_UNIX);
		}
		sprintf(hostpath_child, "%s/%s", srcpath, dirent->d_name);
		if (lstat(hostpath_child, &hst) < 0) {
			perror(hostpath_child);
			closedir(rdd);
			return(ERROR_UNIX);
		}
		strcpy(ffs_childp, dirent->d_name);
		switch (hst.st_mode & S_IFMT) {
		case S_IFREG:
			printf("uploading %s\n", ffs_childpath);
			rc = fwrite_from_file(ffs_childpath, hostpath_child);
			if (rc) {
				closedir(rdd);
				return(rc);
			}
			break;
		case S_IFDIR:
			if (depth >= MAX_NAME_DEPTH-1) {
				fprintf(stderr,
				"error: directory nesting too deep at %s\n",
					hostpath_child);
				closedir(rdd);
				return(ERROR_USAGE);
			}
			printf("mkdir %s\n", ffs_childpath);
			rc = do_mkdir_existok(ffs_childpath);
			if (rc) {
				closedir(rdd);
				return(rc);
			}
			rc = uploadfs_level(hostpath_child, depth + 1,
						ffs_childpath);
			if (rc) {
				closedir(rdd);
				return(rc);
			}
			break;
		default:
			fprintf(stderr,
			"error: %s is neither a regular file nor a directory\n",
				hostpath_child);
			closedir(rdd);
			return(ERROR_USAGE);
		}
	}
	closedir(rdd);
	return(0);
}

cmd_uploadfs(argc, argv)
	char **argv;
{
	return uploadfs_level(argv[1], 0, "");
}

cmd_upload_file(argc, argv)
	char **argv;
{
	if (strlen(argv[2]) >= TMFFS_STRING_SIZE) {
		fprintf(stderr,
			"error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	return fwrite_from_file(argv[2], argv[1]);
}

cmd_upload_subtree(argc, argv)
	char **argv;
{
	int rc, depth;

	depth = validate_ffs_pathname(argv[2]);
	if (depth < 0)
		return(ERROR_USAGE);	/* error msg already printed */
	if (depth == 0) {
		fprintf(stderr, "please use upload-fs command instead\n");
		return(ERROR_USAGE);
	}
	if (depth >= MAX_NAME_DEPTH) {
		fprintf(stderr, "cannot upload into max-depth directory\n");
		return(ERROR_USAGE);
	}
	printf("mkdir %s\n", argv[2]);
	rc = do_mkdir_existok(argv[2]);
	if (rc)
		return(rc);
	return uploadfs_level(argv[1], depth, argv[2]);
}