X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f X-Recipient: djgpp-workers AT delorie DOT com X-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=yaTdm7B9WgX1QRxX4LXRFA0wMk64Ak6aFaTDXcnM25Y=; b=kWZCBhpDWFSOfaZKuVPgA6f9q5QtydU2fK+z5q189rX7QdXOtlW38ZTe/2Ew/B3bDa PNzAyJaIhm+qaHHKytFMFmSB9u1V1tJCMvUw5I7R3XWTvHBBpWjYIcNtj3CUyJtpcGyb 297c8EpOrRjljj003H7/gKQualm5FospS5P55Hjnyp2Q9s013K6mpW1Jwhbnh4Rokxe3 JOXYyNMY43hmoPamY2W3ZdP/wGePoy22w13OUR5So4c8RiKhz6xlrYKqzQGPKeCEKjaO JuIRc7XdTNJgXJNDhXzpgwWy1KpfMu8kmZq/XKfUqeHPvy/8ycJrK9TkPGU+gCH+cYj3 +MLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=yaTdm7B9WgX1QRxX4LXRFA0wMk64Ak6aFaTDXcnM25Y=; b=Ey5igi0othc/63uSpbjaJP3ZU3tSrKOq+gJcUpaPlPM2UPwCppRNm8CMszGV9kPoP1 OsgK744I/oouGM0tiB2wHHFc7UJ0SAIY7Gnfhd18ah4Gwmi0BizONS8GbwbspR6PHI82 UgfmdLn9Q/IAqoC5JZ6QVarB0jJcGw2kbc8bisXIGZEnianE43HjHn3czHvfXbN0y9G4 cxUokaYeoutXMInY9yYsKw9+GsGdO5nxVi8OxKVAYDl1S3egq2+OMiOFsThLp6jR5w9B P6rbHnZI+/XS7Tgb+HD1OUAVMGqhO/+exAnWIvJXdu4Y6JgAOEhxvOJXljjZhHCtXDvR Ae1Q== X-Gm-Message-State: AEkooutSu9PAhiKIHHWK7Vjc5blMy/XCidBymJMuWfqV0jrYA4jkRX3z+ZcJpOB34FVP90ynzPAtSayPYv5o4A== X-Received: by 10.159.55.168 with SMTP id q37mr46867416uaq.1.1470572220304; Sun, 07 Aug 2016 05:17:00 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: "Rugxulo (rugxulo AT gmail DOT com) [via djgpp-workers AT delorie DOT com]" Date: Sun, 7 Aug 2016 07:16:59 -0500 Message-ID: Subject: Re: linking more bloat from ctime.c To: djgpp-workers AT delorie DOT com Content-Type: text/plain; charset=UTF-8 Reply-To: djgpp-workers AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp-workers AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk Hi again, So I whipped up a quick attempt to fix this. Patch + simple example + makefile are inlined here for your pleasure / consternation. On Sat, Aug 6, 2016 at 11:23 AM, Rugxulo wrote: > > I assume ctime.c can be "fixed" to not need sprintf() at all Feel free to test this further. I don't claim it's perfect, but I did try. EDIT: Oops, just fixed a bug in printing year (bottom half needs to have leading zero). How to exhaustively test this? Should I brute-force try to run through every time_t since 1970 to make sure that there are no bugs? Any help is welcome, of course. === ctime.dif begins === --- ctime.c 2014-04-19 13:50:30 -0500 +++ ctime.new 2016-08-07 06:17:20 -0500 @@ -40,6 +40,7 @@ ** (guy AT auspex DOT com). */ +#include #include #include #include @@ -1547,12 +1548,37 @@ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +#ifndef NO_SPRINTF /* avoid linking in doprnt.o unnecessarily */ (void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n", wday_name[timeptr->tm_wday], mon_name[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, TM_YEAR_BASE + timeptr->tm_year); +#endif /* NO_SPRINTF */ + + static char newresult[26]; char *newres = newresult; + + strncpy(newres,wday_name[timeptr->tm_wday],3); newres[3] = ' '; + strncpy(newres+4,mon_name[timeptr->tm_mon],3); newres[7] = ' '; +#define ASC(n,f,r) r[n]=(f/10)==0 ? ' ' : (f/10)+'0';r[n+1]=(f%10)+'0' +#define ASC0(n,f,r) r[n]=(f/10)+'0';r[n+1]=(f%10)+'0' + ASC(8,timeptr->tm_mday,newres); newres[10]=' '; + ASC0(11,timeptr->tm_hour,newres); newres[13]=':'; + ASC0(14,timeptr->tm_min,newres); newres[16]=':'; + ASC0(17,timeptr->tm_sec,newres); newres[19]=' '; + ASC(20,((TM_YEAR_BASE+timeptr->tm_year)/100),newres); + ASC0(22,((TM_YEAR_BASE+timeptr->tm_year)%100),newres); + newres[24]='\n'; +#undef ASC +#undef ASC0 + +#ifndef NO_SPRINTF + assert(strcmp(result,newres)==0); +#else + strcpy(result,newres); +#endif + return result; } === ctime.dif ends === === asctime.c begins === #include #include void say(const char* s) { size_t i; for (i=0; s[i]; i++) putchar(s[i]); } int main(void) { time_t now; time(&now); say("The current time is "); say(asctime(localtime(&now))); return 0; } === asctime.c ends === === makefile begins === .PHONY: all clean .RECIPEPREFIX := _ CC=gcc CFLAGS=-I. -s -O MAPFILE=$(basename $@).map MAP=-Wl,-Map,$(MAPFILE) LS=ls -l RM=rm -f ECHO=djecho GREP=grep GREPLINE="\.text .* 0x[^01][[:xdigit:]]\{3,\} [a-z]:.*\.a(.*\.o" GREPIT=$(GREP) $(GREPLINE) $(MAPFILE) all: asctime.exe lite.exe _@-$(ECHO) _$< asctime.exe: asctime.c ctime.c _$(CC) $(CFLAGS) $(MAP) $^ -o $@ _-$(LS) $@ _-@$(GREPIT) _@-$(ECHO) lite.exe: asctime.c ctime.c _$(CC) $(CFLAGS) $(MAP) -DNO_SPRINTF $^ -o $@ _-$(LS) $@ _-@$(GREPIT) ctime.c: posixrul.h clean: _-$(RM) *.exe *.map === makefile ends ===