1 /* xzgv v0.2 - picture viewer for X, with file selector. 2 * Copyright (C) 1999 Russell Marks. See main.c for license details. 3 * 4 * misc.c - miscellaneous util routines. 5 */ 6 7 /* XXX there are probably many other routines that should get shunted 8 * out to this :-) 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <unistd.h> 14 15 #include "misc.h" 16 17 18 /* a crude quit for when malloc and the like fails */ 19 void quit_no_mem(void) 20 { 21 fprintf(stderr,"xzgv: out of memory\n"); 22 exit(1); 23 } 24 25 26 /* a de-hassled getcwd(). You need to free the memory after use, though. 27 * XXX should get all remaining getcwd()s in the code to use this... 28 */ 29 char *getcwd_allocated(void) 30 { 31 int incr=1024; 32 int size=incr; 33 char *buf=malloc(size); 34 35 while(buf!=NULL && getcwd(buf,size-1)==NULL) 36 { 37 free(buf); 38 size+=incr; 39 buf=malloc(size); 40 } 41 42 if(buf==NULL) 43 quit_no_mem(); 44 45 return(buf); 46 }