use m4_normalize on the output strings to remove trailing spaces/tabs and consolidate...
[AROS.git] / workbench / c / Lock.c
blobb4f7fae3972f09866e7766d74c761bc199366677
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Lock - Set the write protect status of a device.
6 Lang: english
7 */
9 /**************************************************************************
11 NAME
12 Lock
14 FORMAT
15 Lock <drive> [ON|OFF] [<passkey>]
17 SYNOPSIS
18 DRIVE/A,ON/S,OFF/S,PASSKEY
20 LOCATION
23 FUNCTION
24 Lock will cause the specified device or partition to be made write-
25 protected or write-enabled. This write protection is a soft write
26 protection which is handled by the volume filesystem. Hence the
27 protection will be reset (to writable) on the next system reboot.
29 It is possible to specify an optional passkey which can be used to
30 password protect the locking. The same passkey that is used to lock
31 the volume must be used to unlock the volume. The passkey may be
32 any number of characters in length.
34 The volume given MUST be the device or root volume name, not an
35 assign.
37 EXAMPLE
39 1.SYS:> Lock Work:
41 This will lock the volume called Work: without a passkey.
43 1.SYS:> Lock Work:
44 1.SYS:> MakeDir Work:SomeDir
45 Can't create directory Work:Test
46 MakeDir: Disk is write-protected
48 The volume Work: is locked, so it is impossible to create a
49 directory.
51 1.SYS:> Lock Work: OFF
53 This will unlock the volume work.
55 1.SYS:> Lock Work: MyPassword
57 This will lock Work: with the passkey "MyPassword"
59 **************************************************************************/
60 #define DEBUG 0
61 #include <aros/debug.h>
63 #include <exec/types.h>
64 #include <exec/memory.h>
65 #include <dos/dosextens.h>
66 #include <dos/rdargs.h>
67 #include <proto/exec.h>
68 #include <proto/dos.h>
69 #include <utility/tagitem.h>
71 #define ARG_TEMPLATE "DRIVE/A,ON/S,OFF/S,PASSKEY"
72 #define ARG_DRIVE 0
73 #define ARG_ON 1
74 #define ARG_OFF 2
75 #define ARG_PASSKEY 3
76 #define TOTAL_ARGS 4
78 const TEXT version[] = "$VER: Lock 41.3 (7.12.2011)";
79 static const char exthelp[] =
80 "Lock : Set the write-protect state of a volume\n"
81 "\tDRIVE/A The name of the volume to lock\n"
82 "\tON/S Turn write protect on\n"
83 "\tOFF/S Turn write protect off\n"
84 "\tPASSKEY The password string for the lock (optional)\n";
86 BOOL lockDevice(struct DevProc *dvp, ULONG key)
88 return DoPkt(dvp->dvp_Port, ACTION_WRITE_PROTECT, DOSTRUE, key, 0, 0, 0);
91 BOOL unlockDevice(struct DevProc *dvp, ULONG key)
93 return DoPkt(dvp->dvp_Port, ACTION_WRITE_PROTECT, DOSFALSE, key, 0, 0, 0);
96 /* Perl's hash() algorithm
98 #define HASH_MULTIPLIER 33
100 ULONG Hash(CONST_STRPTR key)
102 ULONG hash;
104 if (key == 0 || key[0] == 0)
105 return 0;
107 for (hash = 0; *key; key++)
108 hash = HASH_MULTIPLIER * hash + *key;
110 /* Improve distribution of low-order bits */
111 hash += (hash >> 5);
113 return hash;
116 int __nocommandline;
118 int main(void)
120 struct RDArgs *rd, *rda = NULL;
121 IPTR args[TOTAL_ARGS] = { 0, FALSE, FALSE, 0 };
122 struct DevProc *dp;
123 int error = 0;
125 if((rda = AllocDosObject(DOS_RDARGS, NULL)))
127 rda->RDA_ExtHelp = (STRPTR)exthelp;
128 if((rd = ReadArgs(ARG_TEMPLATE, args, NULL)))
130 /* Firstly, find out the volume */
131 dp = GetDeviceProc((STRPTR)args[ARG_DRIVE], NULL);
133 if( dp == NULL || dp->dvp_DevNode == NULL
134 || ( dp->dvp_DevNode->dol_Type != DLT_DEVICE
135 && dp->dvp_DevNode->dol_Type != DLT_VOLUME )
138 if( dp != NULL )
139 error = ERROR_OBJECT_WRONG_TYPE;
140 else
141 error = IoErr();
143 else
145 /* First of find out current state. */
146 if (args[ARG_ON] && args[ARG_OFF])
148 /* Both are set? */
149 error = ERROR_TOO_MANY_ARGS;
151 else if (args[ARG_OFF])
153 error = unlockDevice(dp, Hash((CONST_STRPTR)args[ARG_PASSKEY])) ? IoErr() : 0;
155 else
157 error = lockDevice(dp, Hash((CONST_STRPTR)args[ARG_PASSKEY])) ? IoErr() : 0;
161 if( dp )
162 FreeDeviceProc(dp);
164 FreeArgs(rd);
165 } /* if( ReadArgs() ) */
166 else
167 error = IoErr();
169 FreeDosObject(DOS_RDARGS, rda);
170 } /* if( AllocDosObject() ) */
171 else
172 error = ERROR_NO_FREE_STORE;
174 if( error != 0 )
176 PrintFault(error, "Lock");
177 return RETURN_FAIL;
180 SetIoErr(0);
181 return 0;