Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / c / Identify / main.c
blobe886e4b1434bf6db47f11a120bbb8dc7c0ac4b96
1 /*
2 Copyright © 2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: File Identifier/starter
6 Lang: English
7 */
8 /******************************************************************************
11 NAME
13 Identify
15 SYNOPSIS
17 FILE/M/A, VERBOSE/S
19 LOCATION
21 Sys:C
23 FUNCTION
25 Identifies the file type or directory
27 INPUTS
29 FILE -- file to be recognized
30 VERBOSE -- activates verbose output
32 RESULT
34 NOTES
36 EXAMPLE
38 Identify s:startup-sequenceS:startup-sequence
39 > Text/Ascii
41 (This will identify the startup-sequence as a text file.)
43 BUGS
45 SEE ALSO
47 AddDatatypes
49 INTERNALS
51 HISTORY
53 ******************************************************************************/
55 #include <proto/dos.h>
56 #include <proto/datatypes.h>
57 #include <datatypes/datatypes.h>
59 #include <stdio.h>
61 enum
63 ARG_FILE,
64 ARG_VERBOSE,
65 ARG_COUNT
68 #define ERROR_HEADER "Identify"
70 /*** Prototypes *************************************************************/
71 int identify(CONST_STRPTR filename, BOOL verbose);
72 CONST_STRPTR gid2string(ULONG gid);
74 /*** Functions **************************************************************/
75 int main(void)
77 int rc = RETURN_OK;
78 struct RDArgs *rdargs = NULL;
79 IPTR args[ARG_COUNT] = { 0 };
81 if ((rdargs = ReadArgs("FILE/M/A,VERBOSE/S", args, NULL)) != NULL)
83 CONST_STRPTR *files = (CONST_STRPTR *) args[ARG_FILE], file;
85 while ((file = *files++) != NULL)
87 if (identify(file, args[ARG_VERBOSE]) != RETURN_OK)
89 rc = RETURN_WARN;
93 FreeArgs(rdargs);
95 else
97 PrintFault(IoErr(), ERROR_HEADER);
98 rc = RETURN_FAIL;
101 return rc;
104 int identify(CONST_STRPTR filename, BOOL verbose)
106 int rc = RETURN_OK;
107 BPTR lock = Lock(filename, ACCESS_READ);
109 if (lock != NULL)
111 struct DataType *dt = ObtainDataType(DTST_FILE, lock, TAG_DONE);
112 if (dt != NULL)
114 struct DataTypeHeader *dth = dt->dtn_Header;
116 if (!verbose)
118 Printf
120 "%s:\t%s/%s\n",
121 filename, gid2string(dth->dth_GroupID), dth->dth_Name
124 else
126 ULONG gid = AROS_LONG2BE(dth->dth_GroupID),
127 id = AROS_LONG2BE(dth->dth_ID);
129 Printf
131 "File: %s\n"
132 "Type: %s/%s\t(GID: %.4s, ID: %.4s)\n"
133 "DT Basename: %s\n\n",
134 filename, gid2string(dth->dth_GroupID), dth->dth_Name,
135 (CONST_STRPTR) &gid, (CONST_STRPTR) &id,
136 dth->dth_BaseName
140 else
142 FPrintf(Error(), ERROR_HEADER": Could not obtain datatype for file.\n");
143 rc = RETURN_FAIL;
146 UnLock(lock);
148 else
150 PrintFault(IoErr(), ERROR_HEADER);
151 rc = RETURN_FAIL;
154 return rc;
157 CONST_STRPTR gid2string(ULONG gid)
159 switch (gid)
161 case GID_SYSTEM: return "System";
162 case GID_TEXT: return "Text";
163 case GID_DOCUMENT: return "Document";
164 case GID_SOUND: return "Sound";
165 case GID_INSTRUMENT: return "Instrument";
166 case GID_MUSIC: return "Music";
167 case GID_PICTURE: return "Picture";
168 case GID_ANIMATION: return "Animation";
169 case GID_MOVIE: return "Movie";
170 default: return NULL;