Annotation of researchv9/X11/src/X.V11R1/clients/xmh/util.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char rcs_id[] = "$Header: util.c,v 1.14 87/09/11 08:18:35 toddb Exp $";
        !             3: #endif lint
        !             4: /*
        !             5:  *                       COPYRIGHT 1987
        !             6:  *                DIGITAL EQUIPMENT CORPORATION
        !             7:  *                    MAYNARD, MASSACHUSETTS
        !             8:  *                     ALL RIGHTS RESERVED.
        !             9:  *
        !            10:  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
        !            11:  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
        !            12:  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
        !            13:  * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
        !            14:  *
        !            15:  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
        !            16:  * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
        !            17:  * SET FORTH ABOVE.
        !            18:  *
        !            19:  *
        !            20:  * Permission to use, copy, modify, and distribute this software and its
        !            21:  * documentation for any purpose and without fee is hereby granted, provided
        !            22:  * that the above copyright notice appear in all copies and that both that
        !            23:  * copyright notice and this permission notice appear in supporting documentation,
        !            24:  * and that the name of Digital Equipment Corporation not be used in advertising
        !            25:  * or publicity pertaining to distribution of the software without specific,
        !            26:  * written prior permission.
        !            27:  */
        !            28: 
        !            29: /* util.c -- little miscellaneous utilities. */
        !            30: 
        !            31: #include "xmh.h"
        !            32: #include <sys/file.h>
        !            33: #include <sys/types.h>
        !            34: #include <sys/stat.h>
        !            35: #include <sys/time.h>
        !            36: #include <errno.h>
        !            37: #include <ctype.h>
        !            38: 
        !            39: /* Something went wrong; panic and quit. */
        !            40: 
        !            41: Punt(str)
        !            42:   char *str;
        !            43: {
        !            44:     extern void abort();
        !            45:     (void) fprintf(stderr, "%s\nerrno = %d\007\n", str, errno);
        !            46:     (void) fflush(stderr);
        !            47:     abort();
        !            48: }
        !            49: 
        !            50: 
        !            51: int myopen(path, flags, mode)
        !            52: char *path;
        !            53: int flags, mode;
        !            54: {
        !            55:     int fid;
        !            56:     fid = open(path, flags, mode);
        !            57:     if (debug && fid >= 0) fprintf(stderr, "# %d : %s\n", fid, path);
        !            58:     return fid;
        !            59: }
        !            60: 
        !            61: 
        !            62: FILE *myfopen(path, mode)
        !            63: char *path, *mode;
        !            64: {
        !            65:     FILE *result;
        !            66:     result = fopen(path, mode);
        !            67:     if (debug && result)  fprintf(stderr, "# %d : %s\n", result->_file, path);
        !            68:     return result;
        !            69: }
        !            70: 
        !            71: 
        !            72: 
        !            73: int myclose(fid)
        !            74: {
        !            75:     if (close(fid) < 0) Punt("Error in myclose!");
        !            76:     if (debug) fprintf(stderr, "# %d : <Closed>\n", fid);
        !            77: }
        !            78: 
        !            79: 
        !            80: int myfclose(file)
        !            81: FILE *file;
        !            82: {
        !            83:     int fid = file->_file;
        !            84:     if (fclose(file) < 0) Punt("Error in myfclose!");
        !            85:     if (debug) fprintf(stderr, "# %d : <Closed>\n", fid);
        !            86: }
        !            87: 
        !            88: 
        !            89: 
        !            90: /* Return a unique file name. */
        !            91: 
        !            92: char *MakeNewTempFileName()
        !            93: {
        !            94:     static char name[60];
        !            95:     static int  uniqueid = 0;
        !            96:     do {
        !            97:        (void) sprintf(name, "%s/xmh_%ld_%d", tempDir, getpid(), uniqueid++);
        !            98:     } while (FileExists(name));
        !            99:     return name;
        !           100: }
        !           101: 
        !           102: 
        !           103: /* Make an array of string pointers big enough to hold n+1 entries. */
        !           104: 
        !           105: char **MakeArgv(n)
        !           106:   int n;
        !           107: {
        !           108:     char **result;
        !           109:     result = ((char **) XtMalloc((unsigned) (n+1) * sizeof(char *)));
        !           110:     result[n] = 0;
        !           111:     return result;
        !           112: }
        !           113: 
        !           114: 
        !           115: char **ResizeArgv(argv, n)
        !           116:   char **argv;
        !           117:   int n;
        !           118: {
        !           119:     argv = ((char **) XtRealloc((char *) argv, (unsigned) (n+1) * sizeof(char *)));
        !           120:     argv[n] = 0;
        !           121:     return argv;
        !           122: }
        !           123: 
        !           124: /* Open a file, and punt if we can't. */
        !           125: 
        !           126: FILEPTR FOpenAndCheck(name, mode)
        !           127:   char *name, *mode;
        !           128: {
        !           129:     FILEPTR result;
        !           130:     result = myfopen(name, mode);
        !           131:     if (result == NULL)
        !           132:        Punt("Error in FOpenAndCheck");
        !           133:     return result;
        !           134: }
        !           135: 
        !           136: 
        !           137: /* Read one line from a file. */
        !           138: 
        !           139: static char *DoReadLine(fid, lastchar)
        !           140:   FILEPTR fid;
        !           141:   char lastchar;
        !           142: {
        !           143:     static char *buf;
        !           144:     static int  maxlength = 0;
        !           145:     char   *ptr, c;
        !           146:     int     length = 0;
        !           147:     ptr = buf;
        !           148:     c = ' ';
        !           149:     while (c != '\n' && !feof(fid)) {
        !           150:        c = getc(fid);
        !           151:        if (length++ > maxlength - 5) {
        !           152:            if (maxlength)
        !           153:                buf = XtRealloc(buf, (unsigned) (maxlength *= 2));
        !           154:            else
        !           155:                buf = XtMalloc((unsigned) (maxlength = 512));
        !           156:            ptr = buf + length - 1;
        !           157:        }
        !           158:        *ptr++ = c;
        !           159:     }
        !           160:     if (!feof(fid) || length > 1) {
        !           161:        *ptr = 0;
        !           162:        *--ptr = lastchar;
        !           163:        return buf;
        !           164:     }
        !           165:     return NULL;
        !           166: }
        !           167: 
        !           168: 
        !           169: char *ReadLine(fid)
        !           170:   FILEPTR fid;
        !           171: {
        !           172:     return DoReadLine(fid, 0);
        !           173: }
        !           174: 
        !           175: 
        !           176: /* Read a line, and keep the CR at the end. */
        !           177: 
        !           178: char *ReadLineWithCR(fid)
        !           179:   FILEPTR fid;
        !           180: {
        !           181:     return DoReadLine(fid, '\n');
        !           182: }
        !           183: 
        !           184: 
        !           185: 
        !           186: /* Delete a file, and Punt if it fails. */
        !           187: 
        !           188: DeleteFileAndCheck(name)
        !           189:   char *name;
        !           190: {
        !           191:     if (strcmp(name, "/dev/null") != 0 && unlink(name) == -1)
        !           192:        Punt("DeleteFileAndCheck failed!");
        !           193: }
        !           194: 
        !           195: CopyFileAndCheck(from, to)
        !           196:   char *from, *to;
        !           197: {
        !           198:     int fromfid, tofid, n;
        !           199:     char buf[512];
        !           200:     fromfid = myopen(from, O_RDONLY, 0666);
        !           201:     tofid = myopen(to, O_WRONLY | O_TRUNC | O_CREAT, 0666);
        !           202:     if (fromfid < 0 || tofid < 0) Punt("CopyFileAndCheck failed!");
        !           203:     do {
        !           204:        n = read(fromfid, buf, 512);
        !           205:        if (n) (void) write(tofid, buf, n);
        !           206:     } while (n);
        !           207:     (void) myclose(fromfid);
        !           208:     (void) myclose(tofid);
        !           209: }
        !           210: 
        !           211: 
        !           212: RenameAndCheck(from, to)
        !           213:   char *from, *to;
        !           214: {
        !           215:     if (rename(from, to) == -1) {
        !           216:        if (errno != EXDEV) Punt("RenameAndCheck failed!");
        !           217:        CopyFileAndCheck(from, to);
        !           218:        DeleteFileAndCheck(from);
        !           219:     }
        !           220: }
        !           221: 
        !           222: 
        !           223: char *MallocACopy(str)
        !           224:   char *str;
        !           225: {
        !           226:     return strcpy(XtMalloc((unsigned) strlen(str)+1),str);
        !           227: }
        !           228: 
        !           229: 
        !           230: 
        !           231: char *CreateGeometry(gbits, x, y, width, height)
        !           232:   int gbits;
        !           233:   Position x, y;
        !           234:   Dimension width, height;
        !           235: {
        !           236:     char   *result, str1[10], str2[10], str3[10], str4[10];
        !           237:     if (gbits & WidthValue)
        !           238:        (void) sprintf(str1, "=%d", width);
        !           239:     else
        !           240:        (void) strcpy(str1, "=");
        !           241:     if (gbits & HeightValue)
        !           242:        (void) sprintf(str2, "x%d", height);
        !           243:     else
        !           244:        (void) strcpy(str2, "x");
        !           245:     if (gbits & XValue)
        !           246:        (void) sprintf(str3, "%c%d", (gbits & XNegative) ? '-' : '+', abs(x));
        !           247:     else
        !           248:        (void) strcpy(str3, "");
        !           249:     if (gbits & YValue)
        !           250:        (void) sprintf(str4, "%c%d", (gbits & YNegative) ? '-' : '+', abs(y));
        !           251:     else
        !           252:        (void) strcpy(str4, "");
        !           253:     result = XtMalloc(22);
        !           254:     (void) sprintf(result, "%s%s%s%s", str1, str2, str3, str4);
        !           255:     return result;
        !           256: }
        !           257: 
        !           258: 
        !           259: FileExists(file)
        !           260:   char *file;
        !           261: {
        !           262:     return (access(file, F_OK) == 0);
        !           263: }
        !           264: 
        !           265: LastModifyDate(file)
        !           266:   char *file;
        !           267: {
        !           268:     struct stat buf;
        !           269:     if (stat(file, &buf)) return -1;
        !           270:     return buf.st_mtime;
        !           271: }
        !           272: 
        !           273: CurrentDate()
        !           274: {
        !           275:     struct timeval time;
        !           276:     struct timezone zone;
        !           277:     (void) gettimeofday(&time, &zone);
        !           278:     return time.tv_sec;
        !           279: }
        !           280: 
        !           281: GetFileLength(file)
        !           282: char *file;
        !           283: {
        !           284:     struct stat buf;
        !           285:     if (stat(file, &buf)) return -1;
        !           286:     return buf.st_size;
        !           287: }
        !           288: 
        !           289: 
        !           290: 
        !           291: #ifdef NOTDEF
        !           292: SetValues(dpy, window, arglist, argcount)
        !           293: Display *dpy;
        !           294: Window window;
        !           295: ArgList arglist;
        !           296: int argcount;
        !           297: {
        !           298:     (void) XtSendMessage(dpy, window, messageSETVALUES, arglist, argcount);
        !           299: }
        !           300: 
        !           301: 
        !           302: GetValues(dpy, window, arglist, argcount)
        !           303: Display *dpy;
        !           304: Window window;
        !           305: ArgList arglist;
        !           306: int argcount;
        !           307: {
        !           308:     (void) XtSendMessage(dpy, window, messageGETVALUES, arglist, argcount);
        !           309: }
        !           310: #endif
        !           311: 
        !           312: 
        !           313: ChangeLabel(window, str)
        !           314: Window window;
        !           315: char *str;
        !           316: {
        !           317:     labelarglist[0].value = (XtArgVal)MallocACopy(str);
        !           318:     XtLabelSetValues(DISPLAY window, labelarglist, XtNumber(labelarglist));
        !           319: }
        !           320: 
        !           321: 
        !           322: 
        !           323: 
        !           324: Window CreateTextSW(scrn, position, name, options)
        !           325: Scrn scrn;
        !           326: int position;
        !           327: char *name;
        !           328: int options;
        !           329: {
        !           330:     Window result;
        !           331:     static Arg arglist[] = {
        !           332:        {XtNname, NULL},
        !           333:        {XtNtextOptions, NULL},
        !           334:        {XtNfile, (XtArgVal)"/dev/null"}
        !           335:     };
        !           336:     arglist[0].value = (XtArgVal)name;
        !           337:     arglist[1].value = (XtArgVal) (scrollVertical | options);
        !           338:     result = XtTextDiskCreate(DISPLAY scrn->window,
        !           339:                              arglist, XtNumber(arglist));
        !           340:     XtVPanedWindowAddPane(DISPLAY scrn->window,
        !           341:                          result, position, 50, 1000, TRUE);
        !           342:     return result;
        !           343: }
        !           344: 
        !           345: 
        !           346: 
        !           347: Window CreateTitleBar(scrn, position)
        !           348: Scrn scrn;
        !           349: int position;
        !           350: {
        !           351:     Window result;
        !           352:     int width, height;
        !           353:     static Arg arglist[] = {
        !           354:        {XtNname, (XtArgVal)"titlebar"},
        !           355:        {XtNlabel, NULL},
        !           356:     };
        !           357:     arglist[1].value = (XtArgVal) Version();
        !           358:     result = XtLabelCreate(DISPLAY scrn->window, arglist, XtNumber(arglist));
        !           359:     GetWindowSize(result, &width, &height);
        !           360:     XtVPanedWindowAddPane(DISPLAY scrn->window, result, position,
        !           361:                          height, height, TRUE);
        !           362:     return result;
        !           363: }
        !           364: 
        !           365: 
        !           366: GetWindowSize(window, width, height)
        !           367: Window window;
        !           368: int *width, *height;           /* RETURN */
        !           369: {
        !           370:     WindowBox bbox, rbox;
        !           371:     if (XtMakeGeometryRequest(DISPLAY window,
        !           372:                              XtgeometryGetWindowBox, &bbox, &rbox) ==
        !           373:            XtgeometryYes) {
        !           374:        *width = rbox.width;
        !           375:        *height = rbox.height;
        !           376:     } else {
        !           377: #ifdef X11
        !           378:        Drawable root;
        !           379:        int x, y;
        !           380:        unsigned int borderwidth, depth;
        !           381:        (void) XGetGeometry(theDisplay, window, &root, &x, &y,
        !           382:                            (unsigned int *)width, (unsigned int *)height,
        !           383:                            &borderwidth, &depth);
        !           384: #endif
        !           385: #ifdef X10
        !           386:        WindowInfo info;
        !           387:        XQueryWindow(window, &info);
        !           388:        *width = info.width;
        !           389:        *height = info.height;
        !           390: #endif
        !           391:     }
        !           392: }
        !           393: 
        !           394: 
        !           395: Feep()
        !           396: {
        !           397: #ifdef X11
        !           398:     XBell(theDisplay, 50);
        !           399: #endif
        !           400: #ifdef X10
        !           401:     XFeep(0);
        !           402: #endif
        !           403: }
        !           404: 
        !           405: 
        !           406: 
        !           407: MsgList CurMsgListOrCurMsg(toc)
        !           408:   Toc toc;
        !           409: {
        !           410:     MsgList result;
        !           411:     Msg curmsg;
        !           412:     result = TocCurMsgList(toc);
        !           413:     if (result->nummsgs == 0 && (curmsg = TocGetCurMsg(toc))) {
        !           414:        FreeMsgList(result);
        !           415:        result = MakeSingleMsgList(curmsg);
        !           416:     }
        !           417:     return result;
        !           418: }
        !           419: 
        !           420: 
        !           421: 
        !           422: Toc SelectedToc(scrn)
        !           423: Scrn scrn;
        !           424: {
        !           425:     return TocGetNamed(BBoxNameOfButton(scrn->curfolder));
        !           426: }
        !           427: 
        !           428: 
        !           429: 
        !           430: int strncmpIgnoringCase(str1, str2, length)
        !           431: char *str1, *str2;
        !           432: int length;
        !           433: {
        !           434:     int i, diff;
        !           435:     for (i=0 ; i<length ; i++, str1++, str2++) {
        !           436:         diff = ((*str1 >= 'A' && *str1 <= 'Z') ? (*str1 + 'a' - 'A') : *str1) -
        !           437:               ((*str2 >= 'A' && *str2 <= 'Z') ? (*str2 + 'a' - 'A') : *str2);
        !           438:        if (diff) return diff;
        !           439:     }
        !           440:     return 0;
        !           441: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.