[uCsimm] date and cal commands - [was] User program sources?

From: billy ball (bball@staffnet.com)
Date: Mon Mar 20 2000 - 10:02:01 EST


On Mon, 20 Mar 2000, Damir Kovacic wrote:

> I am very impressed with sizes of user programs, such as vi. I see that
> ucLinux does not use shared libraries, while linux programs on standard
> Linux distributions are typically order of magnitude larger (even when they
> use dynamic linking and symbol stripping).

pretty cool, aren't they?

i've included a date command (to manually set the date and time; i also
like stp *very much*!), along with a small cal command to print the
current or designated month's calender (sorry, no year output)... see
below...

> So, my question is:
>
> Which libc is used (static "Hello world" program on normal Linux/libc6 is
> ~100K)?

libc/libm...

> What sources were used to build user programs (there is a legend, that once
> upon a time UNIX run with 16K:)

this is base src directory:

-rw-r--r-- 1 root root 283 Sep 13 1999 COPYING
-rw-r--r-- 1 root root 589 Mar 11 23:38 Makefile
-rw-r--r-- 1 root root 884 Sep 13 1999 README
drwxr-xr-x 2 root root 1024 Mar 13 11:23 agetty/
drwxr-xr-x 2 root root 1024 Mar 15 12:36 bin/
drwxr-xr-x 3 root root 1024 Mar 13 11:23 boottools/
drwxr-xr-x 2 root root 2048 Mar 13 11:23 fileutils/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 httpd/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 ifattach/
drwxr-xr-x 2 root root 1024 Mar 11 15:43 include/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 inetd/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 init/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 ipfwadm/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 levee/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 libns/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 mount/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 ping/
drwxr-xr-x 8 root root 1024 Mar 11 15:43 pppd/
drwxr-xr-x 2 bball bball 1024 Mar 13 11:23 sash/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 sh/
drwxr-xr-x 2 root root 1024 Mar 20 09:54 shutils/
drwxr-xr-x 2 root root 1024 Mar 15 12:36 sysutils/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 telnetd/
drwxr-xr-x 2 root root 1024 Mar 13 11:23 tty/

> Damir Kovacic
> dkovacic@rasip.fer.hr

base Makefile for most of the binaries uses:

TARGET_ROOT = @target_root@
CROSS = m68k-pic-coff-

CC = $(CROSS)gcc
AR = $(CROSS)ar
LD = $(CROSS)ld
NM = $(CROSS)nm
RANLIB = $(CROSS)ranlib
CFLAGS = -O2 -fomit-frame-pointer

<-------------------------- cut here for cal.c --------------------->
/* cal - return monthly calendar
 *
 * usage: cal [mm] [yyyy]
 *
 * bugs: 1901 - 32767 only
 *
 * based on Kent Porter's code from "Software Spare Parts," 1986
 * (software routines from the book are for use in any project,
 * according to the author, but he deserves credit)
 *
 * returns current month's calendar with no arguments
 * prints calendar for specified month and year
 *
 * -i causes interactive mode to prompt for month and year
 *
 * hacked for uClinux on uCsimm, bball@staffnet.com 2000-3-13
 * (yes, i know some of this is ugly, but i don't program for a living)
 *
 */

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int cal_lm (mo, yr)
int mo, yr;
{
        int d, nd, mt[] = { 0, 21, 59, 90, 120, 151,
                          181, 212, 243, 273, 304, 334 };
        
        if ( yr < 1901 || mo < 1 || mo > 12)
                return (7);
        d = yr - 1901;
        nd = d * 365;
        nd += (( d/4) - (d/100) + (d/400)) + mt[mo-1];
        if (( yr % 4) == 0 && mo > 2)
                nd++;
        return (( nd + 2) % 7);
}

usage() {
        fputs("usage: cal [option] [mm yyyy]\n", stdout);
        fputs("-[h][?] - show help\n",stdout);
        fputs("-i - interactive\n",stdout);
        fputs("print monthly calendar as in: cal 3 2000\n", stdout);
}

