This repository was archived by the owner on Sep 3, 2019. It is now read-only.
forked from AsmusGerman/ABSShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiUtils.pas
More file actions
591 lines (508 loc) · 13 KB
/
MiUtils.pas
File metadata and controls
591 lines (508 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
Unit MiUtils;
interface
Uses
crt,strutils,BaseUnix,TDALista,users,MiResources,MiTypes,SysUtils,Unix,DateUtils,unixutil;
procedure micat1(arch1:string);
procedure micat2(arch1:string; arch2:string);
procedure mibg(P1: string);
procedure mifg(P1: string);
procedure micat;
procedure mikill(S1:string;S2:string);
procedure mipwd;
procedure micd(ruta:string);
procedure milsl (ubicacion:string;redireccion:boolean);
procedure milsa (ubicacion:string;redireccion:boolean);
procedure milsfR (ubicacion:string);
procedure mijobs;
implementation
var
dire: Pdir;
entrada: Pdirent;
archivo: Stat;
aux: T_DATOL;
lis: T_LISTA;
cant,total,ubi:string;
D:TDateTime;
YY,MM,DD,HH,MI,SS,MS : word;
ttotal,cant_archivos:longint;
{
name: micd
@param
* ruta : string
@return
*Cambia el valor de la variable global midir por ruta
}
procedure micd(ruta:string);
begin
if (ruta='~') then
ruta:=homeMasUsuarioActual
{La ruta indica al root}
else
if (ruta='.') or (ruta=' ') then
ruta:=midir
{La ruta indica al directorio actual}
else
if ruta= '..' then
begin
if (midir='') then
ruta:=''
else
ruta:=rutaPadre(midir);
{La ruta indica al directorio padre}
end
else
if ruta= '-' then
begin
ruta:=olddir;
{La ruta indica al directorio viejo}
end
else
if copy(ruta,1,1) <> '/' then
begin
ruta:=midir+'/'+ruta;
end;
if verificarRuta(ruta)=1 then
begin
olddir:=midir;
midir:=ruta;
end
else
begin
devolverMensaje('Error ');
devolverMensaje(ruta);
devolverMensaje(': No existe el archivo o el directorio');
end;
end;
{
name: milsl
@param
* ubicacion : string
* redireccion : boolean
@return
*El procedimiento milsl lista los elementos en formato largo,
*en una ubicacion.
*En caso de que ubicacion sea vacia, milsl trabaja en el directorio
*actual.
*Si redireccion es verdadero, entonces la lista se pasa a guardar
*en dat.
}
procedure milsl (ubicacion:string;redireccion:boolean);
begin
olddir:=midir;
ubi:=ubicacion;
cant_archivos:= 0;
crearlista(lis);
ttotal:=0;
if ubicacion <> ' ' then micd(ubicacion);
ubi:=midir;
dire:= fpOpenDir(ubi);
if dire<>nil then
begin
repeat
entrada := fpReadDir(dire^);
if entrada <> nil then
begin
with entrada^ do
begin
if copy(pchar(@d_name[0]),1,1)<>'.' then
if fpLStat(pchar(@d_name[0]),archivo)=0 then
begin
// permisos
aux.permisos:=GetFilePermissions(archivo.st_mode);
// links
aux.nlink:=archivo.st_nlink;
// usuario
aux.usuario:=GetUserName(archivo.st_uid);
// grupo
aux.grupo:=GetGroupName(archivo.st_gid);
// tamanio
aux.tam:=archivo.st_size;
// fecha de ultima modificacion
D:=UnixToDateTime(archivo.st_ctime);
DecodeDate (D,YY,MM,DD) ;
DecodeTime (D,HH,MI, SS,MS) ;
aux.fecha:=(meses[MM]+' '+dias[DD]+' '+numero[HH]+':'+numero[MI]);
//color
//ejecutables = verde claro
if (not(fpS_ISDIR(archivo.st_mode))) and (STAT_IXUsr and archivo.st_mode=STAT_IXUsr) then
aux.color:=9
else
//Registro = blanco
if fpS_ISREG(archivo.st_mode) then
aux.color:=15
else
//link= celeste claro
if fpS_ISLNK(archivo.st_mode) then
aux.color:=11
else
//Directorio = turquesa
if fpS_ISDIR(archivo.st_mode) then
aux.color:=3;
end;
//INFO AUX
//clave
aux.clave:=upCase(pchar(d_name));
//nombre
aux.nombre:=pchar(d_name);
end;
InsertarEnLista(lis,aux);
end;
until entrada = nil;
if redireccion then
listadolR(lis,cant_archivos,ttotal)
else
listadol(lis,cant_archivos,ttotal);
Str(cant_archivos,cant);
Str(ttotal,total);
devolverMensaje('Cantidad de archivos: ');
devolverMensaje(cant);
devolverMensaje(' Tamanio total: ');
devolverMensaje(total);
fpCloseDir (dire^);
end;
if ubi<> ' ' then
micd('-');
end;
{
name: milsa
@param
* ubicacion : string
* redireccion : boolean
@return
*El procedimiento milsa lista los elementos en formato corto,
*en una ubicacion.
*En caso de que ubicacion sea vacia, milsa trabaja en el directorio
*actual.
*Si redireccion es verdadero, entonces la lista se pasa a guardar
*en dat.
}
procedure milsa (ubicacion:string;redireccion:boolean);
begin
olddir:=midir;
ubi:=ubicacion;
cant_archivos:= 0;
crearlista(lis);
if ubicacion<> ' ' then
micd(ubicacion);
ubi:=midir;
dire:= fpOpenDir(ubi);
if dire<>nil then
begin
if redireccion then //con redir
begin
repeat
Entrada := fpReadDir(dire^);
if Entrada<>nil then
with Entrada^ do
begin
inc(cant_archivos);
if fpStat(pchar(@d_name[0]),archivo)=0 then
begin
aux.clave:=upCase(pchar(entrada^.d_name)); //clave
aux.nombre:=pchar(entrada^.d_name); //nombre
InsertarEnLista(lis,aux);
end;
end;
until entrada = nil;
ListadoaR(lis);
end
else //sin redir
begin
repeat
entrada := fpReadDir(dire^);
if entrada<>nil then
with entrada^ do
begin
//si encuentra el nombre del archivo devuelve 0
if fpLStat(pchar(@d_name[0]),archivo)=0 then
begin
inc(cant_archivos);
//el modo es ejecutable
if (not(fpS_ISDIR(archivo.st_mode))) and (STAT_IXUsr and archivo.st_mode=STAT_IXUsr) then
{verde claro}
aux.color:=10
else
if fpS_ISREG(archivo.st_mode) then
{blanco}
aux.color:=15
else
if fpS_ISLNK(archivo.st_mode) then
{celeste claro}
aux.color:=11
else
if fpS_ISDIR(archivo.st_mode) then
{azul claro}
aux.color:=9;
//clave
aux.clave:=upCase(pchar(entrada^.d_name));
//nombre
aux.nombre:=pchar(entrada^.d_name);
InsertarEnLista(lis,aux);
end;
end;
until entrada = nil;
Listadoa(lis);
end;
fpCloseDir (dire^);
end
else
devolverMensaje('Error en la lectura del directorio');
if ubi<> ' ' then
micd('-');
end;
{
name: milsfR
@param
* ubicacion : string
@return
*El procedimiento milsfR guarda los elementos, de una ubicacion,
*de formato corto en dat de manera no ordenada.
*En caso de que ubicacion sea vacia, milsfR trabaja en el directorio
*actual.
}
procedure milsfR(ubicacion:string);
var lin,aux: string;
begin
olddir:=midir;
ubi:=ubicacion;
cant_archivos:= 0;
if ubicacion<> ' ' then
micd(ubicacion);
ubi:=midir;
dire:= fpOpenDir(ubi);
if dire<>nil then
begin
repeat
entrada := fpReadDir(dire^);
with entrada^ do
begin
if entrada <> nil then
begin
inc(cant_archivos);
if fpStat(pchar(@d_name[0]),archivo)=0 then
begin
lin:=pchar(entrada^.d_name);
//Completar(saux,256-length(saux));
aux:=char(10);
lin:=lin+aux;
aux:=char(13);
lin:=lin+aux;
devolverMensajeA(lin); //nombre
end;
end;
end;
until entrada = nil;
Str(cant_archivos,cant);
devolverMensaje('Cantidad de archivos: ');
devolverMensaje(cant);
fpCloseDir (dire^);
end
else
devolverMensaje('Error en la lectura del directorio');
if ubi<> ' ' then
micd('-');
end;
{
name: mipwd
@param
@return
*Carga en la variable global dat la ruta del directorio actual.
}
procedure mipwd;
var
lin,saux:string;
begin
lin:=midir;
saux:=char(10);
lin:=lin+saux;
saux:=char(13);
lin:=lin+saux;
devolverMensaje(lin);
end;
{
name: mifg
@param
* P1 : string
@return
*Recibe P1 como PID a poner en primer plano.
*Si encuentra el proceso en el registro de procesos activos entonces,
*manda una señal de continuar ejecucion en primer plano.
}
procedure mifg(P1: string);
var
cod: word;
apid,i: longint;
begin
val(P1,apid,cod);
if cod = 0 then
begin
for i:= Low(LJobs) to high(LJobs) do
if LJobs[i].j_pid = apid then
if AnsiContainsStr(LJobs[i].j_nombre,'&') then
Setlength(LJobs[i].j_nombre,Pos('&',LJobs[i].j_nombre)-2);
mikill(P1,'18');
end;
end;
{
name: micat
@param
@return
* Lee los datos mediante la entrada estandar y la guarda en la
* variable global dat.
}
procedure micat;
var
lectura: string;
begin
SetLength(dat,0);
readln(lectura);
devolverMensaje(lectura);
end;
{
name: micat1
@param
* arch1 : string
@return
*Lee los datos de arch1 y los guarda en dat.
}
procedure micat1(arch1:string);
var fd : Longint;
i:longint ;
archivo: Stat;
infoarch:string;
begin
SetLength(dat,0);
if fpStat(arch1,archivo)=0 then
begin
fd := fpOpen(arch1,O_RdOnly);
if fd>0 then
begin
setlength(infoarch, archivo.st_size);
for i:=1 to length(infoarch)-1 do
if fpRead(fd,infoarch[i],length(infoarch)+1) < 0 then
devolverMensaje('Error leyendo archivo!!!');
devolverMensaje(infoarch);
end
else
devolverMensaje('Error al buscar el archivo');
fpClose(fd);
end
else
devolverMensaje('Error al buscar el archivo');
end;
{
name: micat2
@param
* arch1 : string
* arch2 : string
@return
*Este procedimiento recurre a micat1 para leer desde un archivo y
*devuelve a la salida estandar los datos del archivo respectivo.
}
procedure micat2(arch1:string; arch2:string);
var
datos:ArrayChar;
begin
micat1(arch1);
setlength(datos,length(dat));
datos:=dat;
micat1(arch2);
datos:=ConcatArray(datos,dat);
setlength(dat,0);
devolverDatos(datos);
end;
{
name: mibg
@param
* P1 : string
@return
*Recibe P1 como PID a poner en segundo plano.
*Si encuentra el proceso en el registro de procesos activos entonces,
*manda una señal de continuar ejecucion en segundo plano.
}
procedure mibg(P1: string);
var
cod: word;
pid,i: longint;
begin
val(P1,pid,cod);
if cod = 0 then
begin
mikill(P1,'18');
for i:= Low(LJobs) to high(LJobs) do
if (LJobs[i].j_pid = pid) and (AnsiContainsStr(LJobs[i].j_nombre,'&')=false)then
LJobs[i].j_nombre := LJobs[i].j_nombre+' &';
end;
end;
{
name: mikill
@param
* S1 : string
* S2 : string
@return
*Envia una señal a un proceso.
* S1 representa el pid del proceso.
* S2 representa la señal.
}
procedure mikill(S1: string; S2:string);
var
i: integer;
code1,code2,P1,P2: word;
begin
setlength(dat,0);
if (S1='-1') and (S2='-1') then
devolverMensaje('El proceso necesita 2 parametros: pid del proceso - senial')
else
begin
val(S1,P1,code1);
val(S2,P2,code2);
if (code1 <> 0) and (code2 <> 0) then
devolverMensaje('Error en los parametros')
else
begin
i:=Low(Ljobs);
while (i <= high(Ljobs)) and (LJobs[i].j_pid <> P1) do
inc(i);
if i > high(Ljobs) then
devolverMensaje('PID inexistente')
else
begin
Fpkill(P1,P2);
case P2 of
9: LJobs[i].j_estado:= 'Terminado';
18: begin
LJobs[i].j_estado:= 'Ejecutando';
JIndex:=i;
end;
19: LJobs[i].j_estado:= 'Detenido';
end;
devolverMensaje('Proceso ');
devolverMensaje(LJobs[i].j_nombre);
devolverMensaje(' ');
devolverMensaje(LJobs[i].j_estado);
end;
end;
end;
end;
{
name: mijobs
@param
@return
*Devuelve la lista de procesos activos por la salida estandar.
}
procedure mijobs;
var i: integer;
Begin
if length(LJobs)>0 then
for i:=Low(LJobs) to length(LJobs)-1 do
begin
write('[',i,']');
gotoxy(6,wherey);
write(Ljobs[i].j_pid);
gotoxy(12,wherey);
write(Ljobs[i].j_estado);
gotoxy(28,wherey);
writeln(Ljobs[i].j_nombre);
end;
End;
end.