July 05 2002 prog11-01.c /*, */ /* $Id: prog11-01.c,v 1.2 2002-07-01 21:39:41+09 naito Exp $ */ /*, *,,. *, -1, *, -2 *. */ int days(const unsigned int year, const unsigned int month, const unsigned int day) static unsigned int days_for_month_n[] = 31,28,31,30,31,30,31,31,30,31,30,31 ; /* */ static unsigned int days_for_month_l[] = 31,29,31,30,31,30,31,31,30,31,30,31 ; /* */ unsigned int *days_for_month ; int i, n=0 ; days_for_month = days_for_month_n ; if (is_leap_year(year)) /* */ days_for_month = days_for_month_l ; if ((month==0) (month > 12)) return -1 ; /* */ if ((!day) (days_for_month[month-1] < day)) return -2 ; /* */ for(i=0;i<month-1;i++) n += *(days_for_month+i) ; n += day ; return n ; prog11-01.c
July 05 2002 prog11-02.c /*, */ /* $Id: prog11-02.c,v 1.1 2002-07-01 21:48:07+09 naito Exp $ */ #include <stdio.h> #define N 10 int main(int argc, char **argv) int a[n] ; int i ; for(i=0;i<n;i++) *(a+i) = i ; for(i=0;i<n;i++) /* a+i */ printf("a[%d] = %d, address of a[%d] = %p\n", i, a[i], i, a+i) ; /* ************ */ printf("\"sizeof a\" = %lu\n", sizeof(a)) ; printf("\"sizeof a[0]\" = %lu\n", sizeof(a[0])) ; printf("\"sizeof *a\" = %lu\n", sizeof(*a)) ; printf("\"sizeof(a)/sizeof(a[0])\" = %lu\n", sizeof(a)/sizeof(a[0])) ; /*. */ /* ************ */ /*. */ i = 0 ; printf("i[a] = %d\n", 9[a]) ; printf("i[a] = %d\n", i[a]) ; printf("*(i+a) = %d\n", *(9+a)) ; printf("*(i+a) = %d\n", *(a+9)) ; prog11-02.c
July 05 2002 prog11-03.c /* */ /* $Id: prog11-03.c,v 1.1 2002-07-01 21:52:15+09 naito Exp $ */ #include <stdio.h> int _strcpy_0(char *, const char *) ; int _strcpy_1(char *, const char *) ; int main(int argc, char **argv) char *src = "This is a test." ; char tgt[100] ; _strcpy_0(tgt, src) ; printf("%s\n", tgt) ; _strcpy_1(tgt, src) ; printf("%s\n", tgt) ; int _strcpy_0(char *t, const char *s) while(*t++ = *s++) ; int _strcpy_1(char *t, const char *s) int i = 0 ; while(t[i] = s[i]) i += 1 ; prog11-03.c
July 05 2002 prog11-04.c /* argc, argv */ /* $Id: prog11-04.c,v 1.2 2002-07-01 21:54:16+09 naito Exp $ */ #include <stdio.h> int main(int argc, char **argv) int i ; for(i=0;i<argc;i++) printf("%d: %s\n", i, argv[i]) ; prog11-04.c
July 05 2002 prog11-05.c /* *, *, * int. *, */ /* $Id: prog11-05.c,v 1.1 2002-07-02 11:47:08+09 naito Exp $ */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAXLINE 1024 #define N 10 int get_int_values(char *, int *, unsigned long) ; int is_separator(int) ; int is_eol(int) ; int _strncpy(char *,char *, size_t) ; int main(int argc, char **argv) char buf[maxline] ; int a[n], n, i ; while(!feof(stdin)) if (fgets(buf, MAXLINE, stdin)!= NULL) printf("buf = %s\n", buf) ; n = get_int_values(buf, a, N) ; if (n < 0) fprintf(stderr, "error\n") ; continue ; for(i=0;i<n;i++) fprintf(stdout, "a[%d] = %d\n", i, a[i]) ; /* buf, * a int. * a limit * * limit -1. */ int get_int_values(char *buf, int *a, unsigned long limit) char *p, *q, temp[maxline] ; unsigned long l=0l ; p = buf ; q = buf ; /* */ while(isspace((int)*p)) p += 1 ; /* */ while(!is_eol((int)*p)) /* */ while(!is_separator((int)*p)) p += 1 ; _strncpy(temp, q, p-q) ; *(a+l) = atoi(temp) ; l += 1 ; if (l > limit) return -1 ; p += 1 ; q = p ; return l ; prog11-05.c
July 05 2002 prog11-05.c Page 2 /* isspace,, 1 */ int is_separator(int c) if (isspace(c)) return 1 ; if (c == \n ) return 1 ; if (c == 0x00) return 1 ; /*, 1 */ int is_eol(int c) if (c == \n ) return 1 ; if (c == 0x00) return 1 ; /*. n */ int _strncpy(char *t, char *s, size_t n) char *p ; p = s ; while((p-s < n)&&(*t++ = *p++)) ; *t = 0x00 ; prog11-05.c
July 05 2002 prog11-06-1.c /* *, *, * int. *, */ /* $Id: prog11-06-1.c,v 1.2 2002-07-02 12:04:14+09 naito Exp $ */ #include <stdio.h> #include "prog11-06.h" int main(int argc, char **argv) char buf[maxline] ; int a[n], n, i ; while(!feof(stdin)) if (fgets(buf, MAXLINE, stdin)!= NULL) printf("buf = %s\n", buf) ; n = get_int_values(buf, a, N) ; if ((n < 0) (_atoi_error)) fprintf(stderr, "error\n") ; continue ; for(i=0;i<n;i++) fprintf(stdout, "a[%d] = %d\n", i, a[i]) ; prog11-06-1.c
July 05 2002 prog11-06-2.c /* buf, * a int. * a limit * * limit -1. */ /* Library Source Code */ /* $Id: prog11-06-2.c,v 1.2 2002-07-02 12:04:10+09 naito Exp $ */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "prog11-06.h" static int _atoi_error ; int get_int_values(char *buf, int *a, unsigned long limit) char *p, *q, temp[maxline] ; unsigned long l=0l ; _error = 0 ; p = buf ; q = buf ; /* */ while(isspace((int)*p)) p += 1 ; /* */ while(!is_eol((int)*p)) /* */ while(!is_separator((int)*p)) p += 1 ; _strncpy(temp, q, p-q) ; *(a+l) = _atoi(temp) ; /* _atoi, _error */ if (_atoi_error) _error = 1 ; l += 1 ; if (l > limit) return -1 ; p += 1 ; q = p ; return l ; /* isspace,, 1 */ int is_separator(int c) if (isspace(c)) return 1 ; if (c == \n ) return 1 ; if (c == 0x00) return 1 ; /*, 1 */ int is_eol(int c) if (c == \n ) return 1 ; if (c == 0x00) return 1 ; /*. n */ int _strncpy(char *t, char *s, size_t n) char *p ; p = s ; while((p-s < n)&&(*t++ = *p++)) ; *t = 0x00 ; prog11-06-2.c
July 05 2002 prog11-06-2.c Page 2 /* atoi * *, 0, _atoi_error 1 * ASCII * * * [ ]*(+ -)[0-9]+ */ int _atoi(char *s) char *p ; int r=0 ; int sign=0 ; _atoi_error = 0 ; p = s ; /* */ while(isspace((int)*s)) s += 1 ; /* */ if (*s == - ) sign = 1 ; s += 1 ; else if (*s == + ) s += 1 ; while(!is_eol((int)*s)) r *= 10 ; if (isdigit((int)*s)) r += (*s) - 0 ; else /* */ _atoi_error = 1 ; s += 1 ; return sign? -r : r ; prog11-06-2.c
July 05 2002 prog11-06.h /* Program 11-06 Header file */ /* $Id: prog11-06.h,v 1.3 2002-07-02 12:02:17+09 naito Exp $ */ #include <stdio.h> #define MAXLINE 1024 #define N 10 /* */ int get_int_values(char *, int *, unsigned long) ; int is_separator(int) ; int is_eol(int) ; int _strncpy(char *,char *, size_t) ; int _atoi(char *) ; /* */ int _error ; prog11-06.h
July 05 2002 Makefile SRC = prog11-06-1.c prog11-06-2.c OBJ = $(SRC:.c=.o) PROG = prog11-06 CC = gcc all: $(PROG) $(PROG): $(OBJ) gcc $(OBJ) -o $(PROG) clean: /bin/rm -f $(OBJ) $(PROG) *~ Makefile
July 05 2002 atoi.c /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS AS IS AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(libc_sccs) &&!defined(lint) static char sccsid[] = "@(#)atoi.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include <stdlib.h> #include <stddef.h> int atoi(str) const char *str; return((int)strtol(str, (char **)NULL, 10)); atoi.c
July 05 2002 strtol.c /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS AS IS AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(libc_sccs) &&!defined(lint) static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include <limits.h> #include <ctype.h> #include <errno.h> #include <stdlib.h> /* * Convert a string to a long integer. * * Ignores locale stuff. Assumes that the upper and lower case * alphabets and digits are each contiguous. */ long strtol(nptr, endptr, base) const char *nptr; char **endptr; register int base; register const char *s = nptr; register unsigned long acc; register unsigned char c; register unsigned long cutoff; register int neg = 0, any, cutlim; strtol.c /* * Skip white space and pick up leading +/- sign if any. * If base is 0, allow 0x for hex and 0 for octal, else * assume decimal; if base is already 16, allow 0x. */
July 05 2002 strtol.c Page 2 do c = *s++; while (isspace(c)); if (c == - ) neg = 1; c = *s++; else if (c == + ) c = *s++; if ((base == 0 base == 16) && c == 0 && (*s == x *s == X )) c = s[1]; s += 2; base = 16; if (base == 0) base = c == 0? 8 : 10; /* * Compute the cutoff value between legal numbers and illegal * numbers. That is the largest legal value, divided by the * base. An input number that is greater than this value, if * followed by a legal input character, is too big. One that * is equal to this value may be valid or not; the limit * between valid and invalid numbers is then based on the last * digit. For instance, if the range for longs is * [-2147483648..2147483647] and the input base is 10, * cutoff will be set to 214748364 and cutlim to either * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated * a value > 214748364, or equal but the next digit is > 7 (or 8), * the number is too big, and we will return a range error. * * Set any if any digits consumed; make it negative to indicate * overflow. */ cutoff = neg? -(unsigned long)long_min : LONG_MAX; cutlim = cutoff % (unsigned long)base; cutoff /= (unsigned long)base; for (acc = 0, any = 0;; c = *s++) if (!isascii(c)) break; if (isdigit(c)) c -= 0 ; else if (isalpha(c)) c -= isupper(c)? A - 10 : a - 10; else break; if (c >= base) break; if (any < 0 acc > cutoff (acc == cutoff && c > cutlim)) any = -1; else any = 1; acc *= base; acc += c; if (any < 0) acc = neg? LONG_MIN : LONG_MAX; errno = ERANGE; else if (neg) acc = -acc; if (endptr!= 0) *endptr = (char *)(any? s - 1 : nptr); return (acc); strtol.c