perform later so that we can get valid values from kernelbase
[AROS.git] / tools / adflib / adf_util.c
blob380b8a77abeb5b4ef9a3bad46020dcae9db0cf5f
1 /*
2 * ADF Library. (C) 1997-1999 Laurent Clevy
4 * adf_util.c
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <time.h>
12 #include "adf_util.h"
13 #include "adf_err.h"
14 #include "adf_disk.h"
16 extern struct Env adfEnv;
20 * swLong
22 * write an ULONG value (val) (in)
23 * to an unsigned char* buffer (buf) (out)
25 * used in adfWrite----Block() functions
27 void swLong(UBYTE* buf, ULONG val)
29 buf[0]= (UBYTE)((val & 0xff000000) >>24UL);
30 buf[1]= (UBYTE)((val & 0x00ff0000) >>16UL);
31 buf[2]= (UBYTE)((val & 0x0000ff00) >>8UL);
32 buf[3]= (UBYTE)(val & 0x000000ff);
35 void swShort(UBYTE* buf, USHORT val)
37 buf[0]= (val & 0xff00) >>8UL;
38 buf[1]= (val & 0x00ff) ;
42 * newCell
44 * adds a cell at the end the list
46 struct List* newCell(struct List* list, void* content)
48 struct List* cell;
50 cell=(struct List*)malloc(sizeof(struct List));
51 if (!cell) {
52 (*adfEnv.eFct)("newCell : malloc");
53 return NULL;
55 cell->content = content;
56 cell->next = cell->subdir = 0;
57 if (list!=NULL)
58 list->next = cell;
60 return cell;
65 * freeList
68 void freeList(struct List* list)
70 if (list==NULL)
71 return;
73 if (list->next)
74 freeList(list->next);
75 free(list);
82 * Days2Date
84 * amiga disk date format (days) to normal dd/mm/yy format (out)
87 void
88 adfDays2Date(ULONG days, int *yy, int *mm, int *dd)
90 int y,m;
91 int nd;
92 int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
94 /* 0 = 1 Jan 1978, 6988 = 18 feb 1997 */
96 /*--- year ---*/
97 y=1978;
98 if (adfIsLeap(y))
99 nd=366;
100 else
101 nd=365;
102 while( days >= nd ) {
103 days-=nd;
104 y++;
105 if (adfIsLeap(y))
106 nd=366;
107 else
108 nd=365;
112 /*--- month ---*/
113 m=1;
114 if (adfIsLeap(y))
115 jm[2-1]=29;
116 while( days >= jm[m-1] ) {
117 days-=jm[m-1];
118 m++;
121 *yy=y;
122 *mm=m;
123 *dd=days+1;
128 * IsLeap
130 * true if a year (y) is leap
133 BOOL
134 adfIsLeap(int y)
136 return( (BOOL) ( !(y%100) ? !(y%400) : !(y%4) ) );
141 * adfCurrentDateTime
143 * return the current system date and time
145 struct DateTime
146 adfGiveCurrentTime( void )
148 struct tm *local;
149 time_t cal;
150 struct DateTime r;
152 time(&cal);
153 local=localtime(&cal);
155 r.year=local->tm_year; /* since 1900 */
156 r.mon=local->tm_mon+1;
157 r.day=local->tm_mday;
158 r.hour=local->tm_hour;
159 r.min=local->tm_min;
160 r.sec=local->tm_sec;
162 return(r);
167 * adfTime2AmigaTime
169 * converts date and time (dt) into Amiga format : day, min, ticks
171 void
172 adfTime2AmigaTime(struct DateTime dt, ULONG *day, ULONG *min, ULONG *ticks )
174 int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
177 *min= dt.hour*60 + dt.min; /* mins */
178 *ticks= dt.sec*50; /* ticks */
180 /*--- days ---*/
182 *day= dt.day-1; /* current month days */
184 /* previous months days downto january */
185 if (dt.mon>1) { /* if previous month exists */
186 dt.mon--;
187 if (dt.mon>2 && adfIsLeap(dt.year)) /* months after a leap february */
188 jm[2-1]=29;
189 while(dt.mon>0) {
190 *day=*day+jm[dt.mon-1];
191 dt.mon--;
195 /* years days before current year downto 1978 */
196 if (dt.year>78) {
197 dt.year--;
198 while(dt.year>=78) {
199 if (adfIsLeap(dt.year))
200 *day=*day+366;
201 else
202 *day=*day+365;
203 dt.year--;
211 * dumpBlock
213 * debug function : to dump a block before writing the check its contents
216 void dumpBlock(unsigned char *buf)
218 int i, j;
220 for(i=0; i<32; i++) {
221 printf("%5x ",i*16);
222 for (j=0; j<4; j++) {
223 printf("%08x ",Long(buf+j*4+i*16));
225 printf(" ");
226 for (j=0; j<16; j++)
227 if (buf[i*16+j]<32 || buf[i*16+j]>127)
228 putchar('.');
229 else
230 putchar(buf[i*16+j]);
231 putchar('\n');
237 /*################################################################################*/