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

removes ansi escape sequences beginning with "\033["" and ending in "m" or "K" More...

#include <cm.h>
#include <stdio.h>
Include dependency graph for stripansi.c:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 strip ANSI SGR sequences

Detailed Description

removes ansi escape sequences beginning with "\033["" and ending in "m" or "K"

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

Definition in file stripansi.c.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

strip ANSI SGR sequences

Parameters
argcArgument count (should be 2 for the program name and input file)
argvArgument vector (argv[1] should be the input file name)

This function iterates through the input string, copying characters to the output buffer while skipping over any ANSI escape sequences. It looks for sequences that start with "\033[" and end with "m" or "K", and removes them from the output.

< Buffer to hold the input string read from the file

< Buffer to hold the cleaned output string

Definition at line 21 of file stripansi.c.

21 {
22 char
23 in_buf[2048];
24 char out_buf[2048];
25 if (argc != 2) {
26 fprintf(stderr, "Usage: %s [file_with_ansi_codes]\n", argv[0]);
27 return 1;
28 }
29 FILE *in_fp = fopen(argv[1], "r");
30 if (!in_fp) {
31 perror("Error opening file");
32 return 1;
33 }
34 while (fgets(in_buf, sizeof(in_buf), in_fp)) {
35 strip_ansi(out_buf, in_buf);
36 fputs(out_buf, stdout);
37 }
38 fclose(in_fp);
39 return 0;
40}
size_t strip_ansi(char *, char *)
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
Definition futil.c:829

References strip_ansi().

Here is the call graph for this function: