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
|
/* ============================================================================
*
* File: misc.c
* Project: TkDesk
* Started: 05.01.94
* Changed: 31.03.96
*
* Description: Implements several utility Tcl commands for TkDesk.
*
* Copyright (C) 1996 Christian Bolik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* See the file "COPYING" in the base directory of this distribution
* for more.
*
* ----------------------------------------------------------------------------
*
* Functions:
*
*
* ========================================================================= */
#include "libdesk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_STATVFS_H
#include <sys/types.h>
#include <statvfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/types.h>
#include <sys/statvfs.h>
#endif
/* ============================================================================
* Name : dsk_stripc_Cmd
* In : ...
* Out : ...
* Desc : Syntax: dsk_stripc ?-keep? string
* This strips the file type character (one of "/@*+-_") from the
* string string. If the -keep option is given, only one of "+-_"
* will be removed.
* Side-FX: none
* ------------------------------------------------------------------------- */
int dsk_striptc_Cmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
int keep = 0, i;
char tc;
char instr[TCL_RESULT_SIZE], outstr[TCL_RESULT_SIZE];
if (argc < 2 || argc > 3) {
sprintf (interp->result, "usage: dsk_striptc ?-keep? string");
return (TCL_ERROR);
}
if (argc == 3) {
if (strcmp ("-keep", argv[1]) == 0) {
keep = 1;
strcpy(instr, argv[2]);
} else {
sprintf (interp->result, "usage: dsk_striptc ?-keep? string");
return (TCL_ERROR);
}
} else {
strcpy(instr, argv[1]);
}
if (instr[0] == 0) {
*interp->result = 0;
return (TCL_OK);
}
strcpy (outstr, instr);
i = 0;
while (outstr[i+1]) {
if (outstr[i+1] == '\\' && (outstr[i+2] == 't'))
break;
i++;
}
/* outstr[i] now contains the type character */
tc = outstr[i];
if (!keep) {
if (tc == '@' || tc == '*' || tc == '/' || tc == '+'
|| tc == '-' || tc == '=' || tc == '_') {
if (outstr[i+1])
outstr[i] = ' ';
else
outstr[i] = '\0';
}
} else {
if (tc == '_') {
if (outstr[i+1])
outstr[i] = ' ';
else
outstr[i] = '\0';
}
}
strcpy (interp->result, outstr);
return (TCL_OK);
} /* dsk_striptc_Cmd */
/* ----------------------------------------------------------------------------
* dsk_esc_Cmd:
* Tcl command: dsk_esc string chars
* Precedes each char in $string if it is in $chars. Returns the result.
*/
int dsk_esc_Cmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
char *buf;
if (argc != 3) {
sprintf (interp->result, "usage: dsk_esc string chars");
return (TCL_ERROR);
}
/* assume every char needs to be backslashed, for safety reasons */
buf = (char *) ckalloc (strlen (argv[1]) * 2);
if (buf == NULL) {
fprintf (stderr, "out of memory in dsk_esc_Cmd\n");
exit (1);
}
escape_chars (argv[1], argv[2], buf);
Tcl_SetResult (interp, buf, TCL_VOLATILE);
ckfree (buf);
return TCL_OK;
} /* dsk_esc_Cmd */
/* ----------------------------------------------------------------------------
* dsk_unesc_Cmd:
* Tcl command: dsk_unesc string
* Removes all the backslashes in $string except before }.
*/
int dsk_unesc_Cmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
char *buf;
if (argc != 2) {
strcpy (interp->result, "usage: dsk_unesc string");
return (TCL_ERROR);
}
/* assume every char needs to be backslashed, for safety reasons */
buf = (char *) ckalloc (strlen (argv[1]) * 2);
if (buf == NULL) {
fprintf (stderr, "out of memory in dsk_esc_Cmd\n");
exit (1);
}
unescape_chars (argv[1], buf);
Tcl_SetResult (interp, buf, TCL_VOLATILE);
ckfree (buf);
return TCL_OK;
} /* dsk_esc_Cmd */
/* ----------------------------------------------------------------------------
* dsk_localtime_Cmd:
* Tcl command: dsk_localtime_Cmd
* Returns the current time in "array set" format. The elements correspond
* to the entries of "struct tm".
*/
int dsk_localtime_Cmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
time_t t;
struct tm *ts;
char buf[10];
t = time (NULL);
ts = localtime (&t);
sprintf (buf, "%02d ", ts->tm_sec);
Tcl_AppendResult (interp, "sec ", buf, NULL);
sprintf (buf, "%02d ", ts->tm_min);
Tcl_AppendResult (interp, "min ", buf, NULL);
sprintf (buf, "%02d ", ts->tm_hour);
Tcl_AppendResult (interp, "hour ", buf, NULL);
sprintf (buf, "%d ", ts->tm_mday);
Tcl_AppendResult (interp, "mday ", buf, NULL);
sprintf (buf, "%d ", ts->tm_mon);
Tcl_AppendResult (interp, "mon ", buf, NULL);
sprintf (buf, "%d ", ts->tm_year % 100);
Tcl_AppendResult (interp, "year ", buf, NULL);
sprintf (buf, "%d ", ts->tm_wday);
Tcl_AppendResult (interp, "wday ", buf, NULL);
sprintf (buf, "%d ", ts->tm_yday);
Tcl_AppendResult (interp, "yday ", buf, NULL);
sprintf (buf, "%d ", ts->tm_isdst);
Tcl_AppendResult (interp, "isdst ", buf, NULL);
return (TCL_OK);
} /* dsk_localtime_Cmd */
/* ----------------------------------------------------------------------------
* dsk_statfs_Cmd:
* Tcl command: dsk_statfs path?/file?
* Returns info about the file system $path resides on, as a
* list with three elements: {totalBlocks freeBlocks percentFree}
*/
int dsk_statfs_Cmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
#if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
# ifdef HAVE_STATVFS
struct statvfs fs;
# else
struct statfs fs;
# endif
char buf[64];
if (argc != 2) {
strcpy (interp->result, "usage: dskC_statfs path?/file?");
return TCL_ERROR;
}
# ifdef HAVE_STATVFS
if (statvfs (argv[1], &fs) == 0) {
# else
if (statfs (argv[1], &fs) == 0) {
# endif
if (fs.f_bsize == 0)
fs.f_bsize = 1024;
if (fs.f_blocks == 0)
fs.f_blocks = fs.f_bavail;
# ifdef HAVE_STATVFS
strcpy (buf, itoa((long)((double)fs.f_blocks/(1024./fs.f_frsize))));
# else
strcpy (buf, itoa((long)((double)fs.f_blocks/(1024./fs.f_bsize))));
# endif
strcat (buf, " ");
# ifdef HAVE_STATVFS
strcat (buf, itoa((long)((double)fs.f_bavail/(1024./fs.f_frsize))));
# else
strcat (buf, itoa((long)((double)fs.f_bavail/(1024./fs.f_bsize))));
# endif
strcat (buf, " ");
if (fs.f_blocks > 0)
strcat (buf, itoa((long)((double)fs.f_bavail
/ (double)fs.f_blocks * 100)));
else
strcat (buf, "0");
Tcl_AppendResult (interp, buf, NULL);
return TCL_OK;
} else {
sprintf (interp->result, "Error %d", errno);
return TCL_ERROR;
}
#else
strcpy (interp->result, "");
return TCL_OK;
#endif
} /*dsk_statfs_Cmd */
|