Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / nonvolatile / nvdisk / getnvditemlist.c
blob376c278890d179e185e4c46087aa1a825094b6a5
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*****************************************************************************
11 NAME */
13 #include "nvdisk_intern.h"
14 #include <dos/dos.h>
15 #include <exec/memory.h>
16 #include <exec/nodes.h>
17 #include <exec/lists.h>
18 #include <exec/libraries.h>
19 #include <proto/dos.h>
20 #include <proto/exec.h>
21 #include <libraries/nonvolatile.h>
23 #include <string.h>
25 void FreeAll(struct MinList *ml, struct Library *nvdBase);
28 AROS_LH1(struct MinList *, GetNVDItemList,
30 /* SYNOPSIS */
32 AROS_LHA(STRPTR, appName, A0),
34 /* LOCATION */
36 struct Library *, nvdBase, 10, NVDisk)
38 /* FUNCTION
40 Get a list of the data stored in the nonvolatile memory by the
41 'appName' application.
43 INPUTS
45 appName -- the application the data of which to query about
47 RESULT
49 A list of the data items saved by application 'appName'.
51 NOTES
53 EXAMPLE
55 BUGS
57 SEE ALSO
59 INTERNALS
61 HISTORY
63 November 2000, SDuvan -- implemented
65 ******************************************************************************/
67 /* The size of a combined NVEntry and the name associated with it;
68 32 is due to the maximum length of the 'itemName' string being 31 */
69 #define NV_NODESIZE (sizeof(struct NVEntry) + 32)
72 AROS_LIBFUNC_INIT
74 BPTR lock;
75 BPTR oldCDir;
76 struct MinList *minList = (struct MinList *)AllocVec(sizeof(struct MinList),
77 MEMF_CLEAR);
78 ULONG length;
79 struct FileInfoBlock *fib;
80 char *entries;
82 if(minList == NULL)
83 return NULL;
85 fib = AllocDosObject(DOS_FIB, NULL);
87 if(fib == NULL)
89 FreeVec(minList);
90 return NULL;
93 oldCDir = CurrentDir(GPB(nvdBase)->nvd_location);
95 NEWLIST((struct List *)minList);
97 lock = Lock(appName, SHARED_LOCK);
99 if(lock != BNULL)
101 if(Examine(lock, fib))
103 while(ExNext(lock, fib))
105 // Data is stored as _files_
106 if(fib->fib_DirEntryType < 0)
108 struct NVEntry *entry = AllocVec(NV_NODESIZE,
109 MEMF_CLEAR);
111 if(entry == NULL)
113 FreeAll(minList, nvdBase);
114 minList = NULL;
115 break;
118 entry->nve_Name = (STRPTR)(((char *)entry) + sizeof(struct NVEntry));
119 strncpy(entry->nve_Name, fib->fib_FileName, 32);
120 entry->nve_Size = fib->fib_Size;
121 entry->nve_Protection = fib->fib_Protection;
122 AddTail((struct List *)minList, (struct Node *)entry);
127 UnLock(lock);
130 FreeDosObject(DOS_FIB, fib);
132 CurrentDir(oldCDir);
134 ListLength(minList, length);
135 entries = AllocVec(sizeof(struct MinList) + length*NV_NODESIZE,
136 MEMF_ANY);
139 /* We store the whole list in one memory block to make it possible to
140 free the memory by calling nonvolatile.library/FreeNVData() */
141 if(entries != NULL)
143 struct Node *node; /* Temporary variable */
144 ULONG offset = sizeof(struct MinList); /* Offset into the memory
145 allocated */
146 NEWLIST((struct List *)entries);
148 for(node = GetHead((struct List *)minList); node != NULL;
149 node = GetSucc(node))
151 /* 1. Copy the NVEntry plus string
152 2. Add the memory as a node in the list
153 3. Set the name to point to the copied memory */
155 CopyMem(node, entries + offset, NV_NODESIZE);
156 AddTail((struct List *)entries, (struct Node *)(entries + offset));
157 SetNodeName(entries + offset, entries + offset +
158 sizeof(struct NVEntry));
159 offset += NV_NODESIZE;
162 else
164 FreeAll(minList, nvdBase);
165 return NULL;
168 FreeAll(minList, nvdBase);
169 return (struct MinList *)entries;
171 AROS_LIBFUNC_EXIT
172 } /* GetItemList */
175 /* Free all the nodes in a list together with the list structure itself. */
176 void FreeAll(struct MinList *ml, struct Library *nvdBase)
178 struct Node *node;
180 while((node = GetHead((struct List *)ml)) != NULL)
182 Remove(node);
183 FreeVec(node);
186 FreeVec(ml);
189 #undef NV_NODESIZE