commit e655086f6825d74eb9af518efcdbbb91707005f8 from: Oliver Lowe date: Fri Dec 26 07:13:41 2025 UTC amputate mgutils No idea what was going on there commit - 7c0529df50f0f6a59fd45d1a62fe9f45f52215b8 commit + e655086f6825d74eb9af518efcdbbb91707005f8 blob - 41e8135d455a15a2c78a78cad818b2e95e7d23c9 blob + 43578579c6e9a9065c3e9f0d94a9251b368682ff --- src/addressbook.c +++ src/addressbook.c @@ -423,12 +423,8 @@ static gchar *_errMsgUnknown_ = "Unknown" ; */ static ErrMsgTableEntry _lutErrorsGeneral_[] = { { MGU_SUCCESS, "Success" }, - { MGU_BAD_ARGS, "Bad arguments" }, { MGU_NO_FILE, "File not specified" }, { MGU_OPEN_FILE, "Error opening file" }, - { MGU_ERROR_READ, "Error reading file" }, - { MGU_EOF, "End of file encountered" }, - { MGU_OO_MEMORY, "Error allocating memory" }, { MGU_BAD_FORMAT, "Bad file format" }, { MGU_ERROR_WRITE, "Error writing to file" }, { MGU_OPEN_DIRECTORY, "Error opening directory" }, blob - d0e54254e5b70ed66449b9122d2f2d78570525ff blob + 106840ad487e11e37052603dbe76bf8f69671a26 --- src/common/mgutils.c +++ src/common/mgutils.c @@ -16,10 +16,6 @@ * along with this program. If not, see . */ -/* - * Definitions for generic functions. - */ - #include #include #include @@ -28,264 +24,15 @@ #include "mgutils.h" /* -* Dump linked list of character strings (for debug). -*/ -void mgu_print_list( GSList *list, FILE *stream ) { - GSList *node = list; - while( node ) { - int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data ); - if (r < 0) { - perror("fprintf"); - break; - } - node = g_slist_next( node ); - } -} - -/* -* Dump linked list of character strings (for debug). -*/ -void mgu_print_dlist( GList *list, FILE *stream ) { - GList *node = list; - while( node ) { - int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data ); - if (r < 0) { - perror("fprintf"); - break; - } - node = g_list_next( node ); - } -} - -/* -* Coalesce linked list of characaters into one long string. -*/ -gchar *mgu_list_coalesce( GSList *list ) { - gchar *str = NULL; - gchar *buf = NULL; - gchar *start = NULL; - GSList *node = NULL; - gint len; - - if( ! list ) return NULL; - - /* Calculate maximum length of text */ - len = 0; - node = list; - while( node ) { - str = node->data; - len += 1 + strlen( str ); - node = g_slist_next( node ); - } - - /* Create new buffer. */ - buf = g_new0( gchar, len+1 ); - start = buf; - node = list; - while( node ) { - str = node->data; - len = strlen( str ); - strcpy( start, str ); - start += len; - node = g_slist_next( node ); - } - return buf; -} - -/* * Replace existing string with new string. */ -gchar *mgu_replace_string( gchar *str, const gchar *value ) { - g_free( str ); - if( value ) { - str = g_strdup( value ); - g_strstrip( str ); +char *mgu_replace_string(char *old, const char *new) { + free(old); + if (!new) { + old = NULL; + } else { + old = strdup(new); + g_strstrip(old); } - else { - str = NULL; - } - return str; + return old; } - -/* -* Test and reformat an email address. -* Enter: address. -* Return: Address, or NULL if address is empty. -* Note: Leading and trailing white space is removed. -*/ -gchar *mgu_email_check_empty( gchar *address ) { - gchar *retVal = NULL; - if( address ) { - retVal = g_strdup( address ); - retVal = g_strstrip( retVal ); - if( *retVal == '\0' ) { - g_free( retVal ); - retVal = NULL; - } - } - return retVal; -} - -/* -* Parse string into linked list. Whitespace is used as a delimiter in parsing. -* Strings are parsed until maxTokens - 1 is reached. The remainder of the -* input string is copied into last element of list. -* Enter: line String to parse. -* maxTokens Maximum number of tokens to parse. -* tokenCnt If arg supplied, update with count of number of token parsed. -* Return: Linked list. The list contents should be g_free'd and list should -* freed when done. -*/ -GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) { - gchar *ptr, *pStart, *pFound, *str; - gint args = 0; - GList *list = NULL; - gboolean done = FALSE; - - if( tokenCnt ) *tokenCnt = 0; - if( line == NULL ) return NULL; - if( maxTokens < 1 ) return NULL; - - ptr = line; - while( ! done ) { - args++; - /* Skip over leading spaces */ - while( *ptr ) { - if( ! isspace( *ptr ) ) break; - ptr++; - } - - /* Find terminating space */ - pFound = NULL; - pStart = ptr; - while( *ptr ) { - if( isspace( *ptr ) ) { - pFound = pStart; - break; - } - ptr++; - } - - if( pFound ) { - if( args == maxTokens ) { - /* Rest of string */ - str = g_strdup( pStart ); - done = TRUE; - } - else { - /* Extract part of string */ - str = g_strndup( pStart, ptr - pFound ); - } - } - else { - /* Nothing there - treat as rest of string */ - str = g_strdup( pStart ); - done = TRUE; - } - list = g_list_append( list, str ); - } - if( tokenCnt ) *tokenCnt = args; - return list; -} - -/* - * Unescape characters by removing backslash character from input string. - * Enter: str String to process. - */ -void mgu_str_unescape( gchar *str ) { - gchar *p; - gint ilen; - - p = str; - while( *p ) { - if( *p == '\\' ) { - ilen = strlen( p + 1 ); - memmove( p, p + 1, ilen ); - } - p++; - } -} - -/* - * Replace leading and trailing characters (eg, quotes) in input string - * with spaces. Only matching non-blank characters that appear at both - * start and end of string are replaces. Control characters are also - * replaced with spaces. - * Enter: str String to process. - * chlea Lead character to remove. - * chtail Matching trailing character. - */ -void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) { - gchar *as; - gchar *ae; - - /* Search forwards for first non-space match */ - as = str; - ae = -1 + str + strlen( str ); - while( as < ae ) { - if( *as != ' ' ) { - if( *as == chlead ) { - /* Search backwards from end for match */ - while( ae > as ) { - if( *ae != ' ' ) { - if( *ae == chtail ) { - *as = ' '; - *ae = ' '; - return; - } - if( *ae < 32 ) { - *ae = ' '; - } - else if( *ae == 127 ) { - *ae = ' '; - } - else { - return; - } - } - ae--; - } - } - if( *as < 32 ) { - *as = ' '; - } - else if( *as == 127 ) { - *as = ' '; - } - else { - return; - } - } - as++; - } - return; -} - -/* - * Return reference to longest entry in the specified linked list. - * It is assumed that the list contains only gchar objects. - * Enter: list List of gchar strings to examine. - * Return: Reference to longest entry, or NULL if nothing found. - */ -gchar *mgu_slist_longest_entry( GSList *list ) { - GSList *node; - gchar *name = NULL; - gint iLen = 0, iLenT = 0; - - node = list; - while( node ) { - if( name == NULL ) { - name = node->data; - iLen = strlen( name ); - } - else { - iLenT = strlen( node->data ); - if( iLenT > iLen ) { - name = node->data; - iLen = iLenT; - } - } - node = g_slist_next( node ); - } - return name; -} blob - ff725b2f6358820882973ea3779cd64532f2cfdf blob + ca398c1e94d529d033b075a60f7e2cf2a51386f9 --- src/common/mgutils.h +++ src/common/mgutils.h @@ -17,39 +17,17 @@ * */ -/* - * Definitions for generic functions. - */ - #ifndef __MGUTILS_H__ #define __MGUTILS_H__ -#include -#include - -/* Error codes */ #define MGU_SUCCESS 0 -#define MGU_BAD_ARGS -1 #define MGU_NO_FILE -2 #define MGU_OPEN_FILE -3 -#define MGU_ERROR_READ -4 -#define MGU_EOF -5 -#define MGU_OO_MEMORY -6 #define MGU_BAD_FORMAT -7 #define MGU_ERROR_WRITE -15 #define MGU_OPEN_DIRECTORY -16 #define MGU_NO_PATH -17 -/* Function prototypes */ -void mgu_print_list ( GSList *list, FILE *stream ); -void mgu_print_dlist ( GList *list, FILE *stream ); -gchar *mgu_list_coalesce ( GSList *list ); -gchar *mgu_replace_string ( gchar *str, const gchar *value ); -gchar *mgu_email_check_empty ( gchar *address ); -GList *mgu_parse_string ( gchar *line, const gint maxTokens, - gint *tokenCnt ); -void mgu_str_unescape ( gchar *str ); -void mgu_str_ltc2space ( gchar *str, gchar chlead, gchar chtail ); -gchar *mgu_slist_longest_entry ( GSList *list ); +char *mgu_replace_string (char *old, const char *new); #endif /* __MGUTILS_H__ */ blob - d7a5649003d5be319e090b51977f8d13c150ccd8 blob + 634c3df2e60b96658314aa3b9f138c0659dd5171 --- src/common/socket.c +++ src/common/socket.c @@ -149,17 +149,6 @@ static SockLookupData *sock_get_address_info_async gpointer data); static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data); - -gint sock_init(void) -{ -return 0; -} - -gint sock_cleanup(void) -{ - return 0; -} - gint sock_set_io_timeout(guint sec) { io_timeout = sec; blob - fad6b2b907288bf25ff4111764c80b6a6f78c1b5 blob + c2a52227fed84adffd26b8017dd402d2760b36e8 --- src/common/socket.h +++ src/common/socket.h @@ -68,9 +68,6 @@ struct _SockInfo gboolean use_tls_sni; }; -gint sock_init (void); -gint sock_cleanup (void); - gint sock_set_io_timeout (guint sec); gint sock_set_nonblocking_mode (SockInfo *sock, gboolean nonblock); blob - 6b516dba307873d8349b574c11a22d27f055275c blob + db6d403791d787edb480543573a181c0b47d2e7f --- src/editaddress.c +++ src/editaddress.c @@ -25,7 +25,6 @@ #include "alertpanel.h" #include "stock_pixmap.h" -#include "mgutils.h" #include "addressbook.h" #include "addressitem.h" #include "addritem.h" @@ -447,16 +446,15 @@ static void edit_person_email_delete( gpointer data ) static ItemEMail *edit_person_email_edit( gboolean *error, ItemEMail *email ) { ItemEMail *retVal = NULL; - gchar *sEmail, *sAlias, *sRemarks, *sEmail_; + gchar *sEmail, *sAlias, *sRemarks; *error = TRUE; - sEmail_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_email), 0, -1 ); sAlias = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_alias), 0, -1 ); sRemarks = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_remarks), 0, -1 ); - sEmail = mgu_email_check_empty( sEmail_ ); - g_free( sEmail_ ); - if( sEmail ) { + sEmail = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_email), 0, -1 ); + g_strstrip(sEmail); + if (sEmail) { if( email == NULL ) { email = addritem_create_item_email(); } blob - d7183846edf734aa24d8b61eb7456c18e519eeb8 blob + 482e3ad47703b8af52341383fb0eb6a6299be0b8 --- src/main.c +++ src/main.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -37,10 +38,6 @@ #include #include -#ifdef HAVE_VALGRIND -#include -#endif - #include "claws.h" #include "main.h" #include "mainwindow.h" @@ -311,11 +308,10 @@ int main(int argc, char *argv[]) parse_cmd_opt(argc, argv); - sock_init(); - /* check and create unix domain socket for remote operation */ lock_socket = prohibit_duplicate_launch(&argc, &argv); if (lock_socket < 0) { + perror("prohibit_duplicate_launch"); return 0; } @@ -351,7 +347,7 @@ int main(int argc, char *argv[]) } char userrc[PATH_MAX]; - snprintf(userrc, sizeof(userrc), "%s/%s", get_rc_dir, MENU_RC); + snprintf(userrc, sizeof(userrc), "%s/%s", get_rc_dir(), MENU_RC); if (copy_file(userrc, userrc, TRUE) < 0) { warn("backup %s to %s.bak", userrc, userrc); } @@ -1150,12 +1146,6 @@ void app_will_exit(GtkWidget *widget, gpointer data) manage_window_focus_in(mainwin->window, NULL, NULL); } - sock_cleanup(); -#ifdef HAVE_VALGRIND - if (RUNNING_ON_VALGRIND) { - summary_clear_list(mainwin->summaryview); - } -#endif if (folderview_get_selected_item(mainwin->folderview)) folder_item_close(folderview_get_selected_item(mainwin->folderview)); gtk_main_quit(); @@ -1171,31 +1161,26 @@ gboolean claws_is_starting(void) return sc_starting; } -char *claws_get_socket_name(void) -{ - char path[PATH_MAX]; - snprintf(path, sizeof(path), "%s/claws-mail", g_get_user_runtime_dir()); - if (mkdir(path, 0755) < 0 && errno != EEXIST) - warn("mkdir %s", path); - strlcat(path, "/control.sock", sizeof(path)); - fprintf(stderr, "Using control socket %s\n", path); - return strdup(path); +size_t claws_socket_path(char *buf, size_t len) { + size_t n = 0; + n += strlcpy(buf, g_get_user_runtime_dir(), len); + n += strlcat(buf, "/", len); + n += strlcat(buf, "claws-mail/control.sock", len); + return n; } static gint prohibit_duplicate_launch(int *argc, char ***argv) { gint sock; GList *curr; - gchar *path; - path = claws_get_socket_name(); - /* Try to connect to the control socket */ - sock = fd_connect_unix(path); + char sockpath[PATH_MAX]; + claws_socket_path(sockpath, sizeof(sockpath)); + if (mkdir(dirname(sockpath), 0755) < 0) + return -1; + sock = fd_connect_unix(sockpath); if (sock < 0) { - gint ret; - gchar *socket_lock; - gint lock_fd; /* If connect failed, no other process is running. * Unlink the potentially existing socket, then * open it. This has to be done locking a temporary @@ -1203,36 +1188,30 @@ static gint prohibit_duplicate_launch(int *argc, char * process could have created the socket just in * between. */ - socket_lock = g_strconcat(path, ".lock", - NULL); - lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0); + char socklock[PATH_MAX]; + strlcpy(socklock, sockpath, sizeof(socklock)); + strlcat(socklock, ".lock", sizeof(socklock)); + int lock_fd = open(socklock, O_RDWR|O_CREAT, 0); if (lock_fd < 0) { - debug_print("Couldn't open %s: %s (%d)\n", socket_lock, - g_strerror(errno), errno); - g_free(socket_lock); + warn("open %s", socklock); return -1; } if (flock(lock_fd, LOCK_EX) < 0) { - debug_print("Couldn't lock %s: %s (%d)\n", socket_lock, - g_strerror(errno), errno); + warn("lock %s", socklock); close(lock_fd); - g_free(socket_lock); return -1; } - unlink(path); - debug_print("Opening socket %s\n", path); - ret = fd_open_unix(path); + remove(sockpath); + int ret = fd_open_unix(sockpath); flock(lock_fd, LOCK_UN); close(lock_fd); - unlink(socket_lock); - g_free(socket_lock); + remove(socklock); return ret; } + fprintf(stderr, "another Claws Mail instance is already running.\n"); + /* remote command mode */ - - debug_print("another Claws Mail instance is already running.\n"); - if (cmd.receive_all) { CM_FD_WRITE_ALL("receive_all\n"); } else if (cmd.receive) { @@ -1365,7 +1344,7 @@ static gint prohibit_duplicate_launch(int *argc, char g_print("Claws Mail is already running on this display (%s).\n", buf); close(sock); - sock = fd_connect_unix(path); + sock = fd_connect_unix(sockpath); CM_FD_WRITE_ALL("popup\n"); } #else @@ -1377,30 +1356,20 @@ static gint prohibit_duplicate_launch(int *argc, char return -1; } -static gint lock_socket_remove(void) +static int lock_socket_remove(void) { -#ifdef G_OS_UNIX - gchar *filename, *dirname; -#endif - if (lock_socket < 0) { + if (lock_socket < 0) return -1; - } - if (lock_socket_tag > 0) { + if (lock_socket_tag > 0) g_source_remove(lock_socket_tag); - } close(lock_socket); -#ifdef G_OS_UNIX - filename = claws_get_socket_name(); - dirname = g_path_get_dirname(filename); - if (unlink(filename) < 0) - FILE_OP_ERROR(filename, "unlink"); - g_rmdir(dirname); - g_free(dirname); -#endif - - return 0; + char sockpath[PATH_MAX]; + claws_socket_path(sockpath, sizeof(sockpath)); + if (remove(sockpath) < 0) + warn("remove %s", sockpath); + return remove(dirname(sockpath)); } static GPtrArray *get_folder_item_list(gint sock)