view uptools/atcmd/smsend_pduin.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 3eb92855f7b9
children dc2fd8e6f42c
line wrap: on
line source

/*
 * This is the main module for the fcup-smsendpdu utility.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../../rvinterf/include/exitcodes.h"

int sms_write_mode;
char input_line[176*2+2];
int lineno;
int initdone, cmms_done;

process_cmdline(argc, argv)
	char **argv;
{
	int c;
	extern int optind;

	while ((c = getopt(argc, argv, "B:np:RwWX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		switch (c) {
		case 'w':
			sms_write_mode = 1;
			continue;
		case 'W':
			sms_write_mode = 2;
			continue;
		default:
			/* error msg already printed */
			exit(ERROR_USAGE);
		}
	}
	if (argc != optind) {
		fprintf(stderr, "usage: %s [options]\n", argv[0]);
		exit(ERROR_USAGE);
	}
	return(0);
}

more_input()
{
	int c;

	c = getc(stdin);
	if (c == EOF)
		return(0);
	ungetc(c, stdin);
	return(1);
}

init_send_process()
{
	if (initdone)
		return(0);
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
	/* set PDU mode */
	prep_for_pdu_mode();
	if (sms_write_mode == 0 && more_input()) {
		atinterf_exec_cmd_needok("AT+CMMS=1", 0, 0);
		cmms_done = 1;
	}
	initdone = 1;
	return(1);
}

process_line()
{
	char *cp;
	u_char pdubin[176];
	int cc, scalen;

	cp = index(input_line, '\n');
	if (!cp) {
		fprintf(stderr, "input line %d: too long or unterminated\n",
			lineno);
		exit(ERROR_USAGE);
	}
	*cp = '\0';
	cc = decode_hex_line(input_line, pdubin, sizeof pdubin);
	if (cc < 1) {
inv:		fprintf(stderr, "input line %d: not a valid PDU\n", lineno);
		exit(ERROR_USAGE);
	}
	if (pdubin[0] == 1 || pdubin[0] > 11)
		goto inv;
	scalen = pdubin[0] + 1;
	if (cc < scalen + 1)
		goto inv;
	/* good to go */
	init_send_process();
	send_pdu_out(input_line, cc - scalen);
}

main(argc, argv)
	char **argv;
{
	process_cmdline(argc, argv);
	for (lineno = 1; fgets(input_line, sizeof input_line, stdin); lineno++)
		process_line();
	if (!initdone)
		exit(0);
	if (cmms_done)
		atinterf_exec_cmd_needok("AT+CMMS=0", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	exit(0);
}