int print_cal(mo, year)
int mo, year;
{
        int i, day, c, s, cal_lm();
        static char *n[] = { "January", "February", "March", "April",
                            "May", "June", "July", "August", "September",
                             "October", "November", "December" };
        static dmp [] = { 31, 28, 31, 30, 31, 30,
                          31, 31, 30, 31, 30, 31 };

        if (( day = cal_lm (mo, year)) > 6) {
                usage();
                exit();
        }
        mo--;
        printf ("\n %s %d", n[mo], year);
        fputs ("\n Su Mo Tu We Th Fr Sa",stdout);
        putchar('\n');
         s = 0;
        if (day >0)
                for (s =0; s < day; s++)
                        fputs ( " ",stdout);
        c = s;
        day = 1;

        do {
                printf ("%3d", day++);
                if (++c > 6) {
                        putchar ('\n');
                        c = 0;
                }
        } while (day <= dmp [mo] );
        if ( mo == 1 && (year % 4) == 0)
                if (( year % 100 ) != 0 || (year % 400 ) == 0)
                        printf( "%3d", day );
        fputs ("\n\n\n",stdout);
}

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

        int mo, year;
        char m[4], y[5];
        time_t now;
        struct tm *ptr;
        char mbuff[4],ybuff[5];
        char *t;

  if (argc ==1) {
        /* get time */
        time(&now);
        ptr = localtime(&now);
        t = asctime(ptr);

        /* we only need month and year */
        sscanf(t,"%s%s%s%s%s",NULL,mbuff,NULL,NULL,ybuff);
        if (strcmp(mbuff,"Jan") == 0)
                mo = 1;
        if (strcmp(mbuff,"Feb") == 0)
                mo = 2;
        if (strcmp(mbuff,"Mar") == 0)
                mo = 3;
        if (strcmp(mbuff,"Apr") == 0)
                mo = 4;
        if (strcmp(mbuff,"May") == 0)
                mo = 5;
        if (strcmp(mbuff,"Jun") == 0)
                mo = 6;
        if (strcmp(mbuff,"Jul") == 0)
                mo = 7;
        if (strcmp(mbuff,"Aug") == 0)
                mo = 8;
        if (strcmp(mbuff,"Sep") == 0)
                mo = 9;
        if (strcmp(mbuff,"Oct") == 0)
                mo = 10;
        if (strcmp(mbuff,"Nov") == 0)
                mo = 11;
        if (strcmp(mbuff,"Dec") == 0)
                mo = 12;
        if ((mo <= 0) || (mo > 12)) {
                usage(); exit(0);
        }
        year = atoi(ybuff);
        if ((year < 1901) || (year > 32767)) {
                usage(); exit(0);
        }
        print_cal(mo,year);
        exit(0);
  }

  else if (argc > 1) {
        int param = 1;
        if (argv[param][0] == '-') {
                switch(argv[param][1]) {
                case 'i':
                        fputs("Enter month [1-12]:\n", stdout);
                        fgets(m,3,stdin);
                        mo = atoi(m);
                        if ((mo <= 0) || (mo > 12)) {
                                usage(); exit(0);
                        }
                        fputs("Enter year [2000]:\n", stdout);
                        fgets(y,5,stdin);
                        year = atoi(y);
                        if ((year < 1901) || (year > 32767)) {
                                usage(); exit(0);
                        }
                        print_cal(mo,year);
                        exit(0);
                        break;
                case 'h':
                case '?':
                default: usage(); exit(0);
                        break;
                }
        }
        mo = atoi(argv[1]);
        if ((mo <= 0) || (mo > 12)) {
                usage(); exit(0);
        }

        year = atoi(argv[2]);
        if ((year < 1901) || (year > 32767)) {
                usage(); exit(0);
        }
   }
print_cal(mo,year);
exit(0);
}

<----------------------------- cut here for date.c ---------------->
/*
 * date small utility to check and set system time.
 *
 * 1999-11-07 mario.frasca@home.ict.nl
 *
 * Copyright 1999 Mario Frasca
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * hacked by whb (bball@staffnet.com) 2000-3-13 for uClinux
 * (i know, sscanf could parse command line to set date and time)
 */

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

void usage()
{
        fputs("date : read or modify current system date\n", stdout);
        fputs("usage: date [option] \n", stdout);
        fputs(" -i interactively set date and time\n", stdout);
        fputs(" -h show usage\n", stdout);
        exit(1);
}

/* our own happy mktime() replacement, with the following drawbacks: */
/* doesn't check boundary conditions */
/* doesn't set wday or yday */
/* doesn't return the local time */
time_t utc_mktime(t)
struct tm *t;
{
        static int moffset[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
        time_t ret;

  /* calculate days from years */
        ret = t->tm_year - 1970;
        ret *= 365L;

  /* count leap days in preceding years */
        ret += ((t->tm_year -1969) >> 2);

  /* calculate days from months */
        ret += moffset[t->tm_mon];

  /* add in this year's leap day, if any */
   if (((t->tm_year & 3) == 0) && (t->tm_mon > 1)) {
                ret ++;
   }

  /* add in days in this month */
   ret += (t->tm_mday - 1);

  /* convert to hours */
        ret *= 24L;
   ret += t->tm_hour;

  /* convert to minutes */
   ret *= 60L;
   ret += t->tm_min;

  /* convert to seconds */
          ret *= 60L;
   ret += t->tm_sec;

  /* return the result */
   return ret;
}

/* ugly, no error-checking, but it works */
int setdate (void)
{
        time_t systime;
        char * p, buf[5];
        struct tm tm;
        
        tm.tm_year= tm.tm_mon= tm.tm_mday= tm.tm_hour= tm.tm_min= tm.tm_sec=0;

                fputs("Enter year [2000]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_year = atoi(buf);

                fputs("Enter month [1-12]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_mon = atoi(buf);
                tm.tm_mon--;

                fputs("Enter day [1-31]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_mday = atoi(buf);

                fputs("Enter hour [0-23]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_hour = atoi(buf);

                fputs("Enter minute [0-59]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_min = atoi(buf);

                fputs("Enter seconds [0-59]:\n",stdout);
                fgets(buf,5,stdin);
                tm.tm_sec = atoi(buf);

        if(tm.tm_year<70) tm.tm_year+=2000;
        else if(tm.tm_year<100)tm.tm_year+=1900;
        else if(tm.tm_year<1970)
                usage();

        systime = utc_mktime(&tm); /* fill in structure */

        stime(&systime); /* set time */

        fputs(ctime(&systime), stdout); /* display new time */

        exit(0);

}

int main(argc, argv)
char ** argv;
int argc;
{
        time_t systime;
        time(&systime);

        if(argc==1)
        {
                fputs(ctime(&systime), stdout);
        }
        else
        {
                int param = 1;

                if(argv[param][0] != '-')
                        usage();
                        
                switch(argv[param][1]){
                case 'i':
                        setdate(); /* interactively set date and time */
                        break;

                case 'h': /* show help */
                default:
                        usage();
                }

        }

   return 0;
}

<--------------------------- end --------------------------------->
                                 

This message resent by the ucsimm@uclinux.com list server http://www.uClinux.com/



This archive was generated by hypermail 2b30 : Sun Apr 07 2002 - 00:01:35 EST