C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
amort.c File Reference

Installment Loan Amortization. More...

#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for amort.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE

Functions

int main (int argc, char **argv)
 Capture the current terminal settings for later restoration.
void print_totals (Amort *)

Variables

char month [12][4]

Detailed Description

Installment Loan Amortization.

Author
Bill Waller Copyright (c) 2026 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-05-05

Definition in file amort.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 9 of file amort.c.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Capture the current terminal settings for later restoration.

Main function for rsh.

iloan is a trivial application to demonstrate how a command-line program can be integrated into C-Menu Form with simple file, argument, or pipe i-o.

allows the user to enter a string in cooked mode

can be used in scripts to get a single character response from a user. For example: response=$(./enterchr "Enter Y or N: ") if [ "$response" = "Y" ]; then echo "You entered Yes" else echo "You entered No" fi This function saves the current terminal settings into a global variable so that they can be restored later. It should be called before modifying the terminal settings to ensure that the original settings can be restored when the program exits or is interrupted.

allows line editing and other features writes the prompt to stderr and the user's input to stdout handles signals to ensure that the terminal settings are restored if the program is interrupted.

Positional arguments: present_value The present value of the loan (the amount borrowed). number_of_payments The total number of payments to be made. interest_rate The annual interest rate (as a percentage). payment_amount The amount of each payment. The program calculates the missing value based on the three provided values. For example, if the present value, number of payments, and interest rate are provided, it will calculate the payment amount. If the present value, interest rate, and payment amount are provided, it will calculate the number of payments, and so on. The program can be used in a non-interactive way by passing the four values as arguments, with the value to be calculated set to 0. For example, to calculate the payment amount for a $10,000 loan with a 5% annual interest rate and 60 monthly payments, you could run:

iloan 10000 60 5 0

This is proof-of-concept code, and is not intended for production use. It is not designed to be robust, and does not handle all edge cases or input errors. It is intended solely to demonstrate how a simple command-line program can be integrated into C-Menu Form. In the future, more sophisticated abstractions, such as async event handlers, serialization, rpc, database, and a standardized plugin interface will be integrated into C-Menu. Feel free to modify and enhance this code as needed, but please do not use it in production without proper testing and validation. It is provided as-is without any warranty or support. Use at your own risk.

Parameters
argcArgument count
argvArgument vector

If executed as 'rsh', this program sets the user ID and group ID to 0 (root) and then executes the user's default shell (or /usr/bin/bash if SHELL is not set) with the provided arguments. If no arguments are given, it runs the shell in interactive mode. To work properly, this program must be compiled and set with the setuid bit:

$ sudo -s
cc rsh.c -o rsh
sudo chown root:root rsh
sudo chmod 4755 rsh
exit

to verify proper operation:

$ rsh
$ whoami
root

Definition at line 30 of file amort.c.

30 {
31 setlocale(LC_NUMERIC, "");
32 if (argc > 1 &&
33 ((strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) ||
34 argc < 5)) {
35 printf("Usage: amort [present_value][number_of_payments]"
36 "[interest_rate][payment_amount][yyyy-mm-dd]\n\n");
37 exit(EXIT_SUCCESS);
38 }
39 struct tm tm;
40 Amort *amort = malloc(sizeof(Amort));
41 sscanf(argv[1], "%lf", &amort->pv);
42 sscanf(argv[2], "%lf", &amort->n);
43 sscanf(argv[3], "%lf", &amort->i);
44 sscanf(argv[4], "%lf", &amort->pmt);
45 if (amort->pv == 0 || amort->n == 0 || amort->i == 0 || amort->pmt == 0 ||
46 argv[5][0] == '\0') {
47 fprintf(stderr, "Error: All arguments must be non-zero.\n");
48 exit(EXIT_FAILURE);
49 }
50 amort->interest = amort->pv * amort->i / 1200;
51 if (amort->interest > amort->pmt) {
52 fprintf(stderr, "Error: Payment amount less than interest\n");
53 exit(EXIT_FAILURE);
54 }
55 memset(&tm, 0, sizeof(tm));
56 if (strptime(argv[5], "%Y-%m-%d", &tm) == NULL)
57 if (strptime(argv[5], "%Y%m%d", &tm) == NULL)
58 printf("Error: Invalid date format. Use yyyy-mm-dd or yyyymmdd.\n"),
59 exit(EXIT_FAILURE);
60 amort->year_total_interest = 0;
61
62 printf("First Payment: %04d-%02d-%02d\n\n", tm.tm_year + 1900,
63 tm.tm_mon + 1, tm.tm_mday);
64 printf(" Per Mth Year Balance Payment Principal Interest\n");
65 printf("---- --- ---- ------------ ---------- ---------- ----------\n");
66 int m = 0;
67 for (int x = 0; x < amort->n; x++) {
68 if (amort->pv <= 0)
69 break;
70 m = (x + tm.tm_mon) % 12;
71 int y = ((x + tm.tm_mon) / 12) + tm.tm_year + 1900;
72 amort->interest = amort->pv * amort->i / 1200;
73 amort->year_total_interest += amort->interest;
74 if (amort->pv + amort->interest < amort->pmt) {
75 amort->pmt = amort->pv + amort->interest;
76 amort->principal = amort->pv;
77 } else
78 amort->principal = amort->pmt - amort->interest;
79 amort->pv -= amort->principal;
80 printf("%4d %4s %4d %'12.2f %'10.2f %'10.2f %'10.2f\n", x + 1, month[m],
81 y, amort->pv, amort->pmt, amort->principal, amort->interest);
82 if (m == 11)
83 print_totals(amort);
84 }
85 if (m != 11)
86 print_totals(amort);
87 free(amort);
88 return EXIT_SUCCESS;
89}
void print_totals(Amort *)
Definition amort.c:90
char month[12][4]
Definition amort.c:17

References month, and print_totals().

Here is the call graph for this function:

◆ print_totals()

void print_totals ( Amort * amort)

Definition at line 90 of file amort.c.

90 {
91 printf(" "
92 "----------\n");
93 printf(" %'10.2f\n",
94 amort->year_total_interest);
95 printf(" "
96 "==========\n\n");
97 amort->year_total_interest = 0;
98}

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ month

char month
Initial value:
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

Definition at line 17 of file amort.c.

17 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
18 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

Referenced by main().