Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Faqs and RFCs
FileSearching
Google
Google Groups
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
wrap.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define VERSION "0.3" #define DATE "29 April 2024" #define DEFAULT_WIDTH 80 #define MAX_LENGTH 32768 #define MIN_WIDTH 10 void help(void) { printf( "wrap " VERSION " - wrap text at n characters.\n" "\n" "wrap -e # get width from $COLUMNS .\n" "wrap -w n # set width to n .\n" "wrap -h # provide this help message.\n" "\n" "DEFAULT_WIDTH : %d.\n" "MIN_WIDTH : %d.\n" "COLUMNS : ", DEFAULT_WIDTH, MIN_WIDTH); if (getenv("COLUMNS")) printf("%d.\n", atoi(getenv("COLUMNS"))); else printf("(not set).\n"); printf( "\n" "Michael Chappell\n" DATE "\n" "mcsuper5@freeshell.org\n" "\n"); return; } int main(int argc, char ** argv) { int width=DEFAULT_WIDTH; int c; /* current character */ int i=0; /* index into word */ int length=0; /* current line length */ char word[MAX_LENGTH]; if (argc>1) { /* While it is a bit of a hack, the option flag is usually "-" or "/" depending upon your operating system. We will ignore the flag completely and generally ignore the length of the arguments. This may result in some weird uses: "wrap aeiou" is valid and is treated the same as "wrap -e". */ switch (argv[1][1]) { case 'e': if (getenv("COLUMNS")) { width=atoi(getenv("COLUMNS")); } else { fprintf(stderr, "%s : getenv(\"COLUMNS\") failed, using default " "width.\n", argv[0]); fprintf(stderr, "\n" "Try adding \"export COLUMNS\" to your profile.\n"); width=DEFAULT_WIDTH; } break; case 'w': if (argv[1][2] == '\0') width=atoi(argv[2]); else width=atoi(&argv[1][2]); break; default: help(); return EXIT_SUCCESS; break; } } /* Is the width rational */ if (width<MIN_WIDTH) { fprintf(stderr, "%s: Width %d is to small. Setting width to %d.\n", argv[0], width, DEFAULT_WIDTH); width=DEFAULT_WIDTH; } while ((c=getchar())!=EOF) { if ((c=='\n')&&(i==0)&&(length==0)) { printf("\n"); continue; } if (isgraph(c)) { word[i++]=c; } else { word[i]='\0'; if (c=='\n') { if ((length+i)<width) { printf("%s", word); length=0; i=0; } else { printf("\n%s ", word); length=0; i=0; } /* Emit the word and update length */ } else if ((i+length)<width) { printf("%s ", word); length+=i+1; i=0; } else { /* Line would exceed width. wrap */ printf("\n%s", word); if (c!='\n') { printf(" "); length=i+1; } else { printf("\n"); length=0; } i=0; } } /* We should be able to prevent an overflow by not allowing i to increase beyond MAX_LENGTH-1 */ if (i==MAX_LENGTH-1) { word[i]='\0'; printf("%s\n", word); length=0; i=0; } if (c=='\n') printf("\n"); } word[i]='\0'; if (c!='\n') { if ((length+i)>width) printf("\n"); printf("%s\n", word); } return EXIT_SUCCESS; }
This source has been viewed 17 times.
Friday, 6 December 2024
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